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

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

  ViewVC Help
Powered by ViewVC