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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1752 - (hide annotations) (download)
Mon Aug 11 22:51:24 2008 UTC (15 years, 9 months ago) by iliev
File size: 9419 byte(s)
* Added toolbar to the Database Instrument Chooser dialog
* Instrument Chooser and Database Instrument Chooser dialogs
  are now resizable
* Fantasia: Added toolbar to the Right-Side Pane's Instruments Database

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

  ViewVC Help
Powered by ViewVC