001package jmri.jmrit.audio.swing;
002
003import java.awt.FlowLayout;
004import java.awt.event.ActionEvent;
005import javax.swing.BorderFactory;
006import javax.swing.BoxLayout;
007import javax.swing.JButton;
008import javax.swing.JLabel;
009import javax.swing.JOptionPane;
010import javax.swing.JPanel;
011import javax.swing.JSpinner;
012import javax.swing.JTextField;
013import javax.swing.SpinnerNumberModel;
014import jmri.Audio;
015import jmri.AudioException;
016import jmri.InstanceManager;
017import jmri.implementation.AbstractAudio;
018import jmri.jmrit.audio.AudioListener;
019import jmri.jmrit.beantable.AudioTableAction.AudioTableDataModel;
020
021/**
022 * Define a GUI to edit AudioListener objects
023 *
024 * <hr>
025 * This file is part of JMRI.
026 * <p>
027 * JMRI is free software; you can redistribute it and/or modify it under the
028 * terms of version 2 of the GNU General Public License as published by the Free
029 * Software Foundation. See the "COPYING" file for a copy of this license.
030 * <p>
031 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
032 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
033 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
034 *
035 * @author Matthew Harris copyright (c) 2009
036 */
037public class AudioListenerFrame extends AbstractAudioFrame {
038
039    JPanelVector3f position = new JPanelVector3f(Bundle.getMessage("LabelPosition"),
040            Bundle.getMessage("UnitUnits"));
041    JPanelVector3f velocity = new JPanelVector3f(Bundle.getMessage("LabelVelocity"),
042            Bundle.getMessage("UnitU/S"));
043    JLabel oriAtLabel = new JLabel(Bundle.getMessage("LabelOrientationAt"));
044    JPanelVector3f oriAt = new JPanelVector3f("", Bundle.getMessage("UnitUnits"));
045    JLabel oriUpLabel = new JLabel(Bundle.getMessage("LabelOrientationUp"));
046    JPanelVector3f oriUp = new JPanelVector3f("", Bundle.getMessage("UnitUnits"));
047    JPanelSliderf gain = new JPanelSliderf(Bundle.getMessage("LabelGain"), 0.0f, 1.0f, 5, 4);
048    JSpinner metersPerUnit = new JSpinner();
049    JLabel metersPerUnitLabel = new JLabel(Bundle.getMessage("UnitM/U"));
050
051    private final static String PREFIX = "IAL";
052
053    @SuppressWarnings("OverridableMethodCallInConstructor")
054    public AudioListenerFrame(String title, AudioTableDataModel model) {
055        super(title, model);
056        layoutFrame();
057    }
058
059    @Override
060    @SuppressWarnings("UnnecessaryBoxing")
061    public void layoutFrame() {
062        super.layoutFrame();
063        JPanel p;
064
065        main.add(position);
066        main.add(velocity);
067
068        p = new JPanel();
069        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
070        p.setBorder(BorderFactory.createCompoundBorder(
071                BorderFactory.createTitledBorder(Bundle.getMessage("LabelOrientation")),
072                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
073        p.add(oriAtLabel);
074        p.add(oriAt);
075        p.add(oriUpLabel);
076        p.add(oriUp);
077        main.add(p);
078
079        main.add(gain);
080
081        p = new JPanel();
082        p.setLayout(new FlowLayout());
083        p.setBorder(BorderFactory.createCompoundBorder(
084                BorderFactory.createTitledBorder(Bundle.getMessage("LabelMetersPerUnit")),
085                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
086        metersPerUnit.setPreferredSize(new JTextField(8).getPreferredSize());
087        metersPerUnit.setModel(
088                new SpinnerNumberModel(Float.valueOf(0f), Float.valueOf(0f), Float.valueOf(65536f), Float.valueOf(0.0001f)));
089        metersPerUnit.setEditor(new JSpinner.NumberEditor(metersPerUnit, "0.0000"));
090        p.add(metersPerUnit);
091        p.add(metersPerUnitLabel);
092        main.add(p);
093
094        p = new JPanel();
095        JButton apply;
096        p.add(apply = new JButton(Bundle.getMessage("ButtonApply")));
097        apply.addActionListener((ActionEvent e) -> {
098            applyPressed(e);
099        });
100        JButton ok;
101        p.add(ok = new JButton(Bundle.getMessage("ButtonOK")));
102        ok.addActionListener((ActionEvent e) -> {
103            applyPressed(e);
104            frame.dispose();
105        });
106        JButton cancel;
107        p.add(cancel = new JButton(Bundle.getMessage("ButtonCancel")));
108        cancel.addActionListener((ActionEvent e) -> {
109            frame.dispose();
110        });
111        frame.getContentPane().add(p);
112    }
113
114    @Override
115    public void resetFrame() {
116        // Not required
117    }
118
119    /**
120     * Populate the Edit Listener frame with current values.
121     */
122    @Override
123    public void populateFrame(Audio a) {
124        if (!(a instanceof AudioListener)) {
125            throw new IllegalArgumentException(a.getSystemName() + " is not an AudioListener object");
126        }
127        super.populateFrame(a);
128        AudioListener l = (AudioListener) a;
129        position.setValue(l.getPosition());
130        velocity.setValue(l.getVelocity());
131        oriAt.setValue(l.getOrientation(Audio.AT));
132        oriUp.setValue(l.getOrientation(Audio.UP));
133        gain.setValue(l.getGain());
134        metersPerUnit.setValue(l.getMetersPerUnit());
135    }
136
137    private void applyPressed(ActionEvent e) {
138        String sName = sysName.getText();
139        if (entryError(sName, PREFIX, "$")) { // no index for AudioListener
140            return;
141        }
142        String user = userName.getText();
143        if (user.equals("")) {
144            user = null;
145        }
146        AudioListener l;
147        try {
148            l = (AudioListener) InstanceManager.getDefault(jmri.AudioManager.class).provideAudio(sName);
149            l.setUserName(user);
150            l.setPosition(position.getValue());
151            l.setVelocity(velocity.getValue());
152            l.setOrientation(oriAt.getValue(), oriUp.getValue());
153            l.setGain(gain.getValue());
154            l.setMetersPerUnit(AbstractAudio.roundDecimal((Float) metersPerUnit.getValue(), 4d));
155
156            // Notify changes
157            model.fireTableDataChanged();
158        } catch (AudioException | IllegalArgumentException ex) {
159            JOptionPane.showMessageDialog(null, ex.getMessage(), Bundle.getMessage("AudioCreateErrorTitle"), JOptionPane.ERROR_MESSAGE);
160        }
161    }
162
163    //private static final Logger log = LoggerFactory.getLogger(AudioListenerFrame.class);
164
165}