001package jmri.jmrit.operations.rollingstock.cars.tools; 002 003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 004 005import javax.swing.JMenu; 006import javax.swing.JMenuBar; 007 008import jmri.InstanceManager; 009import jmri.jmrit.operations.locations.tools.LocationsByCarTypeFrame; 010import jmri.jmrit.operations.rollingstock.RollingStock; 011import jmri.jmrit.operations.rollingstock.RollingStockAttributeEditFrame; 012import jmri.jmrit.operations.rollingstock.cars.*; 013import jmri.jmrit.operations.rollingstock.engines.EngineManager; 014import jmri.jmrit.operations.rollingstock.tools.AttributeCharacterLengthAction; 015import jmri.jmrit.operations.setup.Control; 016import jmri.jmrit.operations.trains.tools.TrainsByCarTypeFrame; 017import jmri.util.swing.JmriJOptionPane; 018 019/** 020 * Frame for editing a car attribute. 021 * 022 * @author Daniel Boudreau Copyright (C) 2008, 2014, 2020 023 */ 024public class CarAttributeEditFrame extends RollingStockAttributeEditFrame { 025 026 CarManager carManager = InstanceManager.getDefault(CarManager.class); 027 028 // incremental attributes for this frame 029 public static final String KERNEL = "Kernel"; 030 031 public CarAttributeEditFrame(){ 032 } 033 034 /** 035 * 036 * @param attribute One of the six possible attributes for a car. 037 */ 038 public void initComponents(String attribute) { 039 initComponents(attribute, NONE); 040 } 041 042 /** 043 * 044 * @param attribute One of the six possible attributes for a car. 045 * @param name The name of the attribute to edit. 046 */ 047 @Override 048 public void initComponents(String attribute, String name) { 049 super.initComponents(attribute, name); 050 051 setTitle(Bundle.getMessage("TitleCarEditAtrribute", attribute)); 052 carManager.addPropertyChangeListener(this); 053 054 // build menu 055 JMenuBar menuBar = new JMenuBar(); 056 JMenu toolMenu = new JMenu(Bundle.getMessage("MenuTools")); 057 toolMenu.add(new CarAttributeAction(this)); 058 toolMenu.add(new CarDeleteAttributeAction(this)); 059 toolMenu.add(new AttributeCharacterLengthAction()); 060 menuBar.add(toolMenu); 061 setJMenuBar(menuBar); 062 // add help menu to window 063 addHelpMenu("package.jmri.jmrit.operations.Operations_EditCarAttributes", true); // NOI18N 064 } 065 066 @Override 067 protected void deleteAttributeName(String deleteItem) { 068 super.deleteAttributeName(deleteItem); 069 if (_attribute.equals(TYPE)) { 070 InstanceManager.getDefault(CarTypes.class).deleteName(deleteItem); 071 } 072 if (_attribute.equals(LENGTH)) { 073 InstanceManager.getDefault(CarLengths.class).deleteName(deleteItem); 074 } 075 if (_attribute.equals(KERNEL)) { 076 InstanceManager.getDefault(KernelManager.class).deleteKernel(deleteItem); 077 } 078 } 079 080 @Override 081 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "GUI ease of use") 082 protected void addAttributeName(String addItem) { 083 super.addAttributeName(addItem); 084 if (_attribute.equals(TYPE)) { 085 InstanceManager.getDefault(CarTypes.class).addName(addItem); 086 if (showDialogBox) { 087 int results = JmriJOptionPane.showOptionDialog(this, Bundle.getMessage("AddNewCarType"), 088 Bundle.getMessage("ModifyLocations"), JmriJOptionPane.DEFAULT_OPTION, 089 JmriJOptionPane.QUESTION_MESSAGE, null, 090 new Object[] { Bundle.getMessage("ButtonYes"), 091 Bundle.getMessage("ButtonNo"), Bundle.getMessage("ButtonDontShow") }, 092 Bundle.getMessage("ButtonNo")); 093 if (results == 0 ) { // array position 0, ButtonYes 094 LocationsByCarTypeFrame lf = new LocationsByCarTypeFrame(); 095 lf.initComponents(addItem); 096 } 097 if (results == 2 ) { // array position 2, ButtonDontShow 098 showDialogBox = false; 099 } 100 results = JmriJOptionPane.showOptionDialog(this, Bundle.getMessage("AddNewCarType"), 101 Bundle.getMessage("ModifyTrains"), JmriJOptionPane.DEFAULT_OPTION, 102 JmriJOptionPane.QUESTION_MESSAGE, null, new Object[] { Bundle.getMessage("ButtonYes"), 103 Bundle.getMessage("ButtonNo"), Bundle.getMessage("ButtonDontShow") }, 104 Bundle.getMessage("ButtonNo")); 105 if (results == 0 ) { // array position 0, ButtonYes 106 TrainsByCarTypeFrame lf = new TrainsByCarTypeFrame(); 107 lf.initComponents(addItem); 108 } 109 if (results == 2 ) { // array position 2, ButtonDontShow 110 showDialogBox = false; 111 } 112 } 113 } 114 if (_attribute.equals(LENGTH)) { 115 InstanceManager.getDefault(CarLengths.class).addName(addItem); 116 comboBox.setSelectedItem(addItem); 117 } 118 if (_attribute.equals(KERNEL)) { 119 InstanceManager.getDefault(KernelManager.class).newKernel(addItem); 120 } 121 if (_attribute.equals(OWNER)) { 122 InstanceManager.getDefault(CarOwners.class).addName(addItem); 123 } 124 } 125 126 @Override 127 protected void replaceItem(String oldItem, String newItem) { 128 super.replaceItem(oldItem, newItem); 129 // replace kernel 130 if (_attribute.equals(KERNEL)) { 131 InstanceManager.getDefault(KernelManager.class).replaceKernelName(oldItem, newItem); 132 } 133 // now adjust cars, locations and trains 134 if (_attribute.equals(TYPE)) { 135 InstanceManager.getDefault(CarTypes.class).replaceName(oldItem, newItem); 136 InstanceManager.getDefault(CarLoads.class).replaceType(oldItem, newItem); 137 } 138 if (_attribute.equals(LENGTH)) { 139 InstanceManager.getDefault(CarLengths.class).replaceName(oldItem, newItem); 140 } 141 } 142 143 @Override 144 protected void loadCombobox() { 145 super.loadCombobox(); 146 if (_attribute.equals(TYPE)) { 147 comboBox = InstanceManager.getDefault(CarTypes.class).getComboBox(); 148 InstanceManager.getDefault(CarTypes.class).addPropertyChangeListener(this); 149 } 150 if (_attribute.equals(LENGTH)) { 151 comboBox = InstanceManager.getDefault(CarLengths.class).getComboBox(); 152 InstanceManager.getDefault(CarLengths.class).addPropertyChangeListener(this); 153 } 154 if (_attribute.equals(KERNEL)) { 155 comboBox = InstanceManager.getDefault(KernelManager.class).getComboBox(); 156 InstanceManager.getDefault(KernelManager.class).addPropertyChangeListener(this); 157 } 158 } 159 160 @Override 161 protected void updateAttributeQuanity() { 162 if (!showQuanity) { 163 return; 164 } 165 int number = 0; 166 String item = (String) comboBox.getSelectedItem(); 167 log.debug("Selected item {}", item); 168 for (Car car : carManager.getList()) { 169 if (_attribute.equals(ROAD)) { 170 if (car.getRoadName().equals(item)) { 171 number++; 172 } 173 } 174 if (_attribute.equals(TYPE)) { 175 if (car.getTypeName().equals(item)) { 176 number++; 177 } 178 } 179 if (_attribute.equals(COLOR)) { 180 if (car.getColorName().equals(item)) { 181 number++; 182 } 183 } 184 if (_attribute.equals(LENGTH)) { 185 if (car.getLength().equals(item)) { 186 number++; 187 } 188 } 189 if (_attribute.equals(OWNER)) { 190 if (car.getOwnerName().equals(item)) { 191 number++; 192 } 193 } 194 if (_attribute.equals(KERNEL)) { 195 if (car.getKernelName().equals(item)) { 196 number++; 197 } 198 } 199 } 200 quanity.setText(Integer.toString(number)); 201 // Tool to delete all attributes that haven't been assigned to a car 202 if (number == 0 && deleteUnused) { 203 // need to check if an engine is using the road name 204 if (_attribute.equals(ROAD)) { 205 for (RollingStock rs : InstanceManager.getDefault(EngineManager.class).getList()) { 206 if (rs.getRoadName().equals(item)) { 207 log.info("Engine ({} {}) is assigned road name ({})", rs.getRoadName(), rs.getNumber(), item); // NOI18N 208 return; 209 } 210 } 211 } 212 // need to check if an engine is using the road name 213 if (_attribute.equals(OWNER)) { 214 for (RollingStock rs : InstanceManager.getDefault(EngineManager.class).getList()) { 215 if (rs.getOwnerName().equals(item)) { 216 log.info("Engine ({} {}) is assigned owner name ({})", rs.getRoadName(), rs.getNumber(), item); // NOI18N 217 return; 218 } 219 } 220 } 221 // need to check if an engine is using the color name 222 if (_attribute.equals(COLOR)) { 223 for (RollingStock rs : InstanceManager.getDefault(EngineManager.class).getList()) { 224 if (rs.getColorName().equals(item)) { 225 log.info("Engine ({} {}) is assigned color name ({})", rs.getRoadName(), rs.getNumber(), item); // NOI18N 226 return; 227 } 228 } 229 } 230 // confirm that attribute is to be deleted 231 confirmDelete(item); 232 } 233 } 234 235 236 @Override 237 public void dispose() { 238 InstanceManager.getDefault(CarTypes.class).removePropertyChangeListener(this); 239 InstanceManager.getDefault(CarColors.class).removePropertyChangeListener(this); 240 InstanceManager.getDefault(CarLengths.class).removePropertyChangeListener(this); 241 InstanceManager.getDefault(KernelManager.class).removePropertyChangeListener(this); 242 carManager.removePropertyChangeListener(this); 243 super.dispose(); 244 } 245 246 @Override 247 public void propertyChange(java.beans.PropertyChangeEvent e) { 248 if (Control.SHOW_PROPERTY) { 249 log.debug("Property change: ({}) old: ({}) new: ({})", e.getPropertyName(), e.getOldValue(), 250 e.getNewValue()); 251 } 252 if (e.getPropertyName().equals(CarTypes.CARTYPES_CHANGED_PROPERTY)) { 253 InstanceManager.getDefault(CarTypes.class).updateComboBox(comboBox); 254 } 255 if (e.getPropertyName().equals(CarLengths.CARLENGTHS_CHANGED_PROPERTY)) { 256 InstanceManager.getDefault(CarLengths.class).updateComboBox(comboBox); 257 } 258 if (e.getPropertyName().equals(KernelManager.LISTLENGTH_CHANGED_PROPERTY)) { 259 InstanceManager.getDefault(KernelManager.class).updateComboBox(comboBox); 260 } 261 if (e.getPropertyName().equals(CarManager.LISTLENGTH_CHANGED_PROPERTY)) { 262 updateAttributeQuanity(); 263 } 264 super.propertyChange(e); 265 } 266 267 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CarAttributeEditFrame.class); 268}