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