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