001package jmri.jmrit.display;
002
003import java.awt.Dimension;
004import java.awt.GridBagConstraints;
005import java.awt.GridBagLayout;
006import javax.swing.JButton;
007import javax.swing.JComponent;
008import jmri.util.JmriJFrame;
009
010/**
011 * Displays and allows user to modify the text display used in a turnout slip
012 *
013 * This is a modification of CoordinateEdit.java by Dan Boudreau for use with
014 * LayoutEditor
015 *
016 * @author Dan Boudreau Copyright (C) 2007
017 * @author Kevin Dickerson (SlipTurnoutTextEditor version);
018 */
019public class SlipTurnoutTextEdit extends JmriJFrame {
020
021    SlipTurnoutIcon pl;  // layout positional label tracked by this frame
022    static final String INIT = null;
023    String oldLWUE = INIT;
024    String oldUWLE = INIT;
025    String oldLWLE = INIT;
026    String oldUWUE = INIT;
027
028    // member declarations
029    javax.swing.JLabel lableName = new javax.swing.JLabel();
030    javax.swing.JLabel nameText = new javax.swing.JLabel();
031    javax.swing.JLabel textLWUE = new javax.swing.JLabel();
032    javax.swing.JLabel textUWLE = new javax.swing.JLabel();
033    javax.swing.JLabel textLWLE = new javax.swing.JLabel();
034    javax.swing.JLabel textUWUE = new javax.swing.JLabel();
035
036    // buttons
037    javax.swing.JButton okButton = new javax.swing.JButton();
038    javax.swing.JButton cancelButton = new javax.swing.JButton();
039
040    // text field
041    javax.swing.JTextField UWLETextField = new javax.swing.JTextField(15);
042    javax.swing.JTextField LWUETextField = new javax.swing.JTextField(15);
043    javax.swing.JTextField LWLETextField = new javax.swing.JTextField(15);
044    javax.swing.JTextField UWUETextField = new javax.swing.JTextField(15);
045
046    // for padding out panel
047    javax.swing.JLabel space1 = new javax.swing.JLabel();
048    javax.swing.JLabel space2 = new javax.swing.JLabel();
049
050    public SlipTurnoutTextEdit() {
051        super();
052    }
053
054    @Override
055    public void windowClosed(java.awt.event.WindowEvent e) {
056        super.windowClosed(e);
057    }
058
059    public void initComponents(SlipTurnoutIcon l, String name) {
060        pl = l;
061
062        // the following code sets the frame's initial state
063        lableName.setText(Bundle.getMessage("Name") + ": ");
064        lableName.setVisible(true);
065
066        nameText.setText(name);
067        nameText.setVisible(true);
068
069        textLWUE.setText(Bundle.getMessage("UpperWestToLowerEast") + " = " + pl.getUWLEText());
070        textLWUE.setVisible(true);
071        textUWLE.setText(Bundle.getMessage("LowerWestToUpperEast") + " = " + pl.getLWUEText());
072        textUWLE.setVisible(true);
073        textLWLE.setText(Bundle.getMessage("LowerWestToLowerEast") + " = " + pl.getLWLEText());
074        textUWUE.setText(Bundle.getMessage("UpperWestToUpperEast") + " = " + pl.getUWUEText());
075
076        UWLETextField.setText(pl.getUWLEText());
077        UWLETextField.setToolTipText(Bundle.getMessage("EnterUWLEToolTip"));
078        UWLETextField.setMaximumSize(new Dimension(
079                UWLETextField.getMaximumSize().width, UWLETextField
080                .getPreferredSize().height));
081
082        LWUETextField.setText(pl.getLWUEText());
083        LWUETextField.setToolTipText(Bundle.getMessage("EnterLWUEToolTip"));
084        LWUETextField.setMaximumSize(new Dimension(
085                LWUETextField.getMaximumSize().width, LWUETextField
086                .getPreferredSize().height));
087
088        LWLETextField.setText(pl.getLWLEText());
089        LWLETextField.setToolTipText(Bundle.getMessage("EnterLWLEToolTip"));
090        LWLETextField.setMaximumSize(new Dimension(
091                LWLETextField.getMaximumSize().width, LWLETextField
092                .getPreferredSize().height));
093
094        UWUETextField.setText(pl.getUWUEText());
095        UWUETextField.setToolTipText(Bundle.getMessage("EnterUWUEToolTip"));
096        UWUETextField.setMaximumSize(new Dimension(
097                UWUETextField.getMaximumSize().width, UWUETextField
098                .getPreferredSize().height));
099
100        if (l.getTurnoutType() == SlipTurnoutIcon.DOUBLESLIP) {
101            textLWLE.setVisible(true);
102            textUWUE.setVisible(true);
103        } else if (l.getTurnoutType() == SlipTurnoutIcon.THREEWAY) {
104            textLWLE.setVisible(true);
105            LWLETextField.setVisible(true);
106            textUWUE.setVisible(false);
107            UWUETextField.setVisible(false);
108            textLWUE.setText(Bundle.getMessage("Upper") + " = " + pl.getLWUEText());
109            textUWLE.setText(Bundle.getMessage("Middle") + " = " + pl.getUWLEText());
110            textLWLE.setText(Bundle.getMessage("Lower") + " = " + pl.getLWLEText());
111            LWLETextField.setToolTipText(Bundle.getMessage("EnterLowerToolTip"));
112            UWLETextField.setToolTipText(Bundle.getMessage("EnterMiddleToolTip"));
113            LWUETextField.setToolTipText(Bundle.getMessage("EnterUpperToolTip"));
114        } else {
115            if (l.getSingleSlipRoute()) {
116                textUWUE.setVisible(true);
117                UWUETextField.setVisible(true);
118                textLWLE.setVisible(false);
119                LWLETextField.setVisible(false);
120            } else {
121                textUWUE.setVisible(false);
122                UWUETextField.setVisible(false);
123                textLWLE.setVisible(true);
124                LWLETextField.setVisible(true);
125            }
126        }
127
128        okButton.setText(Bundle.getMessage("ButtonOK"));
129        okButton.setVisible(true);
130        okButton.setToolTipText(Bundle.getMessage("SetButtonToolTip"));
131
132        cancelButton.setText(Bundle.getMessage("ButtonCancel"));
133        cancelButton.setVisible(true);
134        cancelButton.setToolTipText(Bundle.getMessage("CancelButtonToolTip"));
135
136        setTitle(Bundle.getMessage("SetTurnoutText"));
137        getContentPane().setLayout(new GridBagLayout());
138
139        setSize(250, 220);
140
141        addItem(lableName, 0, 0);
142        addItem(nameText, 1, 0);
143        addItem(textLWUE, 0, 1);
144        addItem(LWUETextField, 1, 1);
145
146        addItem(textUWLE, 0, 2);
147        addItem(UWLETextField, 1, 2);
148        addItem(textLWLE, 0, 3);
149        addItem(LWLETextField, 1, 3);
150        addItem(textUWUE, 0, 4);
151        addItem(UWUETextField, 1, 4);
152        addItem(cancelButton, 0, 5);
153        addItem(okButton, 1, 5);
154
155        // setup buttons
156        addButtonAction(okButton);
157        addButtonAction(cancelButton);
158        pack();
159
160    }
161
162    private void addItem(JComponent c, int x, int y) {
163        GridBagConstraints gc = new GridBagConstraints();
164        gc.gridx = x;
165        gc.gridy = y;
166        gc.weightx = 100.0;
167        gc.weighty = 100.0;
168        getContentPane().add(c, gc);
169    }
170
171    private void addButtonAction(JButton b) {
172        b.addActionListener(this::buttonActionPerformed);
173    }
174
175    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
176        if ( pl == null ){
177            log.error("No SlipTurnoutIcon defined, call initComponents on this frame.");
178            return;
179        }
180        if (ae.getSource() == okButton) {
181            // save current values in case user cancels
182            if (oldLWUE.equals( INIT ) ) {
183                oldLWUE = pl.getLWUEText();
184                oldUWLE = pl.getUWLEText();
185                oldLWLE = pl.getLWLEText();
186                oldUWUE = pl.getUWUEText();
187            }
188            pl.setUWLEText(UWLETextField.getText());
189            pl.setLWUEText(LWUETextField.getText());
190            pl.setLWLEText(LWLETextField.getText());
191            pl.setUWUEText(UWUETextField.getText());
192            textLWUE.setText(Bundle.getMessage("LowerWestToUpperEast") + " = " + pl.getLWUEText());
193            textUWLE.setText(Bundle.getMessage("UpperWestToLowerEast") + " = " + pl.getUWLEText());
194            textLWLE.setText(Bundle.getMessage("LowerWestToLowerEast") + " = " + pl.getLWLEText());
195            textUWUE.setText(Bundle.getMessage("UpperWestToUpperEast") + " = " + pl.getUWUEText());
196            if (pl.getTurnoutType() == SlipTurnoutIcon.THREEWAY) {
197                textLWUE.setText(Bundle.getMessage("Upper") + " = " + pl.getLWUEText());
198                textUWLE.setText(Bundle.getMessage("Middle") + " = " + pl.getUWLEText());
199                textLWLE.setText(Bundle.getMessage("Lower") + " = " + pl.getLWLEText());
200                /*textLWUE.setText(Bundle.getMessage("Upper") + " = " + pl.getLWLEText());
201                 textUWLE.setText(Bundle.getMessage("Middle") + " = " + pl.getLWUEText());
202                 textLWLE.setText(Bundle.getMessage("Lower") + " = " + pl.getLWLEText());*/
203            }
204        }
205        if (ae.getSource() == cancelButton) {
206            if (!oldLWUE.equals(INIT)) {
207                pl.setUWLEText(oldUWLE);
208                pl.setLWUEText(oldLWUE);
209                pl.setLWLEText(oldLWLE);
210                pl.setUWUEText(oldUWUE);
211            }
212            dispose();
213        }
214    }
215
216    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SlipTurnoutTextEdit.class);
217}