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