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.MidiDeviceModel; |
58 |
|
59 |
import org.jsampler.event.MidiDeviceListEvent; |
60 |
import org.jsampler.event.MidiDeviceListListener; |
61 |
|
62 |
import org.jsampler.task.Midi; |
63 |
|
64 |
import org.jsampler.view.std.JSNewMidiDeviceDlg; |
65 |
|
66 |
import org.linuxsampler.lscp.MidiInputDriver; |
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 MidiDevicesPane extends JPanel { |
77 |
private final DeviceListPane devList = new DeviceListPane(); |
78 |
private final DefaultComponentListModel<MidiDevicePane> listModel = |
79 |
new DefaultComponentListModel<MidiDevicePane>(); |
80 |
|
81 |
/** Creates a new instance of <code>MidiDevicesPane</code> */ |
82 |
public |
83 |
MidiDevicesPane() { |
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().addMidiDeviceListListener(getHandler()); |
94 |
|
95 |
for(MidiDeviceModel m : CC.getSamplerModel().getMidiDevices()) { |
96 |
addDevice(m); |
97 |
} |
98 |
} |
99 |
|
100 |
public int |
101 |
getDevicePaneCount() { return listModel.size(); } |
102 |
|
103 |
public MidiDevicePane |
104 |
getDevicePaneAt(int index) { return listModel.get(index); } |
105 |
|
106 |
private void |
107 |
addDevice(MidiDeviceModel model) { |
108 |
for(int i = 0; i < listModel.getSize(); i++) { |
109 |
if(listModel.get(i).getDeviceId() == model.getDeviceId()) { |
110 |
CC.getLogger().warning("MIDI device exist: " + model.getDeviceId()); |
111 |
return; |
112 |
} |
113 |
} |
114 |
|
115 |
listModel.add(new MidiDevicePane(model)); |
116 |
} |
117 |
|
118 |
private void |
119 |
removeDevice(MidiDeviceModel model) { |
120 |
for(int i = 0; i < listModel.getSize(); i++) { |
121 |
if(listModel.get(i).getDeviceId() == model.getDeviceId()) { |
122 |
listModel.remove(i); |
123 |
return; |
124 |
} |
125 |
} |
126 |
|
127 |
CC.getLogger().warning("MIDI device does not exist: " + model.getDeviceId()); |
128 |
} |
129 |
|
130 |
class DeviceListPane extends ComponentList { |
131 |
private Dimension maxSize = new Dimension(); |
132 |
|
133 |
public Dimension |
134 |
getMaximumSize() { |
135 |
maxSize.width = Short.MAX_VALUE; |
136 |
maxSize.height = getPreferredSize().height; |
137 |
return maxSize; |
138 |
} |
139 |
} |
140 |
|
141 |
|
142 |
class NewDevicePane extends JPanel { |
143 |
private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff18); |
144 |
private JXCollapsiblePane createDevicePane = null; |
145 |
private boolean createDevice = false; |
146 |
|
147 |
NewDevicePane() { |
148 |
setLayout(new BorderLayout()); |
149 |
|
150 |
PixmapPane p = new PixmapPane(Res.gfxDeviceBg); |
151 |
p.setPixmapInsets(new Insets(1, 1, 1, 1)); |
152 |
|
153 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
154 |
p.add(Box.createRigidArea(new Dimension(3, 0))); |
155 |
p.add(btnNew); |
156 |
p.add(Box.createRigidArea(new Dimension(3, 0))); |
157 |
|
158 |
p.add(createVSeparator()); |
159 |
|
160 |
Dimension d = new Dimension(77, 24); |
161 |
p.setPreferredSize(d); |
162 |
p.setMinimumSize(d); |
163 |
p.setMaximumSize(new Dimension(Short.MAX_VALUE, 24)); |
164 |
|
165 |
btnNew.setPressedIcon(Res.gfxPowerOn18); |
166 |
|
167 |
btnNew.addActionListener(new ActionListener() { |
168 |
public void |
169 |
actionPerformed(ActionEvent e) { |
170 |
showHidePopup(); |
171 |
} |
172 |
}); |
173 |
|
174 |
p.addMouseListener(new MouseAdapter() { |
175 |
public void |
176 |
mouseClicked(MouseEvent e) { |
177 |
if(e.getButton() != e.BUTTON1) { |
178 |
return; |
179 |
} |
180 |
|
181 |
showHidePopup(); |
182 |
} |
183 |
}); |
184 |
|
185 |
p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
186 |
|
187 |
String s = i18n.getLabel("MidiDevicesPane.newDevice"); |
188 |
btnNew.setToolTipText(s); |
189 |
p.setToolTipText(s); |
190 |
|
191 |
add(p, BorderLayout.NORTH); |
192 |
setAlignmentX(LEFT_ALIGNMENT); |
193 |
} |
194 |
|
195 |
private JPanel |
196 |
createVSeparator() { |
197 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
198 |
p.setOpaque(false); |
199 |
p.setPreferredSize(new Dimension(2, 24)); |
200 |
p.setMinimumSize(p.getPreferredSize()); |
201 |
p.setMaximumSize(p.getPreferredSize()); |
202 |
return p; |
203 |
} |
204 |
|
205 |
private JXCollapsiblePane |
206 |
getCreateDevicePane() { |
207 |
if(createDevicePane != null) return createDevicePane; |
208 |
|
209 |
createDevicePane = new JXCollapsiblePane(); |
210 |
final JSNewMidiDeviceDlg.Pane pane = new JSNewMidiDeviceDlg.Pane(); |
211 |
|
212 |
PixmapPane p1 = new PixmapPane(Res.gfxChannelOptions); |
213 |
p1.setPixmapInsets(new Insets(1, 1, 1, 1)); |
214 |
p1.setLayout(new BorderLayout()); |
215 |
p1.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); |
216 |
|
217 |
PixmapPane p2 = new PixmapPane(Res.gfxBorder); |
218 |
p2.setPixmapInsets(new Insets(1, 1, 1, 1)); |
219 |
p2.setLayout(new BorderLayout()); |
220 |
p2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
221 |
p2.add(pane); |
222 |
p1.add(p2); |
223 |
|
224 |
p1.setPreferredSize(new Dimension(getWidth(), 210)); |
225 |
p1.setPreferredSize(new Dimension(100, 210)); |
226 |
|
227 |
createDevicePane.setContentPane(p1); |
228 |
createDevicePane.setAnimated(false); |
229 |
createDevicePane.setCollapsed(true); |
230 |
createDevicePane.setAnimated(preferences().getBoolProperty(ANIMATED)); |
231 |
|
232 |
preferences().addPropertyChangeListener(ANIMATED, new PropertyChangeListener() { |
233 |
public void |
234 |
propertyChange(PropertyChangeEvent e) { |
235 |
boolean b = preferences().getBoolProperty(ANIMATED); |
236 |
createDevicePane.setAnimated(b); |
237 |
} |
238 |
}); |
239 |
|
240 |
String s = JXCollapsiblePane.ANIMATION_STATE_KEY; |
241 |
createDevicePane.addPropertyChangeListener(s, new PropertyChangeListener() { |
242 |
public void |
243 |
propertyChange(PropertyChangeEvent e) { |
244 |
Object o = e.getNewValue(); |
245 |
if(o == "collapsed") { |
246 |
if(createDevice) { |
247 |
createMidiDevice0(pane); |
248 |
createDevice = false; |
249 |
} |
250 |
} else if(o == "expanded" || o == "expanding/collapsing") { |
251 |
ensureCreateDevicePaneIsVisible(); |
252 |
} |
253 |
} |
254 |
}); |
255 |
|
256 |
add(createDevicePane); |
257 |
|
258 |
pane.btnCancel.addActionListener(new ActionListener() { |
259 |
public void |
260 |
actionPerformed(ActionEvent e) { |
261 |
createDevicePane.setCollapsed(true); |
262 |
} |
263 |
}); |
264 |
|
265 |
pane.btnCreate.addActionListener(new ActionListener() { |
266 |
public void |
267 |
actionPerformed(ActionEvent e) { |
268 |
createMidiDevice(pane); |
269 |
} |
270 |
}); |
271 |
|
272 |
return createDevicePane; |
273 |
} |
274 |
|
275 |
private void |
276 |
showHidePopup() { |
277 |
if(!CC.verifyConnection()) return; |
278 |
getCreateDevicePane().setCollapsed(!getCreateDevicePane().isCollapsed()); |
279 |
} |
280 |
|
281 |
private void |
282 |
createMidiDevice(final JSNewMidiDeviceDlg.Pane pane) { |
283 |
if(!createDevicePane.isAnimated()) { |
284 |
createMidiDevice0(pane); |
285 |
return; |
286 |
} |
287 |
|
288 |
createDevice = true; |
289 |
createDevicePane.setCollapsed(true); |
290 |
} |
291 |
|
292 |
private void |
293 |
createMidiDevice0(final JSNewMidiDeviceDlg.Pane pane) { |
294 |
pane.btnCreate.setEnabled(false); |
295 |
final MidiInputDriver driver = pane.getSelectedDriver(); |
296 |
final Midi.CreateDevice cmd = |
297 |
new Midi.CreateDevice(driver.getName(), pane.getParameters()); |
298 |
|
299 |
cmd.addTaskListener(new TaskListener() { |
300 |
public void |
301 |
taskPerformed(TaskEvent e) { |
302 |
pane.btnCreate.setEnabled(true); |
303 |
getCreateDevicePane().setCollapsed(true); |
304 |
} |
305 |
}); |
306 |
|
307 |
CC.getTaskQueue().add(cmd); |
308 |
} |
309 |
|
310 |
private void |
311 |
ensureCreateDevicePaneIsVisible() { |
312 |
Container p = createDevicePane.getParent(); |
313 |
JScrollPane sp = null; |
314 |
int i = createDevicePane.getLocation().y + createDevicePane.getHeight(); |
315 |
while(p != null) { |
316 |
if(p instanceof JScrollPane) { |
317 |
sp = (JScrollPane)p; |
318 |
break; |
319 |
} |
320 |
i += p.getLocation().y; |
321 |
p = p.getParent(); |
322 |
} |
323 |
|
324 |
if(sp == null) return; |
325 |
sp.getViewport().scrollRectToVisible(new Rectangle(0, i, 5, 5)); |
326 |
} |
327 |
} |
328 |
|
329 |
private final EventHandler eventHandler = new EventHandler(); |
330 |
|
331 |
private EventHandler |
332 |
getHandler() { return eventHandler; } |
333 |
|
334 |
private class EventHandler implements MidiDeviceListListener { |
335 |
public void |
336 |
deviceAdded(MidiDeviceListEvent e) { |
337 |
addDevice(e.getMidiDeviceModel()); |
338 |
} |
339 |
|
340 |
public void |
341 |
deviceRemoved(MidiDeviceListEvent e) { |
342 |
removeDevice(e.getMidiDeviceModel()); |
343 |
} |
344 |
} |
345 |
} |