001package jmri.jmrit.operations.rollingstock.engines;
002
003import java.awt.Dimension;
004import java.awt.GridBagLayout;
005import java.util.List;
006import java.util.ResourceBundle;
007
008import javax.swing.*;
009
010import jmri.InstanceManager;
011import jmri.jmrit.operations.OperationsXml;
012import jmri.jmrit.operations.rollingstock.RollingStockSetFrame;
013import jmri.jmrit.operations.rollingstock.engines.tools.EngineAttributeEditFrame;
014import jmri.jmrit.operations.setup.Control;
015import jmri.util.swing.JmriJOptionPane;
016
017/**
018 * Frame for user to place engine on the layout
019 *
020 * @author Dan Boudreau Copyright (C) 2008, 2010, 2024
021 */
022public class EngineSetFrame extends RollingStockSetFrame<Engine> {
023
024    protected static final ResourceBundle rb = ResourceBundle
025            .getBundle("jmri.jmrit.operations.rollingstock.engines.JmritOperationsEnginesBundle");
026
027    EngineManager manager = InstanceManager.getDefault(EngineManager.class);
028    EngineManagerXml managerXml = InstanceManager.getDefault(EngineManagerXml.class);
029    ConsistManager consistManager = InstanceManager.getDefault(ConsistManager.class);
030
031    protected JComboBox<String> consistComboBox = consistManager.getComboBox();
032    public JCheckBox ignoreConsistCheckBox = new JCheckBox(Bundle.getMessage("Ignore"));
033    protected JButton editConsistButton = new JButton(Bundle.getMessage("ButtonEdit"));
034
035    protected boolean askConsistChange = true;
036
037    public Engine _engine;
038
039    private String _help = "package.jmri.jmrit.operations.Operations_LocomotivesSet";
040
041    public EngineSetFrame() {
042        super(Bundle.getMessage("TitleEngineSet"));
043    }
044
045    public void initComponents(String help) {
046        _help = help;
047        initComponents();
048    }
049
050    @Override
051    public void initComponents() {
052        super.initComponents();
053
054        // build menu
055        addHelpMenu(_help, true); // NOI18N
056
057        // disable location unknown, final destination fields
058        locationUnknownCheckBox.setVisible(false);
059        pFinalDestination.setVisible(false);
060        autoTrainCheckBox.setVisible(false);
061
062        pOptional.setLayout(new BoxLayout(pOptional, BoxLayout.Y_AXIS));
063
064        // add Consist fields
065        JPanel pConsist = new JPanel();
066        pConsist.setLayout(new GridBagLayout());
067        pConsist.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Consist")));
068        addItemLeft(pConsist, ignoreConsistCheckBox, 1, 0);
069        consistComboBox.setName("consistComboBox"); // NOI18N for UI Test
070        addItem(pConsist, consistComboBox, 2, 0);
071        addItem(pConsist, editConsistButton, 3, 0);
072        pOptional.add(pConsist);
073
074        // don't show ignore checkboxes
075        ignoreConsistCheckBox.setVisible(false);
076
077        addButtonAction(editConsistButton);
078        addCheckBoxAction(ignoreConsistCheckBox);
079
080        consistManager.addPropertyChangeListener(this);
081
082        // tool tips
083        outOfServiceCheckBox.setToolTipText(getRb().getString("TipLocoOutOfService"));
084
085        initMinimumSize(new Dimension(Control.panelWidth500, Control.panelHeight400));
086    }
087
088    public void load(Engine engine) {
089        _engine = engine;
090        updateConsistComboBox();
091        super.load(engine);
092    }
093
094    @Override
095    protected ResourceBundle getRb() {
096        return rb;
097    }
098
099    @Override
100    protected void enableComponents(boolean enabled) {
101        super.enableComponents(enabled);
102        ignoreConsistCheckBox.setEnabled(enabled);
103        consistComboBox.setEnabled(!ignoreConsistCheckBox.isSelected() && enabled);
104        editConsistButton.setEnabled(!ignoreConsistCheckBox.isSelected() && enabled && _engine != null);
105    }
106
107    EngineAttributeEditFrame eaef;
108
109    @Override
110    public void buttonActionPerformed(java.awt.event.ActionEvent ae) {
111        super.buttonActionPerformed(ae);
112        if (ae.getSource() == editConsistButton) {
113            if (eaef != null) {
114                eaef.dispose();
115            }
116            eaef = new EngineAttributeEditFrame();
117            eaef.initComponents(EngineAttributeEditFrame.CONSIST, (String) consistComboBox.getSelectedItem());
118        }
119    }
120
121    @Override
122    public void checkBoxActionPerformed(java.awt.event.ActionEvent ae) {
123        super.checkBoxActionPerformed(ae);
124        if (ae.getSource() == ignoreConsistCheckBox) {
125            consistComboBox.setEnabled(!ignoreConsistCheckBox.isSelected());
126            editConsistButton.setEnabled(!ignoreConsistCheckBox.isSelected());
127        }
128    }
129
130    protected void updateConsistComboBox() {
131        consistManager.updateComboBox(consistComboBox);
132        if (_engine != null) {
133            consistComboBox.setSelectedItem(_engine.getConsistName());
134        }
135    }
136
137    @Override
138    protected boolean save() {
139        if (change(_engine)) {
140            OperationsXml.save();
141            return true;
142        }
143        return false;
144    }
145
146    protected boolean change(Engine engine) {
147        // consist
148        if (consistComboBox.getSelectedItem() != null) {
149            if (consistComboBox.getSelectedItem().equals(ConsistManager.NONE)) {
150                engine.setConsist(null);
151                engine.setBlocking(Engine.DEFAULT_BLOCKING_ORDER);
152            } else if (!engine.getConsistName().equals(consistComboBox.getSelectedItem())) {
153                engine.setConsist(consistManager.getConsistByName((String) consistComboBox.getSelectedItem()));
154                if (engine.getConsist() != null) {
155                    engine.setBlocking(engine.getConsist().getSize());
156                }
157            }
158        }
159        if (!super.change(engine)) {
160            return false;
161        }
162
163        // check for train change
164        checkTrain(engine);
165
166        // is this engine part of a consist?
167        if (askConsistChange && _engine.getConsist() != null) {
168            if (JmriJOptionPane.showConfirmDialog(this, Bundle.getMessage("engineInConsist"),
169                    Bundle.getMessage("enginePartConsist"),
170                    JmriJOptionPane.YES_NO_OPTION) == JmriJOptionPane.YES_OPTION) {
171                // convert cars list to rolling stock list
172                List<Engine> list = _engine.getConsist().getEngines();
173                if (!updateGroup(list)) {
174                    return false;
175                }
176            }
177        }
178        return true;
179    }
180
181    @Override
182    public void dispose() {
183        if (eaef != null) {
184            eaef.dispose();
185        }
186        consistManager.removePropertyChangeListener(this);
187        super.dispose();
188    }
189
190    @Override
191    public void propertyChange(java.beans.PropertyChangeEvent e) {
192        super.propertyChange(e);
193        if (e.getPropertyName().equals(ConsistManager.LISTLENGTH_CHANGED_PROPERTY)) {
194            updateConsistComboBox();
195        }
196    }
197
198    //    private final static Logger log = LoggerFactory.getLogger(EngineSetFrame.class);
199}