001package jmri.jmrit.operations.setup;
002
003import java.awt.GridBagLayout;
004import java.util.ResourceBundle;
005
006import javax.swing.*;
007
008import jmri.InstanceManager;
009import jmri.jmrit.operations.trains.TrainManager;
010import jmri.jmrit.operations.trains.TrainManifestText;
011
012/**
013 * Frame for user edit of manifest text strings
014 *
015 * @author Dan Boudreau Copyright (C) 2013
016 * 
017 */
018public class EditManifestTextPanel extends OperationsPreferencesPanel {
019
020    protected static final ResourceBundle rb = ResourceBundle
021            .getBundle("jmri.jmrit.operations.trains.JmritOperationsTrainsBundle");
022
023    // major buttons
024    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
025    JButton resetButton = new JButton(Bundle.getMessage("Reset"));
026
027    // text field
028    JTextField manifestForTrainTextField = new JTextField(60);
029    JTextField validTextField = new JTextField(60);
030    JTextField scheduledWorkAtTextField = new JTextField(60);
031    JTextField scheduledWorkDepartureTextField = new JTextField(60);
032    JTextField scheduledWorkArrivalTextField = new JTextField(60);
033    JTextField noScheduledWorkAtTextField = new JTextField(60);
034    JTextField noScheduledWorkAtWithRouteCommentTextField = new JTextField(60);
035    JTextField departTimeTextField = new JTextField(60);
036    JTextField trainDepartsCarsTextField = new JTextField(60);
037    JTextField trainDepartsLoadsTextField = new JTextField(60);
038    JTextField trainTerminatesInTextField = new JTextField(60);
039
040    JTextField destinationTextField = new JTextField(60);
041    JTextField toTextField = new JTextField(25);
042    JTextField fromTextField = new JTextField(25);
043    JTextField destTextField = new JTextField(25);
044    JTextField finalDestinationTextField = new JTextField(25);
045
046    JTextField addHelpersAtTextField = new JTextField(60);
047    JTextField removeHelpersAtTextField = new JTextField(60);
048    JTextField locoChangeAtTextField = new JTextField(60);
049    JTextField cabooseChangeAtTextField = new JTextField(60);
050    JTextField locoAndCabooseChangeAtTextField = new JTextField(60);
051
052    public EditManifestTextPanel() {
053
054        // the following code sets the frame's initial state
055        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
056
057        // manifest text fields
058        JPanel pManifest = new JPanel();
059        JScrollPane pManifestPane = new JScrollPane(pManifest);
060        pManifestPane.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("BorderLayoutManifest")));
061        pManifest.setLayout(new BoxLayout(pManifest, BoxLayout.Y_AXIS));
062
063        JPanel pManifestForTrainTextField = new JPanel();
064        pManifestForTrainTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("ManifestForTrain")));
065        pManifestForTrainTextField.add(manifestForTrainTextField);
066        manifestForTrainTextField.setText(TrainManifestText.getStringManifestForTrain());
067        manifestForTrainTextField.setToolTipText(rb.getString("ToolTipManifestForTrain"));
068        pManifest.add(pManifestForTrainTextField);
069
070        JPanel pValidTextField = new JPanel();
071        pValidTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("Valid")));
072        pValidTextField.add(validTextField);
073        validTextField.setText(TrainManifestText.getStringValid());
074        validTextField.setToolTipText(rb.getString("ToolTipValid"));
075        pManifest.add(pValidTextField);
076
077        JPanel pScheduledWorkAtTextField = new JPanel();
078        pScheduledWorkAtTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("ScheduledWorkAt")));
079        pScheduledWorkAtTextField.add(scheduledWorkAtTextField);
080        scheduledWorkAtTextField.setText(TrainManifestText.getStringScheduledWork());
081        scheduledWorkAtTextField.setToolTipText(rb.getString("ToolTipScheduledWorkAt"));
082        pManifest.add(pScheduledWorkAtTextField);
083
084        JPanel pScheduledWorkDepartureTextField = new JPanel();
085        pScheduledWorkDepartureTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("WorkDepartureTime")));
086        pScheduledWorkDepartureTextField.add(scheduledWorkDepartureTextField);
087        scheduledWorkDepartureTextField.setText(TrainManifestText.getStringWorkDepartureTime());
088        scheduledWorkDepartureTextField.setToolTipText(rb.getString("ToolTipWorkDepartureTime"));
089        pManifest.add(pScheduledWorkDepartureTextField);
090
091        JPanel pScheduledWorkArrivalTextField = new JPanel();
092        pScheduledWorkArrivalTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("WorkArrivalTime")));
093        pScheduledWorkArrivalTextField.add(scheduledWorkArrivalTextField);
094        scheduledWorkArrivalTextField.setText(TrainManifestText.getStringWorkArrivalTime());
095        scheduledWorkArrivalTextField.setToolTipText(rb.getString("ToolTipWorkDepartureTime"));
096        pManifest.add(pScheduledWorkArrivalTextField);
097
098        JPanel pNoScheduledWorkAt = new JPanel();
099        pNoScheduledWorkAt.setBorder(BorderFactory.createTitledBorder(rb.getString("NoScheduledWorkAt")));
100        pNoScheduledWorkAt.add(noScheduledWorkAtTextField);
101        noScheduledWorkAtTextField.setText(TrainManifestText.getStringNoScheduledWork());
102        noScheduledWorkAtTextField.setToolTipText(rb.getString("ToolTipScheduledWorkAt"));
103        pManifest.add(pNoScheduledWorkAt);
104
105        JPanel pNoScheduledWorkAtWithRouteComment = new JPanel();
106        pNoScheduledWorkAtWithRouteComment.setBorder(BorderFactory.createTitledBorder(rb
107                .getString("NoScheduledWorkAtWithRouteComment")));
108        pNoScheduledWorkAtWithRouteComment.add(noScheduledWorkAtWithRouteCommentTextField);
109        noScheduledWorkAtWithRouteCommentTextField
110                .setText(TrainManifestText.getStringNoScheduledWorkWithRouteComment());
111        noScheduledWorkAtWithRouteCommentTextField.setToolTipText(rb
112                .getString("ToolTipNoScheduledWorkAtWithRouteComment"));
113        pManifest.add(pNoScheduledWorkAtWithRouteComment);
114
115        JPanel pDepartTime = new JPanel();
116        pDepartTime.setBorder(BorderFactory.createTitledBorder(rb.getString("departureTime")));
117        pDepartTime.add(departTimeTextField);
118        departTimeTextField.setText(TrainManifestText.getStringDepartTime());
119        departTimeTextField.setToolTipText(rb.getString("ToolTipNoScheduledWorkShowTime"));
120        pManifest.add(pDepartTime);
121
122        JPanel pTrainDepartsCars = new JPanel();
123        pTrainDepartsCars.setBorder(BorderFactory.createTitledBorder(rb.getString("TrainDepartsCars")));
124        pTrainDepartsCars.add(trainDepartsCarsTextField);
125        trainDepartsCarsTextField.setText(TrainManifestText.getStringTrainDepartsCars());
126        trainDepartsCarsTextField.setToolTipText(rb.getString("ToolTipTrainDepartsCars"));
127        pManifest.add(pTrainDepartsCars);
128
129        JPanel pTrainDepartsLoadsTextField = new JPanel();
130        pTrainDepartsLoadsTextField.setBorder(BorderFactory.createTitledBorder(rb.getString("TrainDepartsLoads")));
131        pTrainDepartsLoadsTextField.add(trainDepartsLoadsTextField);
132        trainDepartsLoadsTextField.setText(TrainManifestText.getStringTrainDepartsLoads());
133        trainDepartsLoadsTextField.setToolTipText(rb.getString("ToolTipTrainDepartsLoads"));
134        pManifest.add(pTrainDepartsLoadsTextField);
135
136        JPanel pTrainTerminatesIn = new JPanel();
137        pTrainTerminatesIn.setBorder(BorderFactory.createTitledBorder(rb.getString("TrainTerminatesIn")));
138        pTrainTerminatesIn.add(trainTerminatesInTextField);
139        trainTerminatesInTextField.setText(TrainManifestText.getStringTrainTerminates());
140        trainTerminatesInTextField.setToolTipText(rb.getString("ToolTipScheduledWorkAt"));
141        pManifest.add(pTrainTerminatesIn);
142
143        JPanel pDestination = new JPanel();
144        pDestination.setBorder(BorderFactory.createTitledBorder(rb.getString("destination")));
145        pDestination.add(destinationTextField);
146        destinationTextField.setText(TrainManifestText.getStringDestination());
147        pManifest.add(pDestination);
148
149        JPanel pToFrom = new JPanel();
150        pToFrom.setLayout(new BoxLayout(pToFrom, BoxLayout.X_AXIS));
151
152        JPanel pTo = new JPanel();
153        pTo.setBorder(BorderFactory.createTitledBorder(rb.getString("to")));
154        pTo.add(toTextField);
155        toTextField.setText(TrainManifestText.getStringTo());
156        pToFrom.add(pTo);
157
158        JPanel pFrom = new JPanel();
159        pFrom.setBorder(BorderFactory.createTitledBorder(rb.getString("from")));
160        pFrom.add(fromTextField);
161        fromTextField.setText(TrainManifestText.getStringFrom());
162        pToFrom.add(pFrom);
163
164        pManifest.add(pToFrom);
165
166        JPanel pDestFinalDest = new JPanel();
167        pDestFinalDest.setLayout(new BoxLayout(pDestFinalDest, BoxLayout.X_AXIS));
168
169        JPanel pDest = new JPanel();
170        pDest.setBorder(BorderFactory.createTitledBorder(rb.getString("dest")));
171        pDest.add(destTextField);
172        destTextField.setText(TrainManifestText.getStringDest());
173        pDestFinalDest.add(pDest);
174
175        JPanel pFinalDestination = new JPanel();
176        pFinalDestination.setBorder(BorderFactory.createTitledBorder(rb.getString("FD") + " ("
177                + Bundle.getMessage("FinalDestination") + ")"));
178        pFinalDestination.add(finalDestinationTextField);
179        finalDestinationTextField.setText(TrainManifestText.getStringFinalDestination());
180        pDestFinalDest.add(pFinalDestination);
181
182        pManifest.add(pDestFinalDest);
183
184        JPanel pAddHelpersAt = new JPanel();
185        pAddHelpersAt.setBorder(BorderFactory.createTitledBorder(rb.getString("AddHelpersAt")));
186        pAddHelpersAt.add(addHelpersAtTextField);
187        addHelpersAtTextField.setText(TrainManifestText.getStringAddHelpers());
188        addHelpersAtTextField.setToolTipText(rb.getString("ToolTipAddHelpers"));
189        pManifest.add(pAddHelpersAt);
190
191        JPanel pRemoveHelpersAt = new JPanel();
192        pRemoveHelpersAt.setBorder(BorderFactory.createTitledBorder(rb.getString("RemoveHelpersAt")));
193        pRemoveHelpersAt.add(removeHelpersAtTextField);
194        removeHelpersAtTextField.setText(TrainManifestText.getStringRemoveHelpers());
195        removeHelpersAtTextField.setToolTipText(rb.getString("ToolTipRemoveHelpers"));
196        pManifest.add(pRemoveHelpersAt);
197
198        JPanel pLocoChangeAt = new JPanel();
199        pLocoChangeAt.setBorder(BorderFactory.createTitledBorder(rb.getString("LocoChangeAt")));
200        pLocoChangeAt.add(locoChangeAtTextField);
201        locoChangeAtTextField.setText(TrainManifestText.getStringLocoChange());
202        locoChangeAtTextField.setToolTipText(rb.getString("ToolTipScheduledWorkAt"));
203        pManifest.add(pLocoChangeAt);
204
205        JPanel pCabooseChangeAt = new JPanel();
206        pCabooseChangeAt.setBorder(BorderFactory.createTitledBorder(rb.getString("CabooseChangeAt")));
207        pCabooseChangeAt.add(cabooseChangeAtTextField);
208        cabooseChangeAtTextField.setText(TrainManifestText.getStringCabooseChange());
209        cabooseChangeAtTextField.setToolTipText(rb.getString("ToolTipScheduledWorkAt"));
210        pManifest.add(pCabooseChangeAt);
211
212        JPanel pLocoAndCabooseChangeAt = new JPanel();
213        pLocoAndCabooseChangeAt.setBorder(BorderFactory.createTitledBorder(rb.getString("LocoAndCabooseChangeAt")));
214        pLocoAndCabooseChangeAt.add(locoAndCabooseChangeAtTextField);
215        locoAndCabooseChangeAtTextField.setText(TrainManifestText.getStringLocoAndCabooseChange());
216        locoAndCabooseChangeAtTextField.setToolTipText(rb.getString("ToolTipScheduledWorkAt"));
217        pManifest.add(pLocoAndCabooseChangeAt);
218
219        // add tool tips
220        saveButton.setToolTipText(Bundle.getMessage("SaveToolTip"));
221
222        // row 11
223        JPanel pControl = new JPanel();
224        pControl.setBorder(BorderFactory.createTitledBorder(""));
225        pControl.setLayout(new GridBagLayout());
226        addItem(pControl, resetButton, 0, 0);
227        addItem(pControl, saveButton, 1, 0);
228
229        add(pManifestPane);
230        add(pControl);
231
232        // setup buttons
233        addButtonAction(resetButton);
234        addButtonAction(saveButton);
235    }
236
237    // Save buttons
238    @Override
239    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
240        if (ae.getSource() == resetButton) {
241            manifestForTrainTextField.setText(rb.getString("ManifestForTrain"));
242            validTextField.setText(rb.getString("Valid"));
243            scheduledWorkAtTextField.setText(rb.getString("ScheduledWorkAt"));
244            scheduledWorkDepartureTextField.setText(rb.getString("WorkDepartureTime"));
245            scheduledWorkArrivalTextField.setText(rb.getString("WorkArrivalTime"));
246            noScheduledWorkAtTextField.setText(rb.getString("NoScheduledWorkAt"));
247            noScheduledWorkAtWithRouteCommentTextField.setText(rb.getString("NoScheduledWorkAtWithRouteComment"));
248            departTimeTextField.setText(rb.getString("departureTime"));
249            trainDepartsCarsTextField.setText(rb.getString("TrainDepartsCars"));
250            trainDepartsLoadsTextField.setText(rb.getString("TrainDepartsLoads"));
251            trainTerminatesInTextField.setText(rb.getString("TrainTerminatesIn"));
252
253            destinationTextField.setText(rb.getString("destination"));
254            toTextField.setText(rb.getString("to"));
255            fromTextField.setText(rb.getString("from"));
256            destTextField.setText(rb.getString("dest"));
257            finalDestinationTextField.setText(rb.getString("FD"));
258
259            addHelpersAtTextField.setText(rb.getString("AddHelpersAt"));
260            removeHelpersAtTextField.setText(rb.getString("RemoveHelpersAt"));
261            locoChangeAtTextField.setText(rb.getString("LocoChangeAt"));
262            cabooseChangeAtTextField.setText(rb.getString("CabooseChangeAt"));
263            locoAndCabooseChangeAtTextField.setText(rb.getString("LocoAndCabooseChangeAt"));
264        }
265        if (ae.getSource() == saveButton) {
266            this.savePreferences();
267            var topLevelAncestor = getTopLevelAncestor();
268            if (Setup.isCloseWindowOnSaveEnabled() && topLevelAncestor instanceof EditManifestTextFrame) {
269                ((EditManifestTextFrame) topLevelAncestor).dispose();
270            }
271        }
272    }
273
274    @Override
275    public String getTabbedPreferencesTitle() {
276        return Bundle.getMessage("TitleManifestText");
277    }
278
279    @Override
280    public String getPreferencesTooltip() {
281        return null;
282    }
283
284    @Override
285    public void savePreferences() {
286        TrainManifestText.setStringManifestForTrain(manifestForTrainTextField.getText());
287        TrainManifestText.setStringValid(validTextField.getText());
288        TrainManifestText.setStringScheduledWork(scheduledWorkAtTextField.getText());
289        TrainManifestText.setStringWorkDepartureTime(scheduledWorkDepartureTextField.getText());
290        TrainManifestText.setStringWorkArrivalTime(scheduledWorkArrivalTextField.getText());
291        TrainManifestText.setStringNoScheduledWork(noScheduledWorkAtTextField.getText());
292        TrainManifestText.setStringNoScheduledWorkWithRouteComment(noScheduledWorkAtWithRouteCommentTextField.getText());
293        TrainManifestText.setStringDepartTime(departTimeTextField.getText());
294        TrainManifestText.setStringTrainDepartsCars(trainDepartsCarsTextField.getText());
295        TrainManifestText.setStringTrainDepartsLoads(trainDepartsLoadsTextField.getText());
296        TrainManifestText.setStringTrainTerminates(trainTerminatesInTextField.getText());
297
298        TrainManifestText.setStringDestination(destinationTextField.getText());
299        TrainManifestText.setStringTo(toTextField.getText());
300        TrainManifestText.setStringFrom(fromTextField.getText());
301        TrainManifestText.setStringDest(destTextField.getText());
302        TrainManifestText.setStringFinalDestination(finalDestinationTextField.getText());
303
304        TrainManifestText.setStringAddHelpers(addHelpersAtTextField.getText());
305        TrainManifestText.setStringRemoveHelpers(removeHelpersAtTextField.getText());
306        TrainManifestText.setStringLocoChange(locoChangeAtTextField.getText());
307        TrainManifestText.setStringCabooseChange(cabooseChangeAtTextField.getText());
308        TrainManifestText.setStringLocoAndCabooseChange(locoAndCabooseChangeAtTextField.getText());
309
310        InstanceManager.getDefault(OperationsSetupXml.class).writeOperationsFile();
311
312        // recreate all train manifests
313        InstanceManager.getDefault(TrainManager.class).setTrainsModified();
314    }
315
316    @Override
317    public boolean isDirty() {
318        return !(TrainManifestText.getStringManifestForTrain().equals(manifestForTrainTextField.getText())
319                && TrainManifestText.getStringValid().equals(validTextField.getText())
320                && TrainManifestText.getStringScheduledWork().equals(scheduledWorkAtTextField.getText())
321                && TrainManifestText.getStringWorkDepartureTime().equals(scheduledWorkDepartureTextField.getText())
322                && TrainManifestText.getStringWorkArrivalTime().equals(scheduledWorkArrivalTextField.getText())
323                && TrainManifestText.getStringNoScheduledWork().equals(noScheduledWorkAtTextField.getText())
324                && TrainManifestText.getStringNoScheduledWorkWithRouteComment().equals(noScheduledWorkAtWithRouteCommentTextField.getText())
325                && TrainManifestText.getStringDepartTime().equals(departTimeTextField.getText())
326                && TrainManifestText.getStringTrainDepartsCars().equals(trainDepartsCarsTextField.getText())
327                && TrainManifestText.getStringTrainDepartsLoads().equals(trainDepartsLoadsTextField.getText())
328                && TrainManifestText.getStringTrainTerminates().equals(trainTerminatesInTextField.getText())
329                && TrainManifestText.getStringDestination().equals(destinationTextField.getText())
330                && TrainManifestText.getStringTo().equals(toTextField.getText())
331                && TrainManifestText.getStringFrom().equals(fromTextField.getText())
332                && TrainManifestText.getStringDest().equals(destTextField.getText())
333                && TrainManifestText.getStringFinalDestination().equals(finalDestinationTextField.getText())
334                && TrainManifestText.getStringAddHelpers().equals(addHelpersAtTextField.getText())
335                && TrainManifestText.getStringRemoveHelpers().equals(removeHelpersAtTextField.getText())
336                && TrainManifestText.getStringLocoChange().equals(locoChangeAtTextField.getText())
337                && TrainManifestText.getStringCabooseChange().equals(cabooseChangeAtTextField.getText())
338                && TrainManifestText.getStringLocoAndCabooseChange().equals(locoAndCabooseChangeAtTextField.getText()));
339    }
340}