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