001package jmri.jmrit.operations.setup.gui;
002
003import java.awt.GridBagLayout;
004import java.util.ResourceBundle;
005
006import javax.swing.*;
007
008import jmri.InstanceManager;
009import jmri.jmrit.operations.setup.OperationsSetupXml;
010import jmri.jmrit.operations.setup.Setup;
011import jmri.jmrit.operations.trains.TrainSwitchListText;
012
013/**
014 * Frame for user edit of switch list text strings
015 *
016 * @author Dan Boudreau Copyright (C) 2013
017 * 
018 */
019public class EditSwitchListTextPanel extends OperationsPreferencesPanel {
020
021//    private static final Logger log = LoggerFactory.getLogger(EditSwitchListTextPanel.class);
022
023    protected static final ResourceBundle rb = ResourceBundle
024            .getBundle("jmri.jmrit.operations.trains.JmritOperationsTrainsBundle");
025
026    // major buttons
027    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
028    JButton resetButton = new JButton(rb.getString("Reset"));
029
030    // text fields
031    JTextField switchListForTextField = new JTextField(60);
032    JTextField scheduledWorkTextField = new JTextField(60);
033
034    JTextField departsAtTextField = new JTextField(60);
035    JTextField departsAtExpectedArrivalTextField = new JTextField(60);
036    JTextField departedExpectedTextField = new JTextField(60);
037
038    JTextField visitNumberTextField = new JTextField(60);
039    JTextField visitNumberDepartedTextField = new JTextField(60);
040    JTextField visitNumberTerminatesTextField = new JTextField(60);
041    JTextField visitNumberTerminatesDepartedTextField = new JTextField(60);
042    JTextField visitNumberDoneTextField = new JTextField(60);
043
044    JTextField trainDirectionChangeTextField = new JTextField(60);
045    JTextField noCarPickUpsTextField = new JTextField(60);
046    JTextField noCarDropsTextField = new JTextField(60);
047    JTextField trainDoneTextField = new JTextField(60);
048    JTextField trainDepartsCarsTextField = new JTextField(60);
049    JTextField trainDepartsLoadsTextField = new JTextField(60);
050    
051    JTextField switchListByTrackTextField = new JTextField(60);
052    JTextField holdCarTextField = new JTextField(60);
053
054    public EditSwitchListTextPanel() {
055
056        // the following code sets the frame's initial state
057        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
058
059        // text fields
060        JPanel pSwitchList = new JPanel();
061        JScrollPane pSwitchListPane = new JScrollPane(pSwitchList);
062        pSwitchListPane.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutSwitchList")));
063        pSwitchList.setLayout(new BoxLayout(pSwitchList, BoxLayout.Y_AXIS));
064
065        JPanel pSwitchListForTextField = new JPanel();
066        pSwitchListForTextField.setBorder(BorderFactory.createTitledBorder(rb
067                .getString("SwitchListFor")));
068        pSwitchListForTextField.add(switchListForTextField);
069        switchListForTextField.setText(TrainSwitchListText.getStringSwitchListFor());
070        switchListForTextField.setToolTipText(rb.getString("ToolTipSwitchListFor"));
071        pSwitchList.add(pSwitchListForTextField);
072
073        JPanel pScheduledWorkTextField = new JPanel();
074        pScheduledWorkTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("ScheduledWork")));
075        pScheduledWorkTextField.add(scheduledWorkTextField);
076        scheduledWorkTextField.setText(TrainSwitchListText.getStringScheduledWork());
077        scheduledWorkTextField.setToolTipText(rb.getString("ToolTipManifestForTrain"));
078        pSwitchList.add(pScheduledWorkTextField);
079
080        JPanel pDepartsAtTextField = new JPanel();
081        pDepartsAtTextField.setBorder(BorderFactory.createTitledBorder(rb
082                .getString("DepartsAt")));
083        pDepartsAtTextField.add(departsAtTextField);
084        departsAtTextField.setText(TrainSwitchListText.getStringDepartsAt());
085        departsAtTextField.setToolTipText(rb.getString("ToolTipDepartsAt"));
086        pSwitchList.add(pDepartsAtTextField);
087
088        JPanel pDepartsAtExpectedArrivalTextField = new JPanel();
089        pDepartsAtExpectedArrivalTextField
090                .setBorder(BorderFactory.createTitledBorder(rb.getString("DepartsAtExpectedArrival")));
091        pDepartsAtExpectedArrivalTextField.add(departsAtExpectedArrivalTextField);
092        departsAtExpectedArrivalTextField.setText(TrainSwitchListText.getStringDepartsAtExpectedArrival());
093        departsAtExpectedArrivalTextField.setToolTipText(rb.getString("ToolTipArrives"));
094        pSwitchList.add(pDepartsAtExpectedArrivalTextField);
095
096        JPanel pDepartedExpectedTextField = new JPanel();
097        pDepartedExpectedTextField.setBorder(BorderFactory.createTitledBorder(rb
098                .getString("DepartedExpected")));
099        pDepartedExpectedTextField.add(departedExpectedTextField);
100        departedExpectedTextField.setText(TrainSwitchListText.getStringDepartedExpected());
101        departedExpectedTextField.setToolTipText(rb.getString("ToolTipDeparted"));
102        pSwitchList.add(pDepartedExpectedTextField);
103
104        JPanel pVisitNumber = new JPanel();
105        pVisitNumber.setBorder(BorderFactory.createTitledBorder(rb.getString("VisitNumber")));
106        pVisitNumber.add(visitNumberTextField);
107        visitNumberTextField.setText(TrainSwitchListText.getStringVisitNumber());
108        visitNumberTextField.setToolTipText(rb.getString("ToolTipVisitNumber"));
109        pSwitchList.add(pVisitNumber);
110
111        JPanel pVisitNumberDeparted = new JPanel();
112        pVisitNumberDeparted.setBorder(BorderFactory.createTitledBorder(rb.getString("VisitNumberDeparted")));
113        pVisitNumberDeparted.add(visitNumberDepartedTextField);
114        visitNumberDepartedTextField.setText(TrainSwitchListText.getStringVisitNumberDeparted());
115        visitNumberDepartedTextField.setToolTipText(rb.getString("ToolTipVisitNumber"));
116        pSwitchList.add(pVisitNumberDeparted);
117
118        JPanel pVisitNumberTerminates = new JPanel();
119        pVisitNumberTerminates.setBorder(BorderFactory.createTitledBorder(rb.getString("VisitNumberTerminates")));
120        pVisitNumberTerminates.add(visitNumberTerminatesTextField);
121        visitNumberTerminatesTextField.setText(TrainSwitchListText.getStringVisitNumberTerminates());
122        visitNumberTerminatesTextField.setToolTipText(rb.getString("ToolTipVisitNumberTerminates"));
123        pSwitchList.add(pVisitNumberTerminates);
124
125        JPanel pVisitNumberTerminatesDepartedTextField = new JPanel();
126        pVisitNumberTerminatesDepartedTextField.setBorder(BorderFactory.createTitledBorder(rb
127                .getString("VisitNumberTerminatesDeparted")));
128        pVisitNumberTerminatesDepartedTextField.add(visitNumberTerminatesDepartedTextField);
129        visitNumberTerminatesDepartedTextField.setText(TrainSwitchListText.getStringVisitNumberTerminatesDeparted());
130        visitNumberTerminatesDepartedTextField.setToolTipText(rb.getString("ToolTipVisitNumberTerminates"));
131        pSwitchList.add(pVisitNumberTerminatesDepartedTextField);
132
133        JPanel pVisitNumberDone = new JPanel();
134        pVisitNumberDone.setBorder(BorderFactory.createTitledBorder(rb.getString("VisitNumberDone")));
135        pVisitNumberDone.add(visitNumberDoneTextField);
136        visitNumberDoneTextField.setText(TrainSwitchListText.getStringVisitNumberDone());
137        visitNumberDoneTextField.setToolTipText(rb.getString("ToolTipVisitNumberDone"));
138        pSwitchList.add(pVisitNumberDone);
139
140        JPanel pTrainDirectionChange = new JPanel();
141        pTrainDirectionChange.setBorder(BorderFactory.createTitledBorder(rb.getString("TrainDirectionChange")));
142        pTrainDirectionChange.add(trainDirectionChangeTextField);
143        trainDirectionChangeTextField.setText(TrainSwitchListText.getStringTrainDirectionChange());
144        trainDirectionChangeTextField.setToolTipText(rb.getString("ToolTipDirectionChange"));
145        pSwitchList.add(pTrainDirectionChange);
146
147        JPanel pNoCarPickUps = new JPanel();
148        pNoCarPickUps.setBorder(BorderFactory.createTitledBorder(rb.getString("NoCarPickUps")));
149        pNoCarPickUps.add(noCarPickUpsTextField);
150        noCarPickUpsTextField.setText(TrainSwitchListText.getStringNoCarPickUps());
151        noCarPickUpsTextField.setToolTipText(rb.getString("ToolTipTrainDone"));
152        pSwitchList.add(pNoCarPickUps);
153
154        JPanel pNoCarDrops = new JPanel();
155        pNoCarDrops.setBorder(BorderFactory.createTitledBorder(rb.getString("NoCarDrops")));
156        pNoCarDrops.add(noCarDropsTextField);
157        noCarDropsTextField.setText(TrainSwitchListText.getStringNoCarDrops());
158        noCarDropsTextField.setToolTipText(rb.getString("ToolTipTrainDone"));
159        pSwitchList.add(pNoCarDrops);
160
161        JPanel pTrainDone = new JPanel();
162        pTrainDone.setBorder(BorderFactory.createTitledBorder(rb.getString("TrainDone")));
163        pTrainDone.add(trainDoneTextField);
164        trainDoneTextField.setText(TrainSwitchListText.getStringTrainDone());
165        trainDoneTextField.setToolTipText(rb.getString("ToolTipTrainDone"));
166        pSwitchList.add(pTrainDone);
167
168        JPanel pTrainDepartsCars = new JPanel();
169        pTrainDepartsCars.setBorder(BorderFactory.createTitledBorder(rb.getString("TrainDepartsCars")));
170        pTrainDepartsCars.add(trainDepartsCarsTextField);
171        trainDepartsCarsTextField.setText(TrainSwitchListText.getStringTrainDepartsCars());
172        trainDepartsCarsTextField.setToolTipText(rb.getString("ToolTipTrainDepartsCars"));
173        pSwitchList.add(pTrainDepartsCars);
174
175        JPanel pTrainDepartsLoadsTextField = new JPanel();
176        pTrainDepartsLoadsTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("TrainDepartsLoads")));
177        pTrainDepartsLoadsTextField.add(trainDepartsLoadsTextField);
178        trainDepartsLoadsTextField.setText(TrainSwitchListText.getStringTrainDepartsLoads());
179        trainDepartsLoadsTextField.setToolTipText(rb.getString("ToolTipTrainDepartsLoads"));
180        pSwitchList.add(pTrainDepartsLoadsTextField);
181        
182        JPanel pSwitchListByTrackTextField = new JPanel();
183        pSwitchListByTrackTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("SwitchListByTrack")));
184        pSwitchListByTrackTextField.add(switchListByTrackTextField);
185        switchListByTrackTextField.setText(TrainSwitchListText.getStringSwitchListByTrack());
186        switchListByTrackTextField.setToolTipText(rb.getString("ToolTipSwitchListFor"));
187        pSwitchList.add(pSwitchListByTrackTextField);
188        
189        JPanel pHoldCarTextField = new JPanel();
190        pHoldCarTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("HoldCar")));
191        pHoldCarTextField.add(holdCarTextField);
192        holdCarTextField.setText(TrainSwitchListText.getStringHoldCar());
193        holdCarTextField.setToolTipText(rb.getString("ToolTipHoldCar"));
194        pSwitchList.add(pHoldCarTextField);
195
196        // add tool tips
197        saveButton.setToolTipText(Bundle.getMessage("SaveToolTip"));
198
199        // row 11
200        JPanel pControl = new JPanel();
201        pControl.setBorder(BorderFactory.createTitledBorder(""));
202        pControl.setLayout(new GridBagLayout());
203        addItem(pControl, resetButton, 0, 0);
204        addItem(pControl, saveButton, 1, 0);
205
206        add(pSwitchListPane);
207        add(pControl);
208
209        // setup buttons
210        addButtonAction(resetButton);
211        addButtonAction(saveButton);
212    }
213
214    // Save buttons
215    @Override
216    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
217        if (ae.getSource() == resetButton) {
218            switchListForTextField.setText(rb.getString("SwitchListFor"));
219            scheduledWorkTextField.setText(rb.getString("ScheduledWork"));
220
221            departsAtTextField.setText(rb.getString("DepartsAt"));
222            departsAtExpectedArrivalTextField.setText(rb.getString("DepartsAtExpectedArrival"));
223            departedExpectedTextField.setText(rb.getString("DepartedExpected"));
224
225            visitNumberTextField.setText(rb.getString("VisitNumber"));
226            visitNumberDepartedTextField.setText(rb.getString("VisitNumberDeparted"));
227            visitNumberTerminatesTextField.setText(rb.getString("VisitNumberTerminates"));
228            visitNumberTerminatesDepartedTextField.setText(rb.getString("VisitNumberTerminatesDeparted"));
229            visitNumberDoneTextField.setText(rb.getString("VisitNumberDone"));
230
231            trainDirectionChangeTextField.setText(rb.getString("TrainDirectionChange"));
232            noCarPickUpsTextField.setText(rb.getString("NoCarPickUps"));
233            noCarDropsTextField.setText(rb.getString("NoCarDrops"));
234            trainDoneTextField.setText(rb.getString("TrainDone"));
235            trainDepartsCarsTextField.setText(rb.getString("TrainDepartsCars"));
236            trainDepartsLoadsTextField.setText(rb.getString("TrainDepartsLoads"));
237            
238            switchListByTrackTextField.setText(rb.getString("SwitchListByTrack"));
239            holdCarTextField.setText(rb.getString("HoldCar"));
240        }
241        if (ae.getSource() == saveButton) {
242            this.savePreferences();
243            var topLevelAncestor = getTopLevelAncestor();
244            if (Setup.isCloseWindowOnSaveEnabled() && topLevelAncestor instanceof EditSwitchListTextFrame) {
245                ((EditSwitchListTextFrame) topLevelAncestor).dispose();
246            }
247        }
248    }
249
250    @Override
251    public String getTabbedPreferencesTitle() {
252        return Bundle.getMessage("TitleSwitchListText");
253    }
254
255    @Override
256    public String getPreferencesTooltip() {
257        return null;
258    }
259
260    @Override
261    public void savePreferences() {
262        TrainSwitchListText.setStringSwitchListFor(switchListForTextField.getText());
263        TrainSwitchListText.setStringScheduledWork(scheduledWorkTextField.getText());
264
265        TrainSwitchListText.setStringDepartsAt(departsAtTextField.getText());
266        TrainSwitchListText.setStringDepartsAtExpectedArrival(departsAtExpectedArrivalTextField.getText());
267        TrainSwitchListText.setStringDepartedExpected(departedExpectedTextField.getText());
268
269        TrainSwitchListText.setStringVisitNumber(visitNumberTextField.getText());
270        TrainSwitchListText.setStringVisitNumberDeparted(visitNumberDepartedTextField.getText());
271        TrainSwitchListText.setStringVisitNumberTerminates(visitNumberTerminatesTextField.getText());
272        TrainSwitchListText.setStringVisitNumberTerminatesDeparted(visitNumberTerminatesDepartedTextField.getText());
273        TrainSwitchListText.setStringVisitNumberDone(visitNumberDoneTextField.getText());
274
275        TrainSwitchListText.setStringTrainDirectionChange(trainDirectionChangeTextField.getText());
276        TrainSwitchListText.setStringNoCarPickUps(noCarPickUpsTextField.getText());
277        TrainSwitchListText.setStringNoCarDrops(noCarDropsTextField.getText());
278        TrainSwitchListText.setStringTrainDone(trainDoneTextField.getText());
279        TrainSwitchListText.setStringTrainDepartsCars(trainDepartsCarsTextField.getText());
280        TrainSwitchListText.setStringTrainDepartsLoads(trainDepartsLoadsTextField.getText());
281        
282        TrainSwitchListText.setStringSwitchListByTrack(switchListByTrackTextField.getText());
283        TrainSwitchListText.setStringHoldCar(holdCarTextField.getText());
284
285        InstanceManager.getDefault(OperationsSetupXml.class).writeOperationsFile();
286    }
287
288    @Override
289    public boolean isDirty() {
290        return !(TrainSwitchListText.getStringSwitchListFor().equals(switchListForTextField.getText())
291                && TrainSwitchListText.getStringScheduledWork().equals(scheduledWorkTextField.getText())
292                && TrainSwitchListText.getStringDepartsAt().equals(departsAtTextField.getText())
293                && TrainSwitchListText.getStringDepartsAtExpectedArrival().equals(departsAtExpectedArrivalTextField.getText())
294                && TrainSwitchListText.getStringDepartedExpected().equals(departedExpectedTextField.getText())
295                && TrainSwitchListText.getStringVisitNumber().equals(visitNumberTextField.getText())
296                && TrainSwitchListText.getStringVisitNumberDeparted().equals(visitNumberDepartedTextField.getText())
297                && TrainSwitchListText.getStringVisitNumberTerminates().equals(visitNumberTerminatesTextField.getText())
298                && TrainSwitchListText.getStringVisitNumberTerminatesDeparted().equals(visitNumberTerminatesDepartedTextField.getText())
299                && TrainSwitchListText.getStringVisitNumberDone().equals(visitNumberDoneTextField.getText())
300                && TrainSwitchListText.getStringTrainDirectionChange().equals(trainDirectionChangeTextField.getText())
301                && TrainSwitchListText.getStringNoCarPickUps().equals(noCarPickUpsTextField.getText())
302                && TrainSwitchListText.getStringNoCarDrops().equals(noCarDropsTextField.getText())
303                && TrainSwitchListText.getStringTrainDone().equals(trainDoneTextField.getText()))
304                && TrainSwitchListText.getStringTrainDepartsCars().equals(trainDepartsCarsTextField.getText())
305                && TrainSwitchListText.getStringTrainDepartsLoads().equals(trainDepartsLoadsTextField.getText())
306                && TrainSwitchListText.getStringSwitchListByTrack().equals(switchListByTrackTextField.getText())
307                && TrainSwitchListText.getStringHoldCar().equals(holdCarTextField.getText())
308                ;
309    }
310}