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.classic; |
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.BorderFactory; |
35 |
import javax.swing.JComponent; |
36 |
import javax.swing.JMenuItem; |
37 |
import javax.swing.JPopupMenu; |
38 |
import javax.swing.JTree; |
39 |
import javax.swing.KeyStroke; |
40 |
|
41 |
import javax.swing.tree.DefaultTreeCellRenderer; |
42 |
import javax.swing.tree.TreeSelectionModel; |
43 |
|
44 |
import org.jsampler.view.DbDirectoryTreeNode; |
45 |
import org.jsampler.view.InstrumentsDbTreeModel; |
46 |
|
47 |
import static org.jsampler.view.classic.ClassicI18n.i18n; |
48 |
|
49 |
/** |
50 |
* |
51 |
* @author Grigor Iliev |
52 |
*/ |
53 |
public class InstrumentsDbTree extends org.jsampler.view.AbstractInstrumentsDbTree { |
54 |
|
55 |
/** Creates a new instance of <code>InstrumentsDbTree</code>. */ |
56 |
public |
57 |
InstrumentsDbTree(InstrumentsDbTreeModel model) { |
58 |
super(model); |
59 |
CellRenderer renderer = new CellRenderer(); |
60 |
renderer.setClosedIcon(Res.iconFolder16); |
61 |
renderer.setOpenIcon(Res.iconFolderOpen16); |
62 |
|
63 |
setCellRenderer(renderer); |
64 |
|
65 |
setBorder(BorderFactory.createEmptyBorder(3, 3, 0, 0)); |
66 |
|
67 |
getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
68 |
|
69 |
addMouseListener(new MouseAdapter() { |
70 |
public void |
71 |
mousePressed(MouseEvent e) { |
72 |
if(e.getButton() != e.BUTTON3) return; |
73 |
setSelectionPath(getClosestPathForLocation(e.getX(), e.getY())); |
74 |
} |
75 |
}); |
76 |
|
77 |
ContextMenu contextMenu = new ContextMenu(); |
78 |
//addMouseListener(contextMenu); |
79 |
installKeyboardListeners(); |
80 |
} |
81 |
|
82 |
private void |
83 |
installKeyboardListeners() { |
84 |
AbstractAction a = new AbstractAction() { |
85 |
public void |
86 |
actionPerformed(ActionEvent e) { } |
87 |
}; |
88 |
a.setEnabled(false); |
89 |
getActionMap().put("none", a); |
90 |
|
91 |
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put ( |
92 |
KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK), "none" |
93 |
); |
94 |
|
95 |
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put ( |
96 |
KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK), "none" |
97 |
); |
98 |
|
99 |
getInputMap(JComponent.WHEN_FOCUSED).put ( |
100 |
KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK), "none" |
101 |
); |
102 |
|
103 |
getInputMap(JComponent.WHEN_FOCUSED).put ( |
104 |
KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.CTRL_MASK), "none" |
105 |
); |
106 |
} |
107 |
|
108 |
private class CellRenderer extends DefaultTreeCellRenderer { |
109 |
public Component |
110 |
getTreeCellRendererComponent ( |
111 |
JTree tree, |
112 |
Object value, |
113 |
boolean sel, |
114 |
boolean expanded, |
115 |
boolean leaf, |
116 |
int row, |
117 |
boolean hasFocus |
118 |
) { |
119 |
super.getTreeCellRendererComponent ( |
120 |
tree, value, sel,expanded, leaf, row,hasFocus |
121 |
); |
122 |
|
123 |
DbDirectoryTreeNode node = (DbDirectoryTreeNode)value; |
124 |
if(node.getInfo().getName() == "/") setIcon(Res.iconDb16); |
125 |
else if(leaf) setIcon(Res.iconInstrument16); |
126 |
else if(expanded) setIcon(Res.iconFolderOpen16); |
127 |
else setIcon(Res.iconFolder16); |
128 |
|
129 |
String s = node.getInfo().getDescription(); |
130 |
if(s != null && s.length() > 0) setToolTipText(s); |
131 |
else setToolTipText(null); |
132 |
|
133 |
return this; |
134 |
} |
135 |
} |
136 |
|
137 |
class ContextMenu extends MouseAdapter { |
138 |
private final JPopupMenu cmenu = new JPopupMenu(); |
139 |
JMenuItem miEdit = new JMenuItem(i18n.getMenuLabel("ContextMenu.edit")); |
140 |
|
141 |
ContextMenu() { |
142 |
cmenu.add(miEdit); |
143 |
miEdit.addActionListener(new ActionListener() { |
144 |
public void |
145 |
actionPerformed(ActionEvent e) { |
146 |
|
147 |
} |
148 |
}); |
149 |
|
150 |
JMenuItem mi = new JMenuItem(i18n.getMenuLabel("ContextMenu.delete")); |
151 |
cmenu.add(mi); |
152 |
mi.addActionListener(new ActionListener() { |
153 |
public void |
154 |
actionPerformed(ActionEvent e) { |
155 |
removeSelectedDirectory(); |
156 |
} |
157 |
}); |
158 |
|
159 |
} |
160 |
|
161 |
public void |
162 |
mousePressed(MouseEvent e) { |
163 |
if(e.isPopupTrigger()) show(e); |
164 |
} |
165 |
|
166 |
public void |
167 |
mouseReleased(MouseEvent e) { |
168 |
if(e.isPopupTrigger()) show(e); |
169 |
} |
170 |
|
171 |
void |
172 |
show(MouseEvent e) { |
173 |
cmenu.show(e.getComponent(), e.getX(), e.getY()); |
174 |
} |
175 |
} |
176 |
} |