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