Revision 442 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 12
import java.util.List;
12 13

  
13 14
import javax.swing.Icon;
......
30 31
import org.gvsig.scripting.ScriptingUnit;
31 32
import org.gvsig.scripting.swing.api.JScriptingBrowser;
32 33
import org.gvsig.scripting.swing.api.ScriptingUIManager;
34
import org.slf4j.LoggerFactory;
35
import org.slf4j.Logger;
33 36

  
37
public class DefaultJScriptingBrowser extends JScriptingBrowser {
34 38

  
35
public class DefaultJScriptingBrowser extends JScriptingBrowser{
36

  
39
	private static final Logger logger = LoggerFactory.getLogger(DefaultJScriptingBrowser.class);
37 40
	
38 41
	public static final int DEFAULT_ACTION = 1;
39 42
	public static final int SELECTION_ACTION = 2;
......
54 57
	protected ActionListener dropDownActionlistener = null;
55 58
	private final boolean foldersOnly;
56 59

  
57

  
58
	public static class BrowserActionEvent extends ActionEvent{
60
	public static class BrowserActionEvent extends ActionEvent {
59 61
		/**
60 62
		 * 
61 63
		 */
62 64
		private static final long serialVersionUID = -1474410768278633364L;
63
		private ScriptingBaseScript script= null;
64
		
65
		public BrowserActionEvent(Object source, int id, String command, ScriptingBaseScript script) {
66
			super(source,id,command);
65
		private ScriptingBaseScript script = null;
66

  
67
		public BrowserActionEvent(Object source, int id, String command,
68
				ScriptingBaseScript script) {
69
			super(source, id, command);
67 70
			this.script = script;
68 71
		}
69
		
70
		public ScriptingBaseScript getScript(){
72

  
73
		public ScriptingBaseScript getScript() {
71 74
			return this.script;
72 75
		}
73 76
	}
74 77

  
75
	
76
	public DefaultJScriptingBrowser(ScriptingUIManager uimanager,ScriptingFolder root) {
77
		this(uimanager,root,false);
78
	public DefaultJScriptingBrowser(ScriptingUIManager uimanager,
79
			ScriptingFolder root) {
80
		this(uimanager, root, false);
78 81
	}
79
	public DefaultJScriptingBrowser(ScriptingUIManager uimanager,ScriptingFolder root, boolean foldersOnly) {
80
		super(new GridLayout(1,2));
82

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

  
88 93
	@Override
89
    public ScriptingManager getManager(){
94
	public ScriptingManager getManager() {
90 95
		return this.manager;
91 96
	}
92
	
97

  
93 98
	@Override
94
    public ScriptingFolder getRoot(){
99
	public ScriptingFolder getRoot() {
95 100
		return this.root;
96 101
	}
97
	
102

  
98 103
	public void addDefaultActionListener(ActionListener actionlistener) {
99
		this.defaultActionlistener = actionlistener;  
104
		this.defaultActionlistener = actionlistener;
100 105
	}
101
	
106

  
102 107
	public void addSelectionActionListener(ActionListener actionlistener) {
103
		this.selectionActionlistener = actionlistener;  
108
		this.selectionActionlistener = actionlistener;
104 109
	}
105
	
110

  
106 111
	public void addDropDownActionListener(ActionListener actionlistener) {
107
		this.dropDownActionlistener = actionlistener;  
112
		this.dropDownActionlistener = actionlistener;
108 113
	}
109
		
110
	private void makeUI(){
111
		//Creamos el nodo raíz
114

  
115
	private void makeUI() {
116
		// Creamos el nodo raíz
112 117
		String[] iconNames = root.getIconNames();
113 118
		rootNode = new DefaultMutableTreeNode(new IconData(
114
				this.uimanager.getIcon(iconNames[0]),this.uimanager.getIcon(iconNames[1]), root)
115
		);
119
				this.uimanager.getIcon(iconNames[0]),
120
				this.uimanager.getIcon(iconNames[1]), root));
116 121

  
117
		// Lo asignamos al modelo del árbol e iniciamos el árbol con ese modelo
122
		// Lo asignamos al modelo del árbol e iniciamos el árbol con ese
123
		// modelo
118 124
		treeModel = new DefaultTreeModel(rootNode);
119 125
		tree = new JTree(treeModel);
120 126

  
......
125 131
		TreeCellRenderer renderer = new IconCellRenderer();
126 132
		tree.setCellRenderer(renderer);
127 133

  
128
		//Para marcar que no son editables sus propiedades o nodos
134
		// Para marcar que no son editables sus propiedades o nodos
129 135
		tree.setEditable(false);
130 136

  
131
		//Determina que sólo se puede seleccionar una linea
132
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
137
		// Determina que sólo se puede seleccionar una linea
138
		tree.getSelectionModel().setSelectionMode(
139
				TreeSelectionModel.SINGLE_TREE_SELECTION);
133 140

  
134
		// Añadimos un listener que se ejecutará cuando seleccionemos un elemento del árbol
141
		// Añadimos un listener que se ejecutará cuando seleccionemos un
142
		// elemento del árbol
135 143
		tree.addTreeSelectionListener(new TreeSelectionListener() {
136
			public void valueChanged(TreeSelectionEvent e) {	
144
			public void valueChanged(TreeSelectionEvent e) {
137 145
				loadChildrenLazily(e.getPath());
138
				if(selectionActionlistener!=null){
146
				if (selectionActionlistener != null) {
139 147
					JComponent tree = (JComponent) e.getSource();
140
					ActionEvent event = new BrowserActionEvent(tree.getParent(),SELECTION_ACTION,"select",null);
148
					ActionEvent event = new BrowserActionEvent(
149
							tree.getParent(), SELECTION_ACTION, "select", null);
141 150
					selectionActionlistener.actionPerformed(event);
142 151
				}
143 152
			}
......
146 155
			public void mouseClicked(MouseEvent arg0) {
147 156
				if (arg0.getClickCount() == 2) {
148 157
					JTree tree = (JTree) arg0.getSource();
149
					
150
						if(!tree.isSelectionEmpty()){
151
							startingScript(tree.getSelectionPath());
152
						}
153
					
158

  
159
					if (!tree.isSelectionEmpty()) {
160
						startingScript(tree.getSelectionPath());
161
					}
162

  
154 163
				}
155 164
			}
156 165

  
......
167 176
			}
168 177
		});
169 178

  
170
		//Indica si el nodo raíz actúa como un nodo más o queda desplegado de manera fija
179
		// Indica si el nodo raíz actúa como un nodo más o queda desplegado
180
		// de manera fija
171 181
		tree.setShowsRootHandles(true);
172 182

  
173
		//Añadimos el Scroll
183
		// Añadimos el Scroll
174 184
		JScrollPane scrollPane = new JScrollPane(tree);
175 185
		add(scrollPane);
176
			
186

  
177 187
	}
178
	
188

  
179 189
	@Override
180
    public ScriptingUnit getSelectedNode(){
190
	public ScriptingUnit getSelectedNode() {
181 191
		DefaultMutableTreeNode node;
182
		if(tree.getSelectionPath()!=null){
183
			node = (DefaultMutableTreeNode)tree.getSelectionPath().getLastPathComponent();
184
		}else{
192
		if (tree.getSelectionPath() != null) {
193
			node = (DefaultMutableTreeNode) tree.getSelectionPath()
194
					.getLastPathComponent();
195
		} else {
185 196
			node = this.rootNode;
186 197
		}
187
		return ((ScriptingUnit)((IconData)(node.getUserObject())).m_data);
198
		return ((ScriptingUnit) ((IconData) (node.getUserObject())).m_data);
188 199
	}
189
	
190
	private void startingScript(TreePath path){
191
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
192
		ScriptingUnit unit = ((ScriptingUnit)((IconData)(node.getUserObject())).m_data);
193
		
194
		if (unit instanceof ScriptingBaseScript){
195
			if(this.defaultActionlistener!=null){
196
				ActionEvent event = new BrowserActionEvent(this,DEFAULT_ACTION,"default", (ScriptingBaseScript) unit);
200

  
201
	private void startingScript(TreePath path) {
202
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
203
				.getLastPathComponent();
204
		ScriptingUnit unit = ((ScriptingUnit) ((IconData) (node.getUserObject())).m_data);
205

  
206
		if (unit instanceof ScriptingBaseScript) {
207
			if (this.defaultActionlistener != null) {
208
				ActionEvent event = new BrowserActionEvent(this,
209
						DEFAULT_ACTION, "default", (ScriptingBaseScript) unit);
197 210
				this.defaultActionlistener.actionPerformed(event);
198 211
			}
199 212
		}
200 213
	}
201
	
202 214

  
203
	/** 
204
	 * Función para el despliegue de los hijos de un nodo bajo demanda. 
205
	 **/ 
206
	private void loadChildrenLazily(TreePath path){
215
	/**
216
	 * Función para el despliegue de los hijos de un nodo bajo demanda.
217
	 **/
218
	private void loadChildrenLazily(TreePath path) {
207 219
		// Mediante el path recuperamos el último nodo, del que
208 220
		// queremos obtener sus hijos dinámicamente
209
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
210
		ScriptingUnit unit = ((ScriptingUnit)((IconData)(node.getUserObject())).m_data);
211
		
212
        List<ScriptingUnit> files = null;
213
		
221
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
222
				.getLastPathComponent();
223
		ScriptingUnit unit = ((ScriptingUnit) ((IconData) (node.getUserObject())).m_data);
224

  
225
		List<ScriptingUnit> files = null;
226

  
214 227
		// Si es un ScriptingFolder y no hemos obtenido previamente sus hijos
215
		if (unit instanceof ScriptingFolder && node.getChildCount() == 0){
228
		if (unit instanceof ScriptingFolder && node.getChildCount() == 0) {
216 229
			// Obtenemos los hijos
217
			files = ((ScriptingFolder)unit).getUnits();
230
			files = ((ScriptingFolder) unit).getUnits();
218 231

  
219 232
			ScriptingUnit subunit = null;
220 233

  
221
			for (int i = 0; i < files.size(); i++){
234
			for (int i = 0; i < files.size(); i++) {
222 235
				subunit = files.get(i);
223
				if((this.foldersOnly && subunit instanceof ScriptingFolder) || !this.foldersOnly){
236
				if ((this.foldersOnly && subunit instanceof ScriptingFolder)
237
						|| !this.foldersOnly) {
224 238
					// Obtenemos los iconos específicos para ese tipo de objeto
225 239
					String[] iconNames = subunit.getIconNames();
226
					
240

  
227 241
					// Creamos el objeto IconData, con la información
228 242
					// del objeto y de hasta 2 iconos asociados
229 243
					IconData data = new IconData(
230 244
							this.uimanager.getIcon(iconNames[0]),
231
							this.uimanager.getIcon(iconNames[1]),
232
							subunit
233
					);
234
	
245
							this.uimanager.getIcon(iconNames[1]), subunit);
246

  
235 247
					// Insertamos en el árbol el nuevo objeto creado
236
					addObject(node,data);
248
					addObject(node, data);
237 249
				}
238 250
			}
239 251
		}
240
		
252

  
241 253
	}
242 254

  
243
	/** 
255
	/**
244 256
	 * Función para la inicialización de los datos del árbol.
245 257
	 **/
246
	private void initializeTree(){
258
	private void initializeTree() {
247 259

  
248 260
		ScriptingUnit unit = null;
249 261

  
250
        List<ScriptingUnit> files = this.root.getUnits();
262
		List<ScriptingUnit> files = this.root.getUnits();
251 263

  
252
		for (int i = 0; i < files.size(); i++){
264
		for (int i = 0; i < files.size(); i++) {
253 265
			unit = files.get(i);
254
			if((this.foldersOnly && unit instanceof ScriptingFolder) || !this.foldersOnly){
255
			String[] iconNames = unit.getIconNames();
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();
256 272

  
257
			IconData data = new IconData(
258
					uimanager.getIcon(iconNames[0]),
259
					uimanager.getIcon(iconNames[1]),
260
					unit
261
			);
262

  
263
			// Insertamos en el nodo raíz del árbol
264
			this.addObject(this.rootNode,data);
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);
265 281
			}
266 282
		}
267 283

  
268 284
	}
269 285

  
270

  
271
	/** 
272
	 * Función para añadir un hijo al nodo actual. 
286
	/**
287
	 * Función para añadir un hijo al nodo actual.
273 288
	 **/
274
//	private DefaultMutableTreeNode addObject(Object child) {
275
//		DefaultMutableTreeNode parentNode = rootNode;
276
//		return addObject(parentNode, child, true);
277
//	}
289
	// private DefaultMutableTreeNode addObject(Object child) {
290
	// DefaultMutableTreeNode parentNode = rootNode;
291
	// return addObject(parentNode, child, true);
292
	// }
278 293

  
279 294
	private DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
280 295
			Object child) {
......
282 297
	}
283 298

  
284 299
	private DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
285
			Object child, 
286
			boolean shouldBeVisible) {
300
			Object child, boolean shouldBeVisible) {
287 301
		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
288
		
302

  
289 303
		// Si no especificamos padre, lo colgará del raíz
290 304
		if (parent == null) {
291 305
			parent = rootNode;
292 306
		}
293
		
307

  
294 308
		// Insertamos el nuevo nodo en el modelo del árbol
295
		treeModel.insertNodeInto(childNode, parent, 
296
				parent.getChildCount());
309
		treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
297 310

  
298 311
		// Para que el usuario pueda ver el nuevo nodo
299 312
		if (shouldBeVisible) {
......
304 317
	}
305 318

  
306 319
	/**
307
	 * Clase IconData permite asociar hasta dos iconos (para 
308
	 * cuando está expandido y cuando no) a un objeto del árbol
320
	 * Clase IconData permite asociar hasta dos iconos (para cuando está
321
	 * expandido y cuando no) a un objeto del árbol
309 322
	 **/
310
	static private class IconData
311
	{
312
		protected Icon   m_icon;
313
		protected Icon   m_expandedIcon;
323
	static private class IconData {
324
		protected Icon m_icon;
325
		protected Icon m_expandedIcon;
314 326
		protected Object m_data;
315 327

  
316
		public IconData(Icon icon, Icon expandedIcon, Object data)
317
		{
328
		public IconData(Icon icon, Icon expandedIcon, Object data) {
318 329
			m_icon = icon;
319 330
			m_expandedIcon = expandedIcon;
320 331
			m_data = data;
321 332
		}
322 333

  
323
		public Icon getIcon() 
324
		{ 
334
		public Icon getIcon() {
325 335
			return m_icon;
326 336
		}
327 337

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

  
333 342
		@Override
334
        public String toString() 
335
		{ 
343
		public String toString() {
336 344
			return m_data.toString();
337 345
		}
338 346
	}
......
351 359

  
352 360
		protected boolean m_selected;
353 361

  
354
		public IconCellRenderer()
355
		{
362
		public IconCellRenderer() {
356 363
			super();
357
			m_textSelectionColor = UIManager.getColor(
358
			"Tree.selectionForeground");
359
			m_textNonSelectionColor = UIManager.getColor(
360
			"Tree.textForeground");
361
			m_bkSelectionColor = UIManager.getColor(
362
			"Tree.selectionBackground");
363
			m_bkNonSelectionColor = UIManager.getColor(
364
			"Tree.textBackground");
365
			m_borderSelectionColor = UIManager.getColor(
366
			"Tree.selectionBorderColor");
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");
367 371
			setOpaque(false);
368 372
		}
369 373

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

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

  
380 383
			if (obj instanceof Boolean)
381 384
				setText("Retrieving data...");
382 385

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

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

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

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

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

Also available in: Unified diff