1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2008 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.std; |
24 |
|
25 |
import java.awt.Component; |
26 |
|
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.awt.event.KeyEvent; |
30 |
import java.awt.event.MouseAdapter; |
31 |
import java.awt.event.MouseEvent; |
32 |
|
33 |
import javax.swing.AbstractAction; |
34 |
import javax.swing.Action; |
35 |
import javax.swing.BorderFactory; |
36 |
import javax.swing.JComponent; |
37 |
import javax.swing.JMenuItem; |
38 |
import javax.swing.JPopupMenu; |
39 |
import javax.swing.JTree; |
40 |
import javax.swing.KeyStroke; |
41 |
|
42 |
import javax.swing.event.ChangeEvent; |
43 |
import javax.swing.event.ChangeListener; |
44 |
import javax.swing.event.TreeSelectionEvent; |
45 |
import javax.swing.event.TreeSelectionListener; |
46 |
|
47 |
import javax.swing.tree.DefaultTreeCellRenderer; |
48 |
import javax.swing.tree.TreeSelectionModel; |
49 |
|
50 |
import net.sf.juife.DefaultNavigationHistoryModel; |
51 |
|
52 |
import org.jsampler.CC; |
53 |
import org.jsampler.HF; |
54 |
|
55 |
import org.jsampler.view.DbDirectoryTreeNode; |
56 |
import org.jsampler.view.InstrumentsDbTreeModel; |
57 |
|
58 |
import static org.jsampler.view.std.StdI18n.i18n; |
59 |
|
60 |
/** |
61 |
* |
62 |
* @author Grigor Iliev |
63 |
*/ |
64 |
public class JSInstrumentsDbTree extends org.jsampler.view.AbstractInstrumentsDbTree { |
65 |
public final AbstractAction actionGoUp = new GoUp(); |
66 |
public final AbstractAction actionGoBack = new GoBack(); |
67 |
public final AbstractAction actionGoForward = new GoForward(); |
68 |
|
69 |
private final NavigationHistoryModel navigationHistoryModel = new NavigationHistoryModel(); |
70 |
|
71 |
|
72 |
/** |
73 |
* Creates a new instance of <code>JSInstrumentsDbTree</code>. |
74 |
*/ |
75 |
public |
76 |
JSInstrumentsDbTree(InstrumentsDbTreeModel model) { |
77 |
super(model); |
78 |
CellRenderer renderer = new CellRenderer(); |
79 |
setCellRenderer(renderer); |
80 |
|
81 |
setBorder(BorderFactory.createEmptyBorder(3, 3, 0, 0)); |
82 |
|
83 |
getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
84 |
|
85 |
addMouseListener(new MouseAdapter() { |
86 |
public void |
87 |
mousePressed(MouseEvent e) { |
88 |
if(e.getButton() != e.BUTTON3) return; |
89 |
setSelectionPath(getClosestPathForLocation(e.getX(), e.getY())); |
90 |
} |
91 |
}); |
92 |
|
93 |
ContextMenu contextMenu = new ContextMenu(); |
94 |
//addMouseListener(contextMenu); |
95 |
installKeyboardListeners(); |
96 |
|
97 |
CC.addInstrumentsDbChangeListener(new ChangeListener() { |
98 |
public void |
99 |
stateChanged(ChangeEvent e) { |
100 |
setModel(CC.getInstrumentsDbTreeModel()); |
101 |
|
102 |
CC.scheduleInTaskQueue(new Runnable() { |
103 |
public void |
104 |
run() { |
105 |
setSelectedDirectory("/"); |
106 |
navigationHistoryModel.clearHistory(); |
107 |
} |
108 |
}); |
109 |
} |
110 |
}); |
111 |
|
112 |
addTreeSelectionListener((GoUp)actionGoUp); |
113 |
addTreeSelectionListener(navigationHistoryModel); |
114 |
} |
115 |
|
116 |
private void |
117 |
installKeyboardListeners() { |
118 |
AbstractAction a = new AbstractAction() { |
119 |
public void |
120 |
actionPerformed(ActionEvent e) { } |
121 |
}; |
122 |
a.setEnabled(false); |
123 |
getActionMap().put("none", a); |
124 |
|
125 |
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put ( |
126 |
KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK), "none" |
127 |
); |
128 |
|
129 |
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put ( |
130 |
KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK), "none" |
131 |
); |
132 |
|
133 |
getInputMap(JComponent.WHEN_FOCUSED).put ( |
134 |
KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK), "none" |
135 |
); |
136 |
|
137 |
getInputMap(JComponent.WHEN_FOCUSED).put ( |
138 |
KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK), "none" |
139 |
); |
140 |
} |
141 |
|
142 |
public NavigationHistoryModel |
143 |
getNavigationHistoryModel() { return navigationHistoryModel; } |
144 |
|
145 |
private class NavigationHistoryModel |
146 |
extends DefaultNavigationHistoryModel<DbDirectoryTreeNode> |
147 |
implements TreeSelectionListener, ActionListener { |
148 |
|
149 |
private boolean lock = false; |
150 |
|
151 |
NavigationHistoryModel() { |
152 |
addActionListener(this); |
153 |
} |
154 |
|
155 |
public DbDirectoryTreeNode |
156 |
goBack() { |
157 |
lock = true; |
158 |
DbDirectoryTreeNode node = selectDirectory(super.goBack()); |
159 |
lock = false; |
160 |
return node; |
161 |
} |
162 |
|
163 |
public DbDirectoryTreeNode |
164 |
goForward() { |
165 |
lock = true; |
166 |
DbDirectoryTreeNode node = selectDirectory(super.goForward()); |
167 |
lock = false; |
168 |
return node; |
169 |
} |
170 |
|
171 |
private DbDirectoryTreeNode |
172 |
selectDirectory(DbDirectoryTreeNode node) { |
173 |
if(node == null) return null; |
174 |
|
175 |
String path = node.getInfo().getDirectoryPath(); |
176 |
if(CC.getInstrumentsDbTreeModel().getNodeByPath(path) != null) { |
177 |
setSelectedDirectory(path); |
178 |
return node; |
179 |
} |
180 |
|
181 |
removePage(); |
182 |
fireActionPerformed(); |
183 |
String s = i18n.getMessage("JSInstrumentsDbTree.unknownDirectory!", path); |
184 |
HF.showErrorMessage(s, JSInstrumentsDbTree.this); |
185 |
return node; |
186 |
} |
187 |
|
188 |
public void |
189 |
addPage(DbDirectoryTreeNode node) { |
190 |
if(lock) return; |
191 |
if(node == null) return; |
192 |
super.addPage(node); |
193 |
} |
194 |
|
195 |
public void |
196 |
valueChanged(TreeSelectionEvent e) { |
197 |
addPage(getSelectedDirectoryNode()); |
198 |
} |
199 |
|
200 |
public void |
201 |
actionPerformed(ActionEvent e) { |
202 |
actionGoBack.setEnabled(hasBack()); |
203 |
actionGoForward.setEnabled(hasForward()); |
204 |
} |
205 |
} |
206 |
|
207 |
private class GoUp extends AbstractAction implements TreeSelectionListener { |
208 |
GoUp() { |
209 |
super(i18n.getMenuLabel("instrumentsdb.go.up")); |
210 |
|
211 |
String s = i18n.getMenuLabel("instrumentsdb.go.up.tt"); |
212 |
putValue(SHORT_DESCRIPTION, s); |
213 |
putValue(Action.SMALL_ICON, CC.getViewConfig().getBasicIconSet().getUp16Icon()); |
214 |
setEnabled(false); |
215 |
} |
216 |
|
217 |
public void |
218 |
actionPerformed(ActionEvent e) { |
219 |
DbDirectoryTreeNode node = getSelectedDirectoryNode(); |
220 |
if(node == null) return; |
221 |
setSelectedDirectoryNode(node.getParent()); |
222 |
} |
223 |
|
224 |
public void |
225 |
valueChanged(TreeSelectionEvent e) { |
226 |
DbDirectoryTreeNode n = getSelectedDirectoryNode(); |
227 |
if(n == null) { |
228 |
setEnabled(false); |
229 |
return; |
230 |
} |
231 |
|
232 |
setEnabled(n.getParent() != null); |
233 |
} |
234 |
} |
235 |
|
236 |
private class GoBack extends AbstractAction { |
237 |
GoBack() { |
238 |
super(i18n.getMenuLabel("instrumentsdb.go.back")); |
239 |
|
240 |
String s = i18n.getMenuLabel("instrumentsdb.go.back.tt"); |
241 |
putValue(SHORT_DESCRIPTION, s); |
242 |
putValue(Action.SMALL_ICON, CC.getViewConfig().getBasicIconSet().getBack16Icon()); |
243 |
setEnabled(false); |
244 |
} |
245 |
|
246 |
public void |
247 |
actionPerformed(ActionEvent e) { |
248 |
navigationHistoryModel.goBack(); |
249 |
} |
250 |
} |
251 |
|
252 |
private class GoForward extends AbstractAction { |
253 |
GoForward() { |
254 |
super(i18n.getMenuLabel("instrumentsdb.go.forward")); |
255 |
|
256 |
String s = i18n.getMenuLabel("instrumentsdb.go.forward.tt"); |
257 |
putValue(SHORT_DESCRIPTION, s); |
258 |
putValue(Action.SMALL_ICON, CC.getViewConfig().getBasicIconSet().getForward16Icon()); |
259 |
setEnabled(false); |
260 |
} |
261 |
|
262 |
public void |
263 |
actionPerformed(ActionEvent e) { |
264 |
navigationHistoryModel.goForward(); |
265 |
} |
266 |
} |
267 |
|
268 |
private class CellRenderer extends DefaultTreeCellRenderer { |
269 |
CellRenderer() { |
270 |
setOpaque(false); |
271 |
} |
272 |
|
273 |
public Component |
274 |
getTreeCellRendererComponent ( |
275 |
JTree tree, |
276 |
Object value, |
277 |
boolean sel, |
278 |
boolean expanded, |
279 |
boolean leaf, |
280 |
int row, |
281 |
boolean hasFocus |
282 |
) { |
283 |
super.getTreeCellRendererComponent ( |
284 |
tree, value, sel,expanded, leaf, row,hasFocus |
285 |
); |
286 |
|
287 |
DbDirectoryTreeNode node = (DbDirectoryTreeNode)value; |
288 |
if(node.getInfo().getName() == "/") setIcon(getView().getRootIcon()); |
289 |
else if(leaf) setIcon(getView().getInstrumentIcon()); |
290 |
else if(expanded) setIcon(getView().getOpenIcon()); |
291 |
else setIcon(getView().getClosedIcon()); |
292 |
|
293 |
String s = node.getInfo().getDescription(); |
294 |
if(s != null && s.length() > 0) setToolTipText(s); |
295 |
else setToolTipText(null); |
296 |
|
297 |
return this; |
298 |
} |
299 |
} |
300 |
|
301 |
class ContextMenu extends MouseAdapter { |
302 |
private final JPopupMenu cmenu = new JPopupMenu(); |
303 |
JMenuItem miEdit = new JMenuItem(i18n.getMenuLabel("ContextMenu.edit")); |
304 |
|
305 |
ContextMenu() { |
306 |
cmenu.add(miEdit); |
307 |
miEdit.addActionListener(new ActionListener() { |
308 |
public void |
309 |
actionPerformed(ActionEvent e) { |
310 |
|
311 |
} |
312 |
}); |
313 |
|
314 |
JMenuItem mi = new JMenuItem(i18n.getMenuLabel("ContextMenu.delete")); |
315 |
cmenu.add(mi); |
316 |
mi.addActionListener(new ActionListener() { |
317 |
public void |
318 |
actionPerformed(ActionEvent e) { |
319 |
removeSelectedDirectory(); |
320 |
} |
321 |
}); |
322 |
|
323 |
} |
324 |
|
325 |
public void |
326 |
mousePressed(MouseEvent e) { |
327 |
if(e.isPopupTrigger()) show(e); |
328 |
} |
329 |
|
330 |
public void |
331 |
mouseReleased(MouseEvent e) { |
332 |
if(e.isPopupTrigger()) show(e); |
333 |
} |
334 |
|
335 |
void |
336 |
show(MouseEvent e) { |
337 |
cmenu.show(e.getComponent(), e.getX(), e.getY()); |
338 |
} |
339 |
} |
340 |
} |