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