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 4 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.addSeparator();
384        toolMenu.add(new TrainCopyAction(_train));
385        toolMenu.addSeparator();
386        // scripts window closes when the edit train window closes
387        toolMenu.add(new TrainScriptAction(this));
388        toolMenu.addSeparator();
389        toolMenu.add(new TrainConductorAction(_train));
390        toolMenu.addSeparator();
391        toolMenu.add(new TrainByCarTypeAction(_train));
392        toolMenu.addSeparator();
393        toolMenu.add(new PrintTrainAction(false, _train));
394        toolMenu.add(new PrintTrainAction(true, _train));
395        toolMenu.add(new PrintTrainManifestAction(false, _train));
396        toolMenu.add(new PrintTrainManifestAction(true, _train));
397        toolMenu.add(new PrintShowCarsInTrainRouteAction(false, _train));
398        toolMenu.add(new PrintShowCarsInTrainRouteAction(true, _train));
399        toolMenu.add(new PrintTrainBuildReportAction(false, _train));
400        toolMenu.add(new PrintTrainBuildReportAction(true, _train));
401        toolMenu.add(new PrintSavedTrainManifestAction(false, _train));
402        toolMenu.add(new PrintSavedTrainManifestAction(true, _train));
403        toolMenu.add(new PrintSavedBuildReportAction(false, _train));
404        toolMenu.add(new PrintSavedBuildReportAction(true, _train));
405    }
406
407    // Save, Delete, Add, Edit, Reset, Set, Clear
408    @Override
409    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
410        Train train = trainManager.getTrainByName(trainNameTextField.getText().trim());
411        if (ae.getSource() == saveTrainButton) {
412            log.debug("train save button activated");
413            if (_train == null && train == null) {
414                saveNewTrain(); // this can't happen, Save button is disabled
415            } else {
416                if (train != null && train != _train) {
417                    reportTrainExists(Bundle.getMessage("save"));
418                    return;
419                }
420                // check to see if user supplied a route
421                if (!checkRoute() || !saveTrain()) {
422                    return;
423                }
424            }
425            if (Setup.isCloseWindowOnSaveEnabled()) {
426                dispose();
427            }
428        }
429        if (ae.getSource() == deleteTrainButton) {
430            log.debug("train delete button activated");
431            if (train == null) {
432                return;
433            }
434            if (!_train.reset()) {
435                JmriJOptionPane.showMessageDialog(this,
436                        Bundle.getMessage("TrainIsInRoute",
437                                train.getTrainTerminatesName()),
438                        Bundle.getMessage("CanNotDeleteTrain"), JmriJOptionPane.ERROR_MESSAGE);
439                return;
440            }
441            if (JmriJOptionPane.showConfirmDialog(this,
442                    Bundle.getMessage("deleteMsg", train.getName()),
443                    Bundle.getMessage("deleteTrain"), JmriJOptionPane.YES_NO_OPTION) != JmriJOptionPane.YES_OPTION) {
444                return;
445            }
446            routeBox.setSelectedItem(null);
447            trainManager.deregister(train);
448            for (Frame frame : children) {
449                frame.dispose();
450            }
451            _train = null;
452            enableButtons(false);
453            // save train file
454            OperationsXml.save();
455        }
456        if (ae.getSource() == addTrainButton) {
457            if (train != null) {
458                reportTrainExists(Bundle.getMessage("add"));
459                return;
460            }
461            saveNewTrain();
462        }
463        if (ae.getSource() == editButton) {
464            editAddRoute();
465        }
466        if (ae.getSource() == setButton) {
467            selectCheckboxes(true);
468        }
469        if (ae.getSource() == clearButton) {
470            selectCheckboxes(false);
471        }
472        if (ae.getSource() == resetButton) {
473            if (_train != null) {
474                if (!_train.reset()) {
475                    JmriJOptionPane.showMessageDialog(this,
476                            Bundle.getMessage("TrainIsInRoute",
477                                    _train.getTrainTerminatesName()),
478                            Bundle.getMessage("CanNotResetTrain"), JmriJOptionPane.ERROR_MESSAGE);
479                }
480            }
481        }
482    }
483
484    @Override
485    public void radioButtonActionPerformed(java.awt.event.ActionEvent ae) {
486        log.debug("radio button activated");
487        if (_train != null) {
488            if (ae.getSource() == noneRadioButton ||
489                    ae.getSource() == cabooseRadioButton ||
490                    ae.getSource() == fredRadioButton) {
491                updateCabooseRoadComboBox();
492            }
493        }
494    }
495
496    private void saveNewTrain() {
497        if (!checkName(Bundle.getMessage("add"))) {
498            return;
499        }
500        Train train = trainManager.newTrain(trainNameTextField.getText());
501        _train = train;
502        _train.addPropertyChangeListener(this);
503
504        // update check boxes
505        updateCarTypeCheckboxes();
506        updateEngineTypeCheckboxes();
507        // enable check boxes and buttons
508        enableButtons(true);
509        saveTrain();
510        loadToolMenu(toolMenu);
511    }
512
513    private boolean saveTrain() {
514        if (!checkName(Bundle.getMessage("save"))) {
515            return false;
516        }
517        if (!checkModel() || !checkEngineRoad() || !checkCabooseRoad()) {
518            return false;
519        }
520        if (!_train.getName().equals(trainNameTextField.getText().trim()) ||
521                !_train.getRawDescription().equals(trainDescriptionTextField.getText()) ||
522                !_train.getCommentWithColor().equals(
523                        TrainCommon.formatColorString(commentTextArea.getText(), commentColorChooser.getColor()))) {
524            _train.setModified(true);
525        }
526        _train.setDepartureTime(hourBox.getSelectedItem().toString(), minuteBox.getSelectedItem().toString());
527        _train.setNumberEngines((String) numEnginesBox.getSelectedItem());
528        if (_train.getNumberEngines().equals("0")) {
529            modelEngineBox.setSelectedIndex(0);
530            roadEngineBox.setSelectedIndex(0);
531        }
532        _train.setEngineRoad((String) roadEngineBox.getSelectedItem());
533        _train.setEngineModel((String) modelEngineBox.getSelectedItem());
534        if (cabooseRadioButton.isSelected()) {
535            _train.setRequirements(Train.CABOOSE);
536        }
537        if (fredRadioButton.isSelected()) {
538            _train.setRequirements(Train.FRED);
539        }
540        if (noneRadioButton.isSelected()) {
541            _train.setRequirements(Train.NO_CABOOSE_OR_FRED);
542        }
543        _train.setCabooseRoad((String) roadCabooseBox.getSelectedItem());
544        _train.setName(trainNameTextField.getText().trim());
545        _train.setDescription(trainDescriptionTextField.getText());
546        _train.setComment(TrainCommon.formatColorString(commentTextArea.getText(), commentColorChooser.getColor()));
547        // save train file
548        OperationsXml.save();
549        return true;
550    }
551
552    /**
553     *
554     * @return true if name isn't too long and is at least one character
555     */
556    private boolean checkName(String s) {
557        String trainName = trainNameTextField.getText().trim();
558        if (trainName.isEmpty()) {
559            log.debug("Must enter a train name");
560            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("MustEnterName"),
561                    Bundle.getMessage("CanNot", s), JmriJOptionPane.ERROR_MESSAGE);
562            return false;
563        }
564        if (trainName.length() > Control.max_len_string_train_name) {
565            JmriJOptionPane.showMessageDialog(this,
566                    Bundle.getMessage("TrainNameLess",
567                            Control.max_len_string_train_name + 1),
568                    Bundle.getMessage("CanNot", s), JmriJOptionPane.ERROR_MESSAGE);
569            return false;
570        }
571        if (!OperationsXml.checkFileName(trainName)) { // NOI18N
572            log.error("Train name must not contain reserved characters");
573            JmriJOptionPane.showMessageDialog(this,
574                    Bundle.getMessage("NameResChar") + NEW_LINE + Bundle.getMessage("ReservedChar"),
575                    Bundle.getMessage("CanNot", s), JmriJOptionPane.ERROR_MESSAGE);
576            return false;
577        }
578
579        return true;
580    }
581
582    private boolean checkModel() {
583        String model = (String) modelEngineBox.getSelectedItem();
584        if (numEnginesBox.getSelectedItem().equals("0") || model.equals(NONE)) {
585            return true;
586        }
587        String type = InstanceManager.getDefault(EngineModels.class).getModelType(model);
588        if (!_train.isTypeNameAccepted(type)) {
589            JmriJOptionPane.showMessageDialog(this,
590                    Bundle.getMessage("TrainModelService", model, type),
591                    Bundle.getMessage("CanNot", Bundle.getMessage("save")),
592                    JmriJOptionPane.ERROR_MESSAGE);
593            return false;
594        }
595        if (roadEngineBox.getItemCount() == 1) {
596            log.debug("No locos available that match the model selected!");
597            JmriJOptionPane.showMessageDialog(this,
598                    Bundle.getMessage("NoLocosModel", model),
599                    Bundle.getMessage("TrainWillNotBuild", _train.getName()),
600                    JmriJOptionPane.WARNING_MESSAGE);
601        }
602        return true;
603    }
604
605    private boolean checkEngineRoad() {
606        String road = (String) roadEngineBox.getSelectedItem();
607        if (numEnginesBox.getSelectedItem().equals("0") || road.equals(NONE)) {
608            return true;
609        }
610        if (!road.equals(NONE) && !_train.isLocoRoadNameAccepted(road)) {
611            JmriJOptionPane.showMessageDialog(this,
612                    Bundle.getMessage("TrainNotThisRoad", _train.getName(), road),
613                    Bundle.getMessage("TrainWillNotBuild", _train.getName()),
614                    JmriJOptionPane.WARNING_MESSAGE);
615            return false;
616        }
617        for (RollingStock rs : InstanceManager.getDefault(EngineManager.class).getList()) {
618            if (!_train.isTypeNameAccepted(rs.getTypeName())) {
619                continue;
620            }
621            if (rs.getRoadName().equals(road)) {
622                return true;
623            }
624        }
625        JmriJOptionPane.showMessageDialog(this,
626                Bundle.getMessage("NoLocoRoad", road),
627                Bundle.getMessage("TrainWillNotBuild", _train.getName()),
628                JmriJOptionPane.WARNING_MESSAGE);
629        return false; // couldn't find a loco with the selected road
630    }
631
632    private boolean checkCabooseRoad() {
633        String road = (String) roadCabooseBox.getSelectedItem();
634        if (!road.equals(NONE) && cabooseRadioButton.isSelected() && !_train.isCabooseRoadNameAccepted(road)) {
635            JmriJOptionPane.showMessageDialog(this,
636                    Bundle.getMessage("TrainNotCabooseRoad", _train.getName(), road),
637                    Bundle.getMessage("TrainWillNotBuild", _train.getName()),
638                    JmriJOptionPane.WARNING_MESSAGE);
639            return false;
640        }
641        return true;
642    }
643
644    private boolean checkRoute() {
645        if (_train.getRoute() == null) {
646            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("TrainNeedsRoute"), Bundle.getMessage("TrainNoRoute"),
647                    JmriJOptionPane.WARNING_MESSAGE);
648            return false;
649        }
650        return true;
651
652    }
653
654    private void reportTrainExists(String s) {
655        log.debug("Can not {}, train already exists", s);
656        JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("TrainNameExists"),
657                Bundle.getMessage("CanNot", s), JmriJOptionPane.ERROR_MESSAGE);
658    }
659
660    private void enableButtons(boolean enabled) {
661        toolMenu.setEnabled(enabled);
662        editButton.setEnabled(enabled);
663        routeBox.setEnabled(enabled && _train != null && !_train.isBuilt());
664        clearButton.setEnabled(enabled);
665        resetButton.setEnabled(enabled);
666        setButton.setEnabled(enabled);
667        saveTrainButton.setEnabled(enabled);
668        deleteTrainButton.setEnabled(enabled);
669        numEnginesBox.setEnabled(enabled);
670        enableCheckboxes(enabled);
671        noneRadioButton.setEnabled(enabled);
672        fredRadioButton.setEnabled(enabled);
673        cabooseRadioButton.setEnabled(enabled);
674        roadOptionButton.setEnabled(enabled);
675        loadOptionButton.setEnabled(enabled);
676        // the inverse!
677        addTrainButton.setEnabled(!enabled);
678    }
679
680    private void selectCheckboxes(boolean enable) {
681        for (int i = 0; i < typeCarCheckBoxes.size(); i++) {
682            JCheckBox checkBox = typeCarCheckBoxes.get(i);
683            checkBox.setSelected(enable);
684            if (_train != null) {
685                _train.removePropertyChangeListener(this);
686                if (enable) {
687                    _train.addTypeName(checkBox.getText());
688                } else {
689                    _train.deleteTypeName(checkBox.getText());
690                }
691                _train.addPropertyChangeListener(this);
692            }
693        }
694    }
695
696    @Override
697    public void comboBoxActionPerformed(java.awt.event.ActionEvent ae) {
698        if (_train == null) {
699            return;
700        }
701        if (ae.getSource() == numEnginesBox) {
702            modelEngineBox.setEnabled(!numEnginesBox.getSelectedItem().equals("0"));
703            roadEngineBox.setEnabled(!numEnginesBox.getSelectedItem().equals("0"));
704        }
705        if (ae.getSource() == modelEngineBox) {
706            updateEngineRoadComboBox();
707        }
708        if (ae.getSource() == routeBox) {
709            if (routeBox.isEnabled()) {
710                Route route = _train.getRoute();
711                if (route != null) {
712                    route.removePropertyChangeListener(this);
713                }
714                Object selected = routeBox.getSelectedItem();
715                if (selected != null) {
716                    route = (Route) selected;
717                    _train.setRoute(route);
718                    route.addPropertyChangeListener(this);
719                } else {
720                    _train.setRoute(null);
721                }
722                updateLocationCheckboxes();
723                updateDepartureTime();
724                pack();
725                repaint();
726            }
727        }
728    }
729
730    private void enableCheckboxes(boolean enable) {
731        for (int i = 0; i < typeCarCheckBoxes.size(); i++) {
732            typeCarCheckBoxes.get(i).setEnabled(enable);
733        }
734        for (int i = 0; i < typeEngineCheckBoxes.size(); i++) {
735            typeEngineCheckBoxes.get(i).setEnabled(enable);
736        }
737    }
738
739    private void addLocationCheckBoxAction(JCheckBox b) {
740        b.addActionListener(new java.awt.event.ActionListener() {
741            @Override
742            public void actionPerformed(java.awt.event.ActionEvent e) {
743                locationCheckBoxActionPerformed(e);
744            }
745        });
746    }
747
748    public void locationCheckBoxActionPerformed(java.awt.event.ActionEvent ae) {
749        JCheckBox b = (JCheckBox) ae.getSource();
750        log.debug("checkbox change {}", b.getText());
751        if (_train == null) {
752            return;
753        }
754        String id = b.getName();
755        RouteLocation rl = _train.getRoute().getRouteLocationById(id);
756        if (b.isSelected()) {
757            _train.deleteTrainSkipsLocation(rl);
758        } else {
759            // check to see if skipped location is staging
760            if (_train.getRoute().getRouteLocationById(id).getLocation().isStaging()) {
761                int result = JmriJOptionPane.showConfirmDialog(this,
762                        Bundle.getMessage("TrainRouteStaging",
763                                _train.getName(), _train.getRoute().getRouteLocationById(id).getName()),
764                        Bundle.getMessage("TrainRouteNotStaging"), JmriJOptionPane.OK_CANCEL_OPTION);
765                if (result != JmriJOptionPane.OK_OPTION ) {
766                    b.setSelected(true);
767                    return; // don't skip staging
768                }
769            }
770            _train.addTrainSkipsLocation(rl);
771        }
772    }
773
774    private void updateRouteComboBox() {
775        routeBox.setEnabled(false);
776        routeManager.updateComboBox(routeBox);
777        if (_train != null) {
778            routeBox.setSelectedItem(_train.getRoute());
779        }
780        routeBox.setEnabled(true);
781    }
782
783    private void updateCarTypeCheckboxes() {
784        typeCarCheckBoxes.clear();
785        typeCarPanelCheckBoxes.removeAll();
786        loadCarTypes();
787        enableCheckboxes(_train != null);
788        typeCarPanelCheckBoxes.revalidate();
789        repaint();
790    }
791
792    private void loadCarTypes() {
793        int numberOfCheckboxes = getNumberOfCheckboxesPerLine(); // number per line
794        int x = 0;
795        int y = 1; // vertical position in panel
796        for (String type : InstanceManager.getDefault(CarTypes.class).getNames()) {
797            JCheckBox checkBox = new javax.swing.JCheckBox();
798            typeCarCheckBoxes.add(checkBox);
799            checkBox.setText(type);
800            addTypeCheckBoxAction(checkBox);
801            addItemLeft(typeCarPanelCheckBoxes, checkBox, x++, y);
802            if (_train != null && _train.isTypeNameAccepted(type)) {
803                checkBox.setSelected(true);
804            }
805            if (x > numberOfCheckboxes) {
806                y++;
807                x = 0;
808            }
809        }
810
811        JPanel p = new JPanel();
812        p.add(clearButton);
813        p.add(setButton);
814        GridBagConstraints gc = new GridBagConstraints();
815        gc.gridwidth = getNumberOfCheckboxesPerLine() + 1;
816        gc.gridy = ++y;
817        typeCarPanelCheckBoxes.add(p, gc);
818
819    }
820
821    private void updateEngineTypeCheckboxes() {
822        typeEngineCheckBoxes.clear();
823        typeEnginePanelCheckBoxes.removeAll();
824        loadEngineTypes();
825        enableCheckboxes(_train != null);
826        typeEnginePanelCheckBoxes.revalidate();
827        repaint();
828    }
829
830    private void loadEngineTypes() {
831        int numberOfCheckboxes = getNumberOfCheckboxesPerLine(); // number per line
832        int x = 0;
833        int y = 1;
834        for (String type : InstanceManager.getDefault(EngineTypes.class).getNames()) {
835            JCheckBox checkBox = new javax.swing.JCheckBox();
836            typeEngineCheckBoxes.add(checkBox);
837            checkBox.setText(type);
838            addTypeCheckBoxAction(checkBox);
839            addItemLeft(typeEnginePanelCheckBoxes, checkBox, x++, y);
840            if (_train != null && _train.isTypeNameAccepted(type)) {
841                checkBox.setSelected(true);
842            }
843            if (x > numberOfCheckboxes) {
844                y++;
845                x = 0;
846            }
847        }
848    }
849
850    private void updateRoadComboBoxes() {
851        updateCabooseRoadComboBox();
852        updateEngineRoadComboBox();
853    }
854
855    // update caboose road box based on radio selection
856    private void updateCabooseRoadComboBox() {
857        roadCabooseBox.removeAllItems();
858        roadCabooseBox.addItem(NONE);
859        if (noneRadioButton.isSelected()) {
860            roadCabooseBox.setEnabled(false);
861            return;
862        }
863        roadCabooseBox.setEnabled(true);
864        List<String> roads;
865        if (cabooseRadioButton.isSelected()) {
866            roads = InstanceManager.getDefault(CarManager.class).getCabooseRoadNames();
867        } else {
868            roads = InstanceManager.getDefault(CarManager.class).getFredRoadNames();
869        }
870        for (String road : roads) {
871            roadCabooseBox.addItem(road);
872        }
873        if (_train != null) {
874            roadCabooseBox.setSelectedItem(_train.getCabooseRoad());
875        }
876        OperationsPanel.padComboBox(roadCabooseBox);
877    }
878
879    private void updateEngineRoadComboBox() {
880        String engineModel = (String) modelEngineBox.getSelectedItem();
881        if (engineModel == null) {
882            return;
883        }
884        InstanceManager.getDefault(EngineManager.class).updateEngineRoadComboBox(engineModel, roadEngineBox);
885        if (_train != null) {
886            roadEngineBox.setSelectedItem(_train.getEngineRoad());
887        }
888    }
889
890    private void addTypeCheckBoxAction(JCheckBox b) {
891        b.addActionListener(new java.awt.event.ActionListener() {
892            @Override
893            public void actionPerformed(java.awt.event.ActionEvent e) {
894                typeCheckBoxActionPerformed(e);
895            }
896        });
897    }
898
899    public void typeCheckBoxActionPerformed(java.awt.event.ActionEvent ae) {
900        JCheckBox b = (JCheckBox) ae.getSource();
901        log.debug("checkbox change {}", b.getText());
902        if (_train == null) {
903            return;
904        }
905        if (b.isSelected()) {
906            _train.addTypeName(b.getText());
907        } else {
908            _train.deleteTypeName(b.getText());
909        }
910    }
911
912    // the train's route shown as locations with checkboxes
913    private void updateLocationCheckboxes() {
914        updateRouteStatus();
915        locationCheckBoxes.clear();
916        locationPanelCheckBoxes.removeAll();
917        int y = 0; // vertical position in panel
918        Route route = null;
919        if (_train != null) {
920            route = _train.getRoute();
921        }
922        if (route != null) {
923            List<RouteLocation> routeList = route.getLocationsBySequenceList();
924            for (RouteLocation rl : routeList) {
925                JCheckBox checkBox = new javax.swing.JCheckBox();
926                locationCheckBoxes.add(checkBox);
927                checkBox.setText(rl.toString());
928                checkBox.setName(rl.getId());
929                addItemLeft(locationPanelCheckBoxes, checkBox, 0, y++);
930                Location loc = InstanceManager.getDefault(LocationManager.class).getLocationByName(rl.getName());
931                // does the location exist?
932                if (loc != null) {
933                    // need to listen for name and direction changes
934                    loc.removePropertyChangeListener(this);
935                    loc.addPropertyChangeListener(this);
936                    boolean services = false;
937                    // does train direction service location?
938                    if ((rl.getTrainDirection() & loc.getTrainDirections()) != 0) {
939                        services = true;
940                    } // train must service last location or single location
941                    else if (_train.isLocalSwitcher() || rl == _train.getTrainTerminatesRouteLocation()) {
942                        services = true;
943                    }
944                    // check can drop and pick up, and moves > 0
945                    if (services &&
946                            (rl.isDropAllowed() || rl.isPickUpAllowed() || rl.isLocalMovesAllowed()) &&
947                            rl.getMaxCarMoves() > 0) {
948                        checkBox.setSelected(!_train.isLocationSkipped(rl));
949                    } else {
950                        checkBox.setEnabled(false);
951                    }
952                    addLocationCheckBoxAction(checkBox);
953                } else {
954                    checkBox.setEnabled(false);
955                }
956            }
957        }
958        locationPanelCheckBoxes.revalidate();
959    }
960
961    private void updateRouteStatus() {
962        Route route = null;
963        textRouteStatus.setText(NONE); // clear out previous status
964        if (_train != null) {
965            route = _train.getRoute();
966        }
967        if (route != null) {
968            if (!route.getStatus().equals(Route.OKAY)) {
969                textRouteStatus.setText(route.getStatus());
970                textRouteStatus.setForeground(Color.RED);
971            }
972        }
973    }
974
975    RouteEditFrame ref;
976
977    private void editAddRoute() {
978        log.debug("Edit/add route");
979        if (ref != null) {
980            ref.dispose();
981        }
982        ref = new RouteEditFrame();
983        setChildFrame(ref);
984        Route route = null;
985        Object selected = routeBox.getSelectedItem();
986        if (selected != null) {
987            route = (Route) selected;
988        }
989        // warn user if train is built that they shouldn't edit the train's route
990        if (route != null && route.getStatus().equals(Route.TRAIN_BUILT)) {
991            // list the built trains for this route
992            StringBuffer buf = new StringBuffer(Bundle.getMessage("DoNotModifyRoute"));
993            for (Train train : InstanceManager.getDefault(TrainManager.class).getTrainsByIdList()) {
994                if (train.getRoute() == route && train.isBuilt()) {
995                    buf.append(NEW_LINE +
996                            Bundle.getMessage("TrainIsBuilt",
997                                    train.getName(), route.getName()));
998                }
999            }
1000            JmriJOptionPane.showMessageDialog(this, buf.toString(), Bundle.getMessage("BuiltTrain"),
1001                    JmriJOptionPane.WARNING_MESSAGE);
1002        }
1003        ref.initComponents(route, _train);
1004    }
1005
1006    private void updateDepartureTime() {
1007        hourBox.setSelectedItem(_train.getDepartureTimeHour());
1008        minuteBox.setSelectedItem(_train.getDepartureTimeMinute());
1009        // check to see if route has a departure time from the 1st location
1010        RouteLocation rl = _train.getTrainDepartsRouteLocation();
1011        if (rl != null && !rl.getDepartureTime().equals(NONE)) {
1012            hourBox.setEnabled(false);
1013            minuteBox.setEnabled(false);
1014        } else {
1015            hourBox.setEnabled(!_train.isBuilt());
1016            minuteBox.setEnabled(!_train.isBuilt());
1017        }
1018    }
1019
1020    private void updateRoadAndLoadStatus() {
1021        if (_train != null) {
1022            // road options
1023            if (_train.getCarRoadOption().equals(Train.INCLUDE_ROADS)) {
1024                roadOptionButton.setText(Bundle.getMessage(
1025                        "AcceptOnly") + " " + _train.getCarRoadNames().length + " " + Bundle.getMessage("RoadsCar"));
1026            } else if (_train.getCarRoadOption().equals(Train.EXCLUDE_ROADS)) {
1027                roadOptionButton.setText(Bundle.getMessage(
1028                        "Exclude") + " " + _train.getCarRoadNames().length + " " + Bundle.getMessage("RoadsCar"));
1029            } else if (_train.getCabooseRoadOption().equals(Train.INCLUDE_ROADS)) {
1030                roadOptionButton.setText(Bundle.getMessage(
1031                        "AcceptOnly") +
1032                        " " +
1033                        _train.getCabooseRoadNames().length +
1034                        " " +
1035                        Bundle.getMessage("RoadsCaboose"));
1036            } else if (_train.getCabooseRoadOption().equals(Train.EXCLUDE_ROADS)) {
1037                roadOptionButton.setText(Bundle.getMessage(
1038                        "Exclude") +
1039                        " " +
1040                        _train.getCabooseRoadNames().length +
1041                        " " +
1042                        Bundle.getMessage("RoadsCaboose"));
1043            } else if (_train.getLocoRoadOption().equals(Train.INCLUDE_ROADS)) {
1044                roadOptionButton.setText(Bundle.getMessage(
1045                        "AcceptOnly") + " " + _train.getLocoRoadNames().length + " " + Bundle.getMessage("RoadsLoco"));
1046            } else if (_train.getLocoRoadOption().equals(Train.EXCLUDE_ROADS)) {
1047                roadOptionButton.setText(Bundle.getMessage(
1048                        "Exclude") + " " + _train.getLocoRoadNames().length + " " + Bundle.getMessage("RoadsLoco"));
1049            } else {
1050                roadOptionButton.setText(Bundle.getMessage("AcceptAll"));
1051            }
1052            // load options
1053            if (_train.getLoadOption().equals(Train.ALL_LOADS)) {
1054                loadOptionButton.setText(Bundle.getMessage("AcceptAll"));
1055            } else if (_train.getLoadOption().equals(Train.INCLUDE_LOADS)) {
1056                loadOptionButton.setText(Bundle.getMessage(
1057                        "AcceptOnly") + " " + _train.getLoadNames().length + " " + Bundle.getMessage("Loads"));
1058            } else {
1059                loadOptionButton.setText(Bundle.getMessage(
1060                        "Exclude") + " " + _train.getLoadNames().length + " " + Bundle.getMessage("Loads"));
1061            }
1062            if (!_train.getCarRoadOption().equals(Train.ALL_ROADS) ||
1063                    !_train.getCabooseRoadOption().equals(Train.ALL_ROADS) ||
1064                    !_train.getLocoRoadOption().equals(Train.ALL_ROADS) ||
1065                    !_train.getLoadOption().equals(Train.ALL_LOADS)) {
1066                roadAndLoadStatusPanel.setVisible(true);
1067            }
1068        }
1069    }
1070
1071    List<Frame> children = new ArrayList<>();
1072
1073    public void setChildFrame(Frame frame) {
1074        if (children.contains(frame)) {
1075            return;
1076        }
1077        children.add(frame);
1078    }
1079
1080    @Override
1081    public void dispose() {
1082        InstanceManager.getDefault(LocationManager.class).removePropertyChangeListener(this);
1083        InstanceManager.getDefault(EngineTypes.class).removePropertyChangeListener(this);
1084        InstanceManager.getDefault(EngineModels.class).removePropertyChangeListener(this);
1085        InstanceManager.getDefault(CarTypes.class).removePropertyChangeListener(this);
1086        InstanceManager.getDefault(CarRoads.class).removePropertyChangeListener(this);
1087        routeManager.removePropertyChangeListener(this);
1088        for (Frame frame : children) {
1089            frame.dispose();
1090        }
1091        if (_train != null) {
1092            _train.removePropertyChangeListener(this);
1093            Route route = _train.getRoute();
1094            if (route != null) {
1095                for (RouteLocation rl : route.getLocationsBySequenceList()) {
1096                    Location loc = rl.getLocation();
1097                    if (loc != null) {
1098                        loc.removePropertyChangeListener(this);
1099                    }
1100                }
1101            }
1102        }
1103        super.dispose();
1104    }
1105
1106    @Override
1107    public void propertyChange(java.beans.PropertyChangeEvent e) {
1108        if (Control.SHOW_PROPERTY) {
1109            log.debug("Property change ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(),
1110                    e.getNewValue()); // NOI18N
1111        }
1112        if (e.getPropertyName().equals(CarTypes.CARTYPES_CHANGED_PROPERTY) ||
1113                e.getPropertyName().equals(Train.TYPES_CHANGED_PROPERTY)) {
1114            updateCarTypeCheckboxes();
1115        }
1116        if (e.getPropertyName().equals(EngineTypes.ENGINETYPES_CHANGED_PROPERTY)) {
1117            updateEngineTypeCheckboxes();
1118        }
1119        if (e.getPropertyName().equals(RouteManager.LISTLENGTH_CHANGED_PROPERTY)) {
1120            updateRouteComboBox();
1121        }
1122        if (e.getPropertyName().equals(Route.LISTCHANGE_CHANGED_PROPERTY) ||
1123                e.getPropertyName().equals(LocationManager.LISTLENGTH_CHANGED_PROPERTY) ||
1124                e.getPropertyName().equals(Location.NAME_CHANGED_PROPERTY) ||
1125                e.getPropertyName().equals(Location.TRAIN_DIRECTION_CHANGED_PROPERTY)) {
1126            updateLocationCheckboxes();
1127            pack();
1128            repaint();
1129        }
1130        if (e.getPropertyName().equals(CarRoads.CARROADS_CHANGED_PROPERTY)) {
1131            updateRoadComboBoxes();
1132        }
1133        if (e.getPropertyName().equals(EngineModels.ENGINEMODELS_CHANGED_PROPERTY)) {
1134            InstanceManager.getDefault(EngineModels.class).updateComboBox(modelEngineBox);
1135            modelEngineBox.insertItemAt(NONE, 0);
1136            modelEngineBox.setSelectedIndex(0);
1137            if (_train != null) {
1138                modelEngineBox.setSelectedItem(_train.getEngineModel());
1139            }
1140        }
1141        if (e.getPropertyName().equals(Train.DEPARTURETIME_CHANGED_PROPERTY)) {
1142            updateDepartureTime();
1143        }
1144        if (e.getPropertyName().equals(Train.TRAIN_ROUTE_CHANGED_PROPERTY) && _train != null) {
1145            routeBox.setSelectedItem(_train.getRoute());
1146        }
1147        if (e.getPropertyName().equals(Route.ROUTE_STATUS_CHANGED_PROPERTY)) {
1148            updateDepartureTime();
1149            enableButtons(_train != null);
1150            updateRouteStatus();
1151        }
1152        if (e.getPropertyName().equals(Train.ROADS_CHANGED_PROPERTY) ||
1153                e.getPropertyName().equals(Train.LOADS_CHANGED_PROPERTY)) {
1154            updateRoadAndLoadStatus();
1155        }
1156    }
1157
1158    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TrainEditFrame.class);
1159}