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 |
// Tree node model methods |
82 |
public DbDirectoryTreeNode |
83 |
getChildAt(int index) { return dirs.get(index); } |
84 |
|
85 |
public int |
86 |
getChildCount() { return dirs.size(); } |
87 |
|
88 |
public DbDirectoryTreeNode |
89 |
getParent() { return parent; } |
90 |
|
91 |
public int |
92 |
getIndex(TreeNode node) { return dirs.indexOf(node); } |
93 |
|
94 |
public boolean |
95 |
getAllowsChildren() { return true; } |
96 |
|
97 |
public boolean |
98 |
isLeaf() { return false; } |
99 |
|
100 |
public Enumeration |
101 |
children() { return dirs.elements(); } |
102 |
/////// |
103 |
|
104 |
public void |
105 |
setParent(DbDirectoryTreeNode parent) { this.parent = parent; } |
106 |
|
107 |
public void |
108 |
addDirectory(DbDirectoryTreeNode child) { |
109 |
addDirectory(child, true); |
110 |
} |
111 |
|
112 |
private void |
113 |
addDirectory(DbDirectoryTreeNode child, boolean doNotify) { |
114 |
for(int i = 0; i < dirs.size(); i++) { |
115 |
String s = dirs.get(i).getInfo().toString(); |
116 |
if(s.compareToIgnoreCase(child.getInfo().toString()) < 0) continue; |
117 |
|
118 |
dirs.insertElementAt(child, i); |
119 |
child.setParent(this); |
120 |
fireDirectoryCountChanged(); |
121 |
return; |
122 |
} |
123 |
|
124 |
dirs.add(child); |
125 |
child.setParent(this); |
126 |
if(doNotify) fireDirectoryCountChanged(); |
127 |
} |
128 |
|
129 |
public void |
130 |
addDirectories(DbDirectoryTreeNode[] children) { |
131 |
if(children == null || children.length == 0) return; |
132 |
for(DbDirectoryTreeNode n : children) addDirectory(n, false); |
133 |
fireDirectoryCountChanged(); |
134 |
} |
135 |
|
136 |
public void |
137 |
removeDirectory(int index) { |
138 |
DbDirectoryTreeNode node = dirs.remove(index); |
139 |
node.setParent(null); |
140 |
fireDirectoryCountChanged(); |
141 |
} |
142 |
|
143 |
public void |
144 |
removeDirectoryByPathName(String path) { |
145 |
if(path == null) return; |
146 |
|
147 |
for(DbDirectoryTreeNode n : dirs) { |
148 |
if(path.equals(n.getInfo().getDirectoryPath())) { |
149 |
dirs.removeElement(n); |
150 |
return; |
151 |
} |
152 |
} |
153 |
} |
154 |
|
155 |
public void |
156 |
removeInstrumentByPathName(String path) { |
157 |
if(path == null) return; |
158 |
|
159 |
for(DbInstrumentInfo info : instrs) { |
160 |
if(path.equals(info.getInstrumentPath())) { |
161 |
instrs.removeElement(info); |
162 |
return; |
163 |
} |
164 |
} |
165 |
} |
166 |
|
167 |
public void |
168 |
removeAllDirectories() { |
169 |
dirs.removeAllElements(); |
170 |
fireDirectoryCountChanged(); |
171 |
} |
172 |
|
173 |
public void |
174 |
updateDirectory(DbDirectoryInfo info) { |
175 |
for(int i = 0; i < dirs.size(); i++) { |
176 |
if(dirs.get(i).getInfo().getName().equals(info.getName())) { |
177 |
dirs.get(i).setInfo(info); |
178 |
fireDirectoryInfoChanged(info.getDirectoryPath()); |
179 |
return; |
180 |
} |
181 |
} |
182 |
} |
183 |
|
184 |
public void |
185 |
addInstrument(DbInstrumentInfo info) { |
186 |
instrs.add(info); |
187 |
fireInstrumentCountChanged(); |
188 |
} |
189 |
|
190 |
public void |
191 |
addInstruments(DbInstrumentInfo[] infos) { |
192 |
if(infos == null || infos.length == 0) return; |
193 |
for(DbInstrumentInfo i : infos) instrs.add(i); |
194 |
fireInstrumentCountChanged(); |
195 |
} |
196 |
|
197 |
public void |
198 |
removeAllInstruments() { |
199 |
instrs.removeAllElements(); |
200 |
fireInstrumentCountChanged(); |
201 |
} |
202 |
|
203 |
public void |
204 |
updateInstrument(DbInstrumentInfo info) { |
205 |
for(int i = 0; i < instrs.size(); i++) { |
206 |
if(instrs.get(i).getName().equals(info.getName())) { |
207 |
instrs.setElementAt(info, i); |
208 |
fireInstrumentInfoChanged(info.getInstrumentPath()); |
209 |
return; |
210 |
} |
211 |
} |
212 |
} |
213 |
|
214 |
public DbInstrumentInfo |
215 |
getInstrumentAt(int index) { return instrs.get(index); } |
216 |
|
217 |
public int |
218 |
getInstrumentCount() { return instrs.size(); } |
219 |
|
220 |
public int |
221 |
getInstrumentIndex(DbInstrumentInfo instr) { return instrs.indexOf(instr); } |
222 |
|
223 |
public void |
224 |
removeInstrument(int index) { |
225 |
instrs.remove(index); |
226 |
fireInstrumentCountChanged(); |
227 |
} |
228 |
|
229 |
/** |
230 |
* Gets the instrument with the specified name. |
231 |
* @param instrName The name of the instrument to return. |
232 |
* @return The instrument with the specified name or <code>null</code> |
233 |
* if there is no instrument with the specified name in this directory. |
234 |
*/ |
235 |
public DbInstrumentInfo |
236 |
getInstrument(String instrName) { |
237 |
for(int i = 0; i < getInstrumentCount(); i++) { |
238 |
if(instrName.equals(getInstrumentAt(i).getName())) { |
239 |
return getInstrumentAt(i); |
240 |
} |
241 |
} |
242 |
|
243 |
return null; |
244 |
} |
245 |
|
246 |
public boolean |
247 |
isConnected() { return connected; } |
248 |
|
249 |
public void |
250 |
setConnected(boolean b) { connected = b; } |
251 |
|
252 |
/** |
253 |
* Determines whether the node is part of a tree or not. |
254 |
* The default value is <code>false</code>. |
255 |
*/ |
256 |
public boolean |
257 |
isDetached() { return detachedNode; } |
258 |
|
259 |
/** |
260 |
* Sets whether the node is part of a tree or not. |
261 |
*/ |
262 |
public void |
263 |
setDetached(boolean b) { detachedNode = b; } |
264 |
|
265 |
public String |
266 |
toString() { return info.getName(); } |
267 |
|
268 |
private void |
269 |
fireDirectoryCountChanged() { |
270 |
String s = getInfo() == null ? null : getInfo().getDirectoryPath(); |
271 |
InstrumentsDbEvent e = new InstrumentsDbEvent(this, s); |
272 |
for(InstrumentsDbListener l : listeners) l.directoryCountChanged(e); |
273 |
} |
274 |
|
275 |
private void |
276 |
fireDirectoryInfoChanged(String dir) { |
277 |
InstrumentsDbEvent e = new InstrumentsDbEvent(this, dir); |
278 |
for(InstrumentsDbListener l : listeners) l.directoryInfoChanged(e); |
279 |
} |
280 |
|
281 |
private void |
282 |
fireInstrumentCountChanged() { |
283 |
String s = getInfo() == null ? null : getInfo().getDirectoryPath(); |
284 |
InstrumentsDbEvent e = new InstrumentsDbEvent(this, s); |
285 |
for(InstrumentsDbListener l : listeners) l.instrumentCountChanged(e); |
286 |
} |
287 |
|
288 |
private void |
289 |
fireInstrumentInfoChanged(String instr) { |
290 |
InstrumentsDbEvent e = new InstrumentsDbEvent(this, instr); |
291 |
for(InstrumentsDbListener l : listeners) l.instrumentInfoChanged(e); |
292 |
} |
293 |
} |