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.view; |
24 |
|
25 |
import java.util.Enumeration; |
26 |
import java.util.Vector; |
27 |
|
28 |
import javax.swing.tree.TreeNode; |
29 |
|
30 |
import org.linuxsampler.lscp.DbDirectoryInfo; |
31 |
import org.linuxsampler.lscp.DbInstrumentInfo; |
32 |
|
33 |
import org.linuxsampler.lscp.event.InstrumentsDbEvent; |
34 |
import org.linuxsampler.lscp.event.InstrumentsDbListener; |
35 |
|
36 |
/** |
37 |
* |
38 |
* @author Grigor Iliev |
39 |
*/ |
40 |
public class DbDirectoryTreeNode implements TreeNode { |
41 |
private DbDirectoryTreeNode parent = null; |
42 |
private DbDirectoryInfo info; |
43 |
private Vector<DbDirectoryTreeNode> dirs = new Vector<DbDirectoryTreeNode>(); |
44 |
private Vector<DbInstrumentInfo> instrs = new Vector<DbInstrumentInfo>(); |
45 |
private boolean connected = false; |
46 |
private boolean detachedNode = false; |
47 |
|
48 |
private final Vector<InstrumentsDbListener> listeners = new Vector<InstrumentsDbListener>(); |
49 |
|
50 |
|
51 |
/** Creates a new instance of <code>DbDirectoryTreeNode</code>. */ |
52 |
public |
53 |
DbDirectoryTreeNode(DbDirectoryInfo info) { |
54 |
this.info = info; |
55 |
} |
56 |
|
57 |
/** |
58 |
* Registers the specified listener for receiving event messages. |
59 |
* @param l The <code>InstrumentsDbListener</code> to register. |
60 |
*/ |
61 |
public void |
62 |
addInstrumentsDbListener(InstrumentsDbListener l) { |
63 |
listeners.add(l); |
64 |
} |
65 |
|
66 |
/** |
67 |
* Removes the specified listener. |
68 |
* @param l The <code>InstrumentsDbListener</code> to remove. |
69 |
*/ |
70 |
public void |
71 |
removeInstrumentsDbListener(InstrumentsDbListener l) { |
72 |
listeners.remove(l); |
73 |
} |
74 |
|
75 |
public DbDirectoryInfo |
76 |
getInfo() { return info; } |
77 |
|
78 |
public void |
79 |
setInfo(DbDirectoryInfo info) { this.info = info; } |
80 |
|
81 |
/** |
82 |
* Renames this node. |
83 |
* Also updates the paths of all children. |
84 |
*/ |
85 |
protected void |
86 |
setName(String newName) { |
87 |
getInfo().setName(newName); |
88 |
for(DbInstrumentInfo i : instrs) { |
89 |
i.setDirectoryPath(getInfo().getDirectoryPath()); |
90 |
} |
91 |
|
92 |
for(DbDirectoryTreeNode n : dirs) n.updateDirectoryPaths(); |
93 |
} |
94 |
|
95 |
/** |
96 |
* Invoked when the name of a parent node is changed. |
97 |
*/ |
98 |
protected void |
99 |
updateDirectoryPaths() { |
100 |
getInfo().setParentDirectoryPath(parent.getInfo().getDirectoryPath()); |
101 |
|
102 |
for(DbInstrumentInfo i : instrs) { |
103 |
i.setDirectoryPath(getInfo().getDirectoryPath()); |
104 |
} |
105 |
|
106 |
for(DbDirectoryTreeNode n : dirs) n.updateDirectoryPaths(); |
107 |
} |
108 |
|
109 |
// Tree node model methods |
110 |
public DbDirectoryTreeNode |
111 |
getChildAt(int index) { return dirs.get(index); } |
112 |
|
113 |
public int |
114 |
getChildCount() { return dirs.size(); } |
115 |
|
116 |
public DbDirectoryTreeNode |
117 |
getParent() { return parent; } |
118 |
|
119 |
public int |
120 |
getIndex(TreeNode node) { return dirs.indexOf(node); } |
121 |
|
122 |
public boolean |
123 |
getAllowsChildren() { return true; } |
124 |
|
125 |
public boolean |
126 |
isLeaf() { return false; } |
127 |
|
128 |
public Enumeration |
129 |
children() { return dirs.elements(); } |
130 |
/////// |
131 |
|
132 |
public void |
133 |
setParent(DbDirectoryTreeNode parent) { this.parent = parent; } |
134 |
|
135 |
public void |
136 |
addDirectory(DbDirectoryTreeNode child) { |
137 |
addDirectory(child, true); |
138 |
} |
139 |
|
140 |
private void |
141 |
addDirectory(DbDirectoryTreeNode child, boolean doNotify) { |
142 |
for(int i = 0; i < dirs.size(); i++) { |
143 |
String s = dirs.get(i).getInfo().toString(); |
144 |
if(s.compareToIgnoreCase(child.getInfo().toString()) < 0) continue; |
145 |
|
146 |
dirs.insertElementAt(child, i); |
147 |
child.setParent(this); |
148 |
fireDirectoryCountChanged(); |
149 |
return; |
150 |
} |
151 |
|
152 |
dirs.add(child); |
153 |
child.setParent(this); |
154 |
if(doNotify) fireDirectoryCountChanged(); |
155 |
} |
156 |
|
157 |
public void |
158 |
addDirectories(DbDirectoryTreeNode[] children) { |
159 |
if(children == null || children.length == 0) return; |
160 |
for(DbDirectoryTreeNode n : children) addDirectory(n, false); |
161 |
fireDirectoryCountChanged(); |
162 |
} |
163 |
|
164 |
public void |
165 |
removeDirectory(int index) { |
166 |
DbDirectoryTreeNode node = dirs.remove(index); |
167 |
node.setParent(null); |
168 |
fireDirectoryCountChanged(); |
169 |
} |
170 |
|
171 |
public void |
172 |
removeDirectoryByPathName(String path) { |
173 |
if(path == null) return; |
174 |
|
175 |
for(DbDirectoryTreeNode n : dirs) { |
176 |
if(path.equals(n.getInfo().getDirectoryPath())) { |
177 |
dirs.removeElement(n); |
178 |
return; |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
public void |
184 |
removeInstrumentByPathName(String path) { |
185 |
if(path == null) return; |
186 |
|
187 |
for(DbInstrumentInfo info : instrs) { |
188 |
if(path.equals(info.getInstrumentPath())) { |
189 |
instrs.removeElement(info); |
190 |
return; |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
public void |
196 |
removeAllDirectories() { |
197 |
dirs.removeAllElements(); |
198 |
fireDirectoryCountChanged(); |
199 |
} |
200 |
|
201 |
public void |
202 |
updateDirectory(DbDirectoryInfo info) { |
203 |
for(int i = 0; i < dirs.size(); i++) { |
204 |
if(dirs.get(i).getInfo().getName().equals(info.getName())) { |
205 |
dirs.get(i).setInfo(info); |
206 |
fireDirectoryInfoChanged(info.getDirectoryPath()); |
207 |
return; |
208 |
} |
209 |
} |
210 |
} |
211 |
|
212 |
public void |
213 |
addInstrument(DbInstrumentInfo info) { |
214 |
instrs.add(info); |
215 |
fireInstrumentCountChanged(); |
216 |
} |
217 |
|
218 |
public void |
219 |
addInstruments(DbInstrumentInfo[] infos) { |
220 |
if(infos == null || infos.length == 0) return; |
221 |
for(DbInstrumentInfo i : infos) instrs.add(i); |
222 |
fireInstrumentCountChanged(); |
223 |
} |
224 |
|
225 |
public void |
226 |
removeAllInstruments() { |
227 |
instrs.removeAllElements(); |
228 |
fireInstrumentCountChanged(); |
229 |
} |
230 |
|
231 |
public void |
232 |
updateInstrument(DbInstrumentInfo info) { |
233 |
for(int i = 0; i < instrs.size(); i++) { |
234 |
if(instrs.get(i).getName().equals(info.getName())) { |
235 |
instrs.setElementAt(info, i); |
236 |
fireInstrumentInfoChanged(info.getInstrumentPath()); |
237 |
return; |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
public DbInstrumentInfo |
243 |
getInstrumentAt(int index) { return instrs.get(index); } |
244 |
|
245 |
public int |
246 |
getInstrumentCount() { return instrs.size(); } |
247 |
|
248 |
public int |
249 |
getInstrumentIndex(DbInstrumentInfo instr) { return instrs.indexOf(instr); } |
250 |
|
251 |
public void |
252 |
removeInstrument(int index) { |
253 |
instrs.remove(index); |
254 |
fireInstrumentCountChanged(); |
255 |
} |
256 |
|
257 |
/** |
258 |
* Gets the instrument with the specified name. |
259 |
* @param instrName The name of the instrument to return. |
260 |
* @return The instrument with the specified name or <code>null</code> |
261 |
* if there is no instrument with the specified name in this directory. |
262 |
*/ |
263 |
public DbInstrumentInfo |
264 |
getInstrument(String instrName) { |
265 |
for(int i = 0; i < getInstrumentCount(); i++) { |
266 |
if(instrName.equals(getInstrumentAt(i).getName())) { |
267 |
return getInstrumentAt(i); |
268 |
} |
269 |
} |
270 |
|
271 |
return null; |
272 |
} |
273 |
|
274 |
public boolean |
275 |
isConnected() { return connected; } |
276 |
|
277 |
public void |
278 |
setConnected(boolean b) { connected = b; } |
279 |
|
280 |
/** |
281 |
* Determines whether the node is part of a tree or not. |
282 |
* The default value is <code>false</code>. |
283 |
*/ |
284 |
public boolean |
285 |
isDetached() { return detachedNode; } |
286 |
|
287 |
/** |
288 |
* Sets whether the node is part of a tree or not. |
289 |
*/ |
290 |
public void |
291 |
setDetached(boolean b) { detachedNode = b; } |
292 |
|
293 |
public String |
294 |
toString() { return info.getName(); } |
295 |
|
296 |
private void |
297 |
fireDirectoryCountChanged() { |
298 |
String s = getInfo() == null ? null : getInfo().getDirectoryPath(); |
299 |
InstrumentsDbEvent e = new InstrumentsDbEvent(this, s); |
300 |
for(InstrumentsDbListener l : listeners) l.directoryCountChanged(e); |
301 |
} |
302 |
|
303 |
private void |
304 |
fireDirectoryInfoChanged(String dir) { |
305 |
InstrumentsDbEvent e = new InstrumentsDbEvent(this, dir); |
306 |
for(InstrumentsDbListener l : listeners) l.directoryInfoChanged(e); |
307 |
} |
308 |
|
309 |
private void |
310 |
fireInstrumentCountChanged() { |
311 |
String s = getInfo() == null ? null : getInfo().getDirectoryPath(); |
312 |
InstrumentsDbEvent e = new InstrumentsDbEvent(this, s); |
313 |
for(InstrumentsDbListener l : listeners) l.instrumentCountChanged(e); |
314 |
} |
315 |
|
316 |
private void |
317 |
fireInstrumentInfoChanged(String instr) { |
318 |
InstrumentsDbEvent e = new InstrumentsDbEvent(this, instr); |
319 |
for(InstrumentsDbListener l : listeners) l.instrumentInfoChanged(e); |
320 |
} |
321 |
} |