1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005, 2006 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 org.jsampler.event.OrchestraListListener; |
28 |
import org.jsampler.event.OrchestraListEvent; |
29 |
|
30 |
import org.w3c.dom.Document; |
31 |
import org.w3c.dom.Element; |
32 |
import org.w3c.dom.Node; |
33 |
import org.w3c.dom.NodeList; |
34 |
|
35 |
|
36 |
/** |
37 |
* This class provides default implementation of the <code>OrchestraListModel</code> interface. |
38 |
* @author Grigor Iliev |
39 |
*/ |
40 |
public class DefaultOrchestraListModel implements OrchestraListModel { |
41 |
private final Vector<OrchestraModel> orchestras = new Vector<OrchestraModel>(); |
42 |
private final Vector<OrchestraListListener> listeners = new Vector<OrchestraListListener>(); |
43 |
|
44 |
|
45 |
/** Creates a new instance of <code>DefaultOrchestraListModel</code>. */ |
46 |
public |
47 |
DefaultOrchestraListModel() { |
48 |
|
49 |
} |
50 |
|
51 |
/** |
52 |
* Registers the specified listener for receiving event messages. |
53 |
* @param l The <code>OrchestraListListener</code> to register. |
54 |
*/ |
55 |
public void |
56 |
addOrchestraListListener(OrchestraListListener l) { listeners.add(l); } |
57 |
|
58 |
/** |
59 |
* Removes the specified listener. |
60 |
* @param l The <code>OrchestraListListener</code> to remove. |
61 |
*/ |
62 |
public void |
63 |
removeOrchestraListListener(OrchestraListListener l) { listeners.remove(l); } |
64 |
|
65 |
/** |
66 |
* Gets the current number of orchestras in the list. |
67 |
* @return The current number of orchestras in the list. |
68 |
*/ |
69 |
public int |
70 |
getOrchestraCount() { return orchestras.size(); } |
71 |
|
72 |
/** |
73 |
* Gets the orchestra at the specified position. |
74 |
* @param idx The index of the orchestra to be returned. |
75 |
* @return The orchestra at the specified position. |
76 |
*/ |
77 |
public OrchestraModel |
78 |
getOrchestra(int idx) { return orchestras.get(idx); } |
79 |
|
80 |
/** |
81 |
* Adds the specified orchestra to the list. |
82 |
* @param orchestra The model of the orchestra to be added. |
83 |
* @throws IllegalArgumentException If <code>orchestra</code> is <code>null</code>. |
84 |
*/ |
85 |
public void |
86 |
addOrchestra(OrchestraModel orchestra) { |
87 |
insertOrchestra(orchestra, getOrchestraCount()); |
88 |
} |
89 |
|
90 |
/** |
91 |
* Inserts the specified orchestra at the specified position. |
92 |
* @param orchestra The orchestra to be inserted. |
93 |
* @param idx The position of the orchestra. |
94 |
* @throws IllegalArgumentException If <code>orchestra</code> is <code>null</code>. |
95 |
* @throws ArrayIndexOutOfBoundsException If the specified index is invalid. |
96 |
*/ |
97 |
public void |
98 |
insertOrchestra(OrchestraModel orchestra, int idx) { |
99 |
if(orchestra == null) |
100 |
throw new IllegalArgumentException("orchestra should be non-null!"); |
101 |
|
102 |
orchestras.insertElementAt(orchestra, idx); |
103 |
fireOrchestraAdded(orchestra); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Removes the specified orchestra from the list. |
108 |
* @param idx The index of the orchestra to remove. |
109 |
*/ |
110 |
public void |
111 |
removeOrchestra(int idx) { |
112 |
OrchestraModel orchestraModel = orchestras.get(idx); |
113 |
orchestras.removeElementAt(idx); |
114 |
fireOrchestraRemoved(orchestraModel); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Removes the specified orchestra from the list. |
119 |
* @param orchestraModel The model of the orchestra to remove. |
120 |
* @return <code>true</code> if the specified orchestra was in the list, |
121 |
* <code>false</code> otherwise. |
122 |
*/ |
123 |
public boolean |
124 |
removeOrchestra(OrchestraModel orchestraModel) { |
125 |
boolean b = orchestras.removeElement(orchestraModel); |
126 |
if(b) fireOrchestraRemoved(orchestraModel); |
127 |
return b; |
128 |
} |
129 |
|
130 |
/** |
131 |
* Gets the position of the specified orchestra in this orchestra list. |
132 |
* @param orchestra The orchestra whose index should be returned. |
133 |
* @return The position of the specified orchestra in this orchestra list, |
134 |
* and -1 if <code>orchestra</code> is <code>null</code> or |
135 |
* the orchestra list does not contain the specified orchestra. |
136 |
*/ |
137 |
public int |
138 |
getOrchestraIndex(OrchestraModel orchestra) { |
139 |
if(orchestra == null) return -1; |
140 |
|
141 |
for(int i = 0; i < getOrchestraCount(); i++) { |
142 |
if(getOrchestra(i) == orchestra) return i; |
143 |
} |
144 |
|
145 |
return -1; |
146 |
} |
147 |
|
148 |
/** |
149 |
* Moves the specified orchestra one the top of the orchestra list. |
150 |
* This method does nothing if <code>orchestra</code> is <code>null</code>, |
151 |
* the orchestra list does not contain the specified orchestra, |
152 |
* or if the orchestra is already on the top. |
153 |
* @param orchestra The orchestra to move on top. |
154 |
*/ |
155 |
public void |
156 |
moveOrchestraOnTop(OrchestraModel orchestra) { |
157 |
if(orchestra == null) return; |
158 |
|
159 |
int idx = getOrchestraIndex(orchestra); |
160 |
if(idx <= 0) return; |
161 |
|
162 |
removeOrchestra(idx); |
163 |
insertOrchestra(orchestra, 0); |
164 |
} |
165 |
|
166 |
/** |
167 |
* Moves the specified orchestra one position up in the orchestra list. |
168 |
* This method does nothing if <code>orchestra</code> is <code>null</code>, |
169 |
* the orchestra list does not contain the specified orchestra, |
170 |
* or if the orchestra is already on the top. |
171 |
* @param orchestra The orchestra to move up. |
172 |
*/ |
173 |
public void |
174 |
moveOrchestraUp(OrchestraModel orchestra) { |
175 |
if(orchestra == null) return; |
176 |
|
177 |
int idx = getOrchestraIndex(orchestra); |
178 |
if(idx <= 0) return; |
179 |
|
180 |
removeOrchestra(idx); |
181 |
insertOrchestra(orchestra, idx - 1); |
182 |
} |
183 |
|
184 |
/** |
185 |
* Moves the specified orchestra one position down in the orchestra list. |
186 |
* This method does nothing if <code>orchestra</code> is <code>null</code>, |
187 |
* the orchestra list does not contain the specified orchestra, |
188 |
* or if the orchestra is already at the bottom. |
189 |
* @param orchestra The orchestra to move down. |
190 |
*/ |
191 |
public void |
192 |
moveOrchestraDown(OrchestraModel orchestra) { |
193 |
if(orchestra == null) return; |
194 |
|
195 |
int idx = getOrchestraIndex(orchestra); |
196 |
if(idx < 0 || idx == getOrchestraCount() - 1) return; |
197 |
removeOrchestra(idx); |
198 |
insertOrchestra(orchestra, idx + 1); |
199 |
} |
200 |
|
201 |
/** |
202 |
* Moves the specified orchestra at the bottom of the orchestra list. |
203 |
* This method does nothing if <code>orchestra</code> is <code>null</code>, |
204 |
* the orchestra list does not contain the specified orchestra, |
205 |
* or if the orchestra is already at the bottom. |
206 |
* @param orchestra The orchestra to move at bottom. |
207 |
*/ |
208 |
public void |
209 |
moveOrchestraAtBottom(OrchestraModel orchestra) { |
210 |
if(orchestra == null) return; |
211 |
|
212 |
int idx = getOrchestraIndex(orchestra); |
213 |
if(idx < 0 || idx == getOrchestraCount() - 1) return; |
214 |
|
215 |
removeOrchestra(idx); |
216 |
insertOrchestra(orchestra, getOrchestraCount()); |
217 |
} |
218 |
|
219 |
/** |
220 |
* Reads and loads the content provided by <code>node</code> to this orchestra list. |
221 |
* @param node The node providing the content of this orchestra list. |
222 |
* @throws IllegalArgumentException If an error occurs while |
223 |
* reading the content of this orchestra list. |
224 |
*/ |
225 |
public void |
226 |
readObject(Node node) { |
227 |
if( |
228 |
node.getNodeType() != Node.ELEMENT_NODE || |
229 |
!(node.getNodeName().equals("orchestras")) |
230 |
) { |
231 |
throw new IllegalArgumentException("Not an orchestra list node!"); |
232 |
} |
233 |
|
234 |
NodeList nl = node.getChildNodes(); |
235 |
|
236 |
for(int i = 0; i < nl.getLength(); i++) { |
237 |
node = nl.item(i); |
238 |
if(node.getNodeType() != Node.ELEMENT_NODE) continue; |
239 |
|
240 |
OrchestraModel om = new DefaultOrchestraModel(); |
241 |
om.readObject(node); |
242 |
addOrchestra(om); |
243 |
} |
244 |
} |
245 |
|
246 |
/** |
247 |
* Writes the content of this orchestra list to the |
248 |
* specified node of document <code>doc</code>. |
249 |
* @param doc The document containing <code>node</code>. |
250 |
* @param node Specifies the node where the content of this orchestra |
251 |
* list should be written. |
252 |
*/ |
253 |
public void |
254 |
writeObject(Document doc, Node node) { |
255 |
Element el = doc.createElement("orchestras"); |
256 |
node.appendChild(el); |
257 |
|
258 |
node = el; |
259 |
|
260 |
for(int i = 0; i < getOrchestraCount(); i++) { |
261 |
getOrchestra(i).writeObject(doc, node); |
262 |
} |
263 |
} |
264 |
|
265 |
/** Notifies listeners that an orchestra has been added to the list. */ |
266 |
private void |
267 |
fireOrchestraAdded(OrchestraModel orchestraModel) { |
268 |
OrchestraListEvent e = new OrchestraListEvent(this, orchestraModel); |
269 |
for(OrchestraListListener l : listeners) l.orchestraAdded(e); |
270 |
} |
271 |
|
272 |
/** Notifies listeners that an orchestra has been removed from the list. */ |
273 |
private void |
274 |
fireOrchestraRemoved(OrchestraModel orchestraModel) { |
275 |
OrchestraListEvent e = new OrchestraListEvent(this, orchestraModel); |
276 |
for(OrchestraListListener l : listeners) l.orchestraRemoved(e); |
277 |
} |
278 |
} |