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 void setMaxCharLength(int length) {
091        max_len_string_attibute = length;
092        InstanceManager.getDefault(OperationsSetupXml.class).setDirty(true);
093    }
094
095    // must synchronize changes with operation-config.dtd
096    public static Element store() {
097        Element values;
098        Element length;
099        Element e = new Element(Xml.CONTROL);
100        // maximum string lengths
101        e.addContent(values = new Element(Xml.MAXIMUM_STRING_LENGTHS));
102        values.addContent(length = new Element(Xml.MAX_LEN_STRING_ATTRIBUTE));
103        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_attibute));
104        values.addContent(length = new Element(Xml.MAX_LEN_STRING_ROAD_NUMBER));
105        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_road_number));
106        values.addContent(length = new Element(Xml.MAX_LEN_STRING_PRINT_ROAD_NUMBER));
107        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_print_road_number));
108        values.addContent(length = new Element(Xml.MAX_LEN_STRING_LOCATION_NAME));
109        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_location_name));
110        values.addContent(length = new Element(Xml.MAX_LEN_STRING_TRACK_NAME));
111        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_track_name));
112        values.addContent(length = new Element(Xml.MAX_LEN_STRING_TRACK_LENGTH_NAME));
113        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_track_length_name));
114        values.addContent(length = new Element(Xml.MAX_LEN_STRING_LENGTH_NAME));
115        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_length_name));
116        values.addContent(length = new Element(Xml.MAX_LEN_STRING_WEIGHT_NAME));
117        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_weight_name));
118        values.addContent(length = new Element(Xml.MAX_LEN_STRING_BUILT_NAME));
119        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_built_name));
120        values.addContent(length = new Element(Xml.MAX_LEN_STRING_TRAIN_NAME));
121        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_train_name));
122        values.addContent(length = new Element(Xml.MAX_LEN_STRING_ROUTE_NAME));
123        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_route_name));
124        values.addContent(length = new Element(Xml.MAX_LEN_STRING_AUTOMATION_NAME));
125        length.setAttribute(Xml.LENGTH, Integer.toString(max_len_string_automation_name));
126        // reports
127        e.addContent(values = new Element(Xml.REPORTS));
128        values.setAttribute(Xml.FONT_SIZE, Integer.toString(reportFontSize));
129        values.setAttribute(Xml.FONT_NAME, reportFontName);
130        // actions
131        e.addContent(values = new Element(Xml.ACTIONS));
132        values.setAttribute(Xml.EXCEL_WAIT_TIME, Integer.toString(excelWaitTime));
133        // print control
134        e.addContent(values = new Element(Xml.PRINT_OPTIONS));
135        values.setAttribute(Xml.DISABLE_PRINT_IF_CUSTOM, disablePrintingIfCustom ? Xml.TRUE : Xml.FALSE);
136        // HPT speed for calculations
137        e.addContent(values = new Element(Xml.SPEED_HPT));
138        values.setAttribute(Xml.MPH, Integer.toString(speedHpt));
139        // show clones?
140        e.addContent(values = new Element(Xml.DISPLAY));
141        values.setAttribute(Xml.SHOW_CLONES, showCloneCars ? Xml.TRUE : Xml.FALSE);
142        return e;
143    }
144
145    public static void load(Element e) {
146        Element eControl = e.getChild(Xml.CONTROL);
147        if (eControl == null) {
148            return;
149        }
150        Element maximumStringLengths = eControl.getChild(Xml.MAXIMUM_STRING_LENGTHS);
151        if (maximumStringLengths != null) {
152            Attribute length;
153            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ATTRIBUTE) != null)
154                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ATTRIBUTE).getAttribute(Xml.LENGTH)) != null) {
155                max_len_string_attibute = Integer.parseInt(length.getValue());
156            }
157            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROAD_NUMBER) != null)
158                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROAD_NUMBER).getAttribute(Xml.LENGTH)) != null) {
159                max_len_string_road_number = Integer.parseInt(length.getValue());
160            }
161            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_PRINT_ROAD_NUMBER) != null)
162                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_PRINT_ROAD_NUMBER).getAttribute(Xml.LENGTH)) != null) {
163                max_len_string_print_road_number = Integer.parseInt(length.getValue());
164            }
165            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LOCATION_NAME) != null)
166                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LOCATION_NAME).getAttribute(Xml.LENGTH)) != null) {
167                max_len_string_location_name = Integer.parseInt(length.getValue());
168            }
169            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_NAME) != null)
170                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_NAME).getAttribute(Xml.LENGTH)) != null) {
171                max_len_string_track_name = Integer.parseInt(length.getValue());
172            }
173            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_LENGTH_NAME) != null)
174                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_LENGTH_NAME).getAttribute(Xml.LENGTH)) != null) {
175                max_len_string_track_length_name = Integer.parseInt(length.getValue());
176            }
177            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LENGTH_NAME) != null)
178                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LENGTH_NAME).getAttribute(Xml.LENGTH)) != null) {
179                max_len_string_length_name = Integer.parseInt(length.getValue());
180            }
181            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_WEIGHT_NAME) != null)
182                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_WEIGHT_NAME).getAttribute(Xml.LENGTH)) != null) {
183                max_len_string_weight_name = Integer.parseInt(length.getValue());
184            }
185            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_BUILT_NAME) != null)
186                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_BUILT_NAME).getAttribute(Xml.LENGTH)) != null) {
187                max_len_string_built_name = Integer.parseInt(length.getValue());
188            }
189            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRAIN_NAME) != null)
190                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRAIN_NAME).getAttribute(Xml.LENGTH)) != null) {
191                max_len_string_train_name = Integer.parseInt(length.getValue());
192            }
193            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROUTE_NAME) != null)
194                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROUTE_NAME).getAttribute(Xml.LENGTH)) != null) {
195                max_len_string_route_name = Integer.parseInt(length.getValue());
196            }
197            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_AUTOMATION_NAME) != null)
198                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_AUTOMATION_NAME).getAttribute(Xml.LENGTH)) != null) {
199                max_len_string_automation_name = Integer.parseInt(length.getValue());
200            }
201        }
202        Element eReports = eControl.getChild(Xml.REPORTS);
203        if (eReports != null) {
204            Attribute a;
205            if ((a = eReports.getAttribute(Xml.FONT_SIZE)) != null) {
206                try {
207                    reportFontSize = a.getIntValue();
208                } catch (DataConversionException e1) {
209                    log.error("Report font size ({}) isn't a number", a.getValue());
210                }
211            }
212            if ((a = eReports.getAttribute(Xml.FONT_NAME)) != null) {
213                reportFontName = a.getValue();
214            }
215        }
216        Element eActions = eControl.getChild(Xml.ACTIONS);
217        if (eActions != null) {
218            Attribute a;
219            if ((a = eActions.getAttribute(Xml.EXCEL_WAIT_TIME)) != null) {
220                try {
221                    excelWaitTime = a.getIntValue();
222                } catch (DataConversionException e1) {
223                    log.error("Excel wait time ({}) isn't a number", a.getValue());
224                }
225            }
226        }
227        Element ePrintOptions = eControl.getChild(Xml.PRINT_OPTIONS);
228        if (ePrintOptions != null) {
229            Attribute format;
230            if ((format = ePrintOptions.getAttribute(Xml.DISABLE_PRINT_IF_CUSTOM)) != null) {
231                disablePrintingIfCustom = format.getValue().equals(Xml.TRUE);
232            }
233        }
234        Element eSpeedHtp = eControl.getChild(Xml.SPEED_HPT);
235        if (eSpeedHtp != null) {
236            Attribute a;
237            if ((a = eSpeedHtp.getAttribute(Xml.MPH)) != null) {
238                try {
239                    speedHpt = a.getIntValue();
240                } catch (DataConversionException e1) {
241                    log.error("HPT speed in MPH ({}) isn't a number", a.getValue());
242                }
243            }
244        }
245        Element eDisplay = eControl.getChild(Xml.DISPLAY);
246        if (eDisplay != null) {
247            Attribute a;
248            if ((a = eDisplay.getAttribute(Xml.SHOW_CLONES)) != null) {
249                showCloneCars = a.getValue().equals(Xml.TRUE);
250            }
251        }
252    }
253
254    private final static Logger log = LoggerFactory.getLogger(Control.class);
255}