001package jmri.jmrit.operations.setup; 002 003import org.jdom2.*; 004import org.slf4j.Logger; 005import org.slf4j.LoggerFactory; 006 007import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 008 009/** 010 * Controls for operations developers. Debug Property changes and instance 011 * creation, maximum panel width, etc. 012 * 013 * @author Daniel Boudreau Copyright (C) 2008 014 * 015 */ 016@SuppressFBWarnings(value = "MS_CANNOT_BE_FINAL") 017public class Control { 018 019 // debug flags 020 public static final boolean SHOW_PROPERTY = false; 021 022 // Default panel width 023 public static final int panelWidth1025 = 1025; 024 public static final int panelWidth700 = 700; 025 public static final int panelWidth600 = 600; 026 public static final int panelWidth500 = 500; 027 public static final int panelWidth400 = 400; 028 public static final int panelWidth300 = 300; 029 030 // Default panel height 031 public static final int panelHeight600 = 600; 032 public static final int panelHeight500 = 500; 033 public static final int panelHeight400 = 400; 034 public static final int panelHeight300 = 300; 035 public static final int panelHeight250 = 250; 036 public static final int panelHeight200 = 200; 037 public static final int panelHeight100 = 100; 038 039 // Train build parameters 040 041 // Car and Engine attribute maximum string length 042 public static int max_len_string_attibute = 12; 043 044 // Car and Engine number maximum string length 045 public static int max_len_string_road_number = 10; 046 047 // Car and Engine number maximum string length when printing 048 public static int max_len_string_print_road_number = 6; 049 050 // Location name maximum string length 051 public static int max_len_string_location_name = 25; 052 053 // Track name maximum string length 054 public static int max_len_string_track_name = 25; 055 056 // Track length maximum string length 057 public static int max_len_string_track_length_name = 5; 058 059 // Car and Engine length maximum string length 060 public static int max_len_string_length_name = 4; 061 062 // Car weight maximum string length 063 public static int max_len_string_weight_name = 4; 064 065 // Car and Engine built date maximum string length 066 public static int max_len_string_built_name = 5; 067 068 // Train name maximum string length 069 public static int max_len_string_train_name = 25; 070 071 // Route name maximum string length 072 public static int max_len_string_route_name = 25; 073 074 // Automation name maximum string length 075 public static int max_len_string_automation_name = 25; 076 077 public static int reportFontSize = 10; 078 @SuppressFBWarnings(value = "MS_PKGPROTECT") // allow access for testing 079 public static String reportFontName = ""; // use default 080 081 public static int excelWaitTime = 120; // in seconds 082 083 public static boolean disablePrintingIfCustom = false; 084 085 public static int speedHpt = 36; 086 087 public static boolean showCloneCars = true; 088 089 // must synchronize changes with operation-config.dtd 090 public static Element store() { 091 Element values; 092 Element length; 093 Element e = new Element(Xml.CONTROL); 094 // maximum string lengths 095 e.addContent(values = new Element(Xml.MAXIMUM_STRING_LENGTHS)); 096 values.addContent(length = new Element(Xml.MAX_LEN_STRING_ATTRIBUTE)); 097 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_attibute)); 098 values.addContent(length = new Element(Xml.MAX_LEN_STRING_ROAD_NUMBER)); 099 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_road_number)); 100 values.addContent(length = new Element(Xml.MAX_LEN_STRING_PRINT_ROAD_NUMBER)); 101 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_print_road_number)); 102 values.addContent(length = new Element(Xml.MAX_LEN_STRING_LOCATION_NAME)); 103 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_location_name)); 104 values.addContent(length = new Element(Xml.MAX_LEN_STRING_TRACK_NAME)); 105 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_track_name)); 106 values.addContent(length = new Element(Xml.MAX_LEN_STRING_TRACK_LENGTH_NAME)); 107 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_track_length_name)); 108 values.addContent(length = new Element(Xml.MAX_LEN_STRING_LENGTH_NAME)); 109 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_length_name)); 110 values.addContent(length = new Element(Xml.MAX_LEN_STRING_WEIGHT_NAME)); 111 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_weight_name)); 112 values.addContent(length = new Element(Xml.MAX_LEN_STRING_BUILT_NAME)); 113 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_built_name)); 114 values.addContent(length = new Element(Xml.MAX_LEN_STRING_TRAIN_NAME)); 115 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_train_name)); 116 values.addContent(length = new Element(Xml.MAX_LEN_STRING_ROUTE_NAME)); 117 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_route_name)); 118 values.addContent(length = new Element(Xml.MAX_LEN_STRING_AUTOMATION_NAME)); 119 length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_automation_name)); 120 // reports 121 e.addContent(values = new Element(Xml.REPORTS)); 122 values.setAttribute(Xml.FONT_SIZE, Integer.toString(reportFontSize)); 123 values.setAttribute(Xml.FONT_NAME, reportFontName); 124 // actions 125 e.addContent(values = new Element(Xml.ACTIONS)); 126 values.setAttribute(Xml.EXCEL_WAIT_TIME, Integer.toString(excelWaitTime)); 127 // print control 128 e.addContent(values = new Element(Xml.PRINT_OPTIONS)); 129 values.setAttribute(Xml.DISABLE_PRINT_IF_CUSTOM, disablePrintingIfCustom ? Xml.TRUE : Xml.FALSE); 130 // HPT speed for calculations 131 e.addContent(values = new Element(Xml.SPEED_HPT)); 132 values.setAttribute(Xml.MPH, Integer.toString(speedHpt)); 133 // show clones? 134 e.addContent(values = new Element(Xml.DISPLAY)); 135 values.setAttribute(Xml.SHOW_CLONES, showCloneCars ? Xml.TRUE : Xml.FALSE); 136 return e; 137 } 138 139 public static void load(Element e) { 140 Element eControl = e.getChild(Xml.CONTROL); 141 if (eControl == null) { 142 return; 143 } 144 Element maximumStringLengths = eControl.getChild(Xml.MAXIMUM_STRING_LENGTHS); 145 if (maximumStringLengths != null) { 146 Attribute length; 147 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ATTRIBUTE) != null) 148 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ATTRIBUTE).getAttribute(Xml.LENGTH)) != null) { 149 max_len_string_attibute = Integer.parseInt(length.getValue()); 150 } 151 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROAD_NUMBER) != null) 152 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROAD_NUMBER).getAttribute(Xml.LENGTH)) != null) { 153 max_len_string_road_number = Integer.parseInt(length.getValue()); 154 } 155 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_PRINT_ROAD_NUMBER) != null) 156 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_PRINT_ROAD_NUMBER).getAttribute(Xml.LENGTH)) != null) { 157 max_len_string_print_road_number = Integer.parseInt(length.getValue()); 158 } 159 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LOCATION_NAME) != null) 160 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LOCATION_NAME).getAttribute(Xml.LENGTH)) != null) { 161 max_len_string_location_name = Integer.parseInt(length.getValue()); 162 } 163 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_NAME) != null) 164 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_NAME).getAttribute(Xml.LENGTH)) != null) { 165 max_len_string_track_name = Integer.parseInt(length.getValue()); 166 } 167 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_LENGTH_NAME) != null) 168 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_LENGTH_NAME).getAttribute(Xml.LENGTH)) != null) { 169 max_len_string_track_length_name = Integer.parseInt(length.getValue()); 170 } 171 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LENGTH_NAME) != null) 172 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LENGTH_NAME).getAttribute(Xml.LENGTH)) != null) { 173 max_len_string_length_name = Integer.parseInt(length.getValue()); 174 } 175 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_WEIGHT_NAME) != null) 176 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_WEIGHT_NAME).getAttribute(Xml.LENGTH)) != null) { 177 max_len_string_weight_name = Integer.parseInt(length.getValue()); 178 } 179 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_BUILT_NAME) != null) 180 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_BUILT_NAME).getAttribute(Xml.LENGTH)) != null) { 181 max_len_string_built_name = Integer.parseInt(length.getValue()); 182 } 183 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRAIN_NAME) != null) 184 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRAIN_NAME).getAttribute(Xml.LENGTH)) != null) { 185 max_len_string_train_name = Integer.parseInt(length.getValue()); 186 } 187 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROUTE_NAME) != null) 188 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROUTE_NAME).getAttribute(Xml.LENGTH)) != null) { 189 max_len_string_route_name = Integer.parseInt(length.getValue()); 190 } 191 if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_AUTOMATION_NAME) != null) 192 && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_AUTOMATION_NAME).getAttribute(Xml.LENGTH)) != null) { 193 max_len_string_automation_name = Integer.parseInt(length.getValue()); 194 } 195 } 196 Element eReports = eControl.getChild(Xml.REPORTS); 197 if (eReports != null) { 198 Attribute a; 199 if ((a = eReports.getAttribute(Xml.FONT_SIZE)) != null) { 200 try { 201 reportFontSize = a.getIntValue(); 202 } catch (DataConversionException e1) { 203 log.error("Report font size ({}) isn't a number", a.getValue()); 204 } 205 } 206 if ((a = eReports.getAttribute(Xml.FONT_NAME)) != null) { 207 reportFontName = a.getValue(); 208 } 209 } 210 Element eActions = eControl.getChild(Xml.ACTIONS); 211 if (eActions != null) { 212 Attribute a; 213 if ((a = eActions.getAttribute(Xml.EXCEL_WAIT_TIME)) != null) { 214 try { 215 excelWaitTime = a.getIntValue(); 216 } catch (DataConversionException e1) { 217 log.error("Excel wait time ({}) isn't a number", a.getValue()); 218 } 219 } 220 } 221 Element ePrintOptions = eControl.getChild(Xml.PRINT_OPTIONS); 222 if (ePrintOptions != null) { 223 Attribute format; 224 if ((format = ePrintOptions.getAttribute(Xml.DISABLE_PRINT_IF_CUSTOM)) != null) { 225 disablePrintingIfCustom = format.getValue().equals(Xml.TRUE); 226 } 227 } 228 Element eSpeedHtp = eControl.getChild(Xml.SPEED_HPT); 229 if (eSpeedHtp != null) { 230 Attribute a; 231 if ((a = eSpeedHtp.getAttribute(Xml.MPH)) != null) { 232 try { 233 speedHpt = a.getIntValue(); 234 } catch (DataConversionException e1) { 235 log.error("HPT speed in MPH ({}) isn't a number", a.getValue()); 236 } 237 } 238 } 239 Element eDisplay = eControl.getChild(Xml.DISPLAY); 240 if (eDisplay != null) { 241 Attribute a; 242 if ((a = eDisplay.getAttribute(Xml.SHOW_CLONES)) != null) { 243 showCloneCars = a.getValue().equals(Xml.TRUE); 244 } 245 } 246 } 247 248 private final static Logger log = LoggerFactory.getLogger(Control.class); 249}