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.fantasia.basic.PixmapButton; |
64 |
import org.jsampler.view.fantasia.basic.PixmapPane; |
65 |
import org.jsampler.view.std.JSNewAudioDeviceDlg; |
66 |
|
67 |
import org.linuxsampler.lscp.AudioOutputDriver; |
68 |
|
69 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
70 |
import static org.jsampler.view.fantasia.FantasiaPrefs.*; |
71 |
|
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 |
@Override |
128 |
public Dimension |
129 |
getMaximumSize() { |
130 |
maxSize.width = Short.MAX_VALUE; |
131 |
maxSize.height = getPreferredSize().height; |
132 |
return maxSize; |
133 |
} |
134 |
} |
135 |
|
136 |
|
137 |
class NewDevicePane extends JPanel { |
138 |
private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff18); |
139 |
private JXCollapsiblePane createDevicePane = null; |
140 |
private boolean createDevice = false; |
141 |
|
142 |
NewDevicePane() { |
143 |
setLayout(new BorderLayout()); |
144 |
|
145 |
PixmapPane p = new PixmapPane(Res.gfxDeviceBg); |
146 |
p.setPixmapInsets(new Insets(1, 1, 1, 1)); |
147 |
|
148 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
149 |
p.add(Box.createRigidArea(new Dimension(3, 0))); |
150 |
p.add(btnNew); |
151 |
p.add(Box.createRigidArea(new Dimension(3, 0))); |
152 |
|
153 |
p.add(createVSeparator()); |
154 |
|
155 |
Dimension d = new Dimension(77, 24); |
156 |
p.setPreferredSize(d); |
157 |
p.setMinimumSize(d); |
158 |
p.setMaximumSize(new Dimension(Short.MAX_VALUE, 24)); |
159 |
|
160 |
btnNew.setPressedIcon(Res.gfxPowerOn18); |
161 |
|
162 |
btnNew.addActionListener(new ActionListener() { |
163 |
public void |
164 |
actionPerformed(ActionEvent e) { |
165 |
showHidePopup(); |
166 |
} |
167 |
}); |
168 |
|
169 |
p.addMouseListener(new MouseAdapter() { |
170 |
public void |
171 |
mouseClicked(MouseEvent e) { |
172 |
if(e.getButton() != e.BUTTON1) { |
173 |
return; |
174 |
} |
175 |
|
176 |
showHidePopup(); |
177 |
} |
178 |
}); |
179 |
|
180 |
p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
181 |
|
182 |
String s = i18n.getLabel("AudioDevicesPane.newDevice"); |
183 |
btnNew.setToolTipText(s); |
184 |
p.setToolTipText(s); |
185 |
|
186 |
add(p, BorderLayout.NORTH); |
187 |
setAlignmentX(LEFT_ALIGNMENT); |
188 |
} |
189 |
|
190 |
private JXCollapsiblePane |
191 |
getCreateDevicePane() { |
192 |
if(createDevicePane != null) return createDevicePane; |
193 |
|
194 |
createDevicePane = new JXCollapsiblePane(); |
195 |
final JSNewAudioDeviceDlg.Pane pane = new JSNewAudioDeviceDlg.Pane(); |
196 |
|
197 |
PixmapPane p1 = new PixmapPane(Res.gfxChannelOptions); |
198 |
p1.setPixmapInsets(new Insets(1, 1, 1, 1)); |
199 |
p1.setLayout(new BorderLayout()); |
200 |
p1.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); |
201 |
|
202 |
PixmapPane p2 = new PixmapPane(Res.gfxBorder); |
203 |
p2.setPixmapInsets(new Insets(1, 1, 1, 1)); |
204 |
p2.setLayout(new BorderLayout()); |
205 |
p2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
206 |
p2.add(pane); |
207 |
p1.add(p2); |
208 |
|
209 |
p1.setPreferredSize(new Dimension(getWidth(), 210)); |
210 |
p1.setPreferredSize(new Dimension(100, 210)); |
211 |
|
212 |
createDevicePane.setContentPane(p1); |
213 |
createDevicePane.setAnimated(false); |
214 |
createDevicePane.setCollapsed(true); |
215 |
createDevicePane.setAnimated(preferences().getBoolProperty(ANIMATED)); |
216 |
|
217 |
preferences().addPropertyChangeListener(ANIMATED, new PropertyChangeListener() { |
218 |
public void |
219 |
propertyChange(PropertyChangeEvent e) { |
220 |
boolean b = preferences().getBoolProperty(ANIMATED); |
221 |
createDevicePane.setAnimated(b); |
222 |
} |
223 |
}); |
224 |
|
225 |
String s = JXCollapsiblePane.ANIMATION_STATE_KEY; |
226 |
createDevicePane.addPropertyChangeListener(s, new PropertyChangeListener() { |
227 |
public void |
228 |
propertyChange(PropertyChangeEvent e) { |
229 |
Object o = e.getNewValue(); |
230 |
if(o == "collapsed") { |
231 |
if(createDevice) { |
232 |
createAudioDevice0(pane); |
233 |
createDevice = false; |
234 |
} |
235 |
} else if(o == "expanded" || o == "expanding/collapsing") { |
236 |
ensureCreateDevicePaneIsVisible(); |
237 |
} |
238 |
} |
239 |
}); |
240 |
|
241 |
add(createDevicePane); |
242 |
|
243 |
pane.btnCancel.addActionListener(new ActionListener() { |
244 |
public void |
245 |
actionPerformed(ActionEvent e) { |
246 |
createDevicePane.setCollapsed(true); |
247 |
} |
248 |
}); |
249 |
|
250 |
pane.btnCreate.addActionListener(new ActionListener() { |
251 |
public void |
252 |
actionPerformed(ActionEvent e) { |
253 |
createAudioDevice(pane); |
254 |
} |
255 |
}); |
256 |
|
257 |
return createDevicePane; |
258 |
} |
259 |
|
260 |
private JPanel |
261 |
createVSeparator() { |
262 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
263 |
p.setOpaque(false); |
264 |
p.setPreferredSize(new Dimension(2, 24)); |
265 |
p.setMinimumSize(p.getPreferredSize()); |
266 |
p.setMaximumSize(p.getPreferredSize()); |
267 |
return p; |
268 |
} |
269 |
|
270 |
private void |
271 |
showHidePopup() { |
272 |
if(!CC.verifyConnection()) return; |
273 |
getCreateDevicePane().setCollapsed(!getCreateDevicePane().isCollapsed()); |
274 |
} |
275 |
|
276 |
private void |
277 |
createAudioDevice(final JSNewAudioDeviceDlg.Pane pane) { |
278 |
if(!createDevicePane.isAnimated()) { |
279 |
createAudioDevice0(pane); |
280 |
return; |
281 |
} |
282 |
|
283 |
createDevice = true; |
284 |
createDevicePane.setCollapsed(true); |
285 |
} |
286 |
|
287 |
private void |
288 |
createAudioDevice0(final JSNewAudioDeviceDlg.Pane pane) { |
289 |
pane.btnCreate.setEnabled(false); |
290 |
final AudioOutputDriver driver = pane.getSelectedDriver(); |
291 |
final Audio.CreateDevice cmd = |
292 |
new Audio.CreateDevice(driver.getName(), pane.getParameters()); |
293 |
|
294 |
cmd.addTaskListener(new TaskListener() { |
295 |
public void |
296 |
taskPerformed(TaskEvent e) { |
297 |
pane.btnCreate.setEnabled(true); |
298 |
getCreateDevicePane().setCollapsed(true); |
299 |
} |
300 |
}); |
301 |
|
302 |
CC.getTaskQueue().add(cmd); |
303 |
} |
304 |
|
305 |
private void |
306 |
ensureCreateDevicePaneIsVisible() { |
307 |
Container p = createDevicePane.getParent(); |
308 |
JScrollPane sp = null; |
309 |
int i = createDevicePane.getLocation().y + createDevicePane.getHeight(); |
310 |
while(p != null) { |
311 |
if(p instanceof JScrollPane) { |
312 |
sp = (JScrollPane)p; |
313 |
break; |
314 |
} |
315 |
i += p.getLocation().y; |
316 |
p = p.getParent(); |
317 |
} |
318 |
|
319 |
if(sp == null) return; |
320 |
sp.getViewport().scrollRectToVisible(new Rectangle(0, i, 5, 5)); |
321 |
} |
322 |
} |
323 |
|
324 |
private final EventHandler eventHandler = new EventHandler(); |
325 |
|
326 |
private EventHandler |
327 |
getHandler() { return eventHandler; } |
328 |
|
329 |
private class EventHandler implements ListListener<AudioDeviceModel> { |
330 |
@Override |
331 |
public void |
332 |
entryAdded(ListEvent<AudioDeviceModel> e) { |
333 |
addDevice(e.getEntry()); |
334 |
} |
335 |
|
336 |
@Override |
337 |
public void |
338 |
entryRemoved(ListEvent<AudioDeviceModel> e) { |
339 |
removeDevice(e.getEntry()); |
340 |
} |
341 |
} |
342 |
} |