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.Cursor; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.Insets; |
29 |
|
30 |
import java.awt.event.ActionEvent; |
31 |
import java.awt.event.ActionListener; |
32 |
import java.awt.event.MouseAdapter; |
33 |
import java.awt.event.MouseEvent; |
34 |
|
35 |
import java.beans.PropertyChangeEvent; |
36 |
import java.beans.PropertyChangeListener; |
37 |
|
38 |
import javax.swing.BorderFactory; |
39 |
import javax.swing.Box; |
40 |
import javax.swing.BoxLayout; |
41 |
import javax.swing.JPanel; |
42 |
import javax.swing.ListSelectionModel; |
43 |
|
44 |
import net.sf.juife.ComponentList; |
45 |
import net.sf.juife.ComponentListModel; |
46 |
import net.sf.juife.DefaultComponentListModel; |
47 |
|
48 |
import net.sf.juife.event.TaskEvent; |
49 |
import net.sf.juife.event.TaskListener; |
50 |
|
51 |
import org.jdesktop.swingx.JXCollapsiblePane; |
52 |
|
53 |
import org.jsampler.CC; |
54 |
import org.jsampler.MidiDeviceModel; |
55 |
|
56 |
import org.jsampler.event.MidiDeviceListEvent; |
57 |
import org.jsampler.event.MidiDeviceListListener; |
58 |
|
59 |
import org.jsampler.task.Midi; |
60 |
|
61 |
import org.jsampler.view.std.JSNewMidiDeviceDlg; |
62 |
|
63 |
import org.linuxsampler.lscp.MidiInputDriver; |
64 |
|
65 |
import static org.jsampler.view.fantasia.A4n.a4n; |
66 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
67 |
import static org.jsampler.view.fantasia.FantasiaPrefs.*; |
68 |
|
69 |
/** |
70 |
* |
71 |
* @author Grigor Iliev |
72 |
*/ |
73 |
public class MidiDevicesPane extends JPanel { |
74 |
private final DeviceListPane devList = new DeviceListPane(); |
75 |
private final DefaultComponentListModel<MidiDevicePane> listModel = |
76 |
new DefaultComponentListModel<MidiDevicePane>(); |
77 |
|
78 |
/** Creates a new instance of <code>MidiDevicesPane</code> */ |
79 |
public |
80 |
MidiDevicesPane() { |
81 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
82 |
|
83 |
devList.setModel(listModel); |
84 |
devList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
85 |
devList.setAlignmentX(LEFT_ALIGNMENT); |
86 |
|
87 |
add(devList); |
88 |
add(new NewDevicePane()); |
89 |
|
90 |
CC.getSamplerModel().addMidiDeviceListListener(getHandler()); |
91 |
|
92 |
for(MidiDeviceModel m : CC.getSamplerModel().getMidiDevices()) { |
93 |
addDevice(m); |
94 |
} |
95 |
} |
96 |
|
97 |
private void |
98 |
addDevice(MidiDeviceModel model) { |
99 |
for(int i = 0; i < listModel.getSize(); i++) { |
100 |
if(listModel.get(i).getDeviceId() == model.getDeviceId()) { |
101 |
CC.getLogger().warning("MIDI device exist: " + model.getDeviceId()); |
102 |
return; |
103 |
} |
104 |
} |
105 |
|
106 |
listModel.add(new MidiDevicePane(model)); |
107 |
} |
108 |
|
109 |
private void |
110 |
removeDevice(MidiDeviceModel model) { |
111 |
for(int i = 0; i < listModel.getSize(); i++) { |
112 |
if(listModel.get(i).getDeviceId() == model.getDeviceId()) { |
113 |
listModel.remove(i); |
114 |
return; |
115 |
} |
116 |
} |
117 |
|
118 |
CC.getLogger().warning("MIDI device does not exist: " + model.getDeviceId()); |
119 |
} |
120 |
|
121 |
class DeviceListPane extends ComponentList { |
122 |
private Dimension maxSize = new Dimension(); |
123 |
|
124 |
public Dimension |
125 |
getMaximumSize() { |
126 |
maxSize.width = Short.MAX_VALUE; |
127 |
maxSize.height = getPreferredSize().height; |
128 |
return maxSize; |
129 |
} |
130 |
} |
131 |
|
132 |
|
133 |
class NewDevicePane extends JPanel { |
134 |
private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff18); |
135 |
private JXCollapsiblePane createDevicePane = null; |
136 |
private boolean createDevice = false; |
137 |
|
138 |
NewDevicePane() { |
139 |
setLayout(new BorderLayout()); |
140 |
|
141 |
PixmapPane p = new PixmapPane(Res.gfxDeviceBg); |
142 |
p.setPixmapInsets(new Insets(1, 1, 1, 1)); |
143 |
|
144 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
145 |
p.add(Box.createRigidArea(new Dimension(3, 0))); |
146 |
p.add(btnNew); |
147 |
p.add(Box.createRigidArea(new Dimension(3, 0))); |
148 |
|
149 |
p.add(createVSeparator()); |
150 |
|
151 |
Dimension d = new Dimension(77, 24); |
152 |
p.setPreferredSize(d); |
153 |
p.setMinimumSize(d); |
154 |
p.setMaximumSize(new Dimension(Short.MAX_VALUE, 24)); |
155 |
|
156 |
btnNew.setPressedIcon(Res.gfxPowerOn18); |
157 |
|
158 |
btnNew.addActionListener(new ActionListener() { |
159 |
public void |
160 |
actionPerformed(ActionEvent e) { |
161 |
showHidePopup(); |
162 |
} |
163 |
}); |
164 |
|
165 |
p.addMouseListener(new MouseAdapter() { |
166 |
public void |
167 |
mouseClicked(MouseEvent e) { |
168 |
if(e.getButton() != e.BUTTON1) { |
169 |
return; |
170 |
} |
171 |
|
172 |
showHidePopup(); |
173 |
} |
174 |
}); |
175 |
|
176 |
p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
177 |
|
178 |
String s = i18n.getLabel("MidiDevicesPane.newDevice"); |
179 |
btnNew.setToolTipText(s); |
180 |
p.setToolTipText(s); |
181 |
|
182 |
add(p, BorderLayout.NORTH); |
183 |
setAlignmentX(LEFT_ALIGNMENT); |
184 |
} |
185 |
|
186 |
private JPanel |
187 |
createVSeparator() { |
188 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
189 |
p.setOpaque(false); |
190 |
p.setPreferredSize(new Dimension(2, 24)); |
191 |
p.setMinimumSize(p.getPreferredSize()); |
192 |
p.setMaximumSize(p.getPreferredSize()); |
193 |
return p; |
194 |
} |
195 |
|
196 |
private JXCollapsiblePane |
197 |
getCreateDevicePane() { |
198 |
if(createDevicePane != null) return createDevicePane; |
199 |
|
200 |
createDevicePane = new JXCollapsiblePane(); |
201 |
final JSNewMidiDeviceDlg.Pane pane = new JSNewMidiDeviceDlg.Pane(); |
202 |
|
203 |
PixmapPane p1 = new PixmapPane(Res.gfxChannelOptions); |
204 |
p1.setPixmapInsets(new Insets(1, 1, 1, 1)); |
205 |
p1.setLayout(new BorderLayout()); |
206 |
p1.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); |
207 |
|
208 |
PixmapPane p2 = new PixmapPane(Res.gfxBorder); |
209 |
p2.setPixmapInsets(new Insets(1, 1, 1, 1)); |
210 |
p2.setLayout(new BorderLayout()); |
211 |
p2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
212 |
p2.add(pane); |
213 |
p1.add(p2); |
214 |
|
215 |
p1.setPreferredSize(new Dimension(getWidth(), 210)); |
216 |
p1.setPreferredSize(new Dimension(100, 210)); |
217 |
|
218 |
createDevicePane.setContentPane(p1); |
219 |
createDevicePane.setAnimated(false); |
220 |
createDevicePane.setCollapsed(true); |
221 |
createDevicePane.setAnimated(preferences().getBoolProperty(ANIMATED)); |
222 |
|
223 |
preferences().addPropertyChangeListener(ANIMATED, new PropertyChangeListener() { |
224 |
public void |
225 |
propertyChange(PropertyChangeEvent e) { |
226 |
boolean b = preferences().getBoolProperty(ANIMATED); |
227 |
createDevicePane.setAnimated(b); |
228 |
} |
229 |
}); |
230 |
|
231 |
String s = JXCollapsiblePane.ANIMATION_STATE_KEY; |
232 |
createDevicePane.addPropertyChangeListener(s, new PropertyChangeListener() { |
233 |
public void |
234 |
propertyChange(PropertyChangeEvent e) { |
235 |
if(e.getNewValue() == "collapsed") { |
236 |
if(createDevice) { |
237 |
createMidiDevice0(pane); |
238 |
createDevice = false; |
239 |
} |
240 |
} |
241 |
} |
242 |
}); |
243 |
|
244 |
add(createDevicePane); |
245 |
|
246 |
pane.btnCancel.addActionListener(new ActionListener() { |
247 |
public void |
248 |
actionPerformed(ActionEvent e) { |
249 |
createDevicePane.setCollapsed(true); |
250 |
} |
251 |
}); |
252 |
|
253 |
pane.btnCreate.addActionListener(new ActionListener() { |
254 |
public void |
255 |
actionPerformed(ActionEvent e) { |
256 |
createMidiDevice(pane); |
257 |
} |
258 |
}); |
259 |
|
260 |
return createDevicePane; |
261 |
} |
262 |
|
263 |
private void |
264 |
showHidePopup() { |
265 |
getCreateDevicePane().setCollapsed(!getCreateDevicePane().isCollapsed()); |
266 |
} |
267 |
|
268 |
private void |
269 |
createMidiDevice(final JSNewMidiDeviceDlg.Pane pane) { |
270 |
if(!createDevicePane.isAnimated()) { |
271 |
createMidiDevice0(pane); |
272 |
return; |
273 |
} |
274 |
|
275 |
createDevice = true; |
276 |
createDevicePane.setCollapsed(true); |
277 |
} |
278 |
|
279 |
private void |
280 |
createMidiDevice0(final JSNewMidiDeviceDlg.Pane pane) { |
281 |
pane.btnCreate.setEnabled(false); |
282 |
final MidiInputDriver driver = pane.getSelectedDriver(); |
283 |
final Midi.CreateDevice cmd = |
284 |
new Midi.CreateDevice(driver.getName(), driver.getParameters()); |
285 |
|
286 |
cmd.addTaskListener(new TaskListener() { |
287 |
public void |
288 |
taskPerformed(TaskEvent e) { |
289 |
pane.btnCreate.setEnabled(true); |
290 |
getCreateDevicePane().setCollapsed(true); |
291 |
} |
292 |
}); |
293 |
|
294 |
CC.getTaskQueue().add(cmd); |
295 |
} |
296 |
} |
297 |
|
298 |
private final EventHandler eventHandler = new EventHandler(); |
299 |
|
300 |
private EventHandler |
301 |
getHandler() { return eventHandler; } |
302 |
|
303 |
private class EventHandler implements MidiDeviceListListener { |
304 |
public void |
305 |
deviceAdded(MidiDeviceListEvent e) { |
306 |
addDevice(e.getMidiDeviceModel()); |
307 |
} |
308 |
|
309 |
public void |
310 |
deviceRemoved(MidiDeviceListEvent e) { |
311 |
removeDevice(e.getMidiDeviceModel()); |
312 |
} |
313 |
} |
314 |
} |