/[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 1688 - (hide annotations) (download)
Thu Feb 14 16:52:36 2008 UTC (16 years, 2 months ago) by iliev
File size: 9268 byte(s)
* Implemented a backend list with option to manually choose a backend
  to connect on startup(Edit/Preferences, then click the `Backend' tab)
  and option to change the backend without restarting JSampler
  (Actions/Change Backend or Ctrl + B)

* Added confirmation messages for removing sampler channels and
  audio/MIDI devices (Edit/Preferences, then click the `View' tab)

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

  ViewVC Help
Powered by ViewVC