Revision 34928 branches/v2_0_0_prep/applications/appgvSIG/src/org/gvsig/app/project/documents/view/toc/gui/TOC.java

View differences:

TOC.java
58 58
import java.awt.event.InputEvent;
59 59
import java.awt.event.MouseAdapter;
60 60
import java.awt.event.MouseEvent;
61
import java.text.MessageFormat;
61 62
import java.util.ArrayList;
62 63
import java.util.Enumeration;
63 64
import java.util.HashMap;
65
import java.util.Map;
64 66

  
65 67
import javax.swing.BorderFactory;
66 68
import javax.swing.JColorChooser;
......
77 79
import javax.swing.tree.TreePath;
78 80
import javax.swing.tree.TreeSelectionModel;
79 81

  
82
import org.slf4j.Logger;
83
import org.slf4j.LoggerFactory;
84

  
80 85
import org.gvsig.andami.PluginServices;
81 86
import org.gvsig.andami.messages.NotificationManager;
82 87
import org.gvsig.app.project.documents.view.IContextMenuAction;
......
85 90
import org.gvsig.app.project.documents.view.toc.ITocOrderListener;
86 91
import org.gvsig.app.project.documents.view.toc.TocItemBranch;
87 92
import org.gvsig.app.project.documents.view.toc.TocItemLeaf;
88
import org.gvsig.app.project.documents.view.toc.actions.LayersUngroupTocMenuEntry;
89 93
import org.gvsig.fmap.dal.exception.ReadException;
90 94
import org.gvsig.fmap.mapcontext.MapContext;
91 95
import org.gvsig.fmap.mapcontext.events.AtomicEvent;
......
99 103
import org.gvsig.fmap.mapcontext.layers.operations.Classifiable;
100 104
import org.gvsig.fmap.mapcontext.layers.operations.IHasImageLegend;
101 105
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection;
102
import org.gvsig.fmap.mapcontext.layers.operations.LayerNotFoundInCollectionException;
103 106
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
104 107
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedLegend;
105 108
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
......
108 111
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
109 112
import org.gvsig.fmap.mapcontext.rendering.legend.events.listeners.LegendListener;
110 113
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
111
import org.slf4j.Logger;
112
import org.slf4j.LoggerFactory;
113 114

  
114 115

  
115 116

  
116 117
/**
117
 * DOCUMENT ME!
118 118
 *
119
 * @author fjp To change the template for this generated type comment go to
120
 *         Window>Preferences>Java>Code Generation>Code and Comments
119
 * @author fjp 
121 120
 */
122 121
public class TOC extends JComponent implements ITocOrderListener,
123 122
		LegendListener, LayerCollectionListener, TreeExpansionListener,
124 123
		ComponentListener {
125 124
	/**
125
     * 
126
     */
127
    private static final long serialVersionUID = 5689047685537359038L;
128

  
129
    /**
126 130
	 * Useful for debug the problems during the implementation.
127 131
	 */
128 132
	private static Logger logger = LoggerFactory.getLogger(TOC.class);
......
140 144
	private JScrollPane m_Scroller;
141 145

  
142 146
	// private ArrayList m_Listeners;
143
	private HashMap m_ItemsExpanded = new HashMap();
147
	private Map<String,Boolean> m_ItemsExpanded = new HashMap<String,Boolean>();
144 148

  
145 149
	private NodeSelectionListener nodeSelectionListener = null;
146 150

  
......
220 224
			/**
221 225
			 * @see org.gvsig.fmap.mapcontext.events.listeners.AtomicEventListener#atomicEvent(org.gvsig.fmap.mapcontext.events.AtomicEvent)
222 226
			 */
223
			public void atomicEvent(AtomicEvent e) {
227
			public void atomicEvent(final AtomicEvent e) {
228
		        if( !SwingUtilities.isEventDispatchThread() ) {
229
		            SwingUtilities.invokeLater(new Runnable() {
230
		                public void run() {
231
		                    atomicEvent(e);
232
		                }
233
		            });
234
		            return;
235
		        }       
236

  
224 237
				if ((e.getLayerCollectionEvents().length > 0)
225 238
						|| (e.getLegendEvents().length > 0)) {
226
					SwingUtilities.invokeLater(new Runnable() {
227
						public void run() {
228
							refresh();
229
						}
230
					});
239
						refresh();
231 240
				}
232 241

  
233 242
				if (e.getLayerEvents().length > 0) {
......
240 249
			}
241 250
		});
242 251

  
243
		SwingUtilities.invokeLater(new Runnable() {
244
			public void run() {
245
				refresh();
246
			}
247
		});
252
		refresh();
248 253
	}
249 254

  
250 255
	/**
......
256 261
		// por layerRemove, se quitan en el evento layerRemoved
257 262
		// de este TOC
258 263
		DefaultMutableTreeNode n;
259
		Enumeration enumeration = node.children();
264
		@SuppressWarnings("rawtypes")
265
        Enumeration enumeration = node.children();
260 266

  
261 267
		while (enumeration.hasMoreElements()) {
262 268
			n = (DefaultMutableTreeNode) enumeration.nextElement();
......
269 275

  
270 276
			if (b == null) // No estaba en el hash todav?a: valor por defecto
271 277
			{
272
				// System.out.println("Primera expansi?n de " +
273
				// item.getLabel());
274 278
				m_Tree.expandPath(path);
275 279

  
276 280
				return;
277 281
			}
278 282

  
279 283
			if (b.booleanValue()) {
280
				// System.out.println("Expansi?n de " + item.getLabel());
281 284
				m_Tree.expandPath(path);
282 285
			} else {
283
				// System.out.println("Colapso de " + item.getLabel());
284 286
				m_Tree.collapsePath(path);
285 287
			}
286 288
		}
......
292 294
	 * @see com.iver.cit.opensig.gui.IToc#refresh()
293 295
	 */
294 296
	public void refresh() {
297
        if( !SwingUtilities.isEventDispatchThread() ) {
298
            SwingUtilities.invokeLater(new Runnable() {
299
                public void run() {
300
                    refresh();
301
                }
302
            });
303
            return;
304
        }       
295 305
		LayerCollection theLayers = mapContext.getLayers();
296 306
		m_Root.removeAllChildren();
297 307
		m_Root.setAllowsChildren(true);
298
		// System.out.println("Refresh del toc");
299 308
		doRefresh(theLayers, m_Root);
300 309

  
301 310
		m_TreeModel.reload();
......
303 312
		setExpandedNodes(m_Root);
304 313
	}
305 314

  
306
	/**
307
	 * DOCUMENT ME!
308
	 *
309
	 * @param theLayers
310
	 *            DOCUMENT ME!
311
	 * @param parentNode
312
	 *            DOCUMENT ME!
313
	 */
314 315
	private void doRefresh(LayerCollection theLayers,
315 316
			DefaultMutableTreeNode parentNode) {
316 317
		Dimension sizeLeaf = new Dimension(m_Tree.getWidth(), 15);
......
330 331
			m_TreeModel.insertNodeInto(nodeLayer, parentNode, parentNode
331 332
					.getChildCount());
332 333

  
333
			// TreePath path = new
334
			// TreePath(m_TreeModel.getPathToRoot(nodeLayer));
335
			// m_Tree.makeVisible(path);
336 334
			if (lyr instanceof LayerCollection) {
337 335
				LayerCollection group = (LayerCollection) lyr;
338 336
				doRefresh(group, nodeLayer);
......
360 358
								m_TreeModel.insertNodeInto(nodeValue,
361 359
										nodeLayer, nodeLayer.getChildCount());
362 360

  
363
								// TreePath pathSymbol = new
364
								// TreePath(m_TreeModel.getPathToRoot(
365
								// nodeValue));
366
								// m_Tree.makeVisible(pathSymbol);
367 361
							}
368 362
						}
369 363

  
......
380 374
									itemLeaf);
381 375
							m_TreeModel.insertNodeInto(nodeValue, nodeLayer,
382 376
									nodeLayer.getChildCount());
383

  
384
							// TreePath pathSymbol = new
385
							// TreePath(m_TreeModel.getPathToRoot(
386
							// nodeValue));
387
							// m_Tree.makeVisible(pathSymbol);
388 377
						}
389 378
					} catch (ReadException e) {
390
						e.printStackTrace();
379
					    logger.error(
380
					        MessageFormat.format("Can't add leyend of layer {0} to the TOC.", lyr),
381
					        e
382
					     );
391 383
					}
392 384
				} else if (lyr instanceof IHasImageLegend) {
393 385
					TocItemLeaf itemLeaf;
......
415 407
	 *      int)
416 408
	 */
417 409
	public void orderChanged(int oldPos, int newPos, FLayers lpd) {
418
		// LayerCollection layers = mapContext.getLayers();
419
		// El orden es el contrario, hay que traducir.
420
		// El orden es el contrario, hay que traducir.
421
		// /oldPos = layers.getLayersCount() - 1 - oldPos;
422
		// /newPos = layers.getLayersCount() - 1 - newPos;
423 410
		try {
424 411
			lpd.moveTo(oldPos, newPos);
425 412
		} catch (CancelationException e) {
426
			// TODO Auto-generated catch block
427
			e.printStackTrace();
413
		    logger.error("Can't change order of layers in TOC", e);
428 414
		}
429

  
430
		// No hace falta un refresh, lo hace mediante eventos.
431
		// refresh();
432 415
		mapContext.invalidate();
433 416
	}
434 417

  
435
	/**
436
	 * DOCUMENT ME!
437
	 *
438
	 * @param lpo
439
	 *            DOCUMENT ME!
440
	 * @param lpd
441
	 *            DOCUMENT ME!
442
	 * @param ls
443
	 *            DOCUMENT ME!
444
	 */
445 418
	public void parentChanged(FLayers lpo, FLayers lpd, FLayer ls) {
446 419
		lpo.move(ls, lpd);
447

  
448
		/*
449
		 * if (lpo.getLayersCount()==0){ lpo.getParentLayer().removeLayer(lpo); }
450
		 */
451 420
		mapContext.invalidate();
452 421
	}
453 422

  
......
473 442
	 * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
474 443
	 */
475 444
	public void componentResized(ComponentEvent e) {
476
		System.out.println("Cambiando tama?o.");
477

  
478
		int i = 0;
479 445
		DefaultMutableTreeNode n;
480
		Enumeration enumeration = m_Root.children();
446
		@SuppressWarnings("rawtypes")
447
        Enumeration enumeration = m_Root.children();
481 448

  
482 449
		while (enumeration.hasMoreElements()) {
483 450
			n = (DefaultMutableTreeNode) enumeration.nextElement();
......
489 456
			}
490 457

  
491 458
		}
492

  
493
		// m_Tree.setSize(this.getSize());
494
		System.out.println("Ancho del tree=" + m_Tree.getWidth() + " "
495
				+ m_Tree.getComponentCount());
496
		System.out.println("Ancho del TOC=" + this.getWidth());
497

  
498
		// m_Tree.repaint();
499 459
	}
500 460

  
501 461
	/*
......
511 471
	 *
512 472
	 * @see com.iver.cit.gvsig.fmap.layers.LayerListener#legendChanged(com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent)
513 473
	 */
514
	public void legendChanged(LegendChangedEvent e) {
515
		System.out.println("Refrescando TOC");
474
	public void legendChanged(final LegendChangedEvent e) {
475
	    if( !SwingUtilities.isEventDispatchThread() ) {
476
	        SwingUtilities.invokeLater(new Runnable() {
477
                public void run() {
478
                    legendChanged(e);
479
                }
480
            });
481
	        return;
482
	    }
516 483
		refresh();
517 484
	}
518 485

  
......
521 488
	 *
522 489
	 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
523 490
	 */
524
	public void layerAdded(LayerCollectionEvent e) {
525
		refresh();
491
	public void layerAdded(final LayerCollectionEvent e) {
492
        if( !SwingUtilities.isEventDispatchThread() ) {
493
            SwingUtilities.invokeLater(new Runnable() {
494
                public void run() {
495
                    layerAdded(e);
496
                }
497
            });
498
            return;
499
        }		
500
        refresh();
526 501
	}
527 502

  
528 503
	/*
......
530 505
	 *
531 506
	 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
532 507
	 */
533
	public void layerMoved(LayerPositionEvent e) {
508
	public void layerMoved(final LayerPositionEvent e) {
509
        if( !SwingUtilities.isEventDispatchThread() ) {
510
            SwingUtilities.invokeLater(new Runnable() {
511
                public void run() {
512
                    layerMoved(e);
513
                }
514
            });
515
            return;
516
        }       
534 517
		refresh();
535 518
	}
536 519

  
......
539 522
	 *
540 523
	 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
541 524
	 */
542
	public void layerRemoved(LayerCollectionEvent e) {
525
	public void layerRemoved(final LayerCollectionEvent e) {
526
        if( !SwingUtilities.isEventDispatchThread() ) {
527
            SwingUtilities.invokeLater(new Runnable() {
528
                public void run() {
529
                    layerRemoved(e);
530
                }
531
            });
532
            return;
533
        }       
543 534
		m_ItemsExpanded.remove(e.getAffectedLayer().getName());
544 535
		refresh();
545 536
	}
......
584 575
	 *
585 576
	 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
586 577
	 */
587
	public void visibilityChanged(LayerCollectionEvent e)
578
	public void visibilityChanged(final LayerCollectionEvent e)
588 579
			throws CancelationException {
580
        if( !SwingUtilities.isEventDispatchThread() ) {
581
            SwingUtilities.invokeLater(new Runnable() {
582
                public void run() {
583
                    visibilityChanged(e);
584
                }
585
            });
586
            return;
587
        }       
589 588
		repaint();
590 589
	}
591 590

  
......
603 602
			ITocItem item = (ITocItem) n.getUserObject();
604 603
			Boolean b = Boolean.FALSE;
605 604

  
606
			// System.out.println("Collapsed: " + item.getLabel());
607 605
			m_ItemsExpanded.put(item.getLabel(), b);
608 606
		}
609 607
	}
......
622 620
			ITocItem item = (ITocItem) n.getUserObject();
623 621
			Boolean b = Boolean.TRUE;
624 622

  
625
			// System.out.println("Expanded: " + item.getLabel());
626 623
			m_ItemsExpanded.put(item.getLabel(), b);
627 624
		}
628 625
	}
......
683 680
			TreePath path = tree.getPathForRow(row);
684 681
			LayerCollection layers = mapContext.getLayers();
685 682

  
686
			// System.out.println(e.getSource());
687 683
			if (path != null) {
688 684
				if (e.getClickCount() == 1) {
689 685
					// this fixes a bug when double-clicking. JTree by default
......
692 688
					node = (DefaultMutableTreeNode) path.getLastPathComponent();
693 689
				}
694 690

  
695
				// System.out.println("Evento de rat?n originado por " +
696
				// e.getSource().getClass().toString());
697 691
				if (node != null
698 692
						&& node.getUserObject() instanceof TocItemBranch) {
699 693
					// double click with left button ON A BRANCH/NODE (layer)
......
1065 1059
		 *            DOCUMENT ME!
1066 1060
		 */
1067 1061
		public void mouseEntered(MouseEvent arg0) {
1068
			// TODO Auto-generated method stub
1069 1062
			super.mouseEntered(arg0);
1070

  
1071
			// FJP: COMENTO ESTO.
1072
			// LO CORRECTO CREO QUE ES IMPLEMENTAR CORRECTAMENTE
1073
			// LOS METODOS DE DRAG AND DROP
1074

  
1075
			/*
1076
			 * if (m_Root.getChildCount()==0){ m_Tree.dropRoot(m_Root); }
1077
			 */
1078 1063
		}
1079 1064
	}
1080 1065

  

Also available in: Unified diff