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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1445 - (show annotations) (download)
Mon Oct 15 20:55:33 2007 UTC (16 years, 6 months ago) by iliev
File size: 8571 byte(s)
* preparations for release 0.7a

1 /*
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 import java.awt.BorderLayout;
26 import java.awt.Cursor;
27 import java.awt.Dimension;
28 import java.awt.Insets;
29
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34
35 import java.beans.PropertyChangeEvent;
36 import java.beans.PropertyChangeListener;
37
38 import javax.swing.BorderFactory;
39 import javax.swing.Box;
40 import javax.swing.BoxLayout;
41 import javax.swing.JPanel;
42 import javax.swing.ListSelectionModel;
43
44 import net.sf.juife.ComponentList;
45 import net.sf.juife.ComponentListModel;
46 import net.sf.juife.DefaultComponentListModel;
47
48 import net.sf.juife.event.TaskEvent;
49 import net.sf.juife.event.TaskListener;
50
51 import org.jdesktop.swingx.JXCollapsiblePane;
52
53 import org.jsampler.CC;
54 import org.jsampler.AudioDeviceModel;
55
56 import org.jsampler.event.ListEvent;
57 import org.jsampler.event.ListListener;
58
59 import org.jsampler.task.Audio;
60
61 import org.jsampler.view.std.JSNewAudioDeviceDlg;
62
63 import org.linuxsampler.lscp.AudioOutputDriver;
64
65 import static org.jsampler.view.fantasia.A4n.a4n;
66 import static org.jsampler.view.fantasia.FantasiaI18n.i18n;
67 import static org.jsampler.view.fantasia.FantasiaPrefs.*;
68
69 /**
70 *
71 * @author Grigor Iliev
72 */
73 public class AudioDevicesPane extends JPanel {
74 private final DeviceListPane devList = new DeviceListPane();
75 private final DefaultComponentListModel<AudioDevicePane> listModel =
76 new DefaultComponentListModel<AudioDevicePane>();
77
78 /** Creates a new instance of <code>AudioDevicesPane</code> */
79 public
80 AudioDevicesPane() {
81 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
82
83 devList.setModel(listModel);
84 devList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
85 devList.setAlignmentX(LEFT_ALIGNMENT);
86
87 add(devList);
88 add(new NewDevicePane());
89
90 CC.getSamplerModel().addAudioDeviceListListener(getHandler());
91
92 for(AudioDeviceModel m : CC.getSamplerModel().getAudioDevices()) {
93 addDevice(m);
94 }
95 }
96
97 private void
98 addDevice(AudioDeviceModel model) {
99 for(int i = 0; i < listModel.getSize(); i++) {
100 if(listModel.get(i).getDeviceId() == model.getDeviceId()) {
101 CC.getLogger().warning("Audio device exist: " + model.getDeviceId());
102 return;
103 }
104 }
105
106 listModel.add(new AudioDevicePane(model));
107 }
108
109 private void
110 removeDevice(AudioDeviceModel model) {
111 for(int i = 0; i < listModel.getSize(); i++) {
112 if(listModel.get(i).getDeviceId() == model.getDeviceId()) {
113 listModel.remove(i);
114 return;
115 }
116 }
117
118 CC.getLogger().warning("Audio device does not exist: " + model.getDeviceId());
119 }
120
121 class DeviceListPane extends ComponentList {
122 private Dimension maxSize = new Dimension();
123
124 public Dimension
125 getMaximumSize() {
126 maxSize.width = Short.MAX_VALUE;
127 maxSize.height = getPreferredSize().height;
128 return maxSize;
129 }
130 }
131
132
133 class NewDevicePane extends JPanel {
134 private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff18);
135 private JXCollapsiblePane createDevicePane = null;
136 private boolean createDevice = false;
137
138 NewDevicePane() {
139 setLayout(new BorderLayout());
140
141 PixmapPane p = new PixmapPane(Res.gfxDeviceBg);
142 p.setPixmapInsets(new Insets(1, 1, 1, 1));
143
144 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
145 p.add(Box.createRigidArea(new Dimension(3, 0)));
146 p.add(btnNew);
147 p.add(Box.createRigidArea(new Dimension(3, 0)));
148
149 p.add(createVSeparator());
150
151 Dimension d = new Dimension(77, 24);
152 p.setPreferredSize(d);
153 p.setMinimumSize(d);
154 p.setMaximumSize(new Dimension(Short.MAX_VALUE, 24));
155
156 btnNew.setPressedIcon(Res.gfxPowerOn18);
157
158 btnNew.addActionListener(new ActionListener() {
159 public void
160 actionPerformed(ActionEvent e) {
161 showHidePopup();
162 }
163 });
164
165 p.addMouseListener(new MouseAdapter() {
166 public void
167 mouseClicked(MouseEvent e) {
168 if(e.getButton() != e.BUTTON1) {
169 return;
170 }
171
172 showHidePopup();
173 }
174 });
175
176 p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
177
178 String s = i18n.getLabel("AudioDevicesPane.newDevice");
179 btnNew.setToolTipText(s);
180 p.setToolTipText(s);
181
182 add(p, BorderLayout.NORTH);
183 setAlignmentX(LEFT_ALIGNMENT);
184 }
185
186 private JXCollapsiblePane
187 getCreateDevicePane() {
188 if(createDevicePane != null) return createDevicePane;
189
190 createDevicePane = new JXCollapsiblePane();
191 final JSNewAudioDeviceDlg.Pane pane = new JSNewAudioDeviceDlg.Pane();
192
193 PixmapPane p1 = new PixmapPane(Res.gfxChannelOptions);
194 p1.setPixmapInsets(new Insets(1, 1, 1, 1));
195 p1.setLayout(new BorderLayout());
196 p1.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
197
198 PixmapPane p2 = new PixmapPane(Res.gfxBorder);
199 p2.setPixmapInsets(new Insets(1, 1, 1, 1));
200 p2.setLayout(new BorderLayout());
201 p2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
202 p2.add(pane);
203 p1.add(p2);
204
205 p1.setPreferredSize(new Dimension(getWidth(), 210));
206 p1.setPreferredSize(new Dimension(100, 210));
207
208 createDevicePane.setContentPane(p1);
209 createDevicePane.setAnimated(false);
210 createDevicePane.setCollapsed(true);
211 createDevicePane.setAnimated(preferences().getBoolProperty(ANIMATED));
212
213 preferences().addPropertyChangeListener(ANIMATED, new PropertyChangeListener() {
214 public void
215 propertyChange(PropertyChangeEvent e) {
216 boolean b = preferences().getBoolProperty(ANIMATED);
217 createDevicePane.setAnimated(b);
218 }
219 });
220
221 String s = JXCollapsiblePane.ANIMATION_STATE_KEY;
222 createDevicePane.addPropertyChangeListener(s, new PropertyChangeListener() {
223 public void
224 propertyChange(PropertyChangeEvent e) {
225 if(e.getNewValue() == "collapsed") {
226 if(createDevice) {
227 createAudioDevice0(pane);
228 createDevice = false;
229 }
230 }
231 }
232 });
233
234 add(createDevicePane);
235
236 pane.btnCancel.addActionListener(new ActionListener() {
237 public void
238 actionPerformed(ActionEvent e) {
239 createDevicePane.setCollapsed(true);
240 }
241 });
242
243 pane.btnCreate.addActionListener(new ActionListener() {
244 public void
245 actionPerformed(ActionEvent e) {
246 createAudioDevice(pane);
247 }
248 });
249
250 return createDevicePane;
251 }
252
253 private JPanel
254 createVSeparator() {
255 PixmapPane p = new PixmapPane(Res.gfxVLine);
256 p.setOpaque(false);
257 p.setPreferredSize(new Dimension(2, 24));
258 p.setMinimumSize(p.getPreferredSize());
259 p.setMaximumSize(p.getPreferredSize());
260 return p;
261 }
262
263 private void
264 showHidePopup() {
265 getCreateDevicePane().setCollapsed(!getCreateDevicePane().isCollapsed());
266 }
267
268 private void
269 createAudioDevice(final JSNewAudioDeviceDlg.Pane pane) {
270 if(!createDevicePane.isAnimated()) {
271 createAudioDevice0(pane);
272 return;
273 }
274
275 createDevice = true;
276 createDevicePane.setCollapsed(true);
277 }
278
279 private void
280 createAudioDevice0(final JSNewAudioDeviceDlg.Pane pane) {
281 pane.btnCreate.setEnabled(false);
282 final AudioOutputDriver driver = pane.getSelectedDriver();
283 final Audio.CreateDevice cmd =
284 new Audio.CreateDevice(driver.getName(), driver.getParameters());
285
286 cmd.addTaskListener(new TaskListener() {
287 public void
288 taskPerformed(TaskEvent e) {
289 pane.btnCreate.setEnabled(true);
290 getCreateDevicePane().setCollapsed(true);
291 }
292 });
293
294 CC.getTaskQueue().add(cmd);
295 }
296 }
297
298 private final EventHandler eventHandler = new EventHandler();
299
300 private EventHandler
301 getHandler() { return eventHandler; }
302
303 private class EventHandler implements ListListener<AudioDeviceModel> {
304 public void
305 entryAdded(ListEvent<AudioDeviceModel> e) {
306 addDevice(e.getEntry());
307 }
308
309 public void
310 entryRemoved(ListEvent<AudioDeviceModel> e) {
311 removeDevice(e.getEntry());
312 }
313 }
314 }

  ViewVC Help
Powered by ViewVC