Revision 468 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

View differences:

DefaultJScriptingBrowser.java
8 8
import java.awt.event.ActionListener;
9 9
import java.awt.event.MouseEvent;
10 10
import java.awt.event.MouseListener;
11
import java.io.File;
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.Iterator;
12 14
import java.util.List;
15
import java.util.Map;
13 16

  
14 17
import javax.swing.Icon;
18
import javax.swing.ImageIcon;
15 19
import javax.swing.JComponent;
16 20
import javax.swing.JLabel;
17 21
import javax.swing.JScrollPane;
18 22
import javax.swing.JTree;
19 23
import javax.swing.UIManager;
24
import javax.swing.event.TreeModelEvent;
25
import javax.swing.event.TreeModelListener;
20 26
import javax.swing.event.TreeSelectionEvent;
21 27
import javax.swing.event.TreeSelectionListener;
22
import javax.swing.tree.DefaultMutableTreeNode;
23
import javax.swing.tree.DefaultTreeModel;
24 28
import javax.swing.tree.TreeCellRenderer;
29
import javax.swing.tree.TreeModel;
25 30
import javax.swing.tree.TreePath;
26 31
import javax.swing.tree.TreeSelectionModel;
27 32

  
......
36 41

  
37 42
public class DefaultJScriptingBrowser extends JScriptingBrowser {
38 43

  
39
	private static final Logger logger = LoggerFactory.getLogger(DefaultJScriptingBrowser.class);
40
	
41
	public static final int DEFAULT_ACTION = 1;
42
	public static final int SELECTION_ACTION = 2;
43
	public static final int DROPDOWN_ACTION = 3;
44
	/**
45
	 * 
46
	 */
47
	private static final long serialVersionUID = 1L;
44
    private static final Logger logger = LoggerFactory.getLogger(DefaultJScriptingBrowser.class);
48 45

  
49
	protected ScriptingUIManager uimanager;
50
	protected ScriptingManager manager;
51
	protected DefaultMutableTreeNode rootNode;
52
	protected DefaultTreeModel treeModel;
53
	protected JTree tree;
54
	protected ScriptingFolder root;
55
	protected ActionListener defaultActionlistener = null;
56
	protected ActionListener selectionActionlistener = null;
57
	protected ActionListener dropDownActionlistener = null;
58
	private final boolean foldersOnly;
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;
59 55

  
60
	public static class BrowserActionEvent extends ActionEvent {
61
		/**
62
		 * 
63
		 */
64
		private static final long serialVersionUID = -1474410768278633364L;
65
		private ScriptingBaseScript script = null;
56
    public static class BrowserActionEvent extends ActionEvent {
66 57

  
67
		public BrowserActionEvent(Object source, int id, String command,
68
				ScriptingBaseScript script) {
69
			super(source, id, command);
70
			this.script = script;
71
		}
58
        /**
59
         *
60
         */
61
        private static final long serialVersionUID = -1474410768278633364L;
62
        private ScriptingBaseScript script = null;
72 63

  
73
		public ScriptingBaseScript getScript() {
74
			return this.script;
75
		}
76
	}
64
        public BrowserActionEvent(Object source, int id, String command,
65
                ScriptingBaseScript script) {
66
            super(source, id, command);
67
            this.script = script;
68
        }
77 69

  
78
	public DefaultJScriptingBrowser(ScriptingUIManager uimanager,
79
			ScriptingFolder root) {
80
		this(uimanager, root, false);
81
	}
70
        public ScriptingBaseScript getScript() {
71
            return this.script;
72
        }
73
    }
82 74

  
83
	public DefaultJScriptingBrowser(ScriptingUIManager uimanager,
84
			ScriptingFolder root, boolean foldersOnly) {
85
		super(new GridLayout(1, 2));
86
		this.foldersOnly = foldersOnly;
87
		this.uimanager = uimanager;
88
		this.manager = uimanager.getManager();
89
		this.root = root;
90
		this.makeUI();
91
	}
75
    class ScriptingFolderTreeModel implements TreeModel {
92 76

  
93
	@Override
94
	public ScriptingManager getManager() {
95
		return this.manager;
96
	}
77
        protected final ScriptingFolder root;
97 78

  
98
	@Override
99
	public ScriptingFolder getRoot() {
100
		return this.root;
101
	}
79
        protected final List<TreeModelListener> listeners = new ArrayList();
102 80

  
103
	public void addDefaultActionListener(ActionListener actionlistener) {
104
		this.defaultActionlistener = actionlistener;
105
	}
81
        public ScriptingFolderTreeModel(ScriptingFolder root) {
82
            this.root = root;
83
        }
106 84

  
107
	public void addSelectionActionListener(ActionListener actionlistener) {
108
		this.selectionActionlistener = actionlistener;
109
	}
85
        @Override
86
        public Object getRoot() {
87
            return root;
88
        }
110 89

  
111
	public void addDropDownActionListener(ActionListener actionlistener) {
112
		this.dropDownActionlistener = actionlistener;
113
	}
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
        }
114 96

  
115
	private void makeUI() {
116
		// Creamos el nodo raíz
117
		String[] iconNames = root.getIconNames();
118
		rootNode = new DefaultMutableTreeNode(new IconData(
119
				this.uimanager.getIcon(iconNames[0]),
120
				this.uimanager.getIcon(iconNames[1]), root));
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
        }
121 105

  
122
		// Lo asignamos al modelo del árbol e iniciamos el árbol con ese
123
		// modelo
124
		treeModel = new DefaultTreeModel(rootNode);
125
		tree = new JTree(treeModel);
106
        @Override
107
        public boolean isLeaf(Object node) {
108
            return !(node instanceof ScriptingFolder);
109
        }
126 110

  
127
		// Cargamos la información inicial del árbol
128
		this.initializeTree();
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
        }
129 124

  
130
		// Para asignar iconos especiales según el tipo de objeto
131
		TreeCellRenderer renderer = new IconCellRenderer();
132
		tree.setCellRenderer(renderer);
125
        @Override
126
        public void valueForPathChanged(TreePath path, Object value) {
127
        }
133 128

  
134
		// Para marcar que no son editables sus propiedades o nodos
135
		tree.setEditable(false);
129
        @Override
130
        public void addTreeModelListener(TreeModelListener listener) {
131
            listeners.add(listener);
132
        }
136 133

  
137
		// Determina que sólo se puede seleccionar una linea
138
		tree.getSelectionModel().setSelectionMode(
139
				TreeSelectionModel.SINGLE_TREE_SELECTION);
134
        @Override
135
        public void removeTreeModelListener(TreeModelListener listener) {
136
            listeners.remove(listener);
137
        }
140 138

  
141
		// Añadimos un listener que se ejecutará cuando seleccionemos un
142
		// elemento del árbol
143
		tree.addTreeSelectionListener(new TreeSelectionListener() {
144
			public void valueChanged(TreeSelectionEvent e) {
145
				loadChildrenLazily(e.getPath());
146
				if (selectionActionlistener != null) {
147
					JComponent tree = (JComponent) e.getSource();
148
					ActionEvent event = new BrowserActionEvent(
149
							tree.getParent(), SELECTION_ACTION, "select", null);
150
					selectionActionlistener.actionPerformed(event);
151
				}
152
			}
153
		});
154
		tree.addMouseListener(new MouseListener() {
155
			public void mouseClicked(MouseEvent arg0) {
156
				if (arg0.getClickCount() == 2) {
157
					JTree tree = (JTree) arg0.getSource();
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
    }
158 149

  
159
					if (!tree.isSelectionEmpty()) {
160
						startingScript(tree.getSelectionPath());
161
					}
150
    class ScriptingFolderTreeModelFoldersOnly extends ScriptingFolderTreeModel {
162 151

  
163
				}
164
			}
152
        public ScriptingFolderTreeModelFoldersOnly(ScriptingFolder root) {
153
            super(root);
154
        }
165 155

  
166
			public void mouseEntered(MouseEvent arg0) {
167
			}
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
        }
168 162

  
169
			public void mouseExited(MouseEvent arg0) {
170
			}
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 171

  
172
			public void mousePressed(MouseEvent arg0) {
173
			}
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
        }
174 185

  
175
			public void mouseReleased(MouseEvent arg0) {
176
			}
177
		});
186
    }
178 187

  
179
		// Indica si el nodo raíz actúa como un nodo más o queda desplegado
180
		// de manera fija
181
		tree.setShowsRootHandles(true);
188
    public DefaultJScriptingBrowser(ScriptingUIManager uimanager,
189
            ScriptingFolder root) {
190
        this(uimanager, root, false);
191
    }
182 192

  
183
		// Añadimos el Scroll
184
		JScrollPane scrollPane = new JScrollPane(tree);
185
		add(scrollPane);
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
    }
186 202

  
187
	}
203
    public void refresh() {
204
        ((ScriptingFolderTreeModel) this.treeModel).reload();
205
    }
188 206

  
189
	@Override
190
	public ScriptingUnit getSelectedNode() {
191
		DefaultMutableTreeNode node;
192
		if (tree.getSelectionPath() != null) {
193
			node = (DefaultMutableTreeNode) tree.getSelectionPath()
194
					.getLastPathComponent();
195
		} else {
196
			node = this.rootNode;
197
		}
198
		return ((ScriptingUnit) ((IconData) (node.getUserObject())).m_data);
199
	}
207
    @Override
208
    public ScriptingManager getManager() {
209
        return this.manager;
210
    }
200 211

  
201
	private void startingScript(TreePath path) {
202
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
203
				.getLastPathComponent();
204
		ScriptingUnit unit = ((ScriptingUnit) ((IconData) (node.getUserObject())).m_data);
212
    @Override
213
    public ScriptingFolder getRoot() {
214
        return this.root;
215
    }
205 216

  
206
		if (unit instanceof ScriptingBaseScript) {
207
			if (this.defaultActionlistener != null) {
208
				ActionEvent event = new BrowserActionEvent(this,
209
						DEFAULT_ACTION, "default", (ScriptingBaseScript) unit);
210
				this.defaultActionlistener.actionPerformed(event);
211
			}
212
		}
213
	}
217
    public void addDefaultActionListener(ActionListener actionlistener) {
218
        this.defaultActionlistener = actionlistener;
219
    }
214 220

  
215
	/**
216
	 * Función para el despliegue de los hijos de un nodo bajo demanda.
217
	 **/
218
	private void loadChildrenLazily(TreePath path) {
219
		// Mediante el path recuperamos el último nodo, del que
220
		// queremos obtener sus hijos dinámicamente
221
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
222
				.getLastPathComponent();
223
		ScriptingUnit unit = ((ScriptingUnit) ((IconData) (node.getUserObject())).m_data);
221
    public void addSelectionActionListener(ActionListener actionlistener) {
222
        this.selectionActionlistener = actionlistener;
223
    }
224 224

  
225
		List<ScriptingUnit> files = null;
225
    public void addDropDownActionListener(ActionListener actionlistener) {
226
        this.dropDownActionlistener = actionlistener;
227
    }
226 228

  
227
		// Si es un ScriptingFolder y no hemos obtenido previamente sus hijos
228
		if (unit instanceof ScriptingFolder && node.getChildCount() == 0) {
229
			// Obtenemos los hijos
230
			files = ((ScriptingFolder) unit).getUnits();
229
    private void makeUI() {
230
        if (this.foldersOnly) {
231
            treeModel = new ScriptingFolderTreeModelFoldersOnly(root);
232
        } else {
233
            treeModel = new ScriptingFolderTreeModel(root);
234
        }
231 235

  
232
			ScriptingUnit subunit = null;
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);
233 242

  
234
			for (int i = 0; i < files.size(); i++) {
235
				subunit = files.get(i);
236
				if ((this.foldersOnly && subunit instanceof ScriptingFolder)
237
						|| !this.foldersOnly) {
238
					// Obtenemos los iconos específicos para ese tipo de objeto
239
					String[] iconNames = subunit.getIconNames();
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
            }
240 268

  
241
					// Creamos el objeto IconData, con la información
242
					// del objeto y de hasta 2 iconos asociados
243
					IconData data = new IconData(
244
							this.uimanager.getIcon(iconNames[0]),
245
							this.uimanager.getIcon(iconNames[1]), subunit);
269
            @Override
270
            public void mouseEntered(MouseEvent arg0) {
271
            }
246 272

  
247
					// Insertamos en el árbol el nuevo objeto creado
248
					addObject(node, data);
249
				}
250
			}
251
		}
273
            @Override
274
            public void mouseExited(MouseEvent arg0) {
275
            }
252 276

  
253
	}
277
            @Override
278
            public void mousePressed(MouseEvent arg0) {
279
            }
254 280

  
255
	/**
256
	 * Función para la inicialización de los datos del árbol.
257
	 **/
258
	private void initializeTree() {
281
            @Override
282
            public void mouseReleased(MouseEvent arg0) {
283
            }
284
        });
285
        JScrollPane scrollPane = new JScrollPane(tree);
286
        add(scrollPane);
287
    }
259 288

  
260
		ScriptingUnit unit = null;
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
    }
261 299

  
262
		List<ScriptingUnit> files = this.root.getUnits();
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
    }
263 314

  
264
		for (int i = 0; i < files.size(); i++) {
265
			unit = files.get(i);
266
			String fname_unit = "(unknow)";
267
			try {
268
				fname_unit = unit.getFile().getAbsolutePath();
269
				if ((this.foldersOnly && unit instanceof ScriptingFolder)
270
						|| !this.foldersOnly) {
271
					String[] iconNames = unit.getIconNames();
315
    private class IconCellRenderer extends JLabel implements TreeCellRenderer {
272 316

  
273
					IconData data = new IconData(
274
							uimanager.getIcon(iconNames[0]),
275
							uimanager.getIcon(iconNames[1]), unit);
276
					// Insertamos en el nodo raiz del arbol
277
					this.addObject(this.rootNode, data);
278
				}
279
			} catch (Exception ex) {
280
				logger.warn("Can't add script unit '"+fname_unit+"' to the tree.", ex);
281
			}
282
		}
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;
283 326

  
284
	}
327
        protected boolean m_selected;
285 328

  
286
	/**
287
	 * Función para añadir un hijo al nodo actual.
288
	 **/
289
	// private DefaultMutableTreeNode addObject(Object child) {
290
	// DefaultMutableTreeNode parentNode = rootNode;
291
	// return addObject(parentNode, child, true);
292
	// }
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
        }
293 340

  
294
	private DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
295
			Object child) {
296
		return addObject(parent, child, false);
297
	}
341
        private Map<String, ImageIcon> icons = new HashMap<String, ImageIcon>();
298 342

  
299
	private DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
300
			Object child, boolean shouldBeVisible) {
301
		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
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
        }
302 351

  
303
		// Si no especificamos padre, lo colgará del raíz
304
		if (parent == null) {
305
			parent = rootNode;
306
		}
352
        private ImageIcon getExpandedIcon(ScriptingUnit unit) {
353
            return getIcon(unit.getIconNames()[1]);
354
        }
307 355

  
308
		// Insertamos el nuevo nodo en el modelo del árbol
309
		treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
356
        private ImageIcon getCollapsedIcon(ScriptingUnit unit) {
357
            return getIcon(unit.getIconNames()[0]);
358
        }
310 359

  
311
		// Para que el usuario pueda ver el nuevo nodo
312
		if (shouldBeVisible) {
313
			tree.scrollPathToVisible(new TreePath(childNode.getPath()));
314
		}
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
            }
315 370

  
316
		return childNode;
317
	}
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
        }
318 377

  
319
	/**
320
	 * Clase IconData permite asociar hasta dos iconos (para cuando está
321
	 * expandido y cuando no) a un objeto del árbol
322
	 **/
323
	static private class IconData {
324
		protected Icon m_icon;
325
		protected Icon m_expandedIcon;
326
		protected Object m_data;
378
        @Override
379
        public void paintComponent(Graphics g) {
380
            Color bColor = getBackground();
381
            Icon icon = getIcon();
327 382

  
328
		public IconData(Icon icon, Icon expandedIcon, Object data) {
329
			m_icon = icon;
330
			m_expandedIcon = expandedIcon;
331
			m_data = data;
332
		}
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);
333 389

  
334
		public Icon getIcon() {
335
			return m_icon;
336
		}
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
    }
337 397

  
338
		public Icon getExpandedIcon() {
339
			return m_expandedIcon != null ? m_expandedIcon : m_icon;
340
		}
341

  
342
		@Override
343
		public String toString() {
344
			return m_data.toString();
345
		}
346
	}
347

  
348
	private class IconCellRenderer extends JLabel implements TreeCellRenderer {
349

  
350
		/**
351
		 * 
352
		 */
353
		private static final long serialVersionUID = 1L;
354
		protected Color m_textSelectionColor;
355
		protected Color m_textNonSelectionColor;
356
		protected Color m_bkSelectionColor;
357
		protected Color m_bkNonSelectionColor;
358
		protected Color m_borderSelectionColor;
359

  
360
		protected boolean m_selected;
361

  
362
		public IconCellRenderer() {
363
			super();
364
			m_textSelectionColor = UIManager
365
					.getColor("Tree.selectionForeground");
366
			m_textNonSelectionColor = UIManager.getColor("Tree.textForeground");
367
			m_bkSelectionColor = UIManager.getColor("Tree.selectionBackground");
368
			m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground");
369
			m_borderSelectionColor = UIManager
370
					.getColor("Tree.selectionBorderColor");
371
			setOpaque(false);
372
		}
373

  
374
		public Component getTreeCellRendererComponent(JTree tree, Object value,
375
				boolean sel, boolean expanded, boolean leaf, int row,
376
				boolean hasFocus)
377

  
378
		{
379
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
380
			Object obj = node.getUserObject();
381
			setText(obj.toString());
382

  
383
			if (obj instanceof Boolean)
384
				setText(uimanager.getTranslation("Retrieving_data"));
385

  
386
			if (obj instanceof IconData) {
387
				IconData idata = (IconData) obj;
388
				if (expanded)
389
					setIcon(idata.getExpandedIcon());
390
				else
391
					setIcon(idata.getIcon());
392
			} else
393
				setIcon(null);
394

  
395
			setFont(tree.getFont());
396
			setForeground(sel ? m_textSelectionColor : m_textNonSelectionColor);
397
			setBackground(sel ? m_bkSelectionColor : m_bkNonSelectionColor);
398
			m_selected = sel;
399
			return this;
400
		}
401

  
402
		@Override
403
		public void paintComponent(Graphics g) {
404
			Color bColor = getBackground();
405
			Icon icon = getIcon();
406

  
407
			g.setColor(bColor);
408
			int offset = 0;
409
			if (icon != null && getText() != null)
410
				offset = (icon.getIconWidth() + getIconTextGap());
411
			g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
412

  
413
			if (m_selected) {
414
				g.setColor(m_borderSelectionColor);
415
				g.drawRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
416
			}
417
			super.paintComponent(g);
418
		}
419
	}
420

  
421 398
}

Also available in: Unified diff