/[svn]/jsampler/trunk/src/org/jsampler/view/fantasia/AudioDevicesPane.java
ViewVC logotype

Annotation of /jsampler/trunk/src/org/jsampler/view/fantasia/AudioDevicesPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1467 - (hide annotations) (download)
Sat Nov 3 13:14:31 2007 UTC (16 years, 5 months ago) by iliev
File size: 9244 byte(s)
* bugfix: The parameter changes were
  discarded when creating new audio/MIDI device
* bugfix: The orchestras changes were not saved for the next session
  when orchestras.xml does not exist in the JSampler's home directory
* bugfix: In some cases the sampler configuration was not exported
  properly to LSCP script
* Some minor bugfixes and enhancements

1 iliev 1286 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4     * Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com>
5     *
6     * This file is part of JSampler.
7     *
8     * JSampler is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License version 2
10     * as published by the Free Software Foundation.
11     *
12     * JSampler is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with JSampler; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20     * MA 02111-1307 USA
21     */
22    
23     package org.jsampler.view.fantasia;
24    
25 iliev 1445 import java.awt.BorderLayout;
26 iliev 1467 import java.awt.Container;
27 iliev 1286 import java.awt.Cursor;
28     import java.awt.Dimension;
29     import java.awt.Insets;
30 iliev 1467 import java.awt.Rectangle;
31 iliev 1286
32 iliev 1445 import java.awt.event.ActionEvent;
33     import java.awt.event.ActionListener;
34 iliev 1286 import java.awt.event.MouseAdapter;
35     import java.awt.event.MouseEvent;
36    
37 iliev 1445 import java.beans.PropertyChangeEvent;
38     import java.beans.PropertyChangeListener;
39    
40     import javax.swing.BorderFactory;
41 iliev 1286 import javax.swing.Box;
42     import javax.swing.BoxLayout;
43     import javax.swing.JPanel;
44 iliev 1467 import javax.swing.JScrollPane;
45 iliev 1286 import javax.swing.ListSelectionModel;
46    
47     import net.sf.juife.ComponentList;
48     import net.sf.juife.ComponentListModel;
49     import net.sf.juife.DefaultComponentListModel;
50    
51 iliev 1445 import net.sf.juife.event.TaskEvent;
52     import net.sf.juife.event.TaskListener;
53    
54     import org.jdesktop.swingx.JXCollapsiblePane;
55    
56 iliev 1286 import org.jsampler.CC;
57     import org.jsampler.AudioDeviceModel;
58    
59     import org.jsampler.event.ListEvent;
60     import org.jsampler.event.ListListener;
61    
62 iliev 1445 import org.jsampler.task.Audio;
63    
64     import org.jsampler.view.std.JSNewAudioDeviceDlg;
65    
66     import org.linuxsampler.lscp.AudioOutputDriver;
67    
68 iliev 1286 import static org.jsampler.view.fantasia.A4n.a4n;
69     import static org.jsampler.view.fantasia.FantasiaI18n.i18n;
70 iliev 1445 import static org.jsampler.view.fantasia.FantasiaPrefs.*;
71 iliev 1286
72     /**
73     *
74     * @author Grigor Iliev
75     */
76     public class AudioDevicesPane extends JPanel {
77     private final DeviceListPane devList = new DeviceListPane();
78     private final DefaultComponentListModel<AudioDevicePane> listModel =
79     new DefaultComponentListModel<AudioDevicePane>();
80    
81     /** Creates a new instance of <code>AudioDevicesPane</code> */
82     public
83     AudioDevicesPane() {
84     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
85    
86     devList.setModel(listModel);
87     devList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
88     devList.setAlignmentX(LEFT_ALIGNMENT);
89    
90     add(devList);
91     add(new NewDevicePane());
92    
93     CC.getSamplerModel().addAudioDeviceListListener(getHandler());
94    
95     for(AudioDeviceModel m : CC.getSamplerModel().getAudioDevices()) {
96     addDevice(m);
97     }
98     }
99    
100     private void
101     addDevice(AudioDeviceModel model) {
102     for(int i = 0; i < listModel.getSize(); i++) {
103     if(listModel.get(i).getDeviceId() == model.getDeviceId()) {
104     CC.getLogger().warning("Audio device exist: " + model.getDeviceId());
105     return;
106     }
107     }
108    
109     listModel.add(new AudioDevicePane(model));
110     }
111    
112     private void
113     removeDevice(AudioDeviceModel model) {
114     for(int i = 0; i < listModel.getSize(); i++) {
115     if(listModel.get(i).getDeviceId() == model.getDeviceId()) {
116     listModel.remove(i);
117     return;
118     }
119     }
120    
121     CC.getLogger().warning("Audio device does not exist: " + model.getDeviceId());
122     }
123    
124     class DeviceListPane extends ComponentList {
125     private Dimension maxSize = new Dimension();
126    
127     public Dimension
128     getMaximumSize() {
129     maxSize.width = Short.MAX_VALUE;
130     maxSize.height = getPreferredSize().height;
131     return maxSize;
132     }
133     }
134    
135    
136 iliev 1445 class NewDevicePane extends JPanel {
137     private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff18);
138     private JXCollapsiblePane createDevicePane = null;
139     private boolean createDevice = false;
140 iliev 1286
141     NewDevicePane() {
142 iliev 1445 setLayout(new BorderLayout());
143 iliev 1286
144 iliev 1445 PixmapPane p = new PixmapPane(Res.gfxDeviceBg);
145     p.setPixmapInsets(new Insets(1, 1, 1, 1));
146 iliev 1286
147 iliev 1445 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
148     p.add(Box.createRigidArea(new Dimension(3, 0)));
149     p.add(btnNew);
150     p.add(Box.createRigidArea(new Dimension(3, 0)));
151 iliev 1286
152 iliev 1445 p.add(createVSeparator());
153    
154 iliev 1286 Dimension d = new Dimension(77, 24);
155 iliev 1445 p.setPreferredSize(d);
156     p.setMinimumSize(d);
157     p.setMaximumSize(new Dimension(Short.MAX_VALUE, 24));
158 iliev 1286
159     btnNew.setPressedIcon(Res.gfxPowerOn18);
160    
161 iliev 1445 btnNew.addActionListener(new ActionListener() {
162 iliev 1286 public void
163 iliev 1445 actionPerformed(ActionEvent e) {
164     showHidePopup();
165     }
166     });
167    
168     p.addMouseListener(new MouseAdapter() {
169     public void
170 iliev 1286 mouseClicked(MouseEvent e) {
171     if(e.getButton() != e.BUTTON1) {
172     return;
173     }
174    
175 iliev 1445 showHidePopup();
176 iliev 1286 }
177     });
178    
179 iliev 1445 p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
180 iliev 1286
181     String s = i18n.getLabel("AudioDevicesPane.newDevice");
182     btnNew.setToolTipText(s);
183 iliev 1445 p.setToolTipText(s);
184    
185     add(p, BorderLayout.NORTH);
186     setAlignmentX(LEFT_ALIGNMENT);
187 iliev 1286 }
188    
189 iliev 1445 private JXCollapsiblePane
190     getCreateDevicePane() {
191     if(createDevicePane != null) return createDevicePane;
192    
193     createDevicePane = new JXCollapsiblePane();
194     final JSNewAudioDeviceDlg.Pane pane = new JSNewAudioDeviceDlg.Pane();
195    
196     PixmapPane p1 = new PixmapPane(Res.gfxChannelOptions);
197     p1.setPixmapInsets(new Insets(1, 1, 1, 1));
198     p1.setLayout(new BorderLayout());
199     p1.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
200    
201     PixmapPane p2 = new PixmapPane(Res.gfxBorder);
202     p2.setPixmapInsets(new Insets(1, 1, 1, 1));
203     p2.setLayout(new BorderLayout());
204     p2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
205     p2.add(pane);
206     p1.add(p2);
207    
208     p1.setPreferredSize(new Dimension(getWidth(), 210));
209     p1.setPreferredSize(new Dimension(100, 210));
210    
211     createDevicePane.setContentPane(p1);
212     createDevicePane.setAnimated(false);
213     createDevicePane.setCollapsed(true);
214     createDevicePane.setAnimated(preferences().getBoolProperty(ANIMATED));
215    
216     preferences().addPropertyChangeListener(ANIMATED, new PropertyChangeListener() {
217     public void
218     propertyChange(PropertyChangeEvent e) {
219     boolean b = preferences().getBoolProperty(ANIMATED);
220     createDevicePane.setAnimated(b);
221     }
222     });
223    
224     String s = JXCollapsiblePane.ANIMATION_STATE_KEY;
225     createDevicePane.addPropertyChangeListener(s, new PropertyChangeListener() {
226     public void
227     propertyChange(PropertyChangeEvent e) {
228 iliev 1467 Object o = e.getNewValue();
229     if(o == "collapsed") {
230 iliev 1445 if(createDevice) {
231     createAudioDevice0(pane);
232     createDevice = false;
233     }
234 iliev 1467 } else if(o == "expanded" || o == "expanding/collapsing") {
235     ensureCreateDevicePaneIsVisible();
236 iliev 1445 }
237     }
238     });
239    
240     add(createDevicePane);
241    
242     pane.btnCancel.addActionListener(new ActionListener() {
243     public void
244     actionPerformed(ActionEvent e) {
245     createDevicePane.setCollapsed(true);
246     }
247     });
248    
249     pane.btnCreate.addActionListener(new ActionListener() {
250     public void
251     actionPerformed(ActionEvent e) {
252     createAudioDevice(pane);
253     }
254     });
255    
256     return createDevicePane;
257     }
258    
259 iliev 1286 private JPanel
260     createVSeparator() {
261     PixmapPane p = new PixmapPane(Res.gfxVLine);
262     p.setOpaque(false);
263     p.setPreferredSize(new Dimension(2, 24));
264     p.setMinimumSize(p.getPreferredSize());
265     p.setMaximumSize(p.getPreferredSize());
266     return p;
267     }
268 iliev 1445
269     private void
270     showHidePopup() {
271     getCreateDevicePane().setCollapsed(!getCreateDevicePane().isCollapsed());
272     }
273    
274     private void
275     createAudioDevice(final JSNewAudioDeviceDlg.Pane pane) {
276     if(!createDevicePane.isAnimated()) {
277     createAudioDevice0(pane);
278     return;
279     }
280    
281     createDevice = true;
282     createDevicePane.setCollapsed(true);
283     }
284    
285     private void
286     createAudioDevice0(final JSNewAudioDeviceDlg.Pane pane) {
287     pane.btnCreate.setEnabled(false);
288     final AudioOutputDriver driver = pane.getSelectedDriver();
289     final Audio.CreateDevice cmd =
290 iliev 1467 new Audio.CreateDevice(driver.getName(), pane.getParameters());
291 iliev 1445
292     cmd.addTaskListener(new TaskListener() {
293     public void
294     taskPerformed(TaskEvent e) {
295     pane.btnCreate.setEnabled(true);
296     getCreateDevicePane().setCollapsed(true);
297     }
298     });
299    
300     CC.getTaskQueue().add(cmd);
301     }
302 iliev 1467
303     private void
304     ensureCreateDevicePaneIsVisible() {
305     Container p = createDevicePane.getParent();
306     JScrollPane sp = null;
307     int i = createDevicePane.getLocation().y + createDevicePane.getHeight();
308     while(p != null) {
309     if(p instanceof JScrollPane) {
310     sp = (JScrollPane)p;
311     break;
312     }
313     i += p.getLocation().y;
314     p = p.getParent();
315     }
316    
317     if(sp == null) return;
318     sp.getViewport().scrollRectToVisible(new Rectangle(0, i, 5, 5));
319     }
320 iliev 1286 }
321    
322     private final EventHandler eventHandler = new EventHandler();
323    
324     private EventHandler
325     getHandler() { return eventHandler; }
326    
327     private class EventHandler implements ListListener<AudioDeviceModel> {
328     public void
329     entryAdded(ListEvent<AudioDeviceModel> e) {
330     addDevice(e.getEntry());
331     }
332    
333     public void
334     entryRemoved(ListEvent<AudioDeviceModel> e) {
335     removeDevice(e.getEntry());
336     }
337     }
338     }

  ViewVC Help
Powered by ViewVC