001package jmri.jmrit.withrottle;
002
003import java.io.File;
004import java.util.Set;
005import jmri.InstanceInitializer;
006import jmri.implementation.AbstractInstanceInitializer;
007import jmri.util.FileUtil;
008import org.jdom2.Attribute;
009import org.jdom2.DataConversionException;
010import org.jdom2.Element;
011import org.openide.util.lookup.ServiceProvider;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * @author Brett Hoffman Copyright (C) 2010
017 */
018public class WiThrottlePreferences extends AbstractWiThrottlePreferences {
019
020    public static final int DEFAULT_PORT = 12090;
021
022    //  Flag that restart is required to apply preferences
023    private boolean isRestartRequired = false;
024
025    private boolean useEStop = true;
026    private int eStopDelay = 10;
027
028    private boolean useMomF2 = true;
029    
030    private boolean exclusiveUseOfAddress = false;
031
032    private int port = DEFAULT_PORT;
033
034    private boolean allowTrackPower = true;
035    private boolean allowTurnout = true;
036    private boolean allowTurnoutCreation = false; //defaults to NOT allowed
037    private boolean allowRoute = true;
038    private boolean allowConsist = true;
039    private boolean useWiFiConsist = true;
040    private boolean displayFastClock = true;
041
042    // track as loaded / as saved state
043    private boolean asLoadedUseEStop = true;
044    private int asLoadedEStopDelay = 10;
045
046    private boolean asLoadedUseMomF2 = true;
047    
048    private boolean asLoadedExclusive = false;
049
050    private int asLoadedPort = 0;
051
052    private boolean asLoadedAllowTrackPower = true;
053    private boolean asLoadedAllowTurnout = true;
054    private boolean asLoadedAllowTurnoutCreation = false;
055    private boolean asLoadedAllowRoute = true;
056    private boolean asLoadedAllowConsist = true;
057    private boolean asLoadedUseWiFiConsist = true;
058    private boolean asLoadedDisplayFastClock = true;
059
060    public WiThrottlePreferences(String fileName) {
061        super.openFile(fileName);
062    }
063
064    public WiThrottlePreferences() {
065    }
066
067    @Override
068    public void load(@javax.annotation.Nonnull Element child) {
069        Attribute a;
070        if ((a = child.getAttribute("isUseEStop")) != null) {
071            setUseEStop(a.getValue().equalsIgnoreCase("true"));
072            this.asLoadedUseEStop = this.isUseEStop();
073        }
074        if ((a = child.getAttribute("isExclusiveUseOfAddress")) != null) {
075            setExclusiveUseOfAddress(a.getValue().equalsIgnoreCase("true"));
076            this.asLoadedExclusive = this.isExclusiveUseOfAddress();
077        }
078        if ((a = child.getAttribute("getEStopDelay")) != null) {
079            try {
080                setEStopDelay(Integer.parseInt(a.getValue()));
081                this.asLoadedEStopDelay = this.getEStopDelay();
082            } catch (NumberFormatException e) {
083                log.debug("EStop Delay \"{}\" is invalid.", a.getValue(), e);
084            }
085        }
086        if ((a = child.getAttribute("isUseMomF2")) != null) {
087            setUseMomF2(a.getValue().equalsIgnoreCase("true"));
088            this.asLoadedUseMomF2 = this.isUseMomF2();
089        }
090        if ((a = child.getAttribute("getPort")) != null) {
091            try {
092                setPort(a.getIntValue());
093            } catch (DataConversionException ex) {
094                log.error("Port {} is invalid.", a.getValue());
095            }
096            this.asLoadedPort = this.getPort();
097        }
098
099        if ((a = child.getAttribute("isAllowTrackPower")) != null) {
100            setAllowTrackPower(a.getValue().equalsIgnoreCase("true"));
101            this.asLoadedAllowTrackPower = this.isAllowTrackPower();
102        }
103        if ((a = child.getAttribute("isAllowTurnout")) != null) {
104            setAllowTurnout(a.getValue().equalsIgnoreCase("true"));
105            this.asLoadedAllowTurnout = this.isAllowTurnout();
106        }
107        if ((a = child.getAttribute("isAllowTurnoutCreation")) != null) {
108            setAllowTurnoutCreation(a.getValue().equalsIgnoreCase("true"));
109            this.asLoadedAllowTurnoutCreation = this.isAllowTurnoutCreation();
110        }
111        if ((a = child.getAttribute("isAllowRoute")) != null) {
112            setAllowRoute(a.getValue().equalsIgnoreCase("true"));
113            this.asLoadedAllowRoute = this.isAllowRoute();
114        }
115        if ((a = child.getAttribute("isAllowConsist")) != null) {
116            setAllowConsist(a.getValue().equalsIgnoreCase("true"));
117            this.asLoadedAllowConsist = this.isAllowConsist();
118        }
119        if ((a = child.getAttribute("isUseWiFiConsist")) != null) {
120            setUseWiFiConsist(a.getValue().equalsIgnoreCase("true"));
121            this.asLoadedUseWiFiConsist = this.isUseWiFiConsist();
122        }
123        if ((a = child.getAttribute("isDisplayFastClock")) != null) {
124            setDisplayFastClock(a.getValue().equalsIgnoreCase("true"));
125            this.asLoadedDisplayFastClock = this.isDisplayFastClock();
126        }
127
128    }
129
130    public boolean compareValuesDifferent(WiThrottlePreferences prefs) {
131                return prefs.isUseEStop() != this.isUseEStop()
132                || prefs.getEStopDelay() != this.getEStopDelay()
133                || prefs.isUseMomF2() != this.isUseMomF2()
134                || prefs.isExclusiveUseOfAddress() != this.isExclusiveUseOfAddress()
135                || prefs.getPort() != this.getPort()
136                || prefs.isAllowTrackPower() != this.isAllowTrackPower()
137                || prefs.isAllowTurnout() != this.isAllowTurnout()
138                || prefs.isAllowTurnoutCreation() != this.isAllowTurnoutCreation()
139                || prefs.isAllowRoute() != this.isAllowRoute()
140                || prefs.isAllowConsist() != this.isAllowConsist()
141                || prefs.isUseWiFiConsist() != this.isUseWiFiConsist()
142                || prefs.isDisplayFastClock() != this.isDisplayFastClock();
143    }
144
145    public void apply(WiThrottlePreferences prefs) {
146        setUseEStop(prefs.isUseEStop());
147        setEStopDelay(prefs.getEStopDelay());
148        setUseMomF2(prefs.isUseMomF2());
149        setExclusiveUseOfAddress(prefs.isExclusiveUseOfAddress());
150        setPort(prefs.getPort());
151        setAllowTrackPower(prefs.isAllowTrackPower());
152        setAllowTurnout(prefs.isAllowTurnout());
153        setAllowTurnoutCreation(prefs.isAllowTurnoutCreation());
154        setAllowRoute(prefs.isAllowRoute());
155        setAllowConsist(prefs.isAllowConsist());
156        setUseWiFiConsist(prefs.isUseWiFiConsist());
157        setDisplayFastClock(prefs.isDisplayFastClock());
158    }
159
160    @Override
161    public Element store() {
162        if (this.isDirty()) {
163            this.isRestartRequired = true;
164        }
165        Element element = new Element("WiThrottlePreferences");
166        element.setAttribute("isUseEStop", "" + isUseEStop());
167        this.asLoadedUseEStop = this.isUseEStop();
168        element.setAttribute("getEStopDelay", "" + getEStopDelay());
169        this.asLoadedEStopDelay = this.getEStopDelay();
170        element.setAttribute("isUseMomF2", "" + isUseMomF2());
171        this.asLoadedUseMomF2 = this.isUseMomF2();
172        element.setAttribute("isExclusiveUseOfAddress", "" + isExclusiveUseOfAddress());
173        this.asLoadedExclusive = this.isExclusiveUseOfAddress();
174        element.setAttribute("getPort", "" + getPort());
175        this.asLoadedPort = this.getPort();
176        element.setAttribute("isAllowTrackPower", "" + isAllowTrackPower());
177        this.asLoadedAllowTrackPower = this.isAllowTrackPower();
178        element.setAttribute("isAllowTurnout", "" + isAllowTurnout());
179        this.asLoadedAllowTurnout = this.isAllowTurnout();
180        element.setAttribute("isAllowTurnoutCreation", "" + isAllowTurnoutCreation());
181        this.asLoadedAllowTurnoutCreation = this.isAllowTurnoutCreation();
182        element.setAttribute("isAllowRoute", "" + isAllowRoute());
183        this.asLoadedAllowRoute = this.isAllowRoute();
184        element.setAttribute("isAllowConsist", "" + isAllowConsist());
185        this.asLoadedAllowConsist = this.isAllowConsist();
186        element.setAttribute("isUseWiFiConsist", "" + isUseWiFiConsist());
187        this.asLoadedUseWiFiConsist = this.isUseWiFiConsist();
188        element.setAttribute("isDisplayFastClock", "" + isDisplayFastClock());
189        this.asLoadedDisplayFastClock = this.isDisplayFastClock();
190        return element;
191    }
192
193    public boolean isDirty() {
194        return this.asLoadedUseEStop != this.isUseEStop()
195                || this.asLoadedEStopDelay != this.getEStopDelay()
196                || this.asLoadedUseMomF2 != this.isUseMomF2()
197                || this.asLoadedExclusive != this.isExclusiveUseOfAddress()
198                || this.asLoadedPort == 0
199                || this.asLoadedPort != this.getPort()
200                || this.asLoadedAllowTrackPower != this.isAllowTrackPower()
201                || this.asLoadedAllowTurnout != this.isAllowTurnout()
202                || this.asLoadedAllowTurnoutCreation != this.isAllowTurnoutCreation()
203                || this.asLoadedAllowRoute != this.isAllowRoute()
204                || this.asLoadedAllowConsist != this.isAllowConsist()
205                || this.asLoadedUseWiFiConsist != this.isUseWiFiConsist()
206                || this.asLoadedDisplayFastClock != this.isDisplayFastClock();
207    }
208
209    public boolean isRestartRequired() {
210        return this.isRestartRequired;
211    }
212
213    public boolean isUseEStop() {
214        return useEStop;
215    }
216
217    public void setUseEStop(boolean value) {
218        useEStop = value;
219    }
220
221    public int getEStopDelay() {
222        return eStopDelay;
223    }
224
225    public void setEStopDelay(int value) {
226        eStopDelay = value;
227    }
228
229    public boolean isUseMomF2() {
230        return useMomF2;
231    }
232
233    public void setUseMomF2(boolean value) {
234        useMomF2 = value;
235    }
236
237    public boolean isExclusiveUseOfAddress() {
238        return exclusiveUseOfAddress;
239    }
240
241    public void setExclusiveUseOfAddress(boolean value) {
242        exclusiveUseOfAddress = value;
243    }
244
245    public int getPort() {
246        return port;
247    }
248
249    public void setPort(int value) {
250        port = value;
251    }
252
253    public boolean isAllowTrackPower() {
254        return allowTrackPower;
255    }
256
257    public void setAllowTrackPower(boolean value) {
258        allowTrackPower = value;
259    }
260
261    public boolean isAllowTurnout() {
262        return allowTurnout;
263    }
264
265    public void setAllowTurnout(boolean value) {
266        allowTurnout = value;
267    }
268
269    public boolean isAllowTurnoutCreation() {
270        return allowTurnoutCreation;
271    }
272    public void setAllowTurnoutCreation(boolean value) {
273        allowTurnoutCreation = value;
274    }
275
276
277    public boolean isAllowRoute() {
278        return allowRoute;
279    }
280
281    public void setAllowRoute(boolean value) {
282        allowRoute = value;
283    }
284
285    public boolean isAllowConsist() {
286        return allowConsist;
287    }
288
289    public void setAllowConsist(boolean value) {
290        allowConsist = value;
291    }
292
293    public boolean isUseWiFiConsist() {
294        return useWiFiConsist;
295    }
296
297    public void setUseWiFiConsist(boolean value) {
298        useWiFiConsist = value;
299    }
300    
301    public boolean isDisplayFastClock() {
302        return displayFastClock;
303    }
304
305    public void setDisplayFastClock(boolean value) {
306        displayFastClock = value;
307    }
308
309    private final static Logger log = LoggerFactory.getLogger(WiThrottlePreferences.class);
310
311    @ServiceProvider(service = InstanceInitializer.class)
312    public static class Initializer extends AbstractInstanceInitializer {
313
314        @Override
315        public <T> Object getDefault(Class<T> type) {
316            if (type.equals(WiThrottlePreferences.class)) {
317                return new WiThrottlePreferences(FileUtil.getUserFilesPath() + "throttle" + File.separator + "WiThrottlePreferences.xml"); // NOI18N
318            }
319            return super.getDefault(type);
320        }
321
322        @Override
323        public Set<Class<?>> getInitalizes() {
324            Set<Class<?>> set = super.getInitalizes();
325            set.add(WiThrottlePreferences.class);
326            return set;
327        }
328    }
329}