001package jmri.jmrit.operations.trains.tools;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005import java.awt.event.ActionEvent;
006
007import javax.swing.*;
008
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012import jmri.InstanceManager;
013import jmri.jmrit.operations.OperationsFrame;
014import jmri.jmrit.operations.OperationsXml;
015import jmri.jmrit.operations.setup.Control;
016import jmri.jmrit.operations.setup.Setup;
017import jmri.jmrit.operations.trains.Train;
018import jmri.jmrit.operations.trains.TrainManager;
019
020/**
021 * Frame for setting up the Trains table colors in operations.
022 *
023 * @author Bob Jacobsen Copyright (C) 2001
024 * @author Daniel Boudreau Copyright (C) 2014, 2016
025 */
026public class TrainsTableSetColorFrame extends OperationsFrame implements java.beans.PropertyChangeListener {
027
028    TrainManager trainManager = InstanceManager.getDefault(TrainManager.class);
029
030    // labels
031    // text field
032    // radio buttons
033    JRadioButton manualRadioButton = new JRadioButton(Bundle.getMessage("Manual"));
034    JRadioButton autoRadioButton = new JRadioButton(Bundle.getMessage("Auto"));
035
036    // major buttons
037    JButton saveButton = new JButton(Bundle.getMessage("ButtonSave"));
038
039    // combo boxes
040    JComboBox<Train> trainBox = InstanceManager.getDefault(TrainManager.class).getTrainComboBox();
041
042    // JColorChooser is not a replacement for getRowColorComboBox as it doesn't
043    // support no color as a selection.
044    JComboBox<String> colorBox = InstanceManager.getDefault(TrainManager.class).getRowColorComboBox();
045    JComboBox<String> colorResetBoxAuto = InstanceManager.getDefault(TrainManager.class).getRowColorComboBox();
046
047    JComboBox<String> colorBuiltBox = InstanceManager.getDefault(TrainManager.class).getRowColorComboBox();
048    JComboBox<String> colorBuildFailedBox = InstanceManager.getDefault(TrainManager.class).getRowColorComboBox();
049    JComboBox<String> colorTrainEnRouteBox = InstanceManager.getDefault(TrainManager.class).getRowColorComboBox();
050    JComboBox<String> colorTerminatedBox = InstanceManager.getDefault(TrainManager.class).getRowColorComboBox();
051    JComboBox<String> colorResetBox = InstanceManager.getDefault(TrainManager.class).getRowColorComboBox();
052
053    // display panels based on which option is selected
054    JPanel pTrains;
055    JPanel pColor;
056    JPanel pColorResetAuto;
057
058    // auto
059    JPanel pColorBuilt;
060    JPanel pColorBuildFailed;
061    JPanel pColorTrainEnRoute;
062    JPanel pColorTerminated;
063    JPanel pColorReset;
064
065    public TrainsTableSetColorFrame(Train train) {
066        // general GUI config
067
068        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
069        // Layout the panel by rows
070
071        // row 1
072        JPanel pOption = new JPanel();
073        pOption.setLayout(new GridBagLayout());
074        pOption.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Option")));
075        addItem(pOption, manualRadioButton, 0, 0);
076        addItem(pOption, autoRadioButton, 1, 0);
077
078        ButtonGroup bGroup = new ButtonGroup();
079        bGroup.add(manualRadioButton);
080        bGroup.add(autoRadioButton);
081
082        manualRadioButton.setSelected(trainManager.isRowColorManual());
083        autoRadioButton.setSelected(!trainManager.isRowColorManual());
084
085        pTrains = new JPanel();
086        pTrains.setLayout(new GridBagLayout());
087        pTrains.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Train")));
088        addItem(pTrains, trainBox, 0, 0);
089
090        trainBox.setSelectedItem(train);
091
092        pColor = new JPanel();
093        pColor.setLayout(new GridBagLayout());
094        pColor.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectRowColor")));
095        addItem(pColor, colorBox, 0, 0);
096
097        pColorResetAuto = new JPanel();
098        pColorResetAuto.setLayout(new GridBagLayout());
099        pColorResetAuto.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectRowColorResetAuto")));
100        addItem(pColorResetAuto, colorResetBoxAuto, 0, 0);
101
102        pColorBuilt = new JPanel();
103        pColorBuilt.setLayout(new GridBagLayout());
104        pColorBuilt.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectRowColorBuilt")));
105        addItem(pColorBuilt, colorBuiltBox, 0, 0);
106
107        colorBuiltBox.setSelectedItem(trainManager.getRowColorNameForBuilt());
108
109        pColorBuildFailed = new JPanel();
110        pColorBuildFailed.setLayout(new GridBagLayout());
111        pColorBuildFailed.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectRowColorBuildFailed")));
112        addItem(pColorBuildFailed, colorBuildFailedBox, 0, 0);
113
114        colorBuildFailedBox.setSelectedItem(trainManager.getRowColorNameForBuildFailed());
115
116        pColorTrainEnRoute = new JPanel();
117        pColorTrainEnRoute.setLayout(new GridBagLayout());
118        pColorTrainEnRoute.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectRowColorTrainEnRoute")));
119        addItem(pColorTrainEnRoute, colorTrainEnRouteBox, 0, 0);
120
121        colorTrainEnRouteBox.setSelectedItem(trainManager.getRowColorNameForTrainEnRoute());
122
123        pColorTerminated = new JPanel();
124        pColorTerminated.setLayout(new GridBagLayout());
125        pColorTerminated.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectRowColorTerminated")));
126        addItem(pColorTerminated, colorTerminatedBox, 0, 0);
127
128        colorTerminatedBox.setSelectedItem(trainManager.getRowColorNameForTerminated());
129
130        pColorReset = new JPanel();
131        pColorReset.setLayout(new GridBagLayout());
132        pColorReset.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("SelectRowColorReset")));
133        addItem(pColorReset, colorResetBox, 0, 0);
134
135        colorResetBox.setSelectedItem(trainManager.getRowColorNameForReset());
136
137        // row 4
138        JPanel pButton = new JPanel();
139        pButton.add(saveButton);
140
141        getContentPane().add(pOption);
142        getContentPane().add(pTrains);
143        getContentPane().add(pColor);
144        getContentPane().add(pColorResetAuto);
145
146        getContentPane().add(pColorBuilt);
147        getContentPane().add(pColorBuildFailed);
148        getContentPane().add(pColorTrainEnRoute);
149        getContentPane().add(pColorTerminated);
150        getContentPane().add(pColorReset);
151        getContentPane().add(pButton);
152
153        // add help menu to window
154        addHelpMenu("package.jmri.jmrit.operations.Operations_TrainsTableColors", true); // NOI18N
155
156        pack();
157        setMinimumSize(new Dimension(Control.panelWidth400, Control.panelHeight400));
158
159        setTitle(Bundle.getMessage("MenuItemSetTrainColor"));
160
161        // setup buttons
162        addButtonAction(saveButton);
163        addRadioButtonAction(manualRadioButton);
164        addRadioButtonAction(autoRadioButton);
165
166        addComboBoxAction(trainBox);
167
168        makePanelsVisible();
169
170        trainManager.addPropertyChangeListener(this);
171    }
172
173    @Override
174    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
175        if (ae.getSource() == saveButton) {
176            // save option manual or auto
177            trainManager.setRowColorsManual(manualRadioButton.isSelected());
178            if (manualRadioButton.isSelected()) {
179                Train train = (Train) trainBox.getSelectedItem();
180                if (train != null) {
181                    train.setTableRowColorName((String) colorBox.getSelectedItem());
182                    train.setTableRowColorNameReset((String) colorResetBoxAuto.getSelectedItem());
183                }
184            } else {
185                trainManager.setRowColorNameForBuildFailed((String) colorBuildFailedBox.getSelectedItem());
186                trainManager.setRowColorNameForBuilt((String) colorBuiltBox.getSelectedItem());
187                trainManager.setRowColorNameForTrainEnRoute((String) colorTrainEnRouteBox.getSelectedItem());
188                trainManager.setRowColorNameForTerminated((String) colorTerminatedBox.getSelectedItem());
189                trainManager.setRowColorNameForReset((String) colorResetBox.getSelectedItem());
190                // update all trains
191                for (Train train : trainManager.getTrainsByNameList()) {
192                    train.updateTrainTableRowColor();
193                }
194            }
195            // save train file
196            OperationsXml.save();
197            if (Setup.isCloseWindowOnSaveEnabled()) {
198                dispose();
199            }
200            return;
201        }
202    }
203
204    @Override
205    public void radioButtonActionPerformed(java.awt.event.ActionEvent ae) {
206        makePanelsVisible();
207    }
208
209    /**
210     * If manual selected, show only trains and colors available. If auto, show
211     * only the three automatic options; color for train built, build failed,
212     * and terminated.
213     */
214    private void makePanelsVisible() {
215        pTrains.setVisible(manualRadioButton.isSelected());
216        pColor.setVisible(manualRadioButton.isSelected());
217        pColorResetAuto.setVisible(manualRadioButton.isSelected());
218        // the inverse
219        pColorBuildFailed.setVisible(!manualRadioButton.isSelected());
220        pColorBuilt.setVisible(!manualRadioButton.isSelected());
221        pColorTrainEnRoute.setVisible(!manualRadioButton.isSelected());
222        pColorTerminated.setVisible(!manualRadioButton.isSelected());
223        pColorReset.setVisible(!manualRadioButton.isSelected());
224    }
225
226    @Override
227    public void comboBoxActionPerformed(ActionEvent ae) {
228        Train train = (Train) trainBox.getSelectedItem();
229        if (train != null) {
230            colorBox.setSelectedItem(train.getTableRowColorName());
231            colorResetBoxAuto.setSelectedItem(train.getTableRowColorNameReset());
232        }
233    }
234
235    @Override
236    public void propertyChange(java.beans.PropertyChangeEvent e) {
237        if (Control.SHOW_PROPERTY) {
238            log.debug("Property change ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(),
239                    e.getNewValue()); // NOI18N
240        }
241        if (e.getPropertyName().equals(TrainManager.LISTLENGTH_CHANGED_PROPERTY)) {
242            trainManager.updateTrainComboBox(trainBox);
243        }
244    }
245
246    @Override
247    public void dispose() {
248        trainManager.removePropertyChangeListener(this);
249        super.dispose();
250    }
251
252    private final static Logger log = LoggerFactory.getLogger(TrainsTableSetColorFrame.class);
253}