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.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.ComponentListModel; |
49 |
import net.sf.juife.DefaultComponentListModel; |
50 |
|
51 |
import net.sf.juife.event.TaskEvent; |
52 |
import net.sf.juife.event.TaskListener; |
53 |
|
54 |
import org.jdesktop.swingx.JXCollapsiblePane; |
55 |
|
56 |
import org.jsampler.CC; |
57 |
import org.jsampler.AudioDeviceModel; |
58 |
|
59 |
import org.jsampler.event.ListEvent; |
60 |
import org.jsampler.event.ListListener; |
61 |
|
62 |
import org.jsampler.task.Audio; |
63 |
|
64 |
import org.jsampler.view.std.JSNewAudioDeviceDlg; |
65 |
|
66 |
import org.linuxsampler.lscp.AudioOutputDriver; |
67 |
|
68 |
import static org.jsampler.view.fantasia.A4n.a4n; |
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 |
public Dimension |
128 |
getMaximumSize() { |
129 |
maxSize.width = Short.MAX_VALUE; |
130 |
maxSize.height = getPreferredSize().height; |
131 |
return maxSize; |
132 |
} |
133 |
} |
134 |
|
135 |
|
136 |
class NewDevicePane extends JPanel { |
137 |
private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff18); |
138 |
private JXCollapsiblePane createDevicePane = null; |
139 |
private boolean createDevice = false; |
140 |
|
141 |
NewDevicePane() { |
142 |
setLayout(new BorderLayout()); |
143 |
|
144 |
PixmapPane p = new PixmapPane(Res.gfxDeviceBg); |
145 |
p.setPixmapInsets(new Insets(1, 1, 1, 1)); |
146 |
|
147 |
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 |
|
152 |
p.add(createVSeparator()); |
153 |
|
154 |
Dimension d = new Dimension(77, 24); |
155 |
p.setPreferredSize(d); |
156 |
p.setMinimumSize(d); |
157 |
p.setMaximumSize(new Dimension(Short.MAX_VALUE, 24)); |
158 |
|
159 |
btnNew.setPressedIcon(Res.gfxPowerOn18); |
160 |
|
161 |
btnNew.addActionListener(new ActionListener() { |
162 |
public void |
163 |
actionPerformed(ActionEvent e) { |
164 |
showHidePopup(); |
165 |
} |
166 |
}); |
167 |
|
168 |
p.addMouseListener(new MouseAdapter() { |
169 |
public void |
170 |
mouseClicked(MouseEvent e) { |
171 |
if(e.getButton() != e.BUTTON1) { |
172 |
return; |
173 |
} |
174 |
|
175 |
showHidePopup(); |
176 |
} |
177 |
}); |
178 |
|
179 |
p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
180 |
|
181 |
String s = i18n.getLabel("AudioDevicesPane.newDevice"); |
182 |
btnNew.setToolTipText(s); |
183 |
p.setToolTipText(s); |
184 |
|
185 |
add(p, BorderLayout.NORTH); |
186 |
setAlignmentX(LEFT_ALIGNMENT); |
187 |
} |
188 |
|
189 |
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 |
Object o = e.getNewValue(); |
229 |
if(o == "collapsed") { |
230 |
if(createDevice) { |
231 |
createAudioDevice0(pane); |
232 |
createDevice = false; |
233 |
} |
234 |
} else if(o == "expanded" || o == "expanding/collapsing") { |
235 |
ensureCreateDevicePaneIsVisible(); |
236 |
} |
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 |
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 |
|
269 |
private void |
270 |
showHidePopup() { |
271 |
if(!CC.verifyConnection()) return; |
272 |
getCreateDevicePane().setCollapsed(!getCreateDevicePane().isCollapsed()); |
273 |
} |
274 |
|
275 |
private void |
276 |
createAudioDevice(final JSNewAudioDeviceDlg.Pane pane) { |
277 |
if(!createDevicePane.isAnimated()) { |
278 |
createAudioDevice0(pane); |
279 |
return; |
280 |
} |
281 |
|
282 |
createDevice = true; |
283 |
createDevicePane.setCollapsed(true); |
284 |
} |
285 |
|
286 |
private void |
287 |
createAudioDevice0(final JSNewAudioDeviceDlg.Pane pane) { |
288 |
pane.btnCreate.setEnabled(false); |
289 |
final AudioOutputDriver driver = pane.getSelectedDriver(); |
290 |
final Audio.CreateDevice cmd = |
291 |
new Audio.CreateDevice(driver.getName(), pane.getParameters()); |
292 |
|
293 |
cmd.addTaskListener(new TaskListener() { |
294 |
public void |
295 |
taskPerformed(TaskEvent e) { |
296 |
pane.btnCreate.setEnabled(true); |
297 |
getCreateDevicePane().setCollapsed(true); |
298 |
} |
299 |
}); |
300 |
|
301 |
CC.getTaskQueue().add(cmd); |
302 |
} |
303 |
|
304 |
private void |
305 |
ensureCreateDevicePaneIsVisible() { |
306 |
Container p = createDevicePane.getParent(); |
307 |
JScrollPane sp = null; |
308 |
int i = createDevicePane.getLocation().y + createDevicePane.getHeight(); |
309 |
while(p != null) { |
310 |
if(p instanceof JScrollPane) { |
311 |
sp = (JScrollPane)p; |
312 |
break; |
313 |
} |
314 |
i += p.getLocation().y; |
315 |
p = p.getParent(); |
316 |
} |
317 |
|
318 |
if(sp == null) return; |
319 |
sp.getViewport().scrollRectToVisible(new Rectangle(0, i, 5, 5)); |
320 |
} |
321 |
} |
322 |
|
323 |
private final EventHandler eventHandler = new EventHandler(); |
324 |
|
325 |
private EventHandler |
326 |
getHandler() { return eventHandler; } |
327 |
|
328 |
private class EventHandler implements ListListener<AudioDeviceModel> { |
329 |
public void |
330 |
entryAdded(ListEvent<AudioDeviceModel> e) { |
331 |
addDevice(e.getEntry()); |
332 |
} |
333 |
|
334 |
public void |
335 |
entryRemoved(ListEvent<AudioDeviceModel> e) { |
336 |
removeDevice(e.getEntry()); |
337 |
} |
338 |
} |
339 |
} |