001package jmri.jmrix.loconet.sdfeditor;
002
003import javax.swing.JLabel;
004import javax.swing.JPanel;
005import javax.swing.tree.DefaultMutableTreeNode;
006import jmri.jmrix.loconet.sdf.BranchTo;
007import jmri.jmrix.loconet.sdf.ChannelStart;
008import jmri.jmrix.loconet.sdf.DelaySound;
009import jmri.jmrix.loconet.sdf.EndSound;
010import jmri.jmrix.loconet.sdf.FourByteMacro;
011import jmri.jmrix.loconet.sdf.GenerateTrigger;
012import jmri.jmrix.loconet.sdf.InitiateSound;
013import jmri.jmrix.loconet.sdf.LoadModifier;
014import jmri.jmrix.loconet.sdf.MaskCompare;
015import jmri.jmrix.loconet.sdf.Play;
016import jmri.jmrix.loconet.sdf.SdfMacro;
017import jmri.jmrix.loconet.sdf.SdlVersion;
018import jmri.jmrix.loconet.sdf.SkemeStart;
019import jmri.jmrix.loconet.sdf.SkipOnTrigger;
020import jmri.jmrix.loconet.sdf.TwoByteMacro;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024/**
025 * Common base for all the SDF macro editors.
026 *
027 * @author Bob Jacobsen Copyright (C) 2007
028 */
029public abstract class SdfMacroEditor extends JPanel {
030
031    public SdfMacroEditor(SdfMacro inst) {
032        this.inst = inst;
033
034        // add a default behavior
035        add(new JLabel("This instruction has no options to set."));
036    }
037
038    SdfMacro inst;
039
040    /**
041     * Update editor when it's reshown
042     */
043    public void update() {
044    }
045
046    SdfMacro getMacro() {
047        return inst;
048    }
049
050    /**
051     * Notify that something has changed
052     */
053    public void updated() {
054        if (treenode != null) {
055            treenode.setUserObject(this);
056        }
057        if (editor != null) {
058            editor.updateSummary();
059        }
060    }
061
062    @Override
063    public String toString() {
064        return inst.toString();
065    }
066
067    public String oneInstructionString() {
068        return inst.oneInstructionString();
069    }
070
071    public String allInstructionString(String indent) {
072        return inst.allInstructionString(indent);
073    }
074
075    DefaultMutableTreeNode treenode = null;
076    EditorPane editor = null;
077
078    public void setNotify(DefaultMutableTreeNode node, EditorPane pane) {
079        treenode = node;
080        editor = pane;
081    }
082
083    /**
084     * Return an editor object for a SdfMacro type.
085     * @param inst macro instance.
086     * @return editor according to macro type.
087     */
088    static public SdfMacroEditor attachEditor(SdfMacro inst) {
089
090        // full 1st byte decoder
091        if (inst instanceof ChannelStart) {
092            return new ChannelStartEditor(inst);
093        } else if (inst instanceof SdlVersion) {
094            return new SdlVersionEditor(inst);
095        } else if (inst instanceof SkemeStart) {
096            return new SkemeStartEditor(inst);
097        } else if (inst instanceof GenerateTrigger) {
098            return new GenerateTriggerEditor(inst);
099        } else if (inst instanceof EndSound) {
100            return new EndSoundEditor(inst);
101        } else // 7 bit decode
102        if (inst instanceof DelaySound) {
103            return new DelaySoundEditor(inst);
104        } else // 6 bit decode
105        if (inst instanceof SkipOnTrigger) {
106            return new SkipOnTriggerEditor(inst);
107        } else // 5 bit decode
108        if (inst instanceof InitiateSound) {
109            return new InitiateSoundEditor(inst);
110        } else if (inst instanceof MaskCompare) {
111            return new MaskCompareEditor(inst);
112        } else // 4 bit decode
113        if (inst instanceof LoadModifier) {
114            return new LoadModifierEditor(inst);
115        } else if (inst instanceof BranchTo) {
116            return new BranchToEditor(inst);
117        } else // 2 bit decode
118        if (inst instanceof Play) {
119            return new PlayEditor(inst);
120        } else // generics
121        if (inst instanceof FourByteMacro) {
122            return new FourByteMacroEditor(inst);
123        } else if (inst instanceof TwoByteMacro) {
124            return new TwoByteMacroEditor(inst);
125        }
126
127        log.error("PANIC");
128        return null;
129    }
130
131    private final static Logger log = LoggerFactory.getLogger(SdfMacroEditor.class);
132
133}