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 changing the port number of the MIDI device. |
136 |
* @param ports The new number of ports. |
137 |
*/ |
138 |
public void |
139 |
setBackendPortCount(int ports) { |
140 |
CC.getTaskQueue().add(new Midi.SetPortCount(getDeviceId(), ports)); |
141 |
} |
142 |
|
143 |
/** |
144 |
* Schedules a new task for altering a specific |
145 |
* setting of the specified MIDI input port. |
146 |
* @param port The port number. |
147 |
* @param prm The parameter to be set. |
148 |
*/ |
149 |
public void |
150 |
setBackendPortParameter(int port, Parameter prm) { |
151 |
CC.getTaskQueue().add(new Midi.SetPortParameter(getDeviceId(), port, prm)); |
152 |
} |
153 |
|
154 |
/** |
155 |
* Notifies listeners that the settings of the MIDI device are changed. |
156 |
*/ |
157 |
private void |
158 |
fireSettingsChanged() { |
159 |
fireSettingsChanged(new MidiDeviceEvent(this, this)); |
160 |
} |
161 |
|
162 |
/** |
163 |
* Notifies listeners that the settings of the MIDI device are changed. |
164 |
* This method should be invoked from the event-dispatching thread. |
165 |
*/ |
166 |
private void |
167 |
fireSettingsChanged(final MidiDeviceEvent e) { |
168 |
SwingUtilities.invokeLater(new Runnable() { |
169 |
public void |
170 |
run() { for(MidiDeviceListener l : listeners) l.settingsChanged(e); } |
171 |
}); |
172 |
} |
173 |
} |