001package jmri.jmrit.operations.trains.gui;
002
003import java.awt.*;
004import java.util.ArrayList;
005import java.util.List;
006
007import javax.swing.*;
008
009import jmri.InstanceManager;
010import jmri.jmrit.operations.*;
011import jmri.jmrit.operations.locations.Location;
012import jmri.jmrit.operations.locations.LocationManager;
013import jmri.jmrit.operations.rollingstock.RollingStock;
014import jmri.jmrit.operations.rollingstock.cars.*;
015import jmri.jmrit.operations.rollingstock.engines.*;
016import jmri.jmrit.operations.routes.*;
017import jmri.jmrit.operations.routes.gui.RouteEditFrame;
018import jmri.jmrit.operations.setup.Control;
019import jmri.jmrit.operations.setup.Setup;
020import jmri.jmrit.operations.trains.Train;
021import jmri.jmrit.operations.trains.TrainManager;
022import jmri.jmrit.operations.trains.tools.*;
023import jmri.jmrit.operations.trains.trainbuilder.TrainCommon;
024import jmri.util.swing.JmriJOptionPane;
025
026/**
027 * Frame for user edit of a train
028 *
029 * @author Dan Boudreau Copyright (C) 2008, 2011, 2012, 2013, 2014
030 */
031public class TrainEditFrame extends OperationsFrame implements java.beans.PropertyChangeListener {
032
033    TrainManager trainManager = InstanceManager.getDefault(TrainManager.class);
034    RouteManager routeManager = InstanceManager.getDefault(RouteManager.class);
035
036    public Train _train = null;
037    List<JCheckBox> typeCarCheckBoxes = new ArrayList<>();
038    List<JCheckBox> typeEngineCheckBoxes = new ArrayList<>();
039    List<JCheckBox> locationCheckBoxes = new ArrayList<>();
040    JPanel typeCarPanelCheckBoxes = new JPanel();
041    JPanel typeEnginePanelCheckBoxes = new JPanel();
042    JPanel roadAndLoadStatusPanel = new JPanel();
043    JPanel locationPanelCheckBoxes = new JPanel();
044    JScrollPane typeCarPane;
045    JScrollPane typeEnginePane;
046    JScrollPane locationsPane;
047
048    // labels
049    JLabel textRouteStatus = new JLabel();
050    JLabel textModel = new JLabel(Bundle.getMessage("Model"));
051    JLabel textRoad2 = new JLabel(Bundle.getMessage("Road"));
052    JLabel textRoad3 = new JLabel(Bundle.getMessage("Road"));
053    JLabel textEngine = new JLabel(Bundle.getMessage("Engines"));
054
055    // major buttons
056    JButton editButton = new JButton(Bundle.getMessage("ButtonEdit")); // edit route
057    JButton clearButton = new JButton(Bundle.getMessage("ClearAll"));
058    JButton setButton = new JButton(Bundle.getMessage("SelectAll"));
059    JButton resetButton = new JButton(Bundle.getMessage("ResetTrain"));
060    JButton saveTrainButton = new JButton(Bundle.getMessage("SaveTrain"));
061    JButton deleteTrainButton = new JButton(Bundle.getMessage("DeleteTrain"));
062    JButton addTrainButton = new JButton(Bundle.getMessage("AddTrain"));
063
064    // alternate buttons
065    JButton loadOptionButton = new JButton(Bundle.getMessage("AcceptAll"));
066    JButton roadOptionButton = new JButton(Bundle.getMessage("AcceptAll"));
067
068    // radio buttons
069    JRadioButton noneRadioButton = new JRadioButton(Bundle.getMessage("None"));
070    JRadioButton cabooseRadioButton = new JRadioButton(Bundle.getMessage("Caboose"));
071    JRadioButton fredRadioButton = new JRadioButton(Bundle.getMessage("FRED"));
072    ButtonGroup group = new ButtonGroup();
073
074    // text field
075    JTextField trainNameTextField = new JTextField(Control.max_len_string_train_name);
076    JTextField trainDescriptionTextField = new JTextField(30);
077
078    // text area
079    JTextArea commentTextArea = new JTextArea(2, 70);
080    JScrollPane commentScroller = new JScrollPane(commentTextArea);
081    JColorChooser commentColorChooser = new JColorChooser(Color.black);
082
083    // for padding out panel
084    JLabel space1 = new JLabel(" "); // before hour
085    JLabel space2 = new JLabel(" "); // between hour and minute
086    JLabel space3 = new JLabel(" "); // after minute
087    JLabel space4 = new JLabel(" "); // between route and edit
088    JLabel space5 = new JLabel(" "); // after edit
089
090    // combo boxes
091    JComboBox<String> hourBox = new JComboBox<>();
092    JComboBox<String> minuteBox = new JComboBox<>();
093    JComboBox<Route> routeBox = routeManager.getComboBox();
094    JComboBox<String> roadCabooseBox = new JComboBox<>();
095    JComboBox<String> roadEngineBox = new JComboBox<>();
096    JComboBox<String> modelEngineBox = InstanceManager.getDefault(EngineModels.class).getComboBox();
097    JComboBox<String> numEnginesBox = new JComboBox<>();
098
099    JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools"));
100
101    public static final String DISPOSE = "dispose"; // NOI18N
102
103    public TrainEditFrame(Train train) {
104        super(Bundle.getMessage("TitleTrainEdit"));
105        // Set up the jtable in a Scroll Pane..
106        locationsPane = new JScrollPane(locationPanelCheckBoxes);
107        locationsPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
108        locationsPane.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Stops")));
109
110        typeCarPane = new JScrollPane(typeCarPanelCheckBoxes);
111        typeCarPane.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TypesCar")));
112        typeCarPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
113
114        typeEnginePane = new JScrollPane(typeEnginePanelCheckBoxes);
115        typeEnginePane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
116        typeEnginePane.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TypesEngine")));
117
118        _train = train;
119
120        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
121
122        // Set up the panels
123        JPanel p = new JPanel();
124        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
125        JScrollPane pPane = new JScrollPane(p);
126        pPane.setMinimumSize(new Dimension(300, 5 * trainNameTextField.getPreferredSize().height));
127        pPane.setBorder(BorderFactory.createTitledBorder(""));
128
129        // Layout the panel by rows
130        // row 1
131        JPanel p1 = new JPanel();
132        p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
133        // row 1a
134        JPanel pName = new JPanel();
135        pName.setLayout(new GridBagLayout());
136        pName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Name")));
137        addItem(pName, trainNameTextField, 0, 0);
138        // row 1b
139        JPanel pDesc = new JPanel();
140        pDesc.setLayout(new GridBagLayout());
141        pDesc.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Description")));
142        trainDescriptionTextField.setToolTipText(Bundle.getMessage("TipTrainDescription"));
143        addItem(pDesc, trainDescriptionTextField, 0, 0);
144
145        p1.add(pName);
146        p1.add(pDesc);
147
148        // row 2
149        JPanel p2 = new JPanel();
150        p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
151        // row 2a
152        JPanel pdt = new JPanel();
153        pdt.setLayout(new GridBagLayout());
154        pdt.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("DepartTime")));
155
156        // build hour and minute menus
157        hourBox.setPrototypeDisplayValue("0000"); // needed for font size 9
158        minuteBox.setPrototypeDisplayValue("0000");
159        for (int i = 0; i < 24; i++) {
160            if (i < 10) {
161                hourBox.addItem("0" + Integer.toString(i));
162            } else {
163                hourBox.addItem(Integer.toString(i));
164            }
165        }
166        for (int i = 0; i < 60; i += 1) {
167            if (i < 10) {
168                minuteBox.addItem("0" + Integer.toString(i));
169            } else {
170                minuteBox.addItem(Integer.toString(i));
171            }
172        }
173
174        addItem(pdt, space1, 0, 5);
175        addItem(pdt, hourBox, 1, 5);
176        addItem(pdt, space2, 2, 5);
177        addItem(pdt, minuteBox, 3, 5);
178        addItem(pdt, space3, 4, 5);
179        // row 2b
180        // BUG! routeBox needs its own panel when resizing frame!
181        JPanel pr = new JPanel();
182        pr.setLayout(new GridBagLayout());
183        pr.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Route")));
184        addItem(pr, routeBox, 0, 5);
185        addItem(pr, space4, 1, 5);
186        addItem(pr, editButton, 2, 5);
187        addItem(pr, space5, 3, 5);
188        addItem(pr, textRouteStatus, 4, 5);
189
190        p2.add(pdt);
191        p2.add(pr);
192
193        p.add(p1);
194        p.add(p2);
195
196        // row 5
197        locationPanelCheckBoxes.setLayout(new GridBagLayout());
198
199        // row 6
200        typeCarPanelCheckBoxes.setLayout(new GridBagLayout());
201
202        // row 8
203        typeEnginePanelCheckBoxes.setLayout(new GridBagLayout());
204
205        // status panel for roads and loads
206        roadAndLoadStatusPanel.setLayout(new BoxLayout(roadAndLoadStatusPanel, BoxLayout.X_AXIS));
207        JPanel pRoadOption = new JPanel();
208        pRoadOption.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("RoadOption")));
209        pRoadOption.add(roadOptionButton);
210        roadOptionButton.addActionListener(new TrainRoadOptionsAction(this));
211
212        JPanel pLoadOption = new JPanel();
213        pLoadOption.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("LoadOption")));
214        pLoadOption.add(loadOptionButton);
215        loadOptionButton.addActionListener(new TrainLoadOptionsAction(this));
216
217        roadAndLoadStatusPanel.add(pRoadOption);
218        roadAndLoadStatusPanel.add(pLoadOption);
219        roadAndLoadStatusPanel.setVisible(false); // don't show unless there's a restriction
220
221        // row 10
222        JPanel trainReq = new JPanel();
223        trainReq.setLayout(new GridBagLayout());
224        trainReq.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TrainRequires")));
225
226        for (int i = 0; i < Setup.getMaxNumberEngines() + 1; i++) {
227            numEnginesBox.addItem(Integer.toString(i));
228        }
229        numEnginesBox.addItem(Train.AUTO);
230        numEnginesBox.setMinimumSize(new Dimension(100, 20));
231        numEnginesBox.setToolTipText(Bundle.getMessage("TipNumberOfLocos"));
232        addItem(trainReq, textEngine, 1, 1);
233        addItem(trainReq, numEnginesBox, 2, 1);
234        addItem(trainReq, textModel, 3, 1);
235        modelEngineBox.insertItemAt(NONE, 0);
236        modelEngineBox.setSelectedIndex(0);
237        modelEngineBox.setMinimumSize(new Dimension(120, 20));
238        modelEngineBox.setToolTipText(Bundle.getMessage("ModelEngineTip"));
239        addItem(trainReq, modelEngineBox, 4, 1);
240        addItem(trainReq, textRoad2, 5, 1);
241        roadEngineBox.insertItemAt(NONE, 0);
242        roadEngineBox.setSelectedIndex(0);
243        roadEngineBox.setMinimumSize(new Dimension(120, 20));
244        roadEngineBox.setToolTipText(Bundle.getMessage("RoadEngineTip"));
245        addItem(trainReq, roadEngineBox, 6, 1);
246
247        JPanel trainLastCar = new JPanel();
248        trainLastCar.setLayout(new GridBagLayout());
249        trainLastCar.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("TrainLastCar")));
250
251        addItem(trainLastCar, noneRadioButton, 2, 2);
252        noneRadioButton.setToolTipText(Bundle.getMessage("TipNoCabooseOrFRED"));
253        addItem(trainLastCar, fredRadioButton, 3, 2);
254        fredRadioButton.setToolTipText(Bundle.getMessage("TipFRED"));
255        addItem(trainLastCar, cabooseRadioButton, 4, 2);
256        cabooseRadioButton.setToolTipText(Bundle.getMessage("TipCaboose"));
257        addItem(trainLastCar, textRoad3, 5, 2);
258        roadCabooseBox.setMinimumSize(new Dimension(120, 20));
259        roadCabooseBox.setToolTipText(Bundle.getMessage("RoadCabooseTip"));
260        addItem(trainLastCar, roadCabooseBox, 6, 2);
261        group.add(noneRadioButton);
262        group.add(cabooseRadioButton);
263        group.add(fredRadioButton);
264        noneRadioButton.setSelected(true);
265
266        // row 13 comment
267        JPanel pC = new JPanel();
268        pC.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Comment")));
269        pC.setLayout(new GridBagLayout());
270        addItem(pC, commentScroller, 1, 0);
271        if (_train != null) {
272            addItem(pC, OperationsPanel.getColorChooserPanel(_train.getCommentWithColor(), commentColorChooser), 2, 0);
273        } else {
274            addItem(pC, OperationsPanel.getColorChooserPanel("", commentColorChooser), 2, 0);
275        }
276
277        // adjust text area width based on window size less color chooser
278        Dimension d = new Dimension(getPreferredSize().width - 100, getPreferredSize().height);
279        adjustTextAreaColumnWidth(commentScroller, commentTextArea, d);
280
281        // row 15 buttons
282        JPanel pB = new JPanel();
283        pB.setLayout(new GridBagLayout());
284        addItem(pB, deleteTrainButton, 0, 0);
285        addItem(pB, resetButton, 1, 0);
286        addItem(pB, addTrainButton, 2, 0);
287        addItem(pB, saveTrainButton, 3, 0);
288
289        getContentPane().add(pPane);
290        getContentPane().add(locationsPane);
291        getContentPane().add(typeCarPane);
292        getContentPane().add(typeEnginePane);
293        getContentPane().add(roadAndLoadStatusPanel);
294        getContentPane().add(trainReq);
295        getContentPane().add(trainLastCar);
296        getContentPane().add(pC);
297        getContentPane().add(pB);
298
299        // setup buttons
300        addButtonAction(editButton);
301        addButtonAction(setButton);
302        addButtonAction(clearButton);
303        addButtonAction(resetButton);
304        addButtonAction(deleteTrainButton);
305        addButtonAction(addTrainButton);
306        addButtonAction(saveTrainButton);
307
308        addRadioButtonAction(noneRadioButton);
309        addRadioButtonAction(cabooseRadioButton);
310        addRadioButtonAction(fredRadioButton);
311
312        // tool tips
313        resetButton.setToolTipText(Bundle.getMessage("TipTrainReset"));
314
315        // build menu
316        JMenuBar menuBar = new JMenuBar();
317        menuBar.add(toolMenu);
318        loadToolMenu(toolMenu);
319        setJMenuBar(menuBar);
320        addHelpMenu("package.jmri.jmrit.operations.Operations_TrainEdit", true); // NOI18N
321
322        if (_train != null) {
323            trainNameTextField.setText(_train.getName());
324            trainDescriptionTextField.setText(_train.getRawDescription());
325            routeBox.setSelectedItem(_train.getRoute());
326            modelEngineBox.setSelectedItem(_train.getEngineModel());
327            commentTextArea.setText(TrainCommon.getTextColorString(_train.getCommentWithColor()));
328            cabooseRadioButton.setSelected(_train.isCabooseNeeded());
329            fredRadioButton.setSelected(_train.isFredNeeded());
330            updateDepartureTime();
331            enableButtons(true);
332            // listen for train changes
333            _train.addPropertyChangeListener(this);
334
335            Route route = _train.getRoute();
336            if (route != null) {
337                if (_train.getTrainDepartsRouteLocation() != null &&
338                        _train.getTrainDepartsRouteLocation().getLocation() != null &&
339                        !_train.getTrainDepartsRouteLocation().getLocation().isStaging())
340                    numEnginesBox.addItem(Train.AUTO_HPT);
341            }
342            numEnginesBox.setSelectedItem(_train.getNumberEngines());
343        } else {
344            setTitle(Bundle.getMessage("TitleTrainAdd"));
345            enableButtons(false);
346        }
347
348        modelEngineBox.setEnabled(!numEnginesBox.getSelectedItem().equals("0"));
349        roadEngineBox.setEnabled(!numEnginesBox.getSelectedItem().equals("0"));
350
351        // load route location checkboxes
352        updateLocationCheckboxes();
353        updateCarTypeCheckboxes();
354        updateEngineTypeCheckboxes();
355        updateRoadAndLoadStatus();
356        updateCabooseRoadComboBox();
357        updateEngineRoadComboBox();
358
359        // setup combobox
360        addComboBoxAction(numEnginesBox);
361        addComboBoxAction(routeBox);
362        addComboBoxAction(modelEngineBox);
363
364        // get notified if combo box gets modified
365        routeManager.addPropertyChangeListener(this);
366        // get notified if car types or roads gets modified
367        InstanceManager.getDefault(CarTypes.class).addPropertyChangeListener(this);
368        InstanceManager.getDefault(CarRoads.class).addPropertyChangeListener(this);
369        InstanceManager.getDefault(EngineTypes.class).addPropertyChangeListener(this);
370        InstanceManager.getDefault(EngineModels.class).addPropertyChangeListener(this);
371        InstanceManager.getDefault(LocationManager.class).addPropertyChangeListener(this);
372
373        initMinimumSize(new Dimension(Control.panelWidth600, Control.panelHeight600));
374    }
375
376    private void loadToolMenu(JMenu toolMenu) {
377        toolMenu.removeAll();
378        // first 5 menu items will also close when the edit train window closes
379        toolMenu.add(new TrainEditBuildOptionsAction(this));
380        toolMenu.add(new TrainLoadOptionsAction(this));
381        toolMenu.add(new TrainRoadOptionsAction(this));
382        toolMenu.add(new TrainManifestOptionAction(this));
383        toolMenu.add(new TrainCopyAction(_train));
384        toolMenu.addSeparator();
385        toolMenu.add(new TrainScriptAction(this));
386        toolMenu.add(new TrainConductorAction(_train));
387        toolMenu.addSeparator();
388        toolMenu.add(new TrainByCarTypeAction(_train));
389        toolMenu.addSeparator();
390        toolMenu.add(new PrintTrainAction(false, _train));
391        toolMenu.add(new PrintTrainAction(true, _train));
392        toolMenu.add(new PrintTrainManifestAction(false, _train));
393        toolMenu.add(new PrintTrainManifestAction(true, _train));
394        toolMenu.add(new PrintShowCarsInTrainRouteAction(false, _train));
395        toolMenu.add(new PrintShowCarsInTrainRouteAction(true, _train));
396        toolMenu.add(new PrintTrainBuildReportAction(false, _train));
397        toolMenu.add(new PrintTrainBuildReportAction(true, _train));
398        toolMenu.add(new PrintSavedTrainManifestAction(false, _train));
399        toolMenu.add(new PrintSavedTrainManifestAction(true, _train));
400        toolMenu.add(new PrintSavedBuildReportAction(false, _train));
401        toolMenu.add(new PrintSavedBuildReportAction(true, _train));
402    }
403
404    // Save, Delete, Add, Edit, Reset, Set, Clear
405    @Override
406    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
407        Train train = trainManager.getTrainByName(trainNameTextField.getText().trim());
408        if (ae.getSource() == saveTrainButton) {
409            log.debug("train save button activated");
410            if (_train == null && train == null) {
411                saveNewTrain(); // this can't happen, Save button is disabled
412            } else {
413                if (train != null && train != _train) {
414                    reportTrainExists(Bundle.getMessage("save"));
415                    return;
416                }
417                // check to see if user supplied a route
418                if (!checkRoute() || !saveTrain()) {
419                    return;
420                }
421            }
422            if (Setup.isCloseWindowOnSaveEnabled()) {
423                dispose();
424            }
425        }
426        if (ae.getSource() == deleteTrainButton) {
427            log.debug("train delete button activated");
428            if (train == null) {
429                return;
430            }
431            if (!_train.reset()) {
432                JmriJOptionPane.showMessageDialog(this,
433                        Bundle.getMessage("TrainIsInRoute",
434                                train.getTrainTerminatesName()),
435                        Bundle.getMessage("CanNotDeleteTrain"), JmriJOptionPane.ERROR_MESSAGE);
436                return;
437            }
438            if (JmriJOptionPane.showConfirmDialog(this,
439                    Bundle.getMessage("deleteMsg", train.getName()),
440                    Bundle.getMessage("deleteTrain"), JmriJOptionPane.YES_NO_OPTION) != JmriJOptionPane.YES_OPTION) {
441                return;
442            }
443            routeBox.setSelectedItem(null);
444            trainManager.deregister(train);
445            for (Frame frame : children) {
446                frame.dispose();
447            }
448            _train = null;
449            enableButtons(false);
450            // save train file
451            OperationsXml.save();
452        }
453        if (ae.getSource() == addTrainButton) {
454            if (train != null) {
455                reportTrainExists(Bundle.getMessage("add"));
456                return;
457            }
458            saveNewTrain();
459        }
460        if (ae.getSource() == editButton) {
461            editAddRoute();
462        }
463        if (ae.getSource() == setButton) {
464            selectCheckboxes(true);
465        }
466        if (ae.getSource() == clearButton) {
467            selectCheckboxes(false);
468        }
469        if (ae.getSource() == resetButton) {
470            if (_train != null) {
471                if (!_train.reset()) {
472                    JmriJOptionPane.showMessageDialog(this,
473                            Bundle.getMessage("TrainIsInRoute",
474                                    _train.getTrainTerminatesName()),
475                            Bundle.getMessage("CanNotResetTrain"), JmriJOptionPane.ERROR_MESSAGE);
476                }
477            }
478        }
479    }
480
481    @Override
482    public void radioButtonActionPerformed(java.awt.event.ActionEvent ae) {
483        log.debug("radio button activated");
484        if (_train != null) {
485            if (ae.getSource() == noneRadioButton ||
486                    ae.getSource() == cabooseRadioButton ||
487                    ae.getSource() == fredRadioButton) {
488                updateCabooseRoadComboBox();
489            }
490        }
491    }
492
493    private void saveNewTrain() {
494        if (!checkName(Bundle.getMessage("add"))) {
495            return;
496        }
497        Train train = trainManager.newTrain(trainNameTextField.getText());
498        _train = train;
499        _train.addPropertyChangeListener(this);
500
501        // update check boxes
502        updateCarTypeCheckboxes();
503        updateEngineTypeCheckboxes();
504        // enable check boxes and buttons
505        enableButtons(true);
506        saveTrain();
507        loadToolMenu(toolMenu);
508    }
509
510    private boolean saveTrain() {
511        if (!checkName(Bundle.getMessage("save"))) {
512            return false;
513        }
514        if (!checkModel() || !checkEngineRoad() || !checkCabooseRoad()) {
515            return false;
516        }
517        if (!_train.getName().equals(trainNameTextField.getText().trim()) ||
518                !_train.getRawDescription().equals(trainDescriptionTextField.getText()) ||
519                !_train.getCommentWithColor().equals(
520                        TrainCommon.formatColorString(commentTextArea.getText(), commentColorChooser.getColor()))) {
521            _train.setModified(true);
522        }
523        _train.setDepartureTime(hourBox.getSelectedItem().toString(), minuteBox.getSelectedItem().toString());
524        _train.setNumberEngines((String) numEnginesBox.getSelectedItem());
525        if (_train.getNumberEngines().equals("0")) {
526            modelEngineBox.setSelectedIndex(0);
527            roadEngineBox.setSelectedIndex(0);
528        }
529        _train.setEngineRoad((String) roadEngineBox.getSelectedItem());
530        _train.setEngineModel((String) modelEngineBox.getSelectedItem());
531        if (cabooseRadioButton.isSelected()) {
532            _train.setRequirements(Train.CABOOSE);
533        }
534        if (fredRadioButton.isSelected()) {
535            _train.setRequirements(Train.FRED);
536        }
537        if (noneRadioButton.isSelected()) {
538            _train.setRequirements(Train.NO_CABOOSE_OR_FRED);
539        }
540        _train.setCabooseRoad((String) roadCabooseBox.getSelectedItem());
541        _train.setName(trainNameTextField.getText().trim());
542        _train.setDescription(trainDescriptionTextField.getText());
543        _train.setComment(TrainCommon.formatColorString(commentTextArea.getText(), commentColorChooser.getColor()));
544        // save train file
545        OperationsXml.save();
546        return true;
547    }
548
549    /**
550     *
551     * @return true if name isn't too long and is at least one character
552     */
553    private boolean checkName(String s) {
554        String trainName = trainNameTextField.getText().trim();
555        if (trainName.isEmpty()) {
556            log.debug("Must enter a train name");
557            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("MustEnterName"),
558                    Bundle.getMessage("CanNot", s), JmriJOptionPane.ERROR_MESSAGE);
559            return false;
560        }
561        if (trainName.length() > Control.max_len_string_train_name) {
562            JmriJOptionPane.showMessageDialog(this,
563                    Bundle.getMessage("TrainNameLess",
564                            Control.max_len_string_train_name + 1),
565                    Bundle.getMessage("CanNot", s), JmriJOptionPane.ERROR_MESSAGE);
566            return false;
567        }
568        if (!OperationsXml.checkFileName(trainName)) { // NOI18N
569            log.error("Train name must not contain reserved characters");
570            JmriJOptionPane.showMessageDialog(this,
571                    Bundle.getMessage("NameResChar") + NEW_LINE + Bundle.getMessage("ReservedChar"),
572                    Bundle.getMessage("CanNot", s), JmriJOptionPane.ERROR_MESSAGE);
573            return false;
574        }
575
576        return true;
577    }
578
579    private boolean checkModel() {
580        String model = (String) modelEngineBox.getSelectedItem();
581        if (numEnginesBox.getSelectedItem().equals("0") || model.equals(NONE)) {
582            return true;
583        }
584        String type = InstanceManager.getDefault(EngineModels.class).getModelType(model);
585        if (!_train.isTypeNameAccepted(type)) {
586            JmriJOptionPane.showMessageDialog(this,
587                    Bundle.getMessage("TrainModelService", model, type),
588                    Bundle.getMessage("CanNot", Bundle.getMessage("save")),
589                    JmriJOptionPane.ERROR_MESSAGE);
590            return false;
591        }
592        if (roadEngineBox.getItemCount() == 1) {
593            log.debug("No locos available that match the model selected!");
594            JmriJOptionPane.showMessageDialog(this,
595                    Bundle.getMessage("NoLocosModel", model),
596                    Bundle.getMessage("TrainWillNotBuild", _train.getName()),
597                    JmriJOptionPane.WARNING_MESSAGE);
598        }
599        return true;
600    }
601
602    private boolean checkEngineRoad() {
603        String road = (String) roadEngineBox.getSelectedItem();
604        if (numEnginesBox.getSelectedItem().equals("0") || road.equals(NONE)) {
605            return true;
606        }
607        if (!road.equals(NONE) && !_train.isLocoRoadNameAccepted(road)) {
608            JmriJOptionPane.showMessageDialog(this,
609                    Bundle.getMessage("TrainNotThisRoad", _train.getName(), road),
610                    Bundle.getMessage("TrainWillNotBuild", _train.getName()),
611                    JmriJOptionPane.WARNING_MESSAGE);
612            return false;
613        }
614        for (RollingStock rs : InstanceManager.getDefault(EngineManager.class).getList()) {
615            if (!_train.isTypeNameAccepted(rs.getTypeName())) {
616                continue;
617            }
618            if (rs.getRoadName().equals(road)) {
619                return true;
620            }
621        }
622        JmriJOptionPane.showMessageDialog(this,
623                Bundle.getMessage("NoLocoRoad", road),
624                Bundle.getMessage("TrainWillNotBuild", _train.getName()),
625                JmriJOptionPane.WARNING_MESSAGE);
626        return false; // couldn't find a loco with the selected road
627    }
628
629    private boolean checkCabooseRoad() {
630        String road = (String) roadCabooseBox.getSelectedItem();
631        if (!road.equals(NONE) && cabooseRadioButton.isSelected() && !_train.isCabooseRoadNameAccepted(road)) {
632            JmriJOptionPane.showMessageDialog(this,
633                    Bundle.getMessage("TrainNotCabooseRoad", _train.getName(), road),
634                    Bundle.getMessage("TrainWillNotBuild", _train.getName()),
635                    JmriJOptionPane.WARNING_MESSAGE);
636            return false;
637        }
638        return true;
639    }
640
641    private boolean checkRoute() {
642        if (_train.getRoute() == null) {
643            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("TrainNeedsRoute"), Bundle.getMessage("TrainNoRoute"),
644                    JmriJOptionPane.WARNING_MESSAGE);
645            return false;
646        }
647        return true;
648
649    }
650
651    private void reportTrainExists(String s) {
652        log.debug("Can not {}, train already exists", s);
653        JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("TrainNameExists"),
654                Bundle.getMessage("CanNot", s), JmriJOptionPane.ERROR_MESSAGE);
655    }
656
657    private void enableButtons(boolean enabled) {
658        toolMenu.setEnabled(enabled);
659        editButton.setEnabled(enabled);
660        routeBox.setEnabled(enabled && _train != null && !_train.isBuilt());
661        clearButton.setEnabled(enabled);
662        resetButton.setEnabled(enabled);
663        setButton.setEnabled(enabled);
664        saveTrainButton.setEnabled(enabled);
665        deleteTrainButton.setEnabled(enabled);
666        numEnginesBox.setEnabled(enabled);
667        enableCheckboxes(enabled);
668        noneRadioButton.setEnabled(enabled);
669        fredRadioButton.setEnabled(enabled);
670        cabooseRadioButton.setEnabled(enabled);
671        roadOptionButton.setEnabled(enabled);
672        loadOptionButton.setEnabled(enabled);
673        // the inverse!
674        addTrainButton.setEnabled(!enabled);
675    }
676
677    private void selectCheckboxes(boolean enable) {
678        for (int i = 0; i < typeCarCheckBoxes.size(); i++) {
679            JCheckBox checkBox = typeCarCheckBoxes.get(i);
680            checkBox.setSelected(enable);
681            if (_train != null) {
682                _train.removePropertyChangeListener(this);
683                if (enable) {
684                    _train.addTypeName(checkBox.getText());
685                } else {
686                    _train.deleteTypeName(checkBox.getText());
687                }
688                _train.addPropertyChangeListener(this);
689            }
690        }
691    }
692
693    @Override
694    public void comboBoxActionPerformed(java.awt.event.ActionEvent ae) {
695        if (_train == null) {
696            return;
697        }
698        if (ae.getSource() == numEnginesBox) {
699            modelEngineBox.setEnabled(!numEnginesBox.getSelectedItem().equals("0"));
700            roadEngineBox.setEnabled(!numEnginesBox.getSelectedItem().equals("0"));
701        }
702        if (ae.getSource() == modelEngineBox) {
703            updateEngineRoadComboBox();
704        }
705        if (ae.getSource() == routeBox) {
706            if (routeBox.isEnabled()) {
707                Route route = _train.getRoute();
708                if (route != null) {
709                    route.removePropertyChangeListener(this);
710                }
711                Object selected = routeBox.getSelectedItem();
712                if (selected != null) {
713                    route = (Route) selected;
714                    _train.setRoute(route);
715                    route.addPropertyChangeListener(this);
716                } else {
717                    _train.setRoute(null);
718                }
719                updateLocationCheckboxes();
720                updateDepartureTime();
721                pack();
722                repaint();
723            }
724        }
725    }
726
727    private void enableCheckboxes(boolean enable) {
728        for (int i = 0; i < typeCarCheckBoxes.size(); i++) {
729            typeCarCheckBoxes.get(i).setEnabled(enable);
730        }
731        for (int i = 0; i < typeEngineCheckBoxes.size(); i++) {
732            typeEngineCheckBoxes.get(i).setEnabled(enable);
733        }
734    }
735
736    private void addLocationCheckBoxAction(JCheckBox b) {
737        b.addActionListener(new java.awt.event.ActionListener() {
738            @Override
739            public void actionPerformed(java.awt.event.ActionEvent e) {
740                locationCheckBoxActionPerformed(e);
741            }
742        });
743    }
744
745    public void locationCheckBoxActionPerformed(java.awt.event.ActionEvent ae) {
746        JCheckBox b = (JCheckBox) ae.getSource();
747        log.debug("checkbox change {}", b.getText());
748        if (_train == null) {
749            return;
750        }
751        String id = b.getName();
752        RouteLocation rl = _train.getRoute().getRouteLocationById(id);
753        if (b.isSelected()) {
754            _train.deleteTrainSkipsLocation(rl);
755        } else {
756            // check to see if skipped location is staging
757            if (_train.getRoute().getRouteLocationById(id).getLocation().isStaging()) {
758                int result = JmriJOptionPane.showConfirmDialog(this,
759                        Bundle.getMessage("TrainRouteStaging",
760                                _train.getName(), _train.getRoute().getRouteLocationById(id).getName()),
761                        Bundle.getMessage("TrainRouteNotStaging"), JmriJOptionPane.OK_CANCEL_OPTION);
762                if (result != JmriJOptionPane.OK_OPTION ) {
763                    b.setSelected(true);
764                    return; // don't skip staging
765                }
766            }
767            _train.addTrainSkipsLocation(rl);
768        }
769    }
770
771    private void updateRouteComboBox() {
772        routeBox.setEnabled(false);
773        routeManager.updateComboBox(routeBox);
774        if (_train != null) {
775            routeBox.setSelectedItem(_train.getRoute());
776        }
777        routeBox.setEnabled(true);
778    }
779
780    private void updateCarTypeCheckboxes() {
781        typeCarCheckBoxes.clear();
782        typeCarPanelCheckBoxes.removeAll();
783        loadCarTypes();
784        enableCheckboxes(_train != null);
785        typeCarPanelCheckBoxes.revalidate();
786        repaint();
787    }
788
789    private void loadCarTypes() {
790        int numberOfCheckboxes = getNumberOfCheckboxesPerLine(); // number per line
791        int x = 0;
792        int y = 1; // vertical position in panel
793        for (String type : InstanceManager.getDefault(CarTypes.class).getNames()) {
794            JCheckBox checkBox = new javax.swing.JCheckBox();
795            typeCarCheckBoxes.add(checkBox);
796            checkBox.setText(type);
797            addTypeCheckBoxAction(checkBox);
798            addItemLeft(typeCarPanelCheckBoxes, checkBox, x++, y);
799            if (_train != null && _train.isTypeNameAccepted(type)) {
800                checkBox.setSelected(true);
801            }
802            if (x > numberOfCheckboxes) {
803                y++;
804                x = 0;
805            }
806        }
807
808        JPanel p = new JPanel();
809        p.add(clearButton);
810        p.add(setButton);
811        GridBagConstraints gc = new GridBagConstraints();
812        gc.gridwidth = getNumberOfCheckboxesPerLine() + 1;
813        gc.gridy = ++y;
814        typeCarPanelCheckBoxes.add(p, gc);
815
816    }
817
818    private void updateEngineTypeCheckboxes() {
819        typeEngineCheckBoxes.clear();
820        typeEnginePanelCheckBoxes.removeAll();
821        loadEngineTypes();
822        enableCheckboxes(_train != null);
823        typeEnginePanelCheckBoxes.revalidate();
824        repaint();
825    }
826
827    private void loadEngineTypes() {
828        int numberOfCheckboxes = getNumberOfCheckboxesPerLine(); // number per line
829        int x = 0;
830        int y = 1;
831        for (String type : InstanceManager.getDefault(EngineTypes.class).getNames()) {
832            JCheckBox checkBox = new javax.swing.JCheckBox();
833            typeEngineCheckBoxes.add(checkBox);
834            checkBox.setText(type);
835            addTypeCheckBoxAction(checkBox);
836            addItemLeft(typeEnginePanelCheckBoxes, checkBox, x++, y);
837            if (_train != null && _train.isTypeNameAccepted(type)) {
838                checkBox.setSelected(true);
839            }
840            if (x > numberOfCheckboxes) {
841                y++;
842                x = 0;
843            }
844        }
845    }
846
847    private void updateRoadComboBoxes() {
848        updateCabooseRoadComboBox();
849        updateEngineRoadComboBox();
850    }
851
852    // update caboose road box based on radio selection
853    private void updateCabooseRoadComboBox() {
854        roadCabooseBox.removeAllItems();
855        roadCabooseBox.addItem(NONE);
856        if (noneRadioButton.isSelected()) {
857            roadCabooseBox.setEnabled(false);
858            return;
859        }
860        roadCabooseBox.setEnabled(true);
861        List<String> roads;
862        if (cabooseRadioButton.isSelected()) {
863            roads = InstanceManager.getDefault(CarManager.class).getCabooseRoadNames();
864        } else {
865            roads = InstanceManager.getDefault(CarManager.class).getFredRoadNames();
866        }
867        for (String road : roads) {
868            roadCabooseBox.addItem(road);
869        }
870        if (_train != null) {
871            roadCabooseBox.setSelectedItem(_train.getCabooseRoad());
872        }
873        OperationsPanel.padComboBox(roadCabooseBox);
874    }
875
876    private void updateEngineRoadComboBox() {
877        String engineModel = (String) modelEngineBox.getSelectedItem();
878        if (engineModel == null) {
879            return;
880        }
881        InstanceManager.getDefault(EngineManager.class).updateEngineRoadComboBox(engineModel, roadEngineBox);
882        if (_train != null) {
883            roadEngineBox.setSelectedItem(_train.getEngineRoad());
884        }
885    }
886
887    private void addTypeCheckBoxAction(JCheckBox b) {
888        b.addActionListener(new java.awt.event.ActionListener() {
889            @Override
890            public void actionPerformed(java.awt.event.ActionEvent e) {
891                typeCheckBoxActionPerformed(e);
892            }
893        });
894    }
895
896    public void typeCheckBoxActionPerformed(java.awt.event.ActionEvent ae) {
897        JCheckBox b = (JCheckBox) ae.getSource();
898        log.debug("checkbox change {}", b.getText());
899        if (_train == null) {
900            return;
901        }
902        if (b.isSelected()) {
903            _train.addTypeName(b.getText());
904        } else {
905            _train.deleteTypeName(b.getText());
906        }
907    }
908
909    // the train's route shown as locations with checkboxes
910    private void updateLocationCheckboxes() {
911        updateRouteStatus();
912        locationCheckBoxes.clear();
913        locationPanelCheckBoxes.removeAll();
914        int y = 0; // vertical position in panel
915        Route route = null;
916        if (_train != null) {
917            route = _train.getRoute();
918        }
919        if (route != null) {
920            List<RouteLocation> routeList = route.getLocationsBySequenceList();
921            for (RouteLocation rl : routeList) {
922                JCheckBox checkBox = new javax.swing.JCheckBox();
923                locationCheckBoxes.add(checkBox);
924                checkBox.setText(rl.toString());
925                checkBox.setName(rl.getId());
926                addItemLeft(locationPanelCheckBoxes, checkBox, 0, y++);
927                Location loc = InstanceManager.getDefault(LocationManager.class).getLocationByName(rl.getName());
928                // does the location exist?
929                if (loc != null) {
930                    // need to listen for name and direction changes
931                    loc.removePropertyChangeListener(this);
932                    loc.addPropertyChangeListener(this);
933                    boolean services = false;
934                    // does train direction service location?
935                    if ((rl.getTrainDirection() & loc.getTrainDirections()) != 0) {
936                        services = true;
937                    } // train must service last location or single location
938                    else if (_train.isLocalSwitcher() || rl == _train.getTrainTerminatesRouteLocation()) {
939                        services = true;
940                    }
941                    // check can drop and pick up, and moves > 0
942                    if (services &&
943                            (rl.isDropAllowed() || rl.isPickUpAllowed() || rl.isLocalMovesAllowed()) &&
944                            rl.getMaxCarMoves() > 0) {
945                        checkBox.setSelected(!_train.isLocationSkipped(rl));
946                    } else {
947                        checkBox.setEnabled(false);
948                    }
949                    addLocationCheckBoxAction(checkBox);
950                } else {
951                    checkBox.setEnabled(false);
952                }
953            }
954        }
955        locationPanelCheckBoxes.revalidate();
956    }
957
958    private void updateRouteStatus() {
959        Route route = null;
960        textRouteStatus.setText(NONE); // clear out previous status
961        if (_train != null) {
962            route = _train.getRoute();
963        }
964        if (route != null) {
965            if (!route.getStatus().equals(Route.OKAY)) {
966                textRouteStatus.setText(route.getStatus());
967                textRouteStatus.setForeground(Color.RED);
968            }
969        }
970    }
971
972    RouteEditFrame ref;
973
974    private void editAddRoute() {
975        log.debug("Edit/add route");
976        if (ref != null) {
977            ref.dispose();
978        }
979        ref = new RouteEditFrame();
980        setChildFrame(ref);
981        Route route = null;
982        Object selected = routeBox.getSelectedItem();
983        if (selected != null) {
984            route = (Route) selected;
985        }
986        // warn user if train is built that they shouldn't edit the train's route
987        if (route != null && route.getStatus().equals(Route.TRAIN_BUILT)) {
988            // list the built trains for this route
989            StringBuffer buf = new StringBuffer(Bundle.getMessage("DoNotModifyRoute"));
990            for (Train train : InstanceManager.getDefault(TrainManager.class).getTrainsByIdList()) {
991                if (train.getRoute() == route && train.isBuilt()) {
992                    buf.append(NEW_LINE +
993                            Bundle.getMessage("TrainIsBuilt",
994                                    train.getName(), route.getName()));
995                }
996            }
997            JmriJOptionPane.showMessageDialog(this, buf.toString(), Bundle.getMessage("BuiltTrain"),
998                    JmriJOptionPane.WARNING_MESSAGE);
999        }
1000        ref.initComponents(route, _train);
1001    }
1002
1003    private void updateDepartureTime() {
1004        hourBox.setSelectedItem(_train.getDepartureTimeHour());
1005        minuteBox.setSelectedItem(_train.getDepartureTimeMinute());
1006        // check to see if route has a departure time from the 1st location
1007        RouteLocation rl = _train.getTrainDepartsRouteLocation();
1008        if (rl != null && !rl.getDepartureTime().equals(NONE)) {
1009            hourBox.setEnabled(false);
1010            minuteBox.setEnabled(false);
1011        } else {
1012            hourBox.setEnabled(!_train.isBuilt());
1013            minuteBox.setEnabled(!_train.isBuilt());
1014        }
1015    }
1016
1017    private void updateRoadAndLoadStatus() {
1018        if (_train != null) {
1019            // road options
1020            if (_train.getCarRoadOption().equals(Train.INCLUDE_ROADS)) {
1021                roadOptionButton.setText(Bundle.getMessage(
1022                        "AcceptOnly") + " " + _train.getCarRoadNames().length + " " + Bundle.getMessage("RoadsCar"));
1023            } else if (_train.getCarRoadOption().equals(Train.EXCLUDE_ROADS)) {
1024                roadOptionButton.setText(Bundle.getMessage(
1025                        "Exclude") + " " + _train.getCarRoadNames().length + " " + Bundle.getMessage("RoadsCar"));
1026            } else if (_train.getCabooseRoadOption().equals(Train.INCLUDE_ROADS)) {
1027                roadOptionButton.setText(Bundle.getMessage(
1028                        "AcceptOnly") +
1029                        " " +
1030                        _train.getCabooseRoadNames().length +
1031                        " " +
1032                        Bundle.getMessage("RoadsCaboose"));
1033            } else if (_train.getCabooseRoadOption().equals(Train.EXCLUDE_ROADS)) {
1034                roadOptionButton.setText(Bundle.getMessage(
1035                        "Exclude") +
1036                        " " +
1037                        _train.getCabooseRoadNames().length +
1038                        " " +
1039                        Bundle.getMessage("RoadsCaboose"));
1040            } else if (_train.getLocoRoadOption().equals(Train.INCLUDE_ROADS)) {
1041                roadOptionButton.setText(Bundle.getMessage(
1042                        "AcceptOnly") + " " + _train.getLocoRoadNames().length + " " + Bundle.getMessage("RoadsLoco"));
1043            } else if (_train.getLocoRoadOption().equals(Train.EXCLUDE_ROADS)) {
1044                roadOptionButton.setText(Bundle.getMessage(
1045                        "Exclude") + " " + _train.getLocoRoadNames().length + " " + Bundle.getMessage("RoadsLoco"));
1046            } else {
1047                roadOptionButton.setText(Bundle.getMessage("AcceptAll"));
1048            }
1049            // load options
1050            if (_train.getLoadOption().equals(Train.ALL_LOADS)) {
1051                loadOptionButton.setText(Bundle.getMessage("AcceptAll"));
1052            } else if (_train.getLoadOption().equals(Train.INCLUDE_LOADS)) {
1053                loadOptionButton.setText(Bundle.getMessage(
1054                        "AcceptOnly") + " " + _train.getLoadNames().length + " " + Bundle.getMessage("Loads"));
1055            } else {
1056                loadOptionButton.setText(Bundle.getMessage(
1057                        "Exclude") + " " + _train.getLoadNames().length + " " + Bundle.getMessage("Loads"));
1058            }
1059            if (!_train.getCarRoadOption().equals(Train.ALL_ROADS) ||
1060                    !_train.getCabooseRoadOption().equals(Train.ALL_ROADS) ||
1061                    !_train.getLocoRoadOption().equals(Train.ALL_ROADS) ||
1062                    !_train.getLoadOption().equals(Train.ALL_LOADS)) {
1063                roadAndLoadStatusPanel.setVisible(true);
1064            }
1065        }
1066    }
1067
1068    List<Frame> children = new ArrayList<>();
1069
1070    public void setChildFrame(Frame frame) {
1071        if (children.contains(frame)) {
1072            return;
1073        }
1074        children.add(frame);
1075    }
1076
1077    @Override
1078    public void dispose() {
1079        InstanceManager.getDefault(LocationManager.class).removePropertyChangeListener(this);
1080        InstanceManager.getDefault(EngineTypes.class).removePropertyChangeListener(this);
1081        InstanceManager.getDefault(EngineModels.class).removePropertyChangeListener(this);
1082        InstanceManager.getDefault(CarTypes.class).removePropertyChangeListener(this);
1083        InstanceManager.getDefault(CarRoads.class).removePropertyChangeListener(this);
1084        routeManager.removePropertyChangeListener(this);
1085        for (Frame frame : children) {
1086            frame.dispose();
1087        }
1088        if (_train != null) {
1089            _train.removePropertyChangeListener(this);
1090            Route route = _train.getRoute();
1091            if (route != null) {
1092                for (RouteLocation rl : route.getLocationsBySequenceList()) {
1093                    Location loc = rl.getLocation();
1094                    if (loc != null) {
1095                        loc.removePropertyChangeListener(this);
1096                    }
1097                }
1098            }
1099        }
1100        super.dispose();
1101    }
1102
1103    @Override
1104    public void propertyChange(java.beans.PropertyChangeEvent e) {
1105        if (Control.SHOW_PROPERTY) {
1106            log.debug("Property change ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(),
1107                    e.getNewValue()); // NOI18N
1108        }
1109        if (e.getPropertyName().equals(CarTypes.CARTYPES_CHANGED_PROPERTY) ||
1110                e.getPropertyName().equals(Train.TYPES_CHANGED_PROPERTY)) {
1111            updateCarTypeCheckboxes();
1112        }
1113        if (e.getPropertyName().equals(EngineTypes.ENGINETYPES_CHANGED_PROPERTY)) {
1114            updateEngineTypeCheckboxes();
1115        }
1116        if (e.getPropertyName().equals(RouteManager.LISTLENGTH_CHANGED_PROPERTY)) {
1117            updateRouteComboBox();
1118        }
1119        if (e.getPropertyName().equals(Route.LISTCHANGE_CHANGED_PROPERTY) ||
1120                e.getPropertyName().equals(LocationManager.LISTLENGTH_CHANGED_PROPERTY) ||
1121                e.getPropertyName().equals(Location.NAME_CHANGED_PROPERTY) ||
1122                e.getPropertyName().equals(Location.TRAIN_DIRECTION_CHANGED_PROPERTY)) {
1123            updateLocationCheckboxes();
1124            pack();
1125            repaint();
1126        }
1127        if (e.getPropertyName().equals(CarRoads.CARROADS_CHANGED_PROPERTY)) {
1128            updateRoadComboBoxes();
1129        }
1130        if (e.getPropertyName().equals(EngineModels.ENGINEMODELS_CHANGED_PROPERTY)) {
1131            InstanceManager.getDefault(EngineModels.class).updateComboBox(modelEngineBox);
1132            modelEngineBox.insertItemAt(NONE, 0);
1133            modelEngineBox.setSelectedIndex(0);
1134            if (_train != null) {
1135                modelEngineBox.setSelectedItem(_train.getEngineModel());
1136            }
1137        }
1138        if (e.getPropertyName().equals(Train.DEPARTURETIME_CHANGED_PROPERTY)) {
1139            updateDepartureTime();
1140        }
1141        if (e.getPropertyName().equals(Train.TRAIN_ROUTE_CHANGED_PROPERTY) && _train != null) {
1142            routeBox.setSelectedItem(_train.getRoute());
1143        }
1144        if (e.getPropertyName().equals(Route.ROUTE_STATUS_CHANGED_PROPERTY)) {
1145            updateDepartureTime();
1146            enableButtons(_train != null);
1147            updateRouteStatus();
1148        }
1149        if (e.getPropertyName().equals(Train.ROADS_CHANGED_PROPERTY) ||
1150                e.getPropertyName().equals(Train.LOADS_CHANGED_PROPERTY)) {
1151            updateRoadAndLoadStatus();
1152        }
1153    }
1154
1155    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TrainEditFrame.class);
1156}