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) { this.engine = engine; } |
93 |
|
94 |
public String |
95 |
getFormatFamily() { return null; } |
96 |
|
97 |
public String |
98 |
getFormatVersion() { return null; } |
99 |
|
100 |
public Integer[] |
101 |
getKeyMapping() { return null; } |
102 |
|
103 |
public Integer[] |
104 |
getKeyswitchMapping() { return null; } |
105 |
|
106 |
/** |
107 |
* Returns the name of this instrument. |
108 |
* @return The name of this instrument. |
109 |
*/ |
110 |
public String |
111 |
toString() { return getName(); } |
112 |
|
113 |
private final StringBuffer sb = new StringBuffer(); |
114 |
|
115 |
/** |
116 |
* Gets a string representation of this |
117 |
* instrument appropriate for Drag & Drop operations. |
118 |
* @return A string representation of this |
119 |
* instrument appropriate for Drag & Drop operations. |
120 |
* @see #isDnDString |
121 |
*/ |
122 |
public String |
123 |
getDnDString() { |
124 |
sb.setLength(0); |
125 |
sb.append("[Instrument Definition]\n"); |
126 |
sb.append(getName()).append("\n"); |
127 |
sb.append("\n"); |
128 |
sb.append(getDescription()).append("\n"); |
129 |
sb.append(getFilePath()).append("\n"); |
130 |
sb.append(getInstrumentIndex()).append("\n"); |
131 |
|
132 |
return sb.toString(); |
133 |
} |
134 |
|
135 |
/** |
136 |
* Sets the instrument properties provided by the specified |
137 |
* Drag & Drop string representation. |
138 |
* @param s String providing Drag & Drop string representation of an instrument. |
139 |
* @throws IllegalArgumentException If the specified string is not |
140 |
* a Drag & Drop string representation of an instrument. |
141 |
* @see #getDnDString |
142 |
*/ |
143 |
public void |
144 |
setDnDString(String s) { |
145 |
if(!isDnDString(s)) throw new IllegalArgumentException("Not a DnD string"); |
146 |
|
147 |
String[] args = s.split("\n"); |
148 |
if(args.length < 6) throw new IllegalArgumentException("Not a DnD string"); |
149 |
|
150 |
setName(args[1]); |
151 |
setDescription(args[3]); |
152 |
setFilePath(args[4]); |
153 |
|
154 |
try { setInstrumentIndex(Integer.parseInt(args[5])); } |
155 |
catch(Exception x) { |
156 |
throw new IllegalArgumentException("Not a DnD string", x); |
157 |
} |
158 |
} |
159 |
|
160 |
/** |
161 |
* Determines whether the specified string is |
162 |
* a Drag & Drop representation of an instrument. |
163 |
* @param s The string to be checked. |
164 |
* @return <code>true</code> if the specified string is |
165 |
* a Drag & Drop representation of an instrument, <code>false</code> otherwise. |
166 |
*/ |
167 |
public static boolean |
168 |
isDnDString(String s) { |
169 |
if(s == null) return false; |
170 |
return s.startsWith("[Instrument Definition]\n"); |
171 |
} |
172 |
|
173 |
/** |
174 |
* Reads and sets the instrument properties by the supplied <code>node</code>. |
175 |
* @param node The node providing the instrument properties. |
176 |
* @throws IllegalArgumentException If an error occurs while |
177 |
* reading the instrument properties. |
178 |
*/ |
179 |
public void |
180 |
readObject(Node node) { |
181 |
if( |
182 |
node.getNodeType() != Node.ELEMENT_NODE || |
183 |
!(node.getNodeName().equals("instrument")) |
184 |
) { |
185 |
throw new IllegalArgumentException("Not an instrument node!"); |
186 |
} |
187 |
|
188 |
NamedNodeMap nnm = node.getAttributes(); |
189 |
Node n = nnm.getNamedItem("name"); |
190 |
if(n == null) { |
191 |
throw new IllegalArgumentException("The instrument name is undefined!"); |
192 |
} |
193 |
DOMUtils.validateTextContent(n); |
194 |
setName(n.getFirstChild().getNodeValue()); |
195 |
|
196 |
String s = null; |
197 |
NodeList nl = node.getChildNodes(); |
198 |
|
199 |
for(int i = 0; i < nl.getLength(); i++) { |
200 |
node = nl.item(i); |
201 |
if(node.getNodeType() != Node.ELEMENT_NODE) continue; |
202 |
|
203 |
s = node.getNodeName(); |
204 |
if(s.equals("description")) { |
205 |
if(node.hasChildNodes()) { |
206 |
DOMUtils.validateTextContent(node); |
207 |
setDescription(node.getFirstChild().getNodeValue()); |
208 |
} |
209 |
} else if(s.equals("path")) { |
210 |
DOMUtils.validateTextContent(node); |
211 |
setFilePath(node.getFirstChild().getNodeValue()); |
212 |
} else if(s.equals("instrument-index")) { |
213 |
DOMUtils.validateTextContent(node); |
214 |
try { |
215 |
int j; |
216 |
j = Integer.parseInt(node.getFirstChild().getNodeValue()); |
217 |
setInstrumentIndex(j); |
218 |
} catch(NumberFormatException x) { |
219 |
throw new IllegalArgumentException("Not a number"); |
220 |
} |
221 |
} else { // Unknown content |
222 |
CC.getLogger().info ("Unknown field: " + s); |
223 |
} |
224 |
} |
225 |
} |
226 |
|
227 |
/** |
228 |
* Writes the instrument properties to the |
229 |
* specified node of document <code>doc</code>. |
230 |
* @param doc The document containing <code>node</code>. |
231 |
* @param node Specifies the node where the instrument properties |
232 |
* should be written. |
233 |
*/ |
234 |
public void |
235 |
writeObject(Document doc, Node node) { |
236 |
Element el = doc.createElement("instrument"); |
237 |
el.setAttribute("name", getName()); |
238 |
node.appendChild(el); |
239 |
|
240 |
node = el; |
241 |
|
242 |
el = doc.createElement("description"); |
243 |
el.appendChild(doc.createTextNode(getDescription())); |
244 |
node.appendChild(el); |
245 |
|
246 |
el = doc.createElement("path"); |
247 |
el.appendChild(doc.createTextNode(getFilePath())); |
248 |
node.appendChild(el); |
249 |
|
250 |
el = doc.createElement("instrument-index"); |
251 |
el.appendChild(doc.createTextNode(String.valueOf(getInstrumentIndex()))); |
252 |
node.appendChild(el); |
253 |
} |
254 |
} |