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