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 if (engine.isClone()) { 149 JmriJOptionPane.showMessageDialog(this, 150 Bundle.getMessage("RsIsClone", engine.toString()), 151 Bundle.getMessage("DoNotModifyClone"), JmriJOptionPane.WARNING_MESSAGE); 152 return false; 153 } 154 // consist 155 if (consistComboBox.getSelectedItem() != null) { 156 if (consistComboBox.getSelectedItem().equals(ConsistManager.NONE)) { 157 engine.setConsist(null); 158 engine.setBlocking(Engine.DEFAULT_BLOCKING_ORDER); 159 } else if (!engine.getConsistName().equals(consistComboBox.getSelectedItem())) { 160 engine.setConsist(consistManager.getConsistByName((String) consistComboBox.getSelectedItem())); 161 if (engine.getConsist() != null) { 162 engine.setBlocking(engine.getConsist().getSize()); 163 } 164 } 165 } 166 if (!super.change(engine)) { 167 return false; 168 } 169 170 // check for train change 171 checkTrain(engine); 172 173 // is this engine part of a consist? 174 if (askConsistChange && _engine.getConsist() != null) { 175 if (JmriJOptionPane.showConfirmDialog(this, Bundle.getMessage("engineInConsist"), 176 Bundle.getMessage("enginePartConsist"), 177 JmriJOptionPane.YES_NO_OPTION) == JmriJOptionPane.YES_OPTION) { 178 // convert cars list to rolling stock list 179 List<Engine> list = _engine.getConsist().getEngines(); 180 if (!updateGroup(list)) { 181 return false; 182 } 183 } 184 } 185 return true; 186 } 187 188 @Override 189 public void dispose() { 190 if (eaef != null) { 191 eaef.dispose(); 192 } 193 consistManager.removePropertyChangeListener(this); 194 super.dispose(); 195 } 196 197 @Override 198 public void propertyChange(java.beans.PropertyChangeEvent e) { 199 super.propertyChange(e); 200 if (e.getPropertyName().equals(ConsistManager.LISTLENGTH_CHANGED_PROPERTY)) { 201 updateConsistComboBox(); 202 } 203 } 204 205 // private final static Logger log = LoggerFactory.getLogger(EngineSetFrame.class); 206}