001package jmri.jmrix.ecos;
002
003import jmri.InstanceManager;
004import jmri.jmrix.ecos.swing.preferences.PreferencesPane;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Stores User Preferences on how to deal with synchronising the Ecos Database
010 * with JMRI.
011 *
012 * @author Kevin Dickerson Copyright (C) 2009
013 */
014public class EcosPreferences /*implements java.beans.PropertyChangeListener*/ {
015
016    public EcosPreferences(EcosSystemConnectionMemo memo) {
017        log.debug("creating a new EcosPreferences object");
018
019        ecosPreferencesShutDownTask = () -> InstanceManager.getDefault(jmri.ConfigureManager.class).storePrefs();
020        InstanceManager.getDefault(jmri.ShutDownManager.class).register(ecosPreferencesShutDownTask);
021
022        adaptermemo = memo;
023        InstanceManager.store(new PreferencesPane(this), jmri.swing.PreferencesPanel.class);
024    }
025
026    private final EcosSystemConnectionMemo adaptermemo;
027
028    boolean preferencesLoaded = false;
029
030    public boolean getPreferencesLoaded() {
031        return preferencesLoaded;
032    }
033
034    public void setPreferencesLoaded() {
035        preferencesLoaded = true;
036        firePropertyChange("loaded", null, null);
037    }
038
039    Runnable ecosPreferencesShutDownTask = null;
040
041    public static final int ASK = 0x00; // ie always ask the question
042    public static final int NO = 0x01; //ie never do the operation
043    public static final int YES = 0x02; //ie always perform the operation
044
045    private boolean _changeMade = false;
046
047    public boolean getChangeMade() {
048        return _changeMade;
049    }
050
051    /**
052     * Reset is used after the preferences have been loaded for the first time.
053     */
054    public void resetChangeMade() {
055        _changeMade = false;
056    }
057
058    /**
059     * Store the user's preference for when a loco is created in the Roster,
060     * should it also be created in the ECoS, if it does not exist? Currently
061     * not implemented.
062     */
063    private int _addlocotoecos = ASK;
064
065    public int getAddLocoToEcos() {
066        return _addlocotoecos;
067    }
068
069    public void setAddLocoToEcos(int boo) {
070        _addlocotoecos = boo;
071        changeMade();
072    }
073
074    /**
075     * Store the user's preference if a loco has been created on the ECoS,
076     * should an entry in the JMRI Roster be created. Currently not implemented.
077     */
078    private int _addlocotojmri = ASK;
079
080    public int getAddLocoToJMRI() {
081        return _addlocotojmri;
082    }
083
084    public void setAddLocoToJMRI(int boo) {
085        _addlocotojmri = boo;
086        changeMade();
087    }
088
089    /**
090     * Store the user's preference on how the ECoS loco description, should be
091     * formatted. Currently not implemented
092     */
093    private String _ecoslocodescription = null;
094
095    public String getEcosLocoDescription() {
096        return _ecoslocodescription;
097    }
098
099    public void setEcosLocoDescription(String descript) {
100        _ecoslocodescription = descript;
101        changeMade();
102    }
103
104    /**
105     * If there is a conflict in loco information between the ECoS and JMRI,
106     * this determines which system wins. Currently not implemented.
107     */
108    private static final int NOSYNC = 0x00;
109    private static final int WARN = 0x01;
110    private static final int JMRI = 0x02;
111    private static final int ECOS = 0x03;
112
113    private int _locomaster = 0x00;
114
115    public int getLocoMaster() {
116        return _locomaster;
117    }
118
119    public void setLocoMaster(int master) {
120        _locomaster = master;
121    }
122
123    /**
124     * Determine system description from GUI string for how to solve conflicts
125     * between rosters in JMRI and ECoS and store in _locomaster.
126     * <p>
127     * Keep identical to
128     * {@link jmri.jmrix.ecos.swing.preferences.PreferencesPane}#initializeMasterControlCombo(javax.swing.JComboBox)
129     *
130     * @param master setting for conflict syncing
131     */
132    public void setLocoMaster(String master) {
133        if (master.equals(Bundle.getMessage("NOSYNC"))) {
134            _locomaster = NOSYNC;
135        } else if (master.equals(Bundle.getMessage("WARNING"))) {
136            _locomaster = WARN;
137        } else if (master.equals("JMRI")) {
138            _locomaster = JMRI;
139        } else if (master.equals("ECoS")) {
140            _locomaster = ECOS;
141        } else {
142            _locomaster = NOSYNC;
143        }
144        changeMade();
145    }
146
147    /**
148     * Determine GUI string from system description for how to solve conflicts
149     * between rosters in JMRI and ECoS.
150     * <p>
151     * Keep identical to
152     * {@link jmri.jmrix.ecos.swing.preferences.PreferencesPane}#initializeMasterControlCombo(javax.swing.JComboBox)
153     *
154     * @return GUI string
155     */
156    public String getLocoMasterAsString() {
157        String result;
158        switch (_locomaster) {
159            case 0x00:
160                result = Bundle.getMessage("NOSYNC");
161                break;
162            case 0x01:
163                result = Bundle.getMessage("WARNING");
164                break;
165            case 0x02:
166                result = "JMRI";
167                break;
168            case 0x03:
169                result = "ECoS";
170                break;
171            default:
172                result = Bundle.getMessage("NOSYNC");
173                break;
174        }
175        return result;
176    }
177
178    /**
179     * Store the user's preference if a loco has been created ad-hoc, on the
180     * Throttle, should the entry created for it in the ECoS be deleted.
181     * Currently not implemented.
182     */
183    private int _adhoclocofromecos = ASK;
184
185    //0x00 - always ask
186    //0x01 - always leave loco
187    //0x02 - always remove loco
188    public int getAdhocLocoFromEcos() {
189        return _adhoclocofromecos;
190    }
191
192    public void setAdhocLocoFromEcos(int boo) {
193        _adhoclocofromecos = boo;
194        changeMade();
195    }
196
197    /**
198     * Store the user's preference to deal with if another device has control
199     * over the loco
200     */
201    private int _forcecontrolfromecos = ASK;
202
203    //0x00 - always ask
204    //0x01 - always always fail
205    //0x02 - always force control
206    public int getForceControlFromEcos() {
207        return _forcecontrolfromecos;
208    }
209
210    public void setForceControlFromEcos(int boo) {
211        _forcecontrolfromecos = boo;
212        changeMade();
213    }
214
215    private boolean _locoControl = false;
216
217    public boolean getLocoControl() {
218        return _locoControl;
219    }
220
221    public void setLocoControl(boolean boo) {
222        _locoControl = boo;
223        changeMade();
224    }
225
226    /**
227     * Store the user's preference if a loco has been created ad-hoc, on the
228     * Throttle, should the entry created for it in the ECOS be deleted.
229     * Currently not implemented.
230     */
231
232    /*private String _defaultecosprotocol = "DCC128";
233
234     public String getDefaultEcosProtocol(){
235     return _defaultecosprotocol;
236     }
237
238     public void setDefaultEcosProtocol(String boo){
239     _defaultecosprotocol = boo;
240     changeMade();
241     }*/
242
243    /**
244     * Store the user's preference for deleting a loco from the roster should
245     * it, also be deleted from the ECOS. Currently not implemented.
246     */
247    //0x00 - always ask
248    //0x01 - always leave loco
249    //0x02 - always remove loco
250    private int _removelocofromecos = ASK;
251
252    public int getRemoveLocoFromEcos() {
253        return _removelocofromecos;
254    }
255
256    public void setRemoveLocoFromEcos(int boo) {
257        _removelocofromecos = boo;
258        changeMade();
259    }
260
261    /**
262     * Store the user's preference for deleting a loco from the ECOS should it,
263     * also be deleted from the JMRI Roster. Currently not implemented.
264     */
265    private int _removelocofromjmri = ASK;
266
267    public int getRemoveLocoFromJMRI() {
268        return _removelocofromjmri;
269    }
270
271    public void setRemoveLocoFromJMRI(int boo) {
272        _removelocofromjmri = boo;
273        changeMade();
274    }
275
276    /**
277     * Store the user's preference when creating a turnout in JMRI should it
278     * also be created on the ECOS. Currently not implemented.
279     */
280    private int _addturnoutstoecos = ASK;
281
282    public int getAddTurnoutsToEcos() {
283        return _addturnoutstoecos;
284    }
285
286    public void setAddTurnoutsToEcos(int boo) {
287        _addturnoutstoecos = boo;
288        changeMade();
289    }
290
291    /**
292     * Store the user's preference when a new turnout is created on the ECOS
293     * should it also be created in JMRI. Currently not implemented.
294     */
295    private int _addturnoutstojmri = ASK;
296
297    public int getAddTurnoutsToJMRI() {
298        return _addturnoutstojmri;
299    }
300
301    public void setAddTurnoutsToJMRI(int boo) {
302        _addturnoutstojmri = boo;
303        changeMade();
304    }
305
306    /**
307     * Store the user's preference when a new turnout is removed from the ECOS
308     * should it also be removed from JMRI. Currently not implemented.
309     */
310    private int _removeturnoutsfromjmri = ASK;
311
312    public int getRemoveTurnoutsFromJMRI() {
313        return _removeturnoutsfromjmri;
314    }
315
316    public void setRemoveTurnoutsFromJMRI(int boo) {
317        _removeturnoutsfromjmri = boo;
318        changeMade();
319    }
320
321    /**
322     * Store the user's preference when a new turnout is removed from JMRI
323     * should it also be removed from the ECoS. Currently not implemented.
324     */
325    private int _removeturnoutsfromecos = ASK;
326
327    public int getRemoveTurnoutsFromEcos() {
328        return _removeturnoutsfromecos;
329    }
330
331    public void setRemoveTurnoutsFromEcos(int boo) {
332        _removeturnoutsfromecos = boo;
333        changeMade();
334    }
335
336    private String _rosterAttribute = "EcosObject";
337
338    public void setRosterAttribute(String att) {
339        //If no suffix is passed then we just use the default.
340        if ((att == null) || (att.equals(""))) {
341            _rosterAttribute = "EcosObject";
342        } else if (att.startsWith("EcosObject")) {
343            _rosterAttribute = att;
344        } else {
345            _rosterAttribute = "EcosObject:" + att;
346        }
347        changeMade();
348    }
349
350    public String getRosterAttribute() {
351        return _rosterAttribute;
352    }
353
354    public String getRosterAttributeSuffix() {
355        /*This is a simple case, if the string is not 11 characters long, then no
356         prefix has been set, therefore we can just return ""*/
357        try {
358            return _rosterAttribute.substring(11);
359        } catch (java.lang.StringIndexOutOfBoundsException ignore) {
360        }
361        return null;
362    }
363
364    public String name() {
365        return null;
366    }
367
368    void changeMade() {
369        _changeMade = true;
370        firePropertyChange("update", null, null);
371    }
372
373    java.beans.PropertyChangeSupport pcs = new java.beans.PropertyChangeSupport(this);
374
375    public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
376        pcs.addPropertyChangeListener(l);
377    }
378
379    public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
380        pcs.removePropertyChangeListener(l);
381    }
382
383    protected void firePropertyChange(String p, Object old, Object n) {
384        pcs.firePropertyChange(p, old, n);
385    }
386
387    private final static Logger log = LoggerFactory.getLogger(EcosPreferences.class);
388
389    /**
390     * @return the adaptermemo
391     */
392    public EcosSystemConnectionMemo getAdaptermemo() {
393        return adaptermemo;
394    }
395
396}