001package jmri.jmrix.loconet;
002
003import javax.annotation.Nonnull;
004import java.util.List;
005import java.util.ArrayList;
006import java.beans.PropertyChangeListener;
007
008import jmri.ProgListener;
009import jmri.Programmer;
010import jmri.ProgrammerException;
011import jmri.ProgrammingMode;
012
013/**
014 * Programmer implementation for Programmer that uses a SlotManager (which is also an AbstractProgrammer)
015 * that might be provided later. This is done by connecting through a LocoNetSystemConnectionMemo.
016 *
017 * @author Bob Jacobsen Copyright (C) 2018
018 */
019public class LnDeferProgrammer implements Programmer {
020
021    public LnDeferProgrammer(@Nonnull LocoNetSystemConnectionMemo memo) {
022        this.memo = memo;
023    }
024    
025    LocoNetSystemConnectionMemo memo;
026    
027    
028    /** {@inheritDoc} */
029    @Override
030    public void writeCV(String CV, int val, ProgListener p) throws ProgrammerException {
031        SlotManager m = memo.getSlotManager();
032        if (m!=null) {
033            m.writeCV(CV, val, p);
034        } else {
035            log.warn("writeCV called without a SlotManager");
036        }
037    }
038
039    /** {@inheritDoc} */
040    @Override
041    public void readCV(String CV, ProgListener p) throws ProgrammerException {
042        readCV(CV, p, 0);
043    }
044
045    /** {@inheritDoc} */
046    @Override
047    public void readCV(String CV, ProgListener p, int startVal) throws ProgrammerException {
048        SlotManager m = memo.getSlotManager();
049        if (m!=null) {
050            m.readCV(CV, p, startVal);
051        } else {
052            log.warn("readCV called without a SlotManager");
053        }
054    }
055
056    /** {@inheritDoc} */
057    @Override
058    public void confirmCV(String CV, int val, ProgListener p) throws ProgrammerException {
059        SlotManager m = memo.getSlotManager();
060        if (m!=null) {
061            m.confirmCV(CV, val, p);
062        } else {
063            log.warn("confirmCV called without a SlotManager");
064        }
065    }
066
067    @Override
068    @Nonnull public List<ProgrammingMode> getSupportedModes() {
069        SlotManager m = memo.getSlotManager();
070        if (m!=null) {
071            return m.getSupportedModes();
072        } else {
073            log.warn("getSupportedModes() called without a SlotManager");
074            return new ArrayList<ProgrammingMode>(); // empty
075        }
076    }
077
078    @Override
079    public void setMode(ProgrammingMode p) {
080        SlotManager m = memo.getSlotManager();
081        if (m!=null) {
082            m.setMode(p);
083        } else {
084            log.warn("setMode() called without a SlotManager");
085        }
086    }
087
088    @Override
089    public ProgrammingMode getMode() {
090        SlotManager m = memo.getSlotManager();
091        if (m!=null) {
092            return m.getMode();
093        } else {
094            log.warn("getMode() called without a SlotManager");
095            return ProgrammingMode.ADDRESSMODE; // being cautious
096        }
097    }
098
099    @Override
100    public boolean getCanRead() {
101        SlotManager m = memo.getSlotManager();
102        if (m!=null) {
103            return m.getCanRead();
104        } else {
105            log.warn("getCanRead() called without a SlotManager");
106            return true; // being cautious
107        }
108    }
109        
110    @Override
111    public boolean getCanRead(String addr) {
112        SlotManager m = memo.getSlotManager();
113        if (m!=null) {
114            return m.getCanRead(addr);
115        } else {
116            log.warn("getCanRead(String) called without a SlotManager");
117            return true; // being cautious
118        }
119    }
120        
121    @Override
122    public boolean getCanWrite() {
123        SlotManager m = memo.getSlotManager();
124        if (m!=null) {
125            return m.getCanWrite();
126        } else {
127            log.warn("getCanWrite() called without a SlotManager");
128            return true; // being cautious
129        }
130    }
131        
132    @Override
133    public boolean getCanWrite(String addr) {
134        SlotManager m = memo.getSlotManager();
135        if (m!=null) {
136            return m.getCanWrite(addr);
137        } else {
138            log.warn("getCanWrite(String) called without a SlotManager");
139            return true; // being cautious
140        }
141    }
142 
143    @Override
144    @Nonnull
145    public Programmer.WriteConfirmMode getWriteConfirmMode(String addr) {
146        SlotManager m = memo.getSlotManager();
147        if (m!=null) {
148            return m.getWriteConfirmMode(addr);
149        } else {
150            log.warn("getWriteConfirmMode() called without a SlotManager");
151            return Programmer.WriteConfirmMode.DecoderReply; // being cautious
152        }
153    }
154        
155
156    @Override
157    public void notifyProgListenerEnd(ProgListener p, int value, int status) {
158        SlotManager m = memo.getSlotManager();
159        if (m!=null) {
160            m.notifyProgListenerEnd(p, value, status);
161        } else {
162            log.warn("notifyProgListenerEnd called without a SlotManager");
163        }
164    }
165
166    @Override
167    public void addPropertyChangeListener(PropertyChangeListener p) {
168        SlotManager m = memo.getSlotManager();
169        if (m!=null) {
170            m.addPropertyChangeListener(p);
171        } else {
172            log.warn("addPropertyChangeListener called without a SlotManager");
173        }
174    }
175
176    @Override
177    public void removePropertyChangeListener(PropertyChangeListener p) {
178        SlotManager m = memo.getSlotManager();
179        if (m!=null) {
180            m.removePropertyChangeListener(p);
181        } else {
182            log.warn("removePropertyChangeListener called without a SlotManager");
183        }
184    }
185
186    @Nonnull
187    @Override
188    public String decodeErrorCode(int i) {
189        SlotManager m = memo.getSlotManager();
190        if (m!=null) {
191            return m.decodeErrorCode(i);
192        } else {
193            log.warn("decodeErrorCode called without a SlotManager");
194            return "<unknown>"; // being cautious
195        }
196    }
197
198    // initialize logging
199    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LnDeferProgrammer.class);
200
201}