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