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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 912 - (show annotations) (download)
Mon Aug 7 18:34:40 2006 UTC (17 years, 8 months ago) by iliev
File size: 8025 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.w3c.dom.Document;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.NamedNodeMap;
33 import org.w3c.dom.Node;
34 import org.w3c.dom.NodeList;
35
36
37 /**
38 *
39 * @author Grigor Iliev
40 */
41 public class Instrument {
42 private String name = "Untitled";
43 private String description = "";
44 private String path = null;
45 private int instrumentIndex = 0;
46
47 private final Vector<ChangeListener> listeners = new Vector<ChangeListener>();
48
49
50 /** Creates a new instance of <code>Instrument</code>. */
51 public Instrument() {
52 }
53
54 /**
55 * Registers the specified listener to be notified when the instrument settings are changed.
56 * @param l The <code>OrchestraListener</code> to register.
57 */
58 public void
59 addChangeListener(ChangeListener l) { listeners.add(l); }
60
61 /**
62 * Removes the specified listener.
63 * @param l The <code>OrchestraListener</code> to remove.
64 */
65 public void
66 removeChangeListener(ChangeListener l) { listeners.remove(l); }
67
68 /**
69 * Gets the name of this instrument.
70 * @return The name of this instrument.
71 */
72 public String
73 getName() { return name; }
74
75 /**
76 * Sets the name of this instrument.
77 * @param name The new name of this instrument.
78 */
79 public void
80 setName(String name) {
81 this.name = name;
82 fireChangeEvent();
83 }
84
85 /**
86 * Gets a brief description about this instrument.
87 * @return A brief description about this instrument.
88 */
89 public String
90 getDescription() { return description; }
91
92 /**
93 * Sets a description about this instrument.
94 * @param desc A brief description about this instrument.
95 */
96 public void
97 setDescription(String desc) {
98 description = desc;
99 fireChangeEvent();
100 }
101
102 /**
103 * Returns the absolute pathname of the instrument location.
104 * @return The absolute pathname of the instrument location.
105 */
106 public String
107 getPath() { return path; }
108
109 /**
110 * Sets the absolute pathname of the instrument location.
111 * @param path Specifies the absolute pathname of the instrument location.
112 */
113 public void
114 setPath(String path) {
115 this.path = path;
116 fireChangeEvent();
117 }
118
119 /**
120 * Returns the index of the instrument in the instrument file.
121 * @return The index of the instrument in the instrument file.
122 */
123 public int
124 getInstrumentIndex() { return instrumentIndex; }
125
126 /**
127 * Sets the index of the instrument in the instrument file.
128 * @param idx The index of the instrument in the instrument file.
129 */
130 public void
131 setInstrumentIndex(int idx) {
132 instrumentIndex = idx;
133 fireChangeEvent();
134 }
135
136 /**
137 * Returns the name of this instrument.
138 * @return The name of this instrument.
139 */
140 public String
141 toString() { return getName(); }
142
143 private final StringBuffer sb = new StringBuffer();
144
145 /**
146 * Gets a string representation of this
147 * instrument appropriate for Drag & Drop operations.
148 * @return A string representation of this
149 * instrument appropriate for Drag & Drop operations.
150 * @see #isDnDString
151 */
152 public String
153 getDnDString() {
154 sb.setLength(0);
155 sb.append("[Instrument Definition]\n");
156 sb.append(getName()).append("\n");
157 sb.append("\n");
158 sb.append(getDescription()).append("\n");
159 sb.append(getPath()).append("\n");
160 sb.append(getInstrumentIndex()).append("\n");
161
162 return sb.toString();
163 }
164
165 /**
166 * Sets the instrument properties provided by the specified
167 * Drag & Drop string representation.
168 * @param s String providing Drag & Drop string representation of an instrument.
169 * @throws IllegalArgumentException If the specified string is not
170 * a Drag & Drop string representation of an instrument.
171 * @see #getDnDString
172 */
173 public void
174 setDnDString(String s) {
175 if(!isDnDString(s)) throw new IllegalArgumentException("Not a DnD string");
176
177 String[] args = s.split("\n");
178 if(args.length < 6) throw new IllegalArgumentException("Not a DnD string");
179
180 setName(args[1]);
181 setDescription(args[3]);
182 setPath(args[4]);
183
184 try { setInstrumentIndex(Integer.parseInt(args[5])); }
185 catch(Exception x) {
186 throw new IllegalArgumentException("Not a DnD string", x);
187 }
188 }
189
190 /**
191 * Determines whether the specified string is
192 * a Drag & Drop representation of an instrument.
193 * @param s The string to be checked.
194 * @return <code>true</code> if the specified string is
195 * a Drag & Drop representation of an instrument, <code>false</code> otherwise.
196 */
197 public static boolean
198 isDnDString(String s) {
199 if(s == null) return false;
200 return s.startsWith("[Instrument Definition]\n");
201 }
202
203 /** Notifies listeners that the instrument settings has changed. */
204 private void
205 fireChangeEvent() {
206 ChangeEvent e = new ChangeEvent(this);
207 for(ChangeListener l : listeners) l.stateChanged(e);
208 }
209
210 /**
211 * Reads and sets the instrument properties by the supplied <code>node</code>.
212 * @param node The node providing the instrument properties.
213 * @throws IllegalArgumentException If an error occurs while
214 * reading the instrument properties.
215 */
216 public void
217 readObject(Node node) {
218 if(
219 node.getNodeType() != Node.ELEMENT_NODE ||
220 !(node.getNodeName().equals("instrument"))
221 ) {
222 throw new IllegalArgumentException("Not an instrument node!");
223 }
224
225 NamedNodeMap nnm = node.getAttributes();
226 Node n = nnm.getNamedItem("name");
227 if(n == null) {
228 throw new IllegalArgumentException("The instrument name is undefined!");
229 }
230 DOMUtils.validateTextContent(n);
231 setName(n.getFirstChild().getNodeValue());
232
233 String s = null;
234 NodeList nl = node.getChildNodes();
235
236 for(int i = 0; i < nl.getLength(); i++) {
237 node = nl.item(i);
238 if(node.getNodeType() != Node.ELEMENT_NODE) continue;
239
240 s = node.getNodeName();
241 if(s.equals("description")) {
242 if(node.hasChildNodes()) {
243 DOMUtils.validateTextContent(node);
244 setDescription(node.getFirstChild().getNodeValue());
245 }
246 } else if(s.equals("path")) {
247 DOMUtils.validateTextContent(node);
248 setPath(node.getFirstChild().getNodeValue());
249 } else if(s.equals("instrument-index")) {
250 DOMUtils.validateTextContent(node);
251 try {
252 int j;
253 j = Integer.parseInt(node.getFirstChild().getNodeValue());
254 setInstrumentIndex(j);
255 } catch(NumberFormatException x) {
256 throw new IllegalArgumentException("Not a number");
257 }
258 } else { // Unknown content
259 CC.getLogger().info ("Unknown field: " + s);
260 }
261 }
262 }
263
264 /**
265 * Writes the instrument properties to the
266 * specified node of document <code>doc</code>.
267 * @param doc The document containing <code>node</code>.
268 * @param node Specifies the node where the instrument properties
269 * should be written.
270 */
271 public void
272 writeObject(Document doc, Node node) {
273 Element el = doc.createElement("instrument");
274 el.setAttribute("name", getName());
275 node.appendChild(el);
276
277 node = el;
278
279 el = doc.createElement("description");
280 el.appendChild(doc.createTextNode(getDescription()));
281 node.appendChild(el);
282
283 el = doc.createElement("path");
284 el.appendChild(doc.createTextNode(getPath()));
285 node.appendChild(el);
286
287 el = doc.createElement("instrument-index");
288 el.appendChild(doc.createTextNode(String.valueOf(getInstrumentIndex())));
289 node.appendChild(el);
290 }
291 }

  ViewVC Help
Powered by ViewVC