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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1688 - (show annotations) (download)
Thu Feb 14 16:52:36 2008 UTC (16 years, 2 months ago) by iliev
File size: 7189 byte(s)
* Implemented a backend list with option to manually choose a backend
  to connect on startup(Edit/Preferences, then click the `Backend' tab)
  and option to change the backend without restarting JSampler
  (Actions/Change Backend or Ctrl + B)

* Added confirmation messages for removing sampler channels and
  audio/MIDI devices (Edit/Preferences, then click the `View' tab)

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2007 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 javax.swing.event.ChangeEvent;
28 import javax.swing.event.ChangeListener;
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 *
38 * @author Grigor Iliev
39 */
40 public class ServerList {
41 private final Vector<Server> servers = new Vector<Server>();
42 private final Vector<ChangeListener> listeners = new Vector<ChangeListener>();
43
44 private final ChangeListener l = new ChangeListener() {
45 public void
46 stateChanged(ChangeEvent e) { fireChangeEvent(); }
47 };
48
49 /**
50 * Creates a new instance of <code>ServerList</code>
51 */
52 public
53 ServerList() {
54
55 }
56
57 /**
58 * Registers the specified listener to be notified when the server list is changed.
59 * @param l The <code>ChangeListener</code> to register.
60 */
61 public void
62 addChangeListener(ChangeListener l) { listeners.add(l); }
63
64 /**
65 * Removes the specified listener.
66 * @param l The <code>ChangeListener</code> to remove.
67 */
68 public void
69 removeChangeListener(ChangeListener l) { listeners.remove(l); }
70
71 /**
72 * Adds the specified server to the server list.
73 */
74 public void
75 addServer(Server server) {
76 servers.add(server);
77 server.addChangeListener(l);
78 fireChangeEvent();
79 }
80
81 /**
82 * Gets the current number of servers in the list.
83 * @return The current number of servers in the list.
84 */
85 public int
86 getServerCount() { return servers.size(); }
87
88 /**
89 * Gets the server at the specified position.
90 * @param idx The index of the server to be returned.
91 * @return The server at the specified position.
92 */
93 public Server
94 getServer(int idx) { return servers.get(idx); }
95
96 /**
97 * Removes the specified server from the list.
98 * @param idx The index of the server to remove.
99 */
100 public void
101 removeServer(int idx) {
102 Server server = servers.get(idx);
103 servers.removeElementAt(idx);
104 server.removeChangeListener(l);
105 fireChangeEvent();
106 }
107
108 /**
109 * Removes the specified server from the list.
110 * @param server The server to remove.
111 * @return <code>true</code> if the list contained the specified server.
112 */
113 public boolean
114 removeServer(Server server) {
115 boolean b = servers.remove(server);
116 fireChangeEvent();
117 return b;
118 }
119
120 /**
121 * Gets the position of the specified server in this server list.
122 * @param server The server whose index should be returned.
123 * @return The position of the specified server in this server list,
124 * and -1 if <code>server</code> is <code>null</code> or
125 * the server list does not contain the specified server.
126 */
127 public int
128 getServerIndex(Server server) {
129 if(server == null) return -1;
130
131 for(int i = 0; i < getServerCount(); i++) {
132 if(getServer(i) == server) return i;
133 }
134
135 return -1;
136 }
137
138 /**
139 * Moves the specified server one the top of the server list.
140 * This method does nothing if <code>server</code> is <code>null</code>,
141 * the server list does not contain the specified server,
142 * or if the server is already on the top.
143 * @param server The server to move on top.
144 */
145 public void
146 moveServerOnTop(Server server) {
147 if(server == null) return;
148
149 int idx = getServerIndex(server);
150 if(idx <= 0) return;
151
152 removeServer(idx);
153 servers.insertElementAt(server, 0);
154 fireChangeEvent();
155 }
156
157 /**
158 * Moves the specified server one position up in the server list.
159 * This method does nothing if <code>server</code> is <code>null</code>,
160 * the server list does not contain the specified server,
161 * or if the server is already on the top.
162 * @param server The server to move up.
163 */
164 public void
165 moveServerUp(Server server) {
166 if(server == null) return;
167
168 int idx = getServerIndex(server);
169 if(idx <= 0) return;
170
171 removeServer(idx);
172 servers.insertElementAt(server, idx - 1);
173 fireChangeEvent();
174 }
175
176 /**
177 * Moves the specified server one position down in the server list.
178 * This method does nothing if <code>server</code> is <code>null</code>,
179 * the server list does not contain the specified server,
180 * or if the server is already at the bottom.
181 * @param server The server to move down.
182 */
183 public void
184 moveServerDown(Server server) {
185 if(server == null) return;
186
187 int idx = getServerIndex(server);
188 if(idx < 0 || idx == getServerCount() - 1) return;
189 removeServer(idx);
190 servers.insertElementAt(server, idx + 1);
191 fireChangeEvent();
192 }
193
194 /**
195 * Moves the specified server at the bottom of the server list.
196 * This method does nothing if <code>server</code> is <code>null</code>,
197 * the server list does not contain the specified server,
198 * or if the server is already at the bottom.
199 * @param server The server to move at bottom.
200 */
201 public void
202 moveServerAtBottom(Server server) {
203 if(server == null) return;
204
205 int idx = getServerIndex(server);
206 if(idx < 0 || idx == getServerCount() - 1) return;
207
208 removeServer(idx);
209 servers.insertElementAt(server, getServerCount());
210 fireChangeEvent();
211 }
212
213 /** Notifies listeners that the server list has changed. */
214 protected void
215 fireChangeEvent() {
216 ChangeEvent e = new ChangeEvent(this);
217 for(ChangeListener l : listeners) l.stateChanged(e);
218 }
219
220 /**
221 * Reads and loads the content provided by <code>node</code> to this server list.
222 * @param node The node providing the content of this server list.
223 * @throws IllegalArgumentException If an error occurs while
224 * reading the content of this server list.
225 */
226 public void
227 readObject(Node node) {
228 if(
229 node.getNodeType() != Node.ELEMENT_NODE ||
230 !(node.getNodeName().equals("servers"))
231 ) {
232 throw new IllegalArgumentException("Not a server list node!");
233 }
234
235 NodeList nl = node.getChildNodes();
236
237 for(int i = 0; i < nl.getLength(); i++) {
238 node = nl.item(i);
239 if(node.getNodeType() != Node.ELEMENT_NODE) continue;
240
241 Server s = new Server();
242 s.readObject(node);
243 addServer(s);
244 }
245 }
246
247 /**
248 * Writes the content of this server list to the
249 * specified node of document <code>doc</code>.
250 * @param doc The document containing <code>node</code>.
251 * @param node Specifies the node where the content of this server
252 * list should be written.
253 */
254 public void
255 writeObject(Document doc, Node node) {
256 Element el = doc.createElement("servers");
257 node.appendChild(el);
258
259 node = el;
260
261 for(int i = 0; i < getServerCount(); i++) {
262 getServer(i).writeObject(doc, node);
263 }
264 }
265 }

  ViewVC Help
Powered by ViewVC