001package jmri.jmrix.loconet.sdfeditor;
002
003import java.awt.event.ActionListener;
004import javax.swing.BoxLayout;
005import javax.swing.JCheckBox;
006import javax.swing.JComboBox;
007import javax.swing.JLabel;
008import javax.swing.JPanel;
009import javax.swing.JSpinner;
010import javax.swing.SpinnerNumberModel;
011import javax.swing.event.ChangeEvent;
012import javax.swing.event.ChangeListener;
013import jmri.jmrix.loconet.sdf.Play;
014import jmri.jmrix.loconet.sdf.SdfConstants;
015import jmri.jmrix.loconet.sdf.SdfMacro;
016
017/**
018 * Editor panel for the PLAY macro from the Digitrax sound definition language
019 *
020 * @author Bob Jacobsen Copyright (C) 2007
021 */
022class PlayEditor extends SdfMacroEditor {
023
024    public PlayEditor(SdfMacro inst) {
025        super(inst);
026        // remove warning message from SdfMacroEditor
027        this.removeAll();
028
029        // find & set selected values
030        update();
031
032        // set up GUI
033        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
034
035        // handle numbers
036        JPanel p = new JPanel();
037        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
038
039        p.add(new JLabel("Play handle number: "));
040        p.add(handle);
041        add(p);
042
043        // loop control
044        p = new JPanel();
045        p.add(new JLabel("Loop control: "));
046        p.add(loop);
047        add(p);
048
049        // loop control
050        p = new JPanel();
051        p.add(wavbrk1);
052        p.add(wavbrk2);
053        add(p);
054
055        // change the instruction when the value is changed
056        ActionListener l = new ActionListener() {
057            @Override
058            public void actionPerformed(java.awt.event.ActionEvent e) {
059                guiChanged();
060            }
061        };
062        loop.addActionListener(l);
063        wavbrk1.addActionListener(l);
064        wavbrk2.addActionListener(l);
065        ChangeListener c = new ChangeListener() {
066            @Override
067            public void stateChanged(ChangeEvent e) {
068                guiChanged();
069            }
070        };
071        handle.addChangeListener(c);
072    }
073
074    SpinnerNumberModel handleModel = new SpinnerNumberModel(0, 0, 63, 1);
075    JSpinner handle = new JSpinner(handleModel);
076    JComboBox<String> loop = new JComboBox<String>(SdfConstants.loopNames);
077    JCheckBox wavbrk1 = new JCheckBox("Invert Loop Reason");
078    JCheckBox wavbrk2 = new JCheckBox("Global Loop Reason");
079
080    /**
081     * update instruction if GUI changes
082     */
083    void guiChanged() {
084        Play instruction = (Play) PlayEditor.this.inst;
085
086        instruction.setHandle(handleModel.getNumber().intValue());
087
088        instruction.setBrk((String) loop.getSelectedItem());
089
090        int flag = 0;
091        if (wavbrk1.isSelected()) {
092            flag |= 0x01;
093        }
094        if (wavbrk2.isSelected()) {
095            flag |= 0x02;
096        }
097        instruction.setWaveBrkFlags(flag);
098
099        // tell the world
100        updated();
101    }
102
103    @Override
104    public void update() {
105        // find & set index of selected trigger
106        Play instruction = (Play) inst;
107        int handleVal = Integer.parseInt(instruction.handleVal());
108        handleModel.setValue(Integer.valueOf(handleVal));
109
110        // loop flag
111        loop.setSelectedItem(instruction.brkVal());
112
113        // wavbreak flags
114        int flags = instruction.getWaveBrkFlags();
115        wavbrk1.setSelected((flags & 0x01) != 0);
116        wavbrk2.setSelected((flags & 0x02) != 0);
117    }
118}