001package jmri.jmrit.operations.rollingstock.engines; 002 003import java.util.Hashtable; 004import java.util.Set; 005 006import org.jdom2.Element; 007import org.openide.util.lookup.ServiceProvider; 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011import jmri.InstanceInitializer; 012import jmri.InstanceManager; 013import jmri.implementation.AbstractInstanceInitializer; 014import jmri.jmrit.operations.rollingstock.RollingStockAttribute; 015 016/** 017 * Represents the various engine models a railroad can have. Each model has a 018 * type, horsepower rating, length, and weight that is kept here. The program provides 019 * some default models for the user. These values can be overridden by the user. 020 * <ul> 021 * <li>Model Horsepower Length Weight Type</li> 022 * 023 * <li>E8 2250 70 150 Diesel</li> 024 * <li>FT 1350 50 115 Diesel</li> 025 * <li>F3 1500 50 115 Diesel</li> 026 * <li>F7 1500 50 115 Diesel</li> 027 * <li>F9 1750 50 115 Diesel</li> 028 * <li>GP20 2000 56 120 Diesel</li> 029 * <li>GP30 2250 56 130 Diesel</li> 030 * <li>GP35 2500 56 130 Diesel</li> 031 * <li>GP38 2000 59 125 Diesel</li> 032 * <li>GP40 3000 59 122 Diesel</li> 033 * <li>RS1 1000 51 124 Diesel</li> 034 * <li>RS2 1500 52 115 Diesel</li> 035 * <li>RS3 1600 51 114 Diesel</li> 036 * <li>RS11 1800 53 125 Diesel</li> 037 * <li>RS18 1800 52 118 Diesel</li> 038 * <li>RS27 2400 57 132 Diesel</li> 039 * <li>RSD4 1600 52 179 Diesel</li> 040 * <li>SD26 2650 61 164 Diesel</li> 041 * <li>SD45 3600 66 195 Diesel</li> 042 * <li>SW1200 1200 45 124 Diesel</li> 043 * <li>SW1500 1500 45 124 Diesel</li> 044 * <li>SW8 800 44 115 Diesel</li> 045 * <li>TRAINMASTER 2400 66 188 Diesel</li> 046 * <li>U28B 2800 60 126 Diesel</li> 047 * </ul> 048 * 049 * @author Daniel Boudreau Copyright (C) 2008 050 */ 051public class EngineModels extends RollingStockAttribute { 052 053 private static final String MODELS = Bundle.getMessage("engineDefaultModels"); 054 // Horsepower, length, and type have a one to one correspondence with the above MODELS 055 private static final String HORSEPOWER = Bundle.getMessage("engineModelHorsepowers"); 056 private static final String ENGINELENGTHS = Bundle.getMessage("engineModelLengths"); 057 private static final String ENGINETYPES = Bundle.getMessage("engineModelTypes"); 058 private static final String ENGINEWEIGHTS = Bundle.getMessage("engineModelWeights"); 059 060 public static final String ENGINEMODELS_CHANGED_PROPERTY = "EngineModels"; // NOI18N 061 public static final String ENGINEMODELS_NAME_CHANGED_PROPERTY = "EngineModelsName"; // NOI18N 062 063 // protected List<String> _list = new ArrayList<String>(); 064 protected Hashtable<String, String> _engineHorsepowerHashTable = new Hashtable<>(); 065 protected Hashtable<String, String> _engineLengthHashTable = new Hashtable<>(); 066 protected Hashtable<String, String> _engineTypeHashTable = new Hashtable<>(); 067 protected Hashtable<String, String> _engineWeightHashTable = new Hashtable<>(); 068 protected Hashtable<String, Boolean> _engineBunitHashTable = new Hashtable<>(); 069 070 public EngineModels() { 071 } 072 073 @Override 074 protected String getDefaultNames() { 075 return MODELS; 076 } 077 078 @Override 079 public void dispose() { 080 _engineHorsepowerHashTable.clear(); 081 _engineLengthHashTable.clear(); 082 _engineTypeHashTable.clear(); 083 _engineWeightHashTable.clear(); 084 _engineBunitHashTable.clear(); 085 super.dispose(); 086 } 087 088 @Override 089 public void addName(String model) { 090 super.addName(model); 091 setDirtyAndFirePropertyChange(ENGINEMODELS_CHANGED_PROPERTY, null, model); 092 } 093 094 @Override 095 public void deleteName(String model) { 096 super.deleteName(model); 097 setDirtyAndFirePropertyChange(ENGINEMODELS_CHANGED_PROPERTY, model, null); 098 } 099 100 public void replaceName(String oldName, String newName) { 101 super.addName(newName); 102 setDirtyAndFirePropertyChange(ENGINEMODELS_NAME_CHANGED_PROPERTY, oldName, newName); 103 super.deleteName(oldName); 104 } 105 106 public void setModelHorsepower(String model, String horsepower) { 107 _engineHorsepowerHashTable.put(model, horsepower); 108 } 109 110 public String getModelHorsepower(String model) { 111 return _engineHorsepowerHashTable.get(model); 112 } 113 114 public void setModelLength(String model, String horsepower) { 115 _engineLengthHashTable.put(model, horsepower); 116 } 117 118 public String getModelLength(String model) { 119 return _engineLengthHashTable.get(model); 120 } 121 122 public void setModelType(String model, String type) { 123 _engineTypeHashTable.put(model, type); 124 } 125 126 public String getModelType(String model) { 127 return _engineTypeHashTable.get(model); 128 } 129 130 public void setModelBunit(String model, boolean bUnit) { 131 _engineBunitHashTable.put(model, bUnit); 132 } 133 134 public boolean isModelBunit(String model) { 135 if (_engineBunitHashTable.containsKey(model)) 136 return _engineBunitHashTable.get(model); 137 return false; 138 } 139 140 public void setModelWeight(String model, String type) { 141 _engineWeightHashTable.put(model, type); 142 } 143 144 /** 145 * 146 * @param model The engine model (example GP20) 147 * @return This model's weight in tons 148 */ 149 public String getModelWeight(String model) { 150 return _engineWeightHashTable.get(model); 151 } 152 153 private void loadDefaults() { 154 String[] models = MODELS.split(","); // NOI18N 155 String[] hps = HORSEPOWER.split(","); // NOI18N 156 String[] lengths = ENGINELENGTHS.split(","); // NOI18N 157 String[] types = ENGINETYPES.split(","); // NOI18N 158 String[] weights = ENGINEWEIGHTS.split(","); // NOI18N 159 if (models.length != hps.length || 160 models.length != lengths.length || 161 models.length != types.length || 162 models.length != weights.length) { 163 log.error("Defaults do not have the right number of items, models={} hps={} lengths={} types={}", models.length, hps.length, lengths.length, types.length); // NOI18N 164 return; 165 } 166 167 for (int i = 0; i < models.length; i++) { 168 setModelHorsepower(models[i], hps[i]); 169 setModelLength(models[i], lengths[i]); 170 setModelType(models[i], types[i]); 171 setModelWeight(models[i], weights[i]); 172 setModelBunit(models[i], false); // there are no B units in the default files 173 } 174 } 175 176 /** 177 * Create an XML element to represent this Entry. This member has to remain 178 * synchronized with the detailed DTD in operations-engines.dtd. 179 * 180 * @param root The common Element for operations-engines.dtd. 181 * 182 */ 183 public void store(Element root) { 184 store(root, Xml.MODELS, Xml.MODEL); 185 } 186 187 public void load(Element root) { 188 load(root, Xml.MODELS, Xml.MODEL, Xml.ENGINE_MODELS); 189 } 190 191 protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) { 192 // Set dirty 193 InstanceManager.getDefault(EngineManagerXml.class).setDirty(true); 194 super.firePropertyChange(p, old, n); 195 } 196 197 private final static Logger log = LoggerFactory.getLogger(EngineModels.class); 198 199 @ServiceProvider(service = InstanceInitializer.class) 200 public static class Initializer extends AbstractInstanceInitializer { 201 202 @Override 203 public <T> Object getDefault(Class<T> type) { 204 if (type.equals(EngineModels.class)) { 205 EngineModels instance = new EngineModels(); 206 instance.loadDefaults(); 207 return instance; 208 } 209 return super.getDefault(type); 210 } 211 212 @Override 213 public Set<Class<?>> getInitalizes() { 214 Set<Class<?>> set = super.getInitalizes(); 215 set.add(EngineModels.class); 216 return set; 217 } 218 219 } 220}