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