gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.swing / org.gvsig.scripting.swing.impl / src / main / java / org / gvsig / scripting / swing / impl / DefaultJScriptingBrowser.java @ 639
History | View | Annotate | Download (14.4 KB)
1 |
package org.gvsig.scripting.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Component; |
5 |
import java.awt.Graphics; |
6 |
import java.awt.GridLayout; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.awt.event.KeyEvent; |
10 |
import java.awt.event.MouseEvent; |
11 |
import java.awt.event.MouseListener; |
12 |
import java.util.ArrayList; |
13 |
import java.util.HashMap; |
14 |
import java.util.Iterator; |
15 |
import java.util.List; |
16 |
import java.util.Map; |
17 |
import javax.swing.AbstractAction; |
18 |
|
19 |
import javax.swing.Icon; |
20 |
import javax.swing.ImageIcon; |
21 |
import javax.swing.JComponent; |
22 |
import javax.swing.JLabel; |
23 |
import javax.swing.JPanel; |
24 |
import javax.swing.JScrollPane; |
25 |
import javax.swing.JTree; |
26 |
import javax.swing.KeyStroke; |
27 |
import javax.swing.UIManager; |
28 |
import javax.swing.event.TreeModelEvent; |
29 |
import javax.swing.event.TreeModelListener; |
30 |
import javax.swing.event.TreeSelectionEvent; |
31 |
import javax.swing.event.TreeSelectionListener; |
32 |
import javax.swing.tree.TreeCellRenderer; |
33 |
import javax.swing.tree.TreeModel; |
34 |
import javax.swing.tree.TreePath; |
35 |
import javax.swing.tree.TreeSelectionModel; |
36 |
|
37 |
import org.gvsig.scripting.ScriptingBaseScript; |
38 |
import org.gvsig.scripting.ScriptingFolder; |
39 |
import org.gvsig.scripting.ScriptingManager; |
40 |
import org.gvsig.scripting.ScriptingUnit; |
41 |
import org.gvsig.scripting.swing.api.JScriptingBrowser; |
42 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
43 |
import org.slf4j.LoggerFactory; |
44 |
import org.slf4j.Logger; |
45 |
|
46 |
public class DefaultJScriptingBrowser extends JPanel implements JScriptingBrowser { |
47 |
|
48 |
private static final Logger logger = LoggerFactory.getLogger(DefaultJScriptingBrowser.class); |
49 |
private static final long serialVersionUID = 3140698910497806258L; |
50 |
|
51 |
protected ScriptingUIManager uimanager;
|
52 |
protected ScriptingManager manager;
|
53 |
protected TreeModel treeModel; |
54 |
protected JTree tree; |
55 |
protected ScriptingFolder root;
|
56 |
protected ActionListener defaultActionlistener = null; |
57 |
protected ActionListener selectionActionlistener = null; |
58 |
private final boolean foldersOnly; |
59 |
|
60 |
public static class DefaultBrowserActionEvent extends ActionEvent implements BrowserActionEvent { |
61 |
|
62 |
/**
|
63 |
*
|
64 |
*/
|
65 |
private static final long serialVersionUID = -1474410768278633364L; |
66 |
private ScriptingBaseScript script = null; |
67 |
|
68 |
public DefaultBrowserActionEvent(Object source, int id, String command, |
69 |
ScriptingBaseScript script) { |
70 |
super(source, id, command);
|
71 |
this.script = script;
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
public ScriptingBaseScript getScript() {
|
76 |
return this.script; |
77 |
} |
78 |
} |
79 |
|
80 |
class ScriptingFolderTreeModel implements TreeModel { |
81 |
|
82 |
protected final ScriptingFolder root; |
83 |
|
84 |
protected final List<TreeModelListener> listeners = new ArrayList(); |
85 |
|
86 |
public ScriptingFolderTreeModel(ScriptingFolder root) {
|
87 |
this.root = root;
|
88 |
} |
89 |
|
90 |
@Override
|
91 |
public Object getRoot() { |
92 |
return root;
|
93 |
} |
94 |
|
95 |
@Override
|
96 |
public Object getChild(Object parent, int index) { |
97 |
ScriptingFolder folder = (ScriptingFolder) parent; |
98 |
List<ScriptingUnit> children = folder.getUnits();
|
99 |
return children.get(index);
|
100 |
} |
101 |
|
102 |
@Override
|
103 |
public int getChildCount(Object parent) { |
104 |
if (!(parent instanceof ScriptingFolder)) { |
105 |
return 0; |
106 |
} |
107 |
ScriptingFolder folder = (ScriptingFolder) parent; |
108 |
return folder.getUnits().size();
|
109 |
} |
110 |
|
111 |
@Override
|
112 |
public boolean isLeaf(Object node) { |
113 |
return !(node instanceof ScriptingFolder); |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public int getIndexOfChild(Object parent, Object child) { |
118 |
String childPath = ((ScriptingUnit) child).getFile().getAbsolutePath();
|
119 |
ScriptingFolder folder = (ScriptingFolder) parent; |
120 |
List<ScriptingUnit> children = folder.getUnits();
|
121 |
for (int i = 0; i < children.size(); i++) { |
122 |
ScriptingUnit aChild = children.get(i); |
123 |
if (aChild.getFile().getAbsolutePath().equals(childPath)) {
|
124 |
return i;
|
125 |
} |
126 |
} |
127 |
return -1; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void valueForPathChanged(TreePath path, Object value) { |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public void addTreeModelListener(TreeModelListener listener) { |
136 |
listeners.add(listener); |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void removeTreeModelListener(TreeModelListener listener) { |
141 |
listeners.remove(listener); |
142 |
} |
143 |
|
144 |
public void reload() { |
145 |
TreeModelEvent event = new TreeModelEvent(this, new TreePath(this.root)); |
146 |
Iterator<TreeModelListener> iterator = listeners.iterator(); |
147 |
TreeModelListener listener;
|
148 |
while (iterator.hasNext()) {
|
149 |
listener = iterator.next(); |
150 |
listener.treeStructureChanged(event); |
151 |
} |
152 |
} |
153 |
} |
154 |
|
155 |
class ScriptingFolderTreeModelFoldersOnly extends ScriptingFolderTreeModel { |
156 |
|
157 |
public ScriptingFolderTreeModelFoldersOnly(ScriptingFolder root) {
|
158 |
super(root);
|
159 |
} |
160 |
|
161 |
@Override
|
162 |
public Object getChild(Object parent, int index) { |
163 |
ScriptingFolder folder = (ScriptingFolder) parent; |
164 |
List<ScriptingFolder> children = folder.getUnitFolders();
|
165 |
return children.get(index);
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public int getChildCount(Object parent) { |
170 |
if (!(parent instanceof ScriptingFolder)) { |
171 |
return 0; |
172 |
} |
173 |
ScriptingFolder folder = (ScriptingFolder) parent; |
174 |
return folder.getUnitFolders().size();
|
175 |
} |
176 |
|
177 |
@Override
|
178 |
public int getIndexOfChild(Object parent, Object child) { |
179 |
String childPath = ((ScriptingFolder) child).getFile().getAbsolutePath();
|
180 |
ScriptingFolder folder = (ScriptingFolder) parent; |
181 |
List<ScriptingFolder> children = folder.getUnitFolders();
|
182 |
for (int i = 0; i < children.size(); i++) { |
183 |
ScriptingUnit aChild = children.get(i); |
184 |
if (aChild.getFile().getAbsolutePath().equals(childPath)) {
|
185 |
return i;
|
186 |
} |
187 |
} |
188 |
return -1; |
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
public DefaultJScriptingBrowser(ScriptingUIManager uimanager,
|
194 |
ScriptingFolder root) { |
195 |
this(uimanager, root, false); |
196 |
} |
197 |
|
198 |
public DefaultJScriptingBrowser(ScriptingUIManager uimanager,
|
199 |
ScriptingFolder root, boolean foldersOnly) {
|
200 |
super(new GridLayout(1, 2)); |
201 |
this.foldersOnly = foldersOnly;
|
202 |
this.uimanager = uimanager;
|
203 |
this.manager = uimanager.getManager();
|
204 |
this.root = root;
|
205 |
this.makeUI();
|
206 |
} |
207 |
|
208 |
@Override
|
209 |
public JComponent asJComponent() { |
210 |
return this; |
211 |
} |
212 |
|
213 |
public void refresh() { |
214 |
((ScriptingFolderTreeModel) this.treeModel).reload();
|
215 |
} |
216 |
|
217 |
@Override
|
218 |
public ScriptingManager getManager() {
|
219 |
return this.manager; |
220 |
} |
221 |
|
222 |
@Override
|
223 |
public ScriptingFolder getRoot() {
|
224 |
return this.root; |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public void addDefaultActionListener(ActionListener actionlistener) { |
229 |
this.defaultActionlistener = actionlistener;
|
230 |
} |
231 |
|
232 |
@Override
|
233 |
public void addSelectionActionListener(ActionListener actionlistener) { |
234 |
this.selectionActionlistener = actionlistener;
|
235 |
} |
236 |
|
237 |
private void makeUI() { |
238 |
if (this.foldersOnly) { |
239 |
treeModel = new ScriptingFolderTreeModelFoldersOnly(root);
|
240 |
} else {
|
241 |
treeModel = new ScriptingFolderTreeModel(root);
|
242 |
} |
243 |
|
244 |
tree = new JTree(treeModel); |
245 |
tree.setCellRenderer(new IconCellRenderer());
|
246 |
tree.setEditable(false);
|
247 |
tree.setShowsRootHandles(true);
|
248 |
tree.getSelectionModel().setSelectionMode( |
249 |
TreeSelectionModel.SINGLE_TREE_SELECTION);
|
250 |
|
251 |
tree.addTreeSelectionListener(new TreeSelectionListener() { |
252 |
@Override
|
253 |
public void valueChanged(TreeSelectionEvent e) { |
254 |
if (selectionActionlistener != null) { |
255 |
JComponent tree = (JComponent) e.getSource(); |
256 |
ActionEvent event = new DefaultBrowserActionEvent( |
257 |
tree.getParent(), |
258 |
SELECTION_ACTION, |
259 |
"select",
|
260 |
null
|
261 |
); |
262 |
selectionActionlistener.actionPerformed(event); |
263 |
} |
264 |
} |
265 |
}); |
266 |
tree.addMouseListener(new MouseListener() { |
267 |
@Override
|
268 |
public void mouseClicked(MouseEvent arg0) { |
269 |
if (arg0.getClickCount() == 2) { |
270 |
JTree tree = (JTree) arg0.getSource(); |
271 |
if (!tree.isSelectionEmpty()) {
|
272 |
fireDefaultAction(tree.getSelectionPath()); |
273 |
} |
274 |
} |
275 |
} |
276 |
|
277 |
@Override
|
278 |
public void mouseEntered(MouseEvent arg0) { |
279 |
} |
280 |
|
281 |
@Override
|
282 |
public void mouseExited(MouseEvent arg0) { |
283 |
} |
284 |
|
285 |
@Override
|
286 |
public void mousePressed(MouseEvent arg0) { |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public void mouseReleased(MouseEvent arg0) { |
291 |
} |
292 |
}); |
293 |
tree.getActionMap().put( |
294 |
KeyStroke.getKeyStroke("+"), |
295 |
new ExpandSelectedNodeAction()
|
296 |
); |
297 |
tree.getActionMap().put( |
298 |
KeyStroke.getKeyStroke("SPACE"), |
299 |
new ExpandSelectedNodeAction()
|
300 |
); |
301 |
tree.getActionMap().put( |
302 |
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), |
303 |
new EnterAction()
|
304 |
); |
305 |
|
306 |
JScrollPane scrollPane = new JScrollPane(tree); |
307 |
add(scrollPane); |
308 |
} |
309 |
|
310 |
@Override
|
311 |
public ScriptingUnit getSelectedNode() {
|
312 |
ScriptingUnit unit; |
313 |
if (tree.getSelectionPath() != null) { |
314 |
unit = (ScriptingUnit) tree.getSelectionPath().getLastPathComponent(); |
315 |
} else {
|
316 |
unit = (ScriptingUnit) this.treeModel.getRoot();
|
317 |
} |
318 |
return unit;
|
319 |
} |
320 |
|
321 |
private void fireDefaultAction(TreePath path) { |
322 |
ScriptingUnit unit = (ScriptingUnit) path.getLastPathComponent(); |
323 |
if (unit instanceof ScriptingBaseScript) { |
324 |
if (this.defaultActionlistener != null) { |
325 |
ActionEvent event = new DefaultBrowserActionEvent( |
326 |
this,
|
327 |
DEFAULT_ACTION, |
328 |
"default",
|
329 |
(ScriptingBaseScript) unit |
330 |
); |
331 |
this.defaultActionlistener.actionPerformed(event);
|
332 |
} |
333 |
} |
334 |
} |
335 |
|
336 |
private class EnterAction extends AbstractAction { |
337 |
private static final long serialVersionUID = -3673546098243190397L; |
338 |
|
339 |
@Override
|
340 |
public void actionPerformed(ActionEvent ae) { |
341 |
TreePath selected = tree.getSelectionPath();
|
342 |
fireDefaultAction(selected); |
343 |
} |
344 |
|
345 |
} |
346 |
|
347 |
private class ExpandSelectedNodeAction extends AbstractAction { |
348 |
private static final long serialVersionUID = -4121272513454221967L; |
349 |
|
350 |
@Override
|
351 |
public void actionPerformed(ActionEvent ae) { |
352 |
try {
|
353 |
TreePath selected = tree.getSelectionPath();
|
354 |
if( tree.isCollapsed(selected) ) {
|
355 |
tree.expandPath(selected); |
356 |
} else {
|
357 |
tree.collapsePath(selected); |
358 |
} |
359 |
} catch(Throwable th) { |
360 |
// Do nothing, ignore errors
|
361 |
} |
362 |
} |
363 |
|
364 |
} |
365 |
|
366 |
private class IconCellRenderer extends JLabel implements TreeCellRenderer { |
367 |
|
368 |
/**
|
369 |
*
|
370 |
*/
|
371 |
private static final long serialVersionUID = 1L; |
372 |
protected Color m_textSelectionColor; |
373 |
protected Color m_textNonSelectionColor; |
374 |
protected Color m_bkSelectionColor; |
375 |
protected Color m_bkNonSelectionColor; |
376 |
protected Color m_borderSelectionColor; |
377 |
|
378 |
protected boolean m_selected; |
379 |
|
380 |
public IconCellRenderer() {
|
381 |
super();
|
382 |
m_textSelectionColor = UIManager
|
383 |
.getColor("Tree.selectionForeground");
|
384 |
m_textNonSelectionColor = UIManager.getColor("Tree.textForeground"); |
385 |
m_bkSelectionColor = UIManager.getColor("Tree.selectionBackground"); |
386 |
m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground"); |
387 |
m_borderSelectionColor = UIManager
|
388 |
.getColor("Tree.selectionBorderColor");
|
389 |
setOpaque(false);
|
390 |
} |
391 |
|
392 |
private final Map<String, ImageIcon> icons = new HashMap<>(); |
393 |
|
394 |
private ImageIcon getIcon(String iconName) { |
395 |
ImageIcon img = this.icons.get(iconName); |
396 |
if (img == null) { |
397 |
img = uimanager.getIcon(iconName); |
398 |
this.icons.put(iconName, img);
|
399 |
} |
400 |
return img;
|
401 |
} |
402 |
|
403 |
private ImageIcon getExpandedIcon(ScriptingUnit unit) { |
404 |
return getIcon(unit.getIconNames()[1]); |
405 |
} |
406 |
|
407 |
private ImageIcon getCollapsedIcon(ScriptingUnit unit) { |
408 |
return getIcon(unit.getIconNames()[0]); |
409 |
} |
410 |
|
411 |
@Override
|
412 |
public Component getTreeCellRendererComponent(JTree tree, Object value, |
413 |
boolean sel, boolean expanded, boolean leaf, int row, |
414 |
boolean hasFocus) {
|
415 |
ScriptingUnit unit = (ScriptingUnit) value; |
416 |
setText(unit.getName()); |
417 |
if (expanded) {
|
418 |
setIcon(this.getExpandedIcon(unit));
|
419 |
} else {
|
420 |
setIcon(this.getCollapsedIcon(unit));
|
421 |
} |
422 |
|
423 |
setFont(tree.getFont()); |
424 |
setForeground(sel ? m_textSelectionColor : m_textNonSelectionColor); |
425 |
setBackground(sel ? m_bkSelectionColor : m_bkNonSelectionColor); |
426 |
m_selected = sel; |
427 |
return this; |
428 |
} |
429 |
|
430 |
@Override
|
431 |
public void paintComponent(Graphics g) { |
432 |
Color bColor = getBackground();
|
433 |
Icon icon = getIcon();
|
434 |
|
435 |
g.setColor(bColor); |
436 |
int offset = 0; |
437 |
if (icon != null && getText() != null) { |
438 |
offset = (icon.getIconWidth() + getIconTextGap()); |
439 |
} |
440 |
g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); |
441 |
|
442 |
if (m_selected) {
|
443 |
g.setColor(m_borderSelectionColor); |
444 |
g.drawRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1); |
445 |
} |
446 |
super.paintComponent(g);
|
447 |
} |
448 |
} |
449 |
|
450 |
} |