1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005 Grigor Kirilov Iliev |
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.linuxsampler.lscp.BoolParameter; |
33 |
import org.linuxsampler.lscp.MidiInputDevice; |
34 |
import org.linuxsampler.lscp.Parameter; |
35 |
|
36 |
|
37 |
/** |
38 |
* |
39 |
* @author Grigor Iliev |
40 |
*/ |
41 |
public class DefaultMidiDeviceModel implements MidiDeviceModel { |
42 |
private MidiInputDevice midiDevice; |
43 |
|
44 |
private final Vector<MidiDeviceListener> listeners = new Vector<MidiDeviceListener>(); |
45 |
|
46 |
/** |
47 |
* Creates a new instance of <code>DefaultMidiDeviceModel</code> using the |
48 |
* specified non-null MIDI device. |
49 |
* @param midiDevice A <code>MidiInputDevice</code> instance providing the current |
50 |
* settings of the MIDI device which will be represented by this model. |
51 |
* @throws IllegalArgumentException If <code>midiDevice</code> is <code>null</code>. |
52 |
*/ |
53 |
public |
54 |
DefaultMidiDeviceModel(MidiInputDevice midiDevice) { |
55 |
if(midiDevice == null) |
56 |
throw new IllegalArgumentException("midiDevice must be non null"); |
57 |
|
58 |
this.midiDevice = midiDevice; |
59 |
} |
60 |
|
61 |
/** |
62 |
* Registers the specified listener to be notified when |
63 |
* the settings of the MIDI device are changed. |
64 |
* @param l The <code>MidiDeviceListener</code> to register. |
65 |
*/ |
66 |
public void |
67 |
addMidiDeviceListener(MidiDeviceListener l) { listeners.add(l); } |
68 |
|
69 |
/** |
70 |
* Removes the specified listener. |
71 |
* @param l The <code>MidiDeviceListener</code> to remove. |
72 |
*/ |
73 |
public void |
74 |
removeMidiDeviceListener(MidiDeviceListener l) { listeners.remove(l); } |
75 |
|
76 |
/** |
77 |
* Gets the numerical ID of this MIDI device. |
78 |
* @return The numerical ID of this MIDI device or |
79 |
* -1 if the device number is not set. |
80 |
*/ |
81 |
public int |
82 |
getDeviceID() { return midiDevice.getDeviceID(); } |
83 |
|
84 |
/** |
85 |
* Gets the current settings of the MIDI device represented by this model. |
86 |
* @return <code>MidiInputDevice</code> instance providing |
87 |
* the current settings of the MIDI device represented by this model. |
88 |
*/ |
89 |
public MidiInputDevice |
90 |
getDeviceInfo() { return midiDevice; } |
91 |
|
92 |
/** |
93 |
* Updates the settings of the MIDI device represented by this model. |
94 |
* @param device The new MIDI device settings. |
95 |
*/ |
96 |
public void |
97 |
setDeviceInfo(MidiInputDevice device) { |
98 |
midiDevice = device; |
99 |
fireSettingsChanged(); |
100 |
} |
101 |
|
102 |
/** |
103 |
* Sets whether the MIDI device is enabled or disabled. |
104 |
* @param active If <code>true</code> the MIDI device is enabled, |
105 |
* else the device is disabled. |
106 |
*/ |
107 |
public void |
108 |
setActive(boolean active) { |
109 |
if(active == getDeviceInfo().isActive()) return; |
110 |
|
111 |
midiDevice.setActive(active); |
112 |
fireSettingsChanged(); |
113 |
} |
114 |
|
115 |
/** |
116 |
* Determines whether the MIDI device is active. |
117 |
* @return <code>true</code> if the device is enabled and <code>false</code> otherwise. |
118 |
*/ |
119 |
public boolean |
120 |
isActive() { return midiDevice.isActive(); } |
121 |
|
122 |
/** |
123 |
* Notifies listeners that the settings of the MIDI device are changed. |
124 |
*/ |
125 |
private void |
126 |
fireSettingsChanged() { |
127 |
fireSettingsChanged(new MidiDeviceEvent(this, this)); |
128 |
} |
129 |
|
130 |
/** |
131 |
* Notifies listeners that the settings of the MIDI device are changed. |
132 |
* This method should be invoked from the event-dispatching thread. |
133 |
*/ |
134 |
private void |
135 |
fireSettingsChanged(final MidiDeviceEvent e) { |
136 |
SwingUtilities.invokeLater(new Runnable() { |
137 |
public void |
138 |
run() { for(MidiDeviceListener l : listeners) l.settingsChanged(e); } |
139 |
}); |
140 |
} |
141 |
} |