001package jmri.jmrit.operations.setup;
002
003import org.jdom2.Attribute;
004import org.jdom2.DataConversionException;
005import org.jdom2.Element;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
010
011/**
012 * Controls for operations developers. Debug Property changes and instance
013 * creation, maximum panel width, etc.
014 *
015 * @author Daniel Boudreau Copyright (C) 2008
016 *
017 */
018@SuppressFBWarnings(value = "MS_CANNOT_BE_FINAL")
019public class Control {
020
021    // debug flags
022    public static final boolean SHOW_PROPERTY = false;
023
024    // Default panel width
025    public static final int panelWidth1025 = 1025;
026    public static final int panelWidth700 = 700;
027    public static final int panelWidth600 = 600;
028    public static final int panelWidth500 = 500;
029    public static final int panelWidth400 = 400;
030    public static final int panelWidth300 = 300;
031
032    // Default panel height
033    public static final int panelHeight600 = 600;
034    public static final int panelHeight500 = 500;
035    public static final int panelHeight400 = 400;
036    public static final int panelHeight300 = 300;
037    public static final int panelHeight250 = 250;
038    public static final int panelHeight200 = 200;
039    public static final int panelHeight100 = 100;
040
041    // Train build parameters
042
043    // Car and Engine attribute maximum string length
044    public static int max_len_string_attibute = 12;
045
046    // Car and Engine number maximum string length
047    public static int max_len_string_road_number = 10;
048
049    // Car and Engine number maximum string length when printing
050    public static int max_len_string_print_road_number = 6;
051
052    // Location name maximum string length
053    public static int max_len_string_location_name = 25;
054
055    // Track name maximum string length
056    public static int max_len_string_track_name = 25;
057
058    // Track length maximum string length
059    public static int max_len_string_track_length_name = 5;
060
061    // Car and Engine length maximum string length
062    public static int max_len_string_length_name = 4;
063
064    // Car weight maximum string length
065    public static int max_len_string_weight_name = 4;
066
067    // Car and Engine built date maximum string length
068    public static int max_len_string_built_name = 5;
069
070    // Train name maximum string length
071    public static int max_len_string_train_name = 25;
072
073    // Route name maximum string length
074    public static int max_len_string_route_name = 25;
075
076    // Automation name maximum string length
077    public static int max_len_string_automation_name = 25;
078
079    public static int reportFontSize = 10;
080    @SuppressFBWarnings(value = "MS_PKGPROTECT") // allow access for testing
081    public static String reportFontName = ""; // use default
082
083    public static int excelWaitTime = 120; // in seconds
084
085    public static boolean disablePrintingIfCustom = false;
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
129        return e;
130    }
131
132    public static void load(Element e) {
133        Element eControl = e.getChild(Xml.CONTROL);
134        if (eControl == null) {
135            return;
136        }
137        Element maximumStringLengths = eControl.getChild(Xml.MAXIMUM_STRING_LENGTHS);
138        if (maximumStringLengths != null) {
139            Attribute length;
140            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ATTRIBUTE) != null)
141                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ATTRIBUTE).getAttribute(Xml.LENGTH)) != null) {
142                max_len_string_attibute = Integer.parseInt(length.getValue());
143            }
144            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROAD_NUMBER) != null)
145                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROAD_NUMBER).getAttribute(Xml.LENGTH)) != null) {
146                max_len_string_road_number = Integer.parseInt(length.getValue());
147            }
148            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_PRINT_ROAD_NUMBER) != null)
149                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_PRINT_ROAD_NUMBER).getAttribute(Xml.LENGTH)) != null) {
150                max_len_string_print_road_number = Integer.parseInt(length.getValue());
151            }
152            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LOCATION_NAME) != null)
153                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LOCATION_NAME).getAttribute(Xml.LENGTH)) != null) {
154                max_len_string_location_name = Integer.parseInt(length.getValue());
155            }
156            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_NAME) != null)
157                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_NAME).getAttribute(Xml.LENGTH)) != null) {
158                max_len_string_track_name = Integer.parseInt(length.getValue());
159            }
160            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_LENGTH_NAME) != null)
161                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRACK_LENGTH_NAME).getAttribute(Xml.LENGTH)) != null) {
162                max_len_string_track_length_name = Integer.parseInt(length.getValue());
163            }
164            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LENGTH_NAME) != null)
165                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_LENGTH_NAME).getAttribute(Xml.LENGTH)) != null) {
166                max_len_string_length_name = Integer.parseInt(length.getValue());
167            }
168            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_WEIGHT_NAME) != null)
169                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_WEIGHT_NAME).getAttribute(Xml.LENGTH)) != null) {
170                max_len_string_weight_name = Integer.parseInt(length.getValue());
171            }
172            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_BUILT_NAME) != null)
173                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_BUILT_NAME).getAttribute(Xml.LENGTH)) != null) {
174                max_len_string_built_name = Integer.parseInt(length.getValue());
175            }
176            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRAIN_NAME) != null)
177                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_TRAIN_NAME).getAttribute(Xml.LENGTH)) != null) {
178                max_len_string_train_name = Integer.parseInt(length.getValue());
179            }
180            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROUTE_NAME) != null)
181                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_ROUTE_NAME).getAttribute(Xml.LENGTH)) != null) {
182                max_len_string_route_name = Integer.parseInt(length.getValue());
183            }
184            if ((maximumStringLengths.getChild(Xml.MAX_LEN_STRING_AUTOMATION_NAME) != null)
185                    && (length = maximumStringLengths.getChild(Xml.MAX_LEN_STRING_AUTOMATION_NAME).getAttribute(Xml.LENGTH)) != null) {
186                max_len_string_automation_name = Integer.parseInt(length.getValue());
187            }
188        }
189        Element eReports = eControl.getChild(Xml.REPORTS);
190        if (eReports != null) {
191            Attribute a;
192            if ((a = eReports.getAttribute(Xml.FONT_SIZE)) != null) {
193                try {
194                    reportFontSize = a.getIntValue();
195                } catch (DataConversionException e1) {
196                    log.error("Report font size ({}) isn't a number", a.getValue());
197                }
198            }
199            if ((a = eReports.getAttribute(Xml.FONT_NAME)) != null) {
200                reportFontName = a.getValue();
201            }
202        }
203        Element eActions = eControl.getChild(Xml.ACTIONS);
204        if (eActions != null) {
205            Attribute a;
206            if ((a = eActions.getAttribute(Xml.EXCEL_WAIT_TIME)) != null) {
207                try {
208                    excelWaitTime = a.getIntValue();
209                } catch (DataConversionException e1) {
210                    log.error("Excel wait time ({}) isn't a number", a.getValue());
211                }
212            }
213        }
214        Element ePrintOptions = eControl.getChild(Xml.PRINT_OPTIONS);
215        if (ePrintOptions != null) {
216            Attribute format;
217            if ((format = ePrintOptions.getAttribute(Xml.DISABLE_PRINT_IF_CUSTOM)) != null) {
218                disablePrintingIfCustom = format.getValue().equals(Xml.TRUE);
219            }
220        }
221    }
222
223    private final static Logger log = LoggerFactory.getLogger(Control.class);
224}