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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2290 - (show annotations) (download)
Thu Nov 24 14:30:58 2011 UTC (12 years, 4 months ago) by iliev
File size: 7447 byte(s)
* fixed bug #156

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

  ViewVC Help
Powered by ViewVC