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