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