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 |
private void |
101 |
addDevice(MidiDeviceModel model) { |
102 |
for(int i = 0; i < listModel.getSize(); i++) { |
103 |
if(listModel.get(i).getDeviceId() == model.getDeviceId()) { |
104 |
CC.getLogger().warning("MIDI device exist: " + model.getDeviceId()); |
105 |
return; |
106 |
} |
107 |
} |
108 |
|
109 |
listModel.add(new MidiDevicePane(model)); |
110 |
} |
111 |
|
112 |
private void |
113 |
removeDevice(MidiDeviceModel 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("MIDI 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("MidiDevicesPane.newDevice"); |
182 |
btnNew.setToolTipText(s); |
183 |
p.setToolTipText(s); |
184 |
|
185 |
add(p, BorderLayout.NORTH); |
186 |
setAlignmentX(LEFT_ALIGNMENT); |
187 |
} |
188 |
|
189 |
private JPanel |
190 |
createVSeparator() { |
191 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
192 |
p.setOpaque(false); |
193 |
p.setPreferredSize(new Dimension(2, 24)); |
194 |
p.setMinimumSize(p.getPreferredSize()); |
195 |
p.setMaximumSize(p.getPreferredSize()); |
196 |
return p; |
197 |
} |
198 |
|
199 |
private JXCollapsiblePane |
200 |
getCreateDevicePane() { |
201 |
if(createDevicePane != null) return createDevicePane; |
202 |
|
203 |
createDevicePane = new JXCollapsiblePane(); |
204 |
final JSNewMidiDeviceDlg.Pane pane = new JSNewMidiDeviceDlg.Pane(); |
205 |
|
206 |
PixmapPane p1 = new PixmapPane(Res.gfxChannelOptions); |
207 |
p1.setPixmapInsets(new Insets(1, 1, 1, 1)); |
208 |
p1.setLayout(new BorderLayout()); |
209 |
p1.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); |
210 |
|
211 |
PixmapPane p2 = new PixmapPane(Res.gfxBorder); |
212 |
p2.setPixmapInsets(new Insets(1, 1, 1, 1)); |
213 |
p2.setLayout(new BorderLayout()); |
214 |
p2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
215 |
p2.add(pane); |
216 |
p1.add(p2); |
217 |
|
218 |
p1.setPreferredSize(new Dimension(getWidth(), 210)); |
219 |
p1.setPreferredSize(new Dimension(100, 210)); |
220 |
|
221 |
createDevicePane.setContentPane(p1); |
222 |
createDevicePane.setAnimated(false); |
223 |
createDevicePane.setCollapsed(true); |
224 |
createDevicePane.setAnimated(preferences().getBoolProperty(ANIMATED)); |
225 |
|
226 |
preferences().addPropertyChangeListener(ANIMATED, new PropertyChangeListener() { |
227 |
public void |
228 |
propertyChange(PropertyChangeEvent e) { |
229 |
boolean b = preferences().getBoolProperty(ANIMATED); |
230 |
createDevicePane.setAnimated(b); |
231 |
} |
232 |
}); |
233 |
|
234 |
String s = JXCollapsiblePane.ANIMATION_STATE_KEY; |
235 |
createDevicePane.addPropertyChangeListener(s, new PropertyChangeListener() { |
236 |
public void |
237 |
propertyChange(PropertyChangeEvent e) { |
238 |
Object o = e.getNewValue(); |
239 |
if(o == "collapsed") { |
240 |
if(createDevice) { |
241 |
createMidiDevice0(pane); |
242 |
createDevice = false; |
243 |
} |
244 |
} else if(o == "expanded" || o == "expanding/collapsing") { |
245 |
ensureCreateDevicePaneIsVisible(); |
246 |
} |
247 |
} |
248 |
}); |
249 |
|
250 |
add(createDevicePane); |
251 |
|
252 |
pane.btnCancel.addActionListener(new ActionListener() { |
253 |
public void |
254 |
actionPerformed(ActionEvent e) { |
255 |
createDevicePane.setCollapsed(true); |
256 |
} |
257 |
}); |
258 |
|
259 |
pane.btnCreate.addActionListener(new ActionListener() { |
260 |
public void |
261 |
actionPerformed(ActionEvent e) { |
262 |
createMidiDevice(pane); |
263 |
} |
264 |
}); |
265 |
|
266 |
return createDevicePane; |
267 |
} |
268 |
|
269 |
private void |
270 |
showHidePopup() { |
271 |
getCreateDevicePane().setCollapsed(!getCreateDevicePane().isCollapsed()); |
272 |
} |
273 |
|
274 |
private void |
275 |
createMidiDevice(final JSNewMidiDeviceDlg.Pane pane) { |
276 |
if(!createDevicePane.isAnimated()) { |
277 |
createMidiDevice0(pane); |
278 |
return; |
279 |
} |
280 |
|
281 |
createDevice = true; |
282 |
createDevicePane.setCollapsed(true); |
283 |
} |
284 |
|
285 |
private void |
286 |
createMidiDevice0(final JSNewMidiDeviceDlg.Pane pane) { |
287 |
pane.btnCreate.setEnabled(false); |
288 |
final MidiInputDriver driver = pane.getSelectedDriver(); |
289 |
final Midi.CreateDevice cmd = |
290 |
new Midi.CreateDevice(driver.getName(), pane.getParameters()); |
291 |
|
292 |
cmd.addTaskListener(new TaskListener() { |
293 |
public void |
294 |
taskPerformed(TaskEvent e) { |
295 |
pane.btnCreate.setEnabled(true); |
296 |
getCreateDevicePane().setCollapsed(true); |
297 |
} |
298 |
}); |
299 |
|
300 |
CC.getTaskQueue().add(cmd); |
301 |
} |
302 |
|
303 |
private void |
304 |
ensureCreateDevicePaneIsVisible() { |
305 |
Container p = createDevicePane.getParent(); |
306 |
JScrollPane sp = null; |
307 |
int i = createDevicePane.getLocation().y + createDevicePane.getHeight(); |
308 |
while(p != null) { |
309 |
if(p instanceof JScrollPane) { |
310 |
sp = (JScrollPane)p; |
311 |
break; |
312 |
} |
313 |
i += p.getLocation().y; |
314 |
p = p.getParent(); |
315 |
} |
316 |
|
317 |
if(sp == null) return; |
318 |
sp.getViewport().scrollRectToVisible(new Rectangle(0, i, 5, 5)); |
319 |
} |
320 |
} |
321 |
|
322 |
private final EventHandler eventHandler = new EventHandler(); |
323 |
|
324 |
private EventHandler |
325 |
getHandler() { return eventHandler; } |
326 |
|
327 |
private class EventHandler implements MidiDeviceListListener { |
328 |
public void |
329 |
deviceAdded(MidiDeviceListEvent e) { |
330 |
addDevice(e.getMidiDeviceModel()); |
331 |
} |
332 |
|
333 |
public void |
334 |
deviceRemoved(MidiDeviceListEvent e) { |
335 |
removeDevice(e.getMidiDeviceModel()); |
336 |
} |
337 |
} |
338 |
} |