1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2009 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 |
int modKey = CC.getViewConfig().getDefaultModKey(); |
126 |
|
127 |
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put ( |
128 |
KeyStroke.getKeyStroke(KeyEvent.VK_X, modKey), "none" |
129 |
); |
130 |
|
131 |
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put ( |
132 |
KeyStroke.getKeyStroke(KeyEvent.VK_V, modKey), "none" |
133 |
); |
134 |
|
135 |
getInputMap(JComponent.WHEN_FOCUSED).put ( |
136 |
KeyStroke.getKeyStroke(KeyEvent.VK_X, modKey), "none" |
137 |
); |
138 |
|
139 |
getInputMap(JComponent.WHEN_FOCUSED).put ( |
140 |
KeyStroke.getKeyStroke(KeyEvent.VK_V, modKey), "none" |
141 |
); |
142 |
} |
143 |
|
144 |
public NavigationHistoryModel |
145 |
getNavigationHistoryModel() { return navigationHistoryModel; } |
146 |
|
147 |
private class NavigationHistoryModel |
148 |
extends DefaultNavigationHistoryModel<DbDirectoryTreeNode> |
149 |
implements TreeSelectionListener, ActionListener { |
150 |
|
151 |
private boolean lock = false; |
152 |
|
153 |
NavigationHistoryModel() { |
154 |
addActionListener(this); |
155 |
} |
156 |
|
157 |
public DbDirectoryTreeNode |
158 |
goBack() { |
159 |
lock = true; |
160 |
DbDirectoryTreeNode node = selectDirectory(super.goBack()); |
161 |
lock = false; |
162 |
return node; |
163 |
} |
164 |
|
165 |
public DbDirectoryTreeNode |
166 |
goForward() { |
167 |
lock = true; |
168 |
DbDirectoryTreeNode node = selectDirectory(super.goForward()); |
169 |
lock = false; |
170 |
return node; |
171 |
} |
172 |
|
173 |
private DbDirectoryTreeNode |
174 |
selectDirectory(DbDirectoryTreeNode node) { |
175 |
if(node == null) return null; |
176 |
|
177 |
String path = node.getInfo().getDirectoryPath(); |
178 |
if(CC.getInstrumentsDbTreeModel().getNodeByPath(path) != null) { |
179 |
setSelectedDirectory(path); |
180 |
return node; |
181 |
} |
182 |
|
183 |
removePage(); |
184 |
fireActionPerformed(); |
185 |
String s = i18n.getMessage("JSInstrumentsDbTree.unknownDirectory!", path); |
186 |
HF.showErrorMessage(s, JSInstrumentsDbTree.this); |
187 |
return node; |
188 |
} |
189 |
|
190 |
public void |
191 |
addPage(DbDirectoryTreeNode node) { |
192 |
if(lock) return; |
193 |
if(node == null) return; |
194 |
super.addPage(node); |
195 |
} |
196 |
|
197 |
public void |
198 |
valueChanged(TreeSelectionEvent e) { |
199 |
addPage(getSelectedDirectoryNode()); |
200 |
} |
201 |
|
202 |
public void |
203 |
actionPerformed(ActionEvent e) { |
204 |
actionGoBack.setEnabled(hasBack()); |
205 |
actionGoForward.setEnabled(hasForward()); |
206 |
} |
207 |
} |
208 |
|
209 |
private class GoUp extends AbstractAction implements TreeSelectionListener { |
210 |
GoUp() { |
211 |
super(i18n.getMenuLabel("instrumentsdb.go.up")); |
212 |
|
213 |
String s = i18n.getMenuLabel("instrumentsdb.go.up.tt"); |
214 |
putValue(SHORT_DESCRIPTION, s); |
215 |
putValue(Action.SMALL_ICON, CC.getViewConfig().getBasicIconSet().getUp16Icon()); |
216 |
setEnabled(false); |
217 |
} |
218 |
|
219 |
public void |
220 |
actionPerformed(ActionEvent e) { |
221 |
DbDirectoryTreeNode node = getSelectedDirectoryNode(); |
222 |
if(node == null) return; |
223 |
setSelectedDirectoryNode(node.getParent()); |
224 |
} |
225 |
|
226 |
public void |
227 |
valueChanged(TreeSelectionEvent e) { |
228 |
DbDirectoryTreeNode n = getSelectedDirectoryNode(); |
229 |
if(n == null) { |
230 |
setEnabled(false); |
231 |
return; |
232 |
} |
233 |
|
234 |
setEnabled(n.getParent() != null); |
235 |
} |
236 |
} |
237 |
|
238 |
private class GoBack extends AbstractAction { |
239 |
GoBack() { |
240 |
super(i18n.getMenuLabel("instrumentsdb.go.back")); |
241 |
|
242 |
String s = i18n.getMenuLabel("instrumentsdb.go.back.tt"); |
243 |
putValue(SHORT_DESCRIPTION, s); |
244 |
putValue(Action.SMALL_ICON, CC.getViewConfig().getBasicIconSet().getBack16Icon()); |
245 |
setEnabled(false); |
246 |
} |
247 |
|
248 |
public void |
249 |
actionPerformed(ActionEvent e) { |
250 |
navigationHistoryModel.goBack(); |
251 |
} |
252 |
} |
253 |
|
254 |
private class GoForward extends AbstractAction { |
255 |
GoForward() { |
256 |
super(i18n.getMenuLabel("instrumentsdb.go.forward")); |
257 |
|
258 |
String s = i18n.getMenuLabel("instrumentsdb.go.forward.tt"); |
259 |
putValue(SHORT_DESCRIPTION, s); |
260 |
putValue(Action.SMALL_ICON, CC.getViewConfig().getBasicIconSet().getForward16Icon()); |
261 |
setEnabled(false); |
262 |
} |
263 |
|
264 |
public void |
265 |
actionPerformed(ActionEvent e) { |
266 |
navigationHistoryModel.goForward(); |
267 |
} |
268 |
} |
269 |
|
270 |
private class CellRenderer extends DefaultTreeCellRenderer { |
271 |
CellRenderer() { |
272 |
setOpaque(false); |
273 |
} |
274 |
|
275 |
public Component |
276 |
getTreeCellRendererComponent ( |
277 |
JTree tree, |
278 |
Object value, |
279 |
boolean sel, |
280 |
boolean expanded, |
281 |
boolean leaf, |
282 |
int row, |
283 |
boolean hasFocus |
284 |
) { |
285 |
super.getTreeCellRendererComponent ( |
286 |
tree, value, sel,expanded, leaf, row,hasFocus |
287 |
); |
288 |
|
289 |
DbDirectoryTreeNode node = (DbDirectoryTreeNode)value; |
290 |
if(node.getInfo().getName() == "/") setIcon(getView().getRootIcon()); |
291 |
else if(leaf) setIcon(getView().getInstrumentIcon()); |
292 |
else if(expanded) setIcon(getView().getOpenIcon()); |
293 |
else setIcon(getView().getClosedIcon()); |
294 |
|
295 |
String s = node.getInfo().getDescription(); |
296 |
if(s != null && s.length() > 0) setToolTipText(s); |
297 |
else setToolTipText(null); |
298 |
|
299 |
return this; |
300 |
} |
301 |
} |
302 |
|
303 |
class ContextMenu extends MouseAdapter { |
304 |
private final JPopupMenu cmenu = new JPopupMenu(); |
305 |
JMenuItem miEdit = new JMenuItem(i18n.getMenuLabel("ContextMenu.edit")); |
306 |
|
307 |
ContextMenu() { |
308 |
cmenu.add(miEdit); |
309 |
miEdit.addActionListener(new ActionListener() { |
310 |
public void |
311 |
actionPerformed(ActionEvent e) { |
312 |
|
313 |
} |
314 |
}); |
315 |
|
316 |
JMenuItem mi = new JMenuItem(i18n.getMenuLabel("ContextMenu.delete")); |
317 |
cmenu.add(mi); |
318 |
mi.addActionListener(new ActionListener() { |
319 |
public void |
320 |
actionPerformed(ActionEvent e) { |
321 |
removeSelectedDirectory(); |
322 |
} |
323 |
}); |
324 |
|
325 |
} |
326 |
|
327 |
public void |
328 |
mousePressed(MouseEvent e) { |
329 |
if(e.isPopupTrigger()) show(e); |
330 |
} |
331 |
|
332 |
public void |
333 |
mouseReleased(MouseEvent e) { |
334 |
if(e.isPopupTrigger()) show(e); |
335 |
} |
336 |
|
337 |
void |
338 |
show(MouseEvent e) { |
339 |
cmenu.show(e.getComponent(), e.getX(), e.getY()); |
340 |
} |
341 |
} |
342 |
} |