/[svn]/jsampler/trunk/src/org/jsampler/DefaultMidiDeviceModel.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/DefaultMidiDeviceModel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1567 - (show annotations) (download)
Thu Dec 6 19:37:41 2007 UTC (16 years, 4 months ago) by iliev
File size: 5557 byte(s)
* added confirmation dialog on exit
* some minor gui enhancements
* preparations for release 0.8a

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2006 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;
24
25 import java.util.Vector;
26
27 import javax.swing.SwingUtilities;
28
29 import org.jsampler.event.MidiDeviceEvent;
30 import org.jsampler.event.MidiDeviceListener;
31
32 import org.jsampler.task.Midi;
33
34 import org.linuxsampler.lscp.BoolParameter;
35 import org.linuxsampler.lscp.MidiInputDevice;
36 import org.linuxsampler.lscp.Parameter;
37
38
39 /**
40 * The Default implementation of the <code>MidiDeviceModel</code> interface.
41 * @author Grigor Iliev
42 */
43 public class DefaultMidiDeviceModel implements MidiDeviceModel {
44 private MidiInputDevice midiDevice;
45
46 private final Vector<MidiDeviceListener> listeners = new Vector<MidiDeviceListener>();
47
48 /**
49 * Creates a new instance of <code>DefaultMidiDeviceModel</code> using the
50 * specified non-null MIDI device.
51 * @param midiDevice A <code>MidiInputDevice</code> instance providing the current
52 * settings of the MIDI device which will be represented by this model.
53 * @throws IllegalArgumentException If <code>midiDevice</code> is <code>null</code>.
54 */
55 public
56 DefaultMidiDeviceModel(MidiInputDevice midiDevice) {
57 if(midiDevice == null)
58 throw new IllegalArgumentException("midiDevice must be non null");
59
60 this.midiDevice = midiDevice;
61 }
62
63 /**
64 * Registers the specified listener to be notified when
65 * the settings of the MIDI device are changed.
66 * @param l The <code>MidiDeviceListener</code> to register.
67 */
68 public void
69 addMidiDeviceListener(MidiDeviceListener l) { listeners.add(l); }
70
71 /**
72 * Removes the specified listener.
73 * @param l The <code>MidiDeviceListener</code> to remove.
74 */
75 public void
76 removeMidiDeviceListener(MidiDeviceListener l) { listeners.remove(l); }
77
78 /**
79 * Gets the numerical ID of this MIDI device.
80 * @return The numerical ID of this MIDI device or
81 * -1 if the device number is not set.
82 */
83 public int
84 getDeviceId() { return midiDevice.getDeviceId(); }
85
86 /**
87 * Gets the current settings of the MIDI device represented by this model.
88 * @return <code>MidiInputDevice</code> instance providing
89 * the current settings of the MIDI device represented by this model.
90 */
91 public MidiInputDevice
92 getDeviceInfo() { return midiDevice; }
93
94 /**
95 * Updates the settings of the MIDI device represented by this model.
96 * @param device The new MIDI device settings.
97 */
98 public void
99 setDeviceInfo(MidiInputDevice device) {
100 midiDevice = device;
101 fireSettingsChanged();
102 }
103
104 /**
105 * Sets whether the MIDI device is enabled or disabled.
106 * @param active If <code>true</code> the MIDI device is enabled,
107 * else the device is disabled.
108 */
109 public void
110 setActive(boolean active) {
111 if(active == getDeviceInfo().isActive()) return;
112
113 midiDevice.setActive(active);
114 fireSettingsChanged();
115 }
116
117 /**
118 * Determines whether the MIDI device is active.
119 * @return <code>true</code> if the device is enabled and <code>false</code> otherwise.
120 */
121 public boolean
122 isActive() { return midiDevice.isActive(); }
123
124 /**
125 * Schedules a new task for enabling/disabling the MIDI device.
126 * @param active If <code>true</code> the MIDI device is enabled,
127 * else the device is disabled.
128 */
129 public void
130 setBackendActive(boolean active) {
131 CC.getTaskQueue().add(new Midi.EnableDevice(getDeviceId(), active));
132 }
133
134 /**
135 * Schedules a new task for altering
136 * a specific setting of the MIDI input device.
137 * @param prm The parameter to be set.
138 */
139 public void
140 setBackendDeviceParameter(Parameter prm) {
141 CC.getTaskQueue().add(new Midi.SetDeviceParameter(getDeviceId(), prm));
142 }
143
144 /**
145 * Schedules a new task for changing the port number of the MIDI device.
146 * @param ports The new number of ports.
147 */
148 public void
149 setBackendPortCount(int ports) {
150 CC.getTaskQueue().add(new Midi.SetPortCount(getDeviceId(), ports));
151 }
152
153 /**
154 * Schedules a new task for altering a specific
155 * setting of the specified MIDI input port.
156 * @param port The port number.
157 * @param prm The parameter to be set.
158 */
159 public void
160 setBackendPortParameter(int port, Parameter prm) {
161 CC.getTaskQueue().add(new Midi.SetPortParameter(getDeviceId(), port, prm));
162 }
163
164 /**
165 * Notifies listeners that the settings of the MIDI device are changed.
166 */
167 private void
168 fireSettingsChanged() {
169 SwingUtilities.invokeLater(new Runnable() {
170 public void
171 run() {
172 MidiDeviceModel model = DefaultMidiDeviceModel.this;
173 fireSettingsChanged(new MidiDeviceEvent(model, model));
174 }
175 });
176 }
177
178 /**
179 * Notifies listeners that the settings of the MIDI device are changed.
180 * This method should be invoked from the event-dispatching thread.
181 */
182 private void
183 fireSettingsChanged(final MidiDeviceEvent e) {
184 CC.getSamplerModel().setModified(true);
185 for(MidiDeviceListener l : listeners) l.settingsChanged(e);
186 }
187 }

  ViewVC Help
Powered by ViewVC