/[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 1143 - (show annotations) (download)
Mon Apr 2 21:18:31 2007 UTC (17 years ago) by iliev
File size: 8975 byte(s)
* upgrading to version 0.4a

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

  ViewVC Help
Powered by ViewVC