001package jmri.jmrit.display;
002
003import java.awt.Dimension;
004import java.awt.GridBagConstraints;
005import java.awt.GridBagLayout;
006import javax.annotation.Nonnull;
007import javax.swing.JButton;
008import javax.swing.JComponent;
009import jmri.util.JmriJFrame;
010
011/**
012 * Displays and allows user to modify the text display used in a sensor.
013 * <p>
014 * This is a modification of CoordinateEdit.java by Dan Boudreau for use with
015 * LayoutEditor.
016 *
017 * @author Dan Boudreau Copyright (C) 2007
018 * @author Kevin Dickerson (SensorTextEdit version);
019 */
020public class SensorTextEdit extends JmriJFrame {
021
022    SensorIcon pl;  // layout positional label tracked by this frame
023    static final String INIT = null;
024    String oldActive = INIT;
025    String oldInactive = INIT;
026    String oldIncon = INIT;
027    String oldUnknown = INIT;
028
029    // member declarations
030    javax.swing.JLabel lableName = new javax.swing.JLabel();
031    javax.swing.JLabel nameText = new javax.swing.JLabel();
032    javax.swing.JLabel textInact = new javax.swing.JLabel();
033    javax.swing.JLabel textAct = new javax.swing.JLabel();
034    javax.swing.JLabel textIncon = new javax.swing.JLabel();
035    javax.swing.JLabel textUnknown = new javax.swing.JLabel();
036
037    // buttons
038    javax.swing.JButton okButton = new javax.swing.JButton();
039    javax.swing.JButton cancelButton = new javax.swing.JButton();
040
041    // text field
042    javax.swing.JTextField inactTextField = new javax.swing.JTextField(8);
043    javax.swing.JTextField actTextField = new javax.swing.JTextField(8);
044    javax.swing.JTextField inconTextField = new javax.swing.JTextField(8);
045    javax.swing.JTextField unknownTextField = new javax.swing.JTextField(8);
046
047    // for padding out panel
048    javax.swing.JLabel space1 = new javax.swing.JLabel();
049    javax.swing.JLabel space2 = new javax.swing.JLabel();
050
051    public SensorTextEdit() {
052        super();
053    }
054
055    @Override
056    public void windowClosed(java.awt.event.WindowEvent e) {
057        super.windowClosed(e);
058    }
059
060    public void initComponents(@Nonnull SensorIcon l, String name) {
061        pl = l;
062
063        // the following code sets the frame's initial state
064        lableName.setText(Bundle.getMessage("Name") + ": ");
065        lableName.setVisible(true);
066
067        nameText.setText(name);
068        nameText.setVisible(true);
069
070        textInact.setText(Bundle.getMessage("SensorStateInactive") + " = " + pl.getInactiveText());
071        textInact.setVisible(true);
072        textAct.setText(Bundle.getMessage("SensorStateActive") + " = " + pl.getActiveText());
073        textAct.setVisible(true);
074        textIncon.setText(Bundle.getMessage("BeanStateInconsistent") + " = " + pl.getInconsistentText());
075        textIncon.setVisible(true);
076        textUnknown.setText(Bundle.getMessage("BeanStateUnknown") + " = " + pl.getUnknownText());
077        textUnknown.setVisible(true);
078
079        inactTextField.setText(pl.getInactiveText());
080        inactTextField.setToolTipText(Bundle.getMessage("EnterInActiveToolTip"));
081        inactTextField.setMaximumSize(new Dimension(
082                inactTextField.getMaximumSize().width, inactTextField
083                .getPreferredSize().height));
084
085        actTextField.setText(pl.getActiveText());
086        actTextField.setToolTipText(Bundle.getMessage("EnterActiveToolTip"));
087        actTextField.setMaximumSize(new Dimension(
088                actTextField.getMaximumSize().width, actTextField
089                .getPreferredSize().height));
090
091        inconTextField.setText(pl.getInconsistentText());
092        inconTextField.setToolTipText(Bundle.getMessage("EnterInconToolTip"));
093        inconTextField.setMaximumSize(new Dimension(
094                inconTextField.getMaximumSize().width, inconTextField
095                .getPreferredSize().height));
096
097        unknownTextField.setText(pl.getUnknownText());
098        unknownTextField.setToolTipText(Bundle.getMessage("EnterUnknownToolTip"));
099        unknownTextField.setMaximumSize(new Dimension(
100                unknownTextField.getMaximumSize().width, unknownTextField
101                .getPreferredSize().height));
102
103        okButton.setText(Bundle.getMessage("ButtonOK"));
104        okButton.setVisible(true);
105        okButton.setToolTipText(Bundle.getMessage("SetButtonToolTipSensor"));
106
107        cancelButton.setText(Bundle.getMessage("ButtonCancel"));
108        cancelButton.setVisible(true);
109        cancelButton.setToolTipText(Bundle.getMessage("CancelButtonToolTipSensor"));
110
111        setTitle(Bundle.getMessage("SetSensorText"));
112        getContentPane().setLayout(new GridBagLayout());
113
114        setSize(250, 220);
115
116        addItem(lableName, 0, 0);
117        addItem(nameText, 1, 0);
118        addItem(textInact, 0, 1);
119        addItem(inactTextField, 1, 1);
120        addItem(textAct, 0, 2);
121        addItem(actTextField, 1, 2);
122        addItem(textIncon, 0, 3);
123        addItem(inconTextField, 1, 3);
124        addItem(textUnknown, 0, 4);
125        addItem(unknownTextField, 1, 4);
126        addItem(cancelButton, 0, 5);
127        addItem(okButton, 1, 5);
128
129        // setup buttons
130        addButtonAction(okButton);
131        addButtonAction(cancelButton);
132        pack();
133
134        /*if (!pl.isBackground()) {
135         // Add listener so we know if the label moves
136         pl.addMouseListener(ml);
137         }*/
138    }
139
140    private void addItem(JComponent c, int x, int y) {
141        GridBagConstraints gc = new GridBagConstraints();
142        gc.gridx = x;
143        gc.gridy = y;
144        gc.weightx = 100.0;
145        gc.weighty = 100.0;
146        getContentPane().add(c, gc);
147    }
148
149    private void addButtonAction(JButton b) {
150        b.addActionListener(this::buttonActionPerformed);
151    }
152
153    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
154        if ( pl == null ){
155            log.error("No SensorIcon defined, call initComponents on this frame.");
156            return;
157        }
158        if (ae.getSource() == okButton) {
159            // save current values in case user cancels
160            if (oldActive == null) {
161                oldActive = pl.getActiveText();
162                oldInactive = pl.getInactiveText();
163                oldIncon = pl.getInconsistentText();
164                oldUnknown = pl.getUnknownText();
165            }
166            pl.setInactiveText(inactTextField.getText());
167            pl.setActiveText(actTextField.getText());
168            pl.setInconsistentText(inconTextField.getText());
169            pl.setUnknownText(unknownTextField.getText());
170            textInact.setText(Bundle.getMessage("SensorStateInactive") + " = " + pl.getInactiveText());
171            textAct.setText(Bundle.getMessage("SensorStateActive") + " = " + pl.getActiveText());
172            textIncon.setText(Bundle.getMessage("BeanStateInconsistent") + " = " + pl.getInconsistentText());
173            textUnknown.setText(Bundle.getMessage("BeanStateUnknown") + " = " + pl.getUnknownText());
174        }
175        if (ae.getSource() == cancelButton) {
176            if (oldActive != null) {
177                pl.setInactiveText(oldInactive);
178                pl.setActiveText(oldActive);
179                pl.setInconsistentText(oldIncon);
180                pl.setUnknownText(oldUnknown);
181            }
182            dispose();
183        }
184    }
185
186    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SensorTextEdit.class);
187
188}