001package jmri.jmrit.beantable;
002
003import java.awt.event.ActionEvent;
004import java.awt.event.ActionListener;
005import java.beans.PropertyChangeEvent;
006import java.beans.PropertyChangeListener;
007
008import javax.annotation.Nonnull;
009import javax.swing.BoxLayout;
010import javax.swing.JCheckBox;
011import javax.swing.JTextField;
012
013import jmri.IdTag;
014import jmri.IdTagManager;
015import jmri.InstanceManager;
016import jmri.Manager;
017import jmri.managers.DefaultRailComManager;
018import jmri.managers.ProxyIdTagManager;
019import jmri.util.JmriJFrame;
020import jmri.util.swing.JmriJOptionPane;
021
022/**
023 * Swing action to create and register a IdTagTable GUI.
024 *
025 * @author  Bob Jacobsen Copyright (C) 2003
026 * @author  Matthew Harris Copyright (C) 2011
027 * @since 2.11.4
028 */
029public class IdTagTableAction extends AbstractTableAction<IdTag> implements PropertyChangeListener {
030
031    /**
032     * Create an action with a specific title.
033     * <p>
034     * Note that the argument is the Action title, not the title of the
035     * resulting frame. Perhaps this should be changed?
036     *
037     * @param actionName title of the action
038     */
039    public IdTagTableAction(String actionName) {
040        super(actionName);
041        init();
042    }
043    
044    final void init(){
045        tagManager.addPropertyChangeListener(this);
046    }
047    
048    @Nonnull
049    protected IdTagManager tagManager = InstanceManager.getDefault(IdTagManager.class);
050
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    public void setManager(@Nonnull Manager<IdTag> t) {
056        tagManager.removePropertyChangeListener(this);
057        if (t instanceof IdTagManager) {
058            tagManager = (IdTagManager) t;
059            if (m != null) {
060                m.setManager(tagManager);
061            }
062        }
063        // if t is not an instance of IdTagManager, tagManager may not change.
064        tagManager.addPropertyChangeListener(this);
065    }
066
067    public IdTagTableAction() {
068        this(Bundle.getMessage("TitleIdTagTable"));
069    }
070
071    /**
072     * Create the JTable DataModel, along with the changes for the specific case
073     * of IdTag objects.
074     */
075    @Override
076    protected void createModel() {
077        m = new IdTagTableDataModel(tagManager);
078    }
079
080    @Override
081    protected void setTitle() {
082        f.setTitle(Bundle.getMessage("TitleIdTagTable"));
083    }
084
085    @Override
086    protected String helpTarget() {
087        return "package.jmri.jmrit.beantable.IdTagTable";
088    }
089
090    JmriJFrame addFrame = null;
091    JTextField sysName = new JTextField(12);
092    JTextField userName = new JTextField(15);
093    JCheckBox isStateStored = new JCheckBox(Bundle.getMessage("IdStoreState"));
094    JCheckBox isFastClockUsed = new JCheckBox(Bundle.getMessage("IdUseFastClock"));
095
096    @Override
097    protected void addPressed(ActionEvent e) {
098        if (addFrame == null) {
099            addFrame = new JmriJFrame(Bundle.getMessage("TitleAddIdTag"), false, true);
100            addFrame.addHelpMenu("package.jmri.jmrit.beantable.IdTagAddEdit", true);
101            addFrame.getContentPane().setLayout(new BoxLayout(addFrame.getContentPane(), BoxLayout.Y_AXIS));
102
103            ActionListener okListener = (ActionEvent ev) -> {
104                okPressed(ev);
105            };
106            ActionListener cancelListener = (ActionEvent ev) -> {
107                cancelPressed(ev);
108            };
109            addFrame.setEscapeKeyClosesWindow(true);
110            addFrame.add(new AddNewDevicePanel(sysName, userName, "ButtonOK", okListener, cancelListener));
111        }
112        addFrame.pack();
113        addFrame.setVisible(true);
114    }
115
116    void cancelPressed(ActionEvent e) {
117        addFrame.setVisible(false);
118        addFrame.dispose();
119        addFrame = null;
120    }
121
122    void okPressed(ActionEvent e) {
123        String user = userName.getText();
124        if (user.isEmpty()) {
125            user = null;
126        }
127        String sName = sysName.getText();
128        try {
129            tagManager.newIdTag(sName, user);
130        } catch (IllegalArgumentException ex) {
131            // user input no good
132            handleCreateException(sName, ex);
133        }
134    }
135    //private boolean noWarn = false;
136
137    void handleCreateException(String sysName, IllegalArgumentException ex) {
138        JmriJOptionPane.showMessageDialog(addFrame,
139                Bundle.getMessage("ErrorIdTagAddFailed", sysName) + "\n" + Bundle.getMessage("ErrorAddFailedCheck")
140                + "\n" + ex.getLocalizedMessage() ,
141                Bundle.getMessage("ErrorTitle"),
142                JmriJOptionPane.ERROR_MESSAGE);
143    }
144
145    @Override
146    public String getClassDescription() {
147        return Bundle.getMessage("TitleIdTagTable");
148    }
149
150    @Override
151    public void addToFrame(BeanTableFrame<IdTag> f) {
152        f.addToBottomBox(isStateStored, this.getClass().getName());
153        isStateStored.setSelected(tagManager.isStateStored());
154        isStateStored.addActionListener((ActionEvent e) -> {
155            tagManager.setStateStored(isStateStored.isSelected());
156        });
157        f.addToBottomBox(isFastClockUsed, this.getClass().getName());
158        isFastClockUsed.setSelected(tagManager.isFastClockUsed());
159        isFastClockUsed.addActionListener((ActionEvent e) -> {
160            tagManager.setFastClockUsed(isFastClockUsed.isSelected());
161        });
162        log.debug("Added CheckBox in addToFrame method");
163    }
164
165    @Override
166    public void addToPanel(AbstractTableTabAction<IdTag> f) {
167        String connectionName = tagManager.getMemo().getUserName();
168        if (tagManager instanceof ProxyIdTagManager) {
169            connectionName = "All";
170        } else if (connectionName == null && (tagManager instanceof DefaultRailComManager)) {
171            connectionName = "RailCom"; // NOI18N (proper name).
172        }
173        f.addToBottomBox(isStateStored, connectionName);
174        isStateStored.setSelected(tagManager.isStateStored());
175        isStateStored.addActionListener((ActionEvent e) -> {
176            tagManager.setStateStored(isStateStored.isSelected());
177        });
178        f.addToBottomBox(isFastClockUsed, connectionName);
179        isFastClockUsed.setSelected(tagManager.isFastClockUsed());
180        isFastClockUsed.addActionListener((ActionEvent e) -> {
181            tagManager.setFastClockUsed(isFastClockUsed.isSelected());
182        });
183        log.debug("Added CheckBox in addToPanel method for system {}", connectionName);
184    }
185
186    /**
187     * {@inheritDoc}
188     */
189    @Override
190    public void propertyChange(PropertyChangeEvent e) {
191        if (e.getPropertyName().equals("StateStored")) {
192           isStateStored.setSelected(tagManager.isStateStored());
193        } else if (e.getPropertyName().equals("UseFastClock")) {
194           isFastClockUsed.setSelected(tagManager.isFastClockUsed()); 
195        }
196    }
197
198    @Override
199    protected String getClassName() {
200        return IdTagTableAction.class.getName();
201    }
202    
203    @Override
204    public void dispose(){
205        tagManager.removePropertyChangeListener(this);
206        super.dispose();
207    }
208    
209    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(IdTagTableAction.class);
210
211}