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