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.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
|
28 |
import javax.swing.JTree; |
29 |
|
30 |
import javax.swing.event.TreeExpansionEvent; |
31 |
import javax.swing.event.TreeSelectionEvent; |
32 |
import javax.swing.event.TreeSelectionListener; |
33 |
import javax.swing.event.TreeWillExpandListener; |
34 |
|
35 |
import javax.swing.tree.TreePath; |
36 |
|
37 |
import org.jsampler.CC; |
38 |
|
39 |
/** |
40 |
* |
41 |
* @author Grigor Iliev |
42 |
*/ |
43 |
public abstract class AbstractInstrumentsDbTree extends JTree { |
44 |
private InstrumentsDbTreeView view = null; |
45 |
|
46 |
/** |
47 |
* Creates a new instance of <code>AbstractInstrumentsDbTree</code>. |
48 |
*/ |
49 |
public |
50 |
AbstractInstrumentsDbTree() { this((ActionListener)null); } |
51 |
|
52 |
/** |
53 |
* Creates a new instance of <code>AbstractInstrumentsDbTree</code>. |
54 |
* |
55 |
* @param l A listener that will be notified when the root |
56 |
* directory content is loaded. |
57 |
*/ |
58 |
public |
59 |
AbstractInstrumentsDbTree(ActionListener l) { this(new InstrumentsDbTreeModel(l)); } |
60 |
|
61 |
/** |
62 |
* Creates a new instance of <code>AbstractInstrumentsDbTree</code> |
63 |
* using the specified tree model. |
64 |
* |
65 |
* @param model The model to be used by this tree. |
66 |
*/ |
67 |
public |
68 |
AbstractInstrumentsDbTree(InstrumentsDbTreeModel model) { |
69 |
setView(CC.getViewConfig().getInstrumentsDbTreeView()); |
70 |
setModel(model); |
71 |
addTreeWillExpandListener(getHandler()); |
72 |
addTreeSelectionListener(getHandler()); |
73 |
} |
74 |
|
75 |
public InstrumentsDbTreeModel |
76 |
getModel() { return (InstrumentsDbTreeModel) super.getModel(); } |
77 |
|
78 |
/** |
79 |
* Removes the selected directory. |
80 |
*/ |
81 |
public void |
82 |
removeSelectedDirectory() { |
83 |
DbDirectoryTreeNode node = getSelectedDirectoryNode(); |
84 |
if(node == null) return; |
85 |
|
86 |
String path = getModel().getPathName(getModel().getPathToRoot(node)); |
87 |
|
88 |
} |
89 |
|
90 |
/** |
91 |
* Returns the currently selected directory, or <code>null</code> |
92 |
* if nothing is selected. |
93 |
*/ |
94 |
public DbDirectoryTreeNode |
95 |
getSelectedDirectoryNode() { |
96 |
if(getSelectionCount() == 0) return null; |
97 |
return (DbDirectoryTreeNode)getSelectionPath().getLastPathComponent(); |
98 |
} |
99 |
|
100 |
public void |
101 |
setSelectedDirectoryNode(DbDirectoryTreeNode node) { |
102 |
Object[] objs = getModel().getPathToRoot(node); |
103 |
setSelectionPath(new TreePath(objs)); |
104 |
} |
105 |
|
106 |
public String |
107 |
getSelectedDirectoryPath() { |
108 |
return getModel().getPathByNode(getSelectedDirectoryNode()); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Selects the specified directory. |
113 |
* Note that if there is at least one directory in the path, |
114 |
* which is not connected the selection will be changed |
115 |
* after the execution of this method. |
116 |
*/ |
117 |
public void |
118 |
setSelectedDirectory(final String dir) { |
119 |
getModel().loadPath(dir, new ActionListener() { |
120 |
public void |
121 |
actionPerformed(ActionEvent e) { |
122 |
DbDirectoryTreeNode node = getModel().getNodeByPath(dir); |
123 |
if(node != null) setSelectedDirectoryNode(node); |
124 |
} |
125 |
}); |
126 |
} |
127 |
|
128 |
/** |
129 |
* Schedules a task for refreshing the content of the specified directory. |
130 |
* Note that the specified directory is expected to be connected. |
131 |
* @param dir The absolute path name of the directory to refresh. |
132 |
*/ |
133 |
public void |
134 |
refreshDirectoryContent(String dir) { |
135 |
getModel().refreshDirectoryContent(dir); |
136 |
} |
137 |
|
138 |
public boolean |
139 |
hasBeenExpanded(TreePath p) { |
140 |
return super.hasBeenExpanded(p) || !getModel().isLeaf(p.getLastPathComponent()); |
141 |
} |
142 |
|
143 |
/** Sets the view to be used for retrieving UI information. */ |
144 |
public void |
145 |
setView(InstrumentsDbTreeView view) { |
146 |
this.view = view; |
147 |
} |
148 |
|
149 |
/** Gets the view used to retrieve UI information. */ |
150 |
public InstrumentsDbTreeView |
151 |
getView() { return view; } |
152 |
|
153 |
private final EventHandler eventHandler = new EventHandler(); |
154 |
|
155 |
private EventHandler |
156 |
getHandler() { return eventHandler; } |
157 |
|
158 |
private class EventHandler implements TreeWillExpandListener, TreeSelectionListener { |
159 |
public void |
160 |
treeWillCollapse(TreeExpansionEvent e) { } |
161 |
|
162 |
public void |
163 |
treeWillExpand(TreeExpansionEvent e) { |
164 |
getModel().treeWillExpand(e.getPath()); |
165 |
} |
166 |
|
167 |
public void |
168 |
valueChanged(TreeSelectionEvent e) { |
169 |
TreePath p = e.getPath(); |
170 |
if(p == null) { |
171 |
System.err.println("p is null"); |
172 |
return; |
173 |
} |
174 |
getModel().treeWillExpand(p); |
175 |
} |
176 |
} |
177 |
} |