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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (show annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 5 months ago) by iliev
File size: 12165 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2011 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 net.sf.juife.event.GenericEvent;
28 import net.sf.juife.event.GenericListener;
29
30 import org.jsampler.event.OrchestraAdapter;
31 import org.jsampler.event.OrchestraEvent;
32 import org.jsampler.event.OrchestraListener;
33
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.w3c.dom.NamedNodeMap;
37 import org.w3c.dom.Node;
38 import org.w3c.dom.NodeList;
39
40
41 /**
42 * This class provides default implementation of the <code>OrchestraModel</code> interface.
43 * @author Grigor Iliev
44 */
45 public class DefaultOrchestraModel implements OrchestraModel {
46 private String name = "";
47 private String description = "";
48
49 private final Vector<OrchestraInstrument> instruments = new Vector<OrchestraInstrument>();
50
51 private final Vector<OrchestraListener> listeners = new Vector<OrchestraListener>();
52
53 /** Creates a new instance of <code>DefaultOrchestraModel</code>. */
54 public
55 DefaultOrchestraModel() {
56 addOrchestraListener(getHandler());
57 }
58
59 /**
60 * Registers the specified listener for receiving event messages.
61 * @param l The <code>OrchestraListener</code> to register.
62 */
63 @Override
64 public void
65 addOrchestraListener(OrchestraListener l) { listeners.add(l); }
66
67 /**
68 * Removes the specified listener.
69 * @param l The <code>OrchestraListener</code> to remove.
70 */
71 @Override
72 public void
73 removeOrchestraListener(OrchestraListener l) { listeners.remove(l); }
74
75 /**
76 * Gets the name of this orchestra.
77 * @return The name of this orchestra.
78 */
79 @Override
80 public String
81 getName() { return name; }
82
83 /**
84 * Sets the name of this orchestra.
85 * @param name The new name of this orchestra.
86 */
87 @Override
88 public void
89 setName(String name) {
90 this.name = name;
91 fireNameChanged();
92 }
93
94 /**
95 * Returns the name of this orchestra.
96 * @return The name of this orchestra.
97 */
98 @Override
99 public String
100 toString() { return getName(); }
101
102 /**
103 * Gets a brief description about this orchestra.
104 * @return A brief description about this orchestra.
105 */
106 @Override
107 public String
108 getDescription() { return description; }
109
110 /**
111 * Sets a description about this orchestra.
112 * @param desc A brief description about this orchestra.
113 */
114 @Override
115 public void
116 setDescription(String desc) {
117 description = desc;
118 fireDescriptionChanged();
119 }
120
121 /**
122 * Gets the current number of instruments in this orchestra.
123 * @return The current number of instruments in this orchestra.
124 */
125 @Override
126 public int
127 getInstrumentCount() { return instruments.size(); }
128
129 /**
130 * Gets the instrument at the specified position.
131 * @param idx The index of the instrument to be returned.
132 * @return The instrument at the specified position.
133 */
134 @Override
135 public OrchestraInstrument
136 getInstrument(int idx) { return instruments.get(idx); }
137
138 /**
139 * Adds the specified instrument to this orchestra.
140 * @param instr The instrument to be added.
141 * @throws IllegalArgumentException If <code>instr</code> is <code>null</code>.
142 */
143 @Override
144 public void
145 addInstrument(OrchestraInstrument instr) {
146 insertInstrument(instr, getInstrumentCount());
147 }
148
149 /**
150 * Inserts the specified instrument at the specified position.
151 * @param instr The instrument to be inserted.
152 * @param idx The position of the instrument.
153 * @throws IllegalArgumentException If <code>instr</code> is <code>null</code>.
154 * @throws ArrayIndexOutOfBoundsException If the specified index is invalid.
155 */
156 @Override
157 public void
158 insertInstrument(OrchestraInstrument instr, int idx) {
159 if(instr == null) throw new IllegalArgumentException("instr should be non-null!");
160 instruments.insertElementAt(instr, idx);
161 fireInstrumentAdded(instr);
162 }
163
164 /**
165 * Removes the specified instrument from this orchestra.
166 * @param idx The index of the instrument to remove.
167 */
168 @Override
169 public void
170 removeInstrument(int idx) {
171 OrchestraInstrument instr = instruments.get(idx);
172 instruments.removeElementAt(idx);
173 fireInstrumentRemoved(instr);
174 }
175
176 /**
177 * Removes the specified instrument from this orchestra.
178 * @param instr The instrument to remove.
179 * @return <code>true</code> if the specified instrument was in this orchestra,
180 * <code>false</code> otherwise.
181 */
182 @Override
183 public boolean
184 removeInstrument(OrchestraInstrument instr) {
185 boolean b = instruments.removeElement(instr);
186 if(b) fireInstrumentRemoved(instr);
187 return b;
188 }
189
190 /**
191 * Gets the position of the specified instrument in this orchestra.
192 * @param instr The instrument whose index should be returned.
193 * @return The position of the specified instrument in this orchestra,
194 * and -1 if <code>instr</code> is <code>null</code> or
195 * the orchestra does not contain the specified instrument.
196 */
197 @Override
198 public int
199 getInstrumentIndex(OrchestraInstrument instr) {
200 if(instr == null) return -1;
201
202 for(int i = 0; i < getInstrumentCount(); i++) {
203 if(getInstrument(i) == instr) return i;
204 }
205
206 return -1;
207 }
208
209 /**
210 * Moves the specified instrument one the top of the instrument list.
211 * This method does nothing if <code>instr</code> is <code>null</code>,
212 * the orchestra does not contain the specified instrument,
213 * or if the instrument is already on the top.
214 * @param instr The instrument to move on top.
215 */
216 @Override
217 public void
218 moveInstrumentOnTop(OrchestraInstrument instr) {
219 if(instr == null) return;
220
221 int idx = getInstrumentIndex(instr);
222 if(idx <= 0) return;
223
224 removeInstrument(idx);
225 insertInstrument(instr, 0);
226 }
227
228 /**
229 * Moves the specified instrument one position up in the instrument list.
230 * This method does nothing if <code>instr</code> is <code>null</code>,
231 * the orchestra does not contain the specified instrument,
232 * or if the instrument is already on the top.
233 * @param instr The instrument to move up.
234 */
235 @Override
236 public void
237 moveInstrumentUp(OrchestraInstrument instr) {
238 if(instr == null) return;
239
240 int idx = getInstrumentIndex(instr);
241 if(idx <= 0) return;
242
243 removeInstrument(idx);
244 insertInstrument(instr, idx - 1);
245
246 }
247
248 /**
249 * Moves the specified instrument one position down in the instrument list.
250 * This method does nothing if <code>instr</code> is <code>null</code>,
251 * the orchestra does not contain the specified instrument,
252 * or if the instrument is already at the bottom.
253 * @param instr The instrument to move down.
254 */
255 @Override
256 public void
257 moveInstrumentDown(OrchestraInstrument instr) {
258 if(instr == null) return;
259
260 int idx = getInstrumentIndex(instr);
261 if(idx < 0 || idx == getInstrumentCount() - 1) return;
262 removeInstrument(idx);
263 insertInstrument(instr, idx + 1);
264 }
265
266 /**
267 * Moves the specified instrument at the bottom of the instrument list.
268 * This method does nothing if <code>instr</code> is <code>null</code>,
269 * the orchestra does not contain the specified instrument,
270 * or if the instrument is already at the bottom.
271 * @param instr The instrument to move at bottom.
272 */
273 @Override
274 public void
275 moveInstrumentAtBottom(OrchestraInstrument instr) {
276 if(instr == null) return;
277
278 int idx = getInstrumentIndex(instr);
279 if(idx < 0 || idx == getInstrumentCount() - 1) return;
280
281 removeInstrument(idx);
282 insertInstrument(instr, getInstrumentCount());
283 }
284
285 /**
286 * Reads and sets the content of this orchestra provided by <code>node</code>.
287 * @param node The node providing the content of this orchestra.
288 * @throws IllegalArgumentException If an error occurs while
289 * reading the content of this orchestra.
290 */
291 @Override
292 public void
293 readObject(Node node) {
294 if(
295 node.getNodeType() != Node.ELEMENT_NODE ||
296 !(node.getNodeName().equals("orchestra"))
297 ) {
298 throw new IllegalArgumentException("Not an orchestra node!");
299 }
300
301 NamedNodeMap nnm = node.getAttributes();
302 Node n = nnm.getNamedItem("name");
303 if(n == null) {
304 throw new IllegalArgumentException("The orchestra name is undefined!");
305 }
306 DOMUtils.validateTextContent(n);
307 setName(n.getFirstChild().getNodeValue());
308
309
310 String s = null;
311 NodeList nl = node.getChildNodes();
312
313 for(int i = 0; i < nl.getLength(); i++) {
314 node = nl.item(i);
315 if(node.getNodeType() != Node.ELEMENT_NODE) continue;
316
317 s = node.getNodeName();
318 if(s.equals("description")) {
319 if(node.hasChildNodes()) {
320 DOMUtils.validateTextContent(node);
321 setDescription(node.getFirstChild().getNodeValue());
322 }
323 } else if(s.equals("instrument")) {
324 OrchestraInstrument instr = new OrchestraInstrument();
325 instr.readObject(node);
326 addInstrument(instr);
327 } else { // Unknown content
328 CC.getLogger().info ("Unknown field: " + s);
329 }
330 }
331 }
332
333 /**
334 * Writes the content of this orchestra to the
335 * specified node of document <code>doc</code>.
336 * @param doc The document containing <code>node</code>.
337 * @param node Specifies the node where the content of this orchestra
338 * should be written.
339 */
340 @Override
341 public void
342 writeObject(Document doc, Node node) {
343 Element el = doc.createElement("orchestra");
344 el.setAttribute("name", getName());
345 node.appendChild(el);
346
347 node = el;
348
349 el = doc.createElement("description");
350 el.appendChild(doc.createTextNode(getDescription()));
351 node.appendChild(el);
352
353 for(int i = 0; i < getInstrumentCount(); i++) {
354 getInstrument(i).writeObject(doc, node);
355 }
356 }
357
358 /** Notifies listeners that the name of the orchestra has changed. */
359 private void
360 fireNameChanged() {
361 OrchestraEvent e = new OrchestraEvent(this);
362 for(OrchestraListener l : listeners) l.nameChanged(e);
363 }
364
365 /** Notifies listeners that the orchestra's description has changed. */
366 private void
367 fireDescriptionChanged() {
368 OrchestraEvent e = new OrchestraEvent(this);
369 for(OrchestraListener l : listeners) l.descriptionChanged(e);
370 }
371
372 /** Notifies listeners that an instrument has been added to this orchestra. */
373 private void
374 fireInstrumentAdded(OrchestraInstrument instr) {
375 OrchestraEvent e = new OrchestraEvent(this, instr);
376 for(OrchestraListener l : listeners) l.instrumentAdded(e);
377 }
378
379 /** Notifies listeners that an instrument has been removed from this orchestra. */
380 private void
381 fireInstrumentRemoved(OrchestraInstrument instr) {
382 OrchestraEvent e = new OrchestraEvent(this, instr);
383 for(OrchestraListener l : listeners) l.instrumentRemoved(e);
384 }
385
386 /**
387 * Notifies listeners that the settings of the specified instrument has changed.
388 * @param instr The instrument whose settings has been changed.
389 */
390 private void
391 fireInstrumentChanged(OrchestraInstrument instr) {
392 OrchestraEvent e = new OrchestraEvent(this, instr);
393 for(OrchestraListener l : listeners) l.instrumentChanged(e);
394 }
395
396 private final Handler eventHandler = new Handler();
397
398 private Handler
399 getHandler() { return eventHandler; }
400
401 private class Handler extends OrchestraAdapter implements GenericListener {
402 /** Invoked when the settings of an instrument are changed. */
403 public void
404 jobDone(GenericEvent e) {
405 fireInstrumentChanged((OrchestraInstrument)e.getSource());
406 }
407
408 /** Invoked when an instrument is added to the orchestra. */
409 public void
410 instrumentAdded(OrchestraEvent e) {
411 e.getInstrument().addChangeListener(getHandler());
412
413 }
414
415 /** Invoked when an instrument is removed from the orchestra. */
416 public void
417 instrumentRemoved(OrchestraEvent e) {
418 e.getInstrument().removeChangeListener(getHandler());
419 }
420 }
421 }

  ViewVC Help
Powered by ViewVC