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