001package jmri.jmrit.operations.rollingstock.cars; 002 003import java.util.ArrayList; 004import java.util.List; 005 006import org.jdom2.Element; 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010import jmri.InstanceManager; 011import jmri.InstanceManagerAutoDefault; 012import jmri.jmrit.operations.rollingstock.RollingStockAttribute; 013 014/** 015 * Represents the road names that cars can have. 016 * 017 * @author Daniel Boudreau Copyright (C) 2008, 2014 018 */ 019public class CarRoads extends RollingStockAttribute implements InstanceManagerAutoDefault { 020 021 private static final String ROADS = Bundle.getMessage("carRoadNames"); 022 public static final String CARROADS_CHANGED_PROPERTY = "CarRoads Length"; // NOI18N 023 public static final String CARROADS_NAME_CHANGED_PROPERTY = "CarRoads Name"; // NOI18N 024 025 public CarRoads() { 026 } 027 028 @Override 029 protected String getDefaultNames() { 030 return ROADS; 031 } 032 033 @Override 034 public void addName(String road) { 035 super.addName(road); 036 setDirtyAndFirePropertyChange(CARROADS_CHANGED_PROPERTY, null, road); 037 } 038 039 @Override 040 public void deleteName(String road) { 041 super.deleteName(road); 042 setDirtyAndFirePropertyChange(CARROADS_CHANGED_PROPERTY, road, null); 043 } 044 045 public void replaceName(String oldName, String newName) { 046 super.addName(newName); 047 setDirtyAndFirePropertyChange(CARROADS_NAME_CHANGED_PROPERTY, oldName, newName); 048 super.deleteName(oldName); 049 if (newName == null) { 050 setDirtyAndFirePropertyChange(CARROADS_CHANGED_PROPERTY, list.size() + 1, list.size()); 051 } 052 } 053 054 /** 055 * Get the maximum character length of a road name when printing on a 056 * manifest or switch list. Characters after the hyphen are ignored. 057 * 058 * @return the maximum character length of a car road name 059 */ 060 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings( value="SLF4J_FORMAT_SHOULD_BE_CONST", 061 justification="I18N of Info Message") 062 @Override 063 public int getMaxNameLength() { 064 if (maxNameSubStringLength == 0) { 065 super.getMaxNameSubStringLength(); 066 log.info(Bundle.getMessage("InfoMaxRoad", maxName, maxNameSubStringLength)); 067 } 068 return maxNameSubStringLength; 069 } 070 071 /** 072 * Gets a sorted list of road names for a given car type 073 * 074 * @param type car type 075 * @return list of road names 076 */ 077 public List<String> getNames(String type) { 078 List<String> names = new ArrayList<>(); 079 List<Car> cars = InstanceManager.getDefault(CarManager.class).getByTypeList(type); 080 for (Car car : cars) { 081 if (!names.contains(car.getRoadName())) { 082 names.add(car.getRoadName()); 083 } 084 } 085 java.util.Collections.sort(names); 086 return names; 087 } 088 089 /** 090 * Create an XML element to represent this Entry. This member has to remain 091 * synchronized with the detailed DTD in operations-cars.dtd. 092 * 093 * @param root The common Element for operations-cars.dtd. 094 * 095 */ 096 public void store(Element root) { 097 store(root, Xml.ROADS, Xml.ROAD); 098 } 099 100 public void load(Element root) { 101 load(root, Xml.ROADS, Xml.ROAD, Xml.ROAD_NAMES); 102 } 103 104 protected void setDirtyAndFirePropertyChange(String p, Object old, Object n) { 105 // Set dirty 106 InstanceManager.getDefault(CarManagerXml.class).setDirty(true); 107 super.firePropertyChange(p, old, n); 108 } 109 110 private final static Logger log = LoggerFactory.getLogger(CarRoads.class); 111}