/[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 912 - (show annotations) (download)
Mon Aug 7 18:34:40 2006 UTC (17 years, 7 months ago) by iliev
File size: 11766 byte(s)
* updating to JSampler 0.3a

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

  ViewVC Help
Powered by ViewVC