001package jmri.jmrix.loconet.sdfeditor;
002
003import java.awt.FlowLayout;
004import java.awt.event.ActionListener;
005import javax.swing.BoxLayout;
006import javax.swing.JCheckBox;
007import javax.swing.JComboBox;
008import javax.swing.JLabel;
009import javax.swing.JPanel;
010import jmri.jmrix.loconet.sdf.InitiateSound;
011import jmri.jmrix.loconet.sdf.SdfConstants;
012import jmri.jmrix.loconet.sdf.SdfMacro;
013
014/**
015 * Editor panel for the INITIATE_SOUND macro.
016 *
017 * @author Bob Jacobsen Copyright (C) 2007
018 */
019class InitiateSoundEditor extends SdfMacroEditor {
020
021    public InitiateSoundEditor(SdfMacro inst) {
022        super(inst);
023
024        // remove warning message from SdfMacroEditor
025        this.removeAll();
026
027        // and set up our own
028        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
029
030        JPanel p = new JPanel();
031        p.setLayout(new FlowLayout());
032
033        p.add(new JLabel("Start sequence when: "));
034        box = new JComboBox<String>(SdfConstants.editorTriggerNames);
035
036        // find & set index of selected value
037        update();
038
039        p.add(box);
040        add(p);
041
042        // check boxes
043        add(zap);
044        add(run);
045        add(noprempt);
046        add(nottrig);
047
048        // change the instruction when the value is changed
049        ActionListener l = new ActionListener() {
050            @Override
051            public void actionPerformed(java.awt.event.ActionEvent e) {
052                // have to convert back from string to 
053                // trigger value
054                String trigger = (String) box.getSelectedItem();
055                int value = jmri.util.StringUtil.getStateFromName(trigger, SdfConstants.triggerCodes, SdfConstants.editorTriggerNames);
056                ((InitiateSound) InitiateSoundEditor.this.inst).setTrigger(value);
057                // buttons
058                int prempt = 0;
059                if (zap.isSelected()) {
060                    prempt = prempt | SdfConstants.ZAP;
061                }
062                if (run.isSelected()) {
063                    prempt = prempt | SdfConstants.RUN_WHILE_TRIG;
064                }
065                if (noprempt.isSelected()) {
066                    prempt = prempt | SdfConstants.NO_PREEMPT_TRIG;
067                }
068                if (nottrig.isSelected()) {
069                    prempt = prempt | SdfConstants.NOT_TRIG;
070                }
071                ((InitiateSound) InitiateSoundEditor.this.inst).setPrempt(prempt);
072                // tell the world
073                updated();
074            }
075        };
076
077        box.addActionListener(l);
078        zap.addActionListener(l);
079        run.addActionListener(l);
080        noprempt.addActionListener(l);
081        nottrig.addActionListener(l);
082    }
083
084    JComboBox<String> box;
085
086    @Override
087    public void update() {
088        // find & set index of selected trigger
089        InitiateSound instruction = (InitiateSound) inst;
090        int trig = instruction.getTrigger();
091        for (int i = 0; i < SdfConstants.triggerCodes.length; i++) {
092            if (SdfConstants.triggerCodes[i] == trig) {
093                box.setSelectedIndex(i);
094                break;
095            }
096        }
097        // buttons
098        int prempt = instruction.getPrempt();
099        zap.setSelected((prempt & SdfConstants.ZAP) != 0);
100        run.setSelected((prempt & SdfConstants.RUN_WHILE_TRIG) != 0);
101        noprempt.setSelected((prempt & SdfConstants.NO_PREEMPT_TRIG) != 0);
102        nottrig.setSelected((prempt & SdfConstants.NOT_TRIG) != 0);
103
104    }
105
106    JCheckBox zap = new JCheckBox("Zap");
107    JCheckBox run = new JCheckBox("Run while triggered");
108    JCheckBox noprempt = new JCheckBox("No preemptive trigger");
109    JCheckBox nottrig = new JCheckBox("Not triggered");
110
111}