/[svn]/jsampler/trunk/src/org/jsampler/view/fantasia/MidiDevicesPane.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/fantasia/MidiDevicesPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1778 - (show annotations) (download)
Sun Sep 28 20:38:36 2008 UTC (15 years, 7 months ago) by iliev
File size: 9958 byte(s)
* Fantasia: Improved look and feel
* Fantasia: Added buttons for increasing/decreasing the key number
  of the MIDI keyboard (Ctrl+Up Arrow/Ctrl+Down Arrow)
* Fantasia: Added buttons for scrolling left/right on the MIDI keyboard
  (Ctrl+Left Arrow/Ctrl+Right Arrow)
* Added key bindings for triggering MIDI notes using the computer keyboard
  (from a to ' for the white keys and from 0 to 7 for changing the octaves)
* Notes are now triggered when dragging the mouse over the MIDI keyboard

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

  ViewVC Help
Powered by ViewVC