Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / gui / TOC.java @ 30011

History | View | Annotate | Download (30.1 KB)

1
/*
2
 * Created on 13-may-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package org.gvsig.app.project.documents.view.toc.gui;
48

    
49
import java.awt.BorderLayout;
50
import java.awt.Dimension;
51
import java.awt.Image;
52
import java.awt.Point;
53
import java.awt.Rectangle;
54
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
56
import java.awt.event.ComponentEvent;
57
import java.awt.event.ComponentListener;
58
import java.awt.event.InputEvent;
59
import java.awt.event.MouseAdapter;
60
import java.awt.event.MouseEvent;
61
import java.util.ArrayList;
62
import java.util.Enumeration;
63
import java.util.HashMap;
64

    
65
import javax.swing.BorderFactory;
66
import javax.swing.JColorChooser;
67
import javax.swing.JComponent;
68
import javax.swing.JDialog;
69
import javax.swing.JScrollPane;
70
import javax.swing.JTree;
71
import javax.swing.SwingUtilities;
72
import javax.swing.UIManager;
73
import javax.swing.event.TreeExpansionEvent;
74
import javax.swing.event.TreeExpansionListener;
75
import javax.swing.tree.DefaultMutableTreeNode;
76
import javax.swing.tree.DefaultTreeModel;
77
import javax.swing.tree.TreePath;
78
import javax.swing.tree.TreeSelectionModel;
79

    
80
import org.gvsig.andami.PluginServices;
81
import org.gvsig.andami.messages.NotificationManager;
82
import org.gvsig.app.project.documents.IContextMenuAction;
83
import org.gvsig.app.project.documents.view.toc.DnDJTree;
84
import org.gvsig.app.project.documents.view.toc.ITocItem;
85
import org.gvsig.app.project.documents.view.toc.ITocOrderListener;
86
import org.gvsig.app.project.documents.view.toc.TocItemBranch;
87
import org.gvsig.app.project.documents.view.toc.TocItemLeaf;
88
import org.gvsig.fmap.dal.exception.ReadException;
89
import org.gvsig.fmap.mapcontext.MapContext;
90
import org.gvsig.fmap.mapcontext.events.AtomicEvent;
91
import org.gvsig.fmap.mapcontext.events.listeners.AtomicEventListener;
92
import org.gvsig.fmap.mapcontext.layers.CancelationException;
93
import org.gvsig.fmap.mapcontext.layers.FLayer;
94
import org.gvsig.fmap.mapcontext.layers.FLayers;
95
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
96
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
97
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
98
import org.gvsig.fmap.mapcontext.layers.operations.Classifiable;
99
import org.gvsig.fmap.mapcontext.layers.operations.IHasImageLegend;
100
import org.gvsig.fmap.mapcontext.layers.operations.LayerCollection;
101
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
102
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
103
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
104
import org.gvsig.fmap.mapcontext.rendering.legend.events.LegendChangedEvent;
105
import org.gvsig.fmap.mapcontext.rendering.legend.events.listeners.LegendListener;
106
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
107
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.IClassifiedLegend;
108
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.ISingleSymbolLegend;
109

    
110

    
111
/**
112
 * DOCUMENT ME!
113
 *
114
 * @author fjp To change the template for this generated type comment go to
115
 *         Window>Preferences>Java>Code Generation>Code and Comments
116
 */
117
public class TOC extends JComponent implements ITocOrderListener,
118
                LegendListener, LayerCollectionListener, TreeExpansionListener,
119
                ComponentListener {
120
        private MapContext mapContext;
121

    
122
        private DnDJTree m_Tree;
123

    
124
        private DefaultTreeModel m_TreeModel;
125

    
126
        private DefaultMutableTreeNode m_Root;
127

    
128
        private TOCRenderer m_TocRenderer;
129

    
130
        private JScrollPane m_Scroller;
131

    
132
        // private ArrayList m_Listeners;
133
        private HashMap m_ItemsExpanded = new HashMap();
134

    
135
        private NodeSelectionListener nodeSelectionListener = null;
136

    
137
        /**
138
         * Crea un nuevo TOC.
139
         */
140
        public TOC() {
141
                this.setName("TOC");
142
                this.setLayout(new BorderLayout());
143
                this.setMinimumSize(new Dimension(100, 80));
144
                this.setPreferredSize(new Dimension(100, 80));
145

    
146
                // Construct the tree.
147
                m_Root = new DefaultMutableTreeNode(java.lang.Object.class);
148
                m_TreeModel = new DefaultTreeModel(m_Root);
149
                m_Tree = new DnDJTree(m_TreeModel);
150

    
151
                m_TocRenderer = new TOCRenderer();
152
                m_Tree.setCellRenderer(m_TocRenderer);
153

    
154
                m_Tree.setRootVisible(false);
155

    
156
                // m_Tree.setExpandsSelectedPaths(true);
157
                // m_Tree.setAutoscrolls(true);
158
                m_Tree.setShowsRootHandles(true);
159

    
160
                // Posibilidad de seleccionar de forma aleatoria nodos de la leyenda.
161
                m_Tree.getSelectionModel().setSelectionMode(
162
                                TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
163
                nodeSelectionListener = new NodeSelectionListener(m_Tree);
164
                m_Tree.addMouseListener(nodeSelectionListener);
165
                m_Tree.setBackground(UIManager.getColor("Button.background"));
166
                m_Tree.setBorder(BorderFactory.createEtchedBorder());
167

    
168
                this.addComponentListener(this);
169

    
170
                m_Tree.addTreeExpansionListener(this);
171

    
172
                m_Tree.addOrderListener(this);
173

    
174
                m_Tree.setRowHeight(0); // Para que lo determine el renderer
175

    
176
                m_Scroller = new JScrollPane(m_Tree);
177
                m_Scroller.setBorder(BorderFactory.createEmptyBorder());
178

    
179
                // scrollPane.setPreferredSize(new Dimension(80,80));
180
                // Add everything to this panel.
181
                /*
182
                 * GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c =
183
                 * new GridBagConstraints(); setLayout(gridbag); c.fill =
184
                 * GridBagConstraints.BOTH; c.weightx = 1.0;
185
                 * gridbag.setConstraints(check,c);
186
                 */
187
                add(m_Scroller); // , BorderLayout.WEST);
188

    
189
                // refresh();
190
        }
191

    
192
        /**
193
         * Elimina los listeners que actuan sobre el TOC, lo ?nico que deja hacer es
194
         * desplegar la leyenda de las capas.
195
         */
196
        public void removeListeners() {
197
                m_Tree.removeMouseListener(nodeSelectionListener);
198
                m_Tree.invalidateListeners();
199
        }
200

    
201
        /**
202
         * Inserta el FMap.
203
         *
204
         * @param mc
205
         *            FMap.
206
         */
207
        public void setMapContext(MapContext mc) {
208
                mapContext = mc;
209
                mapContext.addAtomicEventListener(new AtomicEventListener() {
210
                        /**
211
                         * @see org.gvsig.fmap.mapcontext.events.listeners.AtomicEventListener#atomicEvent(org.gvsig.fmap.mapcontext.events.AtomicEvent)
212
                         */
213
                        public void atomicEvent(AtomicEvent e) {
214
                                if ((e.getLayerCollectionEvents().length > 0)
215
                                                || (e.getLegendEvents().length > 0)) {
216
                                        SwingUtilities.invokeLater(new Runnable() {
217
                                                public void run() {
218
                                                        refresh();
219
                                                }
220
                                        });
221
                                }
222

    
223
                                if (e.getLayerEvents().length > 0) {
224
                                        repaint();
225
                                }
226

    
227
                                if (e.getExtentEvents().length > 0) {
228
                                        repaint();
229
                                }
230
                        }
231
                });
232

    
233
                SwingUtilities.invokeLater(new Runnable() {
234
                        public void run() {
235
                                refresh();
236
                        }
237
                });
238
        }
239

    
240
        /**
241
         * DOCUMENT ME!
242
         */
243
        private void setExpandedNodes(DefaultMutableTreeNode node) {
244
                // int i = 0;
245
                // Las claves sobrantes de m_ItemsExpanded (provocadas
246
                // por layerRemove, se quitan en el evento layerRemoved
247
                // de este TOC
248
                DefaultMutableTreeNode n;
249
                Enumeration enumeration = node.children();
250

    
251
                while (enumeration.hasMoreElements()) {
252
                        n = (DefaultMutableTreeNode) enumeration.nextElement();
253
                        if (n.getChildCount() > 0) {
254
                                setExpandedNodes(n);
255
                        }
256
                        TreePath path = new TreePath(m_TreeModel.getPathToRoot(n));
257
                        ITocItem item = (ITocItem) n.getUserObject();
258
                        Boolean b = (Boolean) m_ItemsExpanded.get(item.getLabel());
259

    
260
                        if (b == null) // No estaba en el hash todav?a: valor por defecto
261
                        {
262
                                // System.out.println("Primera expansi?n de " +
263
                                // item.getLabel());
264
                                m_Tree.expandPath(path);
265

    
266
                                return;
267
                        }
268

    
269
                        if (b.booleanValue()) {
270
                                // System.out.println("Expansi?n de " + item.getLabel());
271
                                m_Tree.expandPath(path);
272
                        } else {
273
                                // System.out.println("Colapso de " + item.getLabel());
274
                                m_Tree.collapsePath(path);
275
                        }
276
                }
277
        }
278

    
279
        /*
280
         * (non-Javadoc)
281
         *
282
         * @see com.iver.cit.opensig.gui.IToc#refresh()
283
         */
284
        public void refresh() {
285
                LayerCollection theLayers = mapContext.getLayers();
286
                m_Root.removeAllChildren();
287
                m_Root.setAllowsChildren(true);
288
                // System.out.println("Refresh del toc");
289
                doRefresh(theLayers, m_Root);
290

    
291
                m_TreeModel.reload();
292

    
293
                setExpandedNodes(m_Root);
294
        }
295

    
296
        /**
297
         * DOCUMENT ME!
298
         *
299
         * @param theLayers
300
         *            DOCUMENT ME!
301
         * @param parentNode
302
         *            DOCUMENT ME!
303
         */
304
        private void doRefresh(LayerCollection theLayers,
305
                        DefaultMutableTreeNode parentNode) {
306
                Dimension sizeLeaf = new Dimension(m_Tree.getWidth(), 15);
307
                Dimension sizeBranch = new Dimension(m_Tree.getWidth(), 25);
308

    
309
                for (int i = theLayers.getLayersCount() - 1; i >= 0; i--) {
310
                        FLayer lyr = theLayers.getLayer(i);
311
                        if (!lyr.isInTOC()) {
312
                                continue;
313
                        }
314
                        TocItemBranch elTema = new TocItemBranch(lyr);
315
                        elTema.setSize(sizeBranch);
316

    
317
                        DefaultMutableTreeNode nodeLayer = new DefaultMutableTreeNode(
318
                                        elTema);
319

    
320
                        m_TreeModel.insertNodeInto(nodeLayer, parentNode, parentNode
321
                                        .getChildCount());
322

    
323
                        // TreePath path = new
324
                        // TreePath(m_TreeModel.getPathToRoot(nodeLayer));
325
                        // m_Tree.makeVisible(path);
326
                        if (lyr instanceof LayerCollection) {
327
                                LayerCollection group = (LayerCollection) lyr;
328
                                doRefresh(group, nodeLayer);
329
                        } else {
330
                                if (lyr instanceof Classifiable) {// && !(lyr instanceof
331
                                                                                                        // FLyrAnnotation)) {
332

    
333
                                        Classifiable aux = (Classifiable) lyr;
334
                                        ILegend legendInfo = aux.getLegend();
335

    
336
                                        try {
337
                                                if (legendInfo instanceof IClassifiedLegend) {
338
                                                        IClassifiedLegend cl = (IClassifiedLegend) legendInfo;
339
                                                        String[] descriptions = cl.getDescriptions();
340
                                                        ISymbol[] symbols = cl.getSymbols();
341

    
342
                                                        for (int j = 0; j < descriptions.length; j++) {
343
                                                                TocItemLeaf itemLeaf;
344
                                                                itemLeaf = new TocItemLeaf(symbols[j],
345
                                                                                descriptions[j], aux.getShapeType());
346
                                                                itemLeaf.setSize(sizeLeaf);
347

    
348
                                                                DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(
349
                                                                                itemLeaf);
350
                                                                m_TreeModel.insertNodeInto(nodeValue,
351
                                                                                nodeLayer, nodeLayer.getChildCount());
352

    
353
                                                                // TreePath pathSymbol = new
354
                                                                // TreePath(m_TreeModel.getPathToRoot(
355
                                                                // nodeValue));
356
                                                                // m_Tree.makeVisible(pathSymbol);
357
                                                        }
358
                                                }
359

    
360
                                                if (legendInfo instanceof ISingleSymbolLegend
361
                                                                && (legendInfo.getDefaultSymbol() != null)) {
362
                                                        TocItemLeaf itemLeaf;
363
                                                        itemLeaf = new TocItemLeaf(legendInfo
364
                                                                        .getDefaultSymbol(), legendInfo
365
                                                                        .getDefaultSymbol().getDescription(), aux
366
                                                                        .getShapeType());
367
                                                        itemLeaf.setSize(sizeLeaf);
368

    
369
                                                        DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(
370
                                                                        itemLeaf);
371
                                                        m_TreeModel.insertNodeInto(nodeValue, nodeLayer,
372
                                                                        nodeLayer.getChildCount());
373

    
374
                                                        // TreePath pathSymbol = new
375
                                                        // TreePath(m_TreeModel.getPathToRoot(
376
                                                        // nodeValue));
377
                                                        // m_Tree.makeVisible(pathSymbol);
378
                                                }
379
                                        } catch (ReadException e) {
380
                                                e.printStackTrace();
381
                                        }
382
                                } else if (lyr instanceof IHasImageLegend) {
383
                                        TocItemLeaf itemLeaf;
384
                                        IHasImageLegend aux = (IHasImageLegend) lyr;
385
                                        Image image = aux.getImageLegend();
386

    
387
                                        if (image != null) {
388
                                                itemLeaf = new TocItemLeaf();
389
                                                itemLeaf.setImageLegend(image, "", new Dimension(image
390
                                                                .getWidth(null), image.getHeight(null)));// new
391
                                                                                                                                                        // Dimension(150,200));
392

    
393
                                                DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(
394
                                                                itemLeaf);
395
                                                m_TreeModel.insertNodeInto(nodeValue, nodeLayer,
396
                                                                nodeLayer.getChildCount());
397
                                        }
398
                                }
399
                        } // if instanceof layers
400
                }
401
        }
402

    
403
        /**
404
         * @see com.iver.cit.opensig.gui.toc.ITocOrderListener#orderChanged(int,
405
         *      int)
406
         */
407
        public void orderChanged(int oldPos, int newPos, FLayers lpd) {
408
                // LayerCollection layers = mapContext.getLayers();
409
                // El orden es el contrario, hay que traducir.
410
                // El orden es el contrario, hay que traducir.
411
                // /oldPos = layers.getLayersCount() - 1 - oldPos;
412
                // /newPos = layers.getLayersCount() - 1 - newPos;
413
                try {
414
                        lpd.moveTo(oldPos, newPos);
415
                } catch (CancelationException e) {
416
                        // TODO Auto-generated catch block
417
                        e.printStackTrace();
418
                }
419

    
420
                // No hace falta un refresh, lo hace mediante eventos.
421
                // refresh();
422
                mapContext.invalidate();
423
        }
424

    
425
        /**
426
         * DOCUMENT ME!
427
         *
428
         * @param lpo
429
         *            DOCUMENT ME!
430
         * @param lpd
431
         *            DOCUMENT ME!
432
         * @param ls
433
         *            DOCUMENT ME!
434
         */
435
        public void parentChanged(FLayers lpo, FLayers lpd, FLayer ls) {
436
                lpo.removeLayer(ls);
437
                lpd.addLayer(ls);
438

    
439
                /*
440
                 * if (lpo.getLayersCount()==0){ lpo.getParentLayer().removeLayer(lpo); }
441
                 */
442
                mapContext.invalidate();
443
        }
444

    
445
        /*
446
         * (non-Javadoc)
447
         *
448
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
449
         */
450
        public void componentHidden(ComponentEvent e) {
451
        }
452

    
453
        /*
454
         * (non-Javadoc)
455
         *
456
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
457
         */
458
        public void componentMoved(ComponentEvent e) {
459
        }
460

    
461
        /*
462
         * (non-Javadoc)
463
         *
464
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
465
         */
466
        public void componentResized(ComponentEvent e) {
467
                System.out.println("Cambiando tama?o.");
468

    
469
                int i = 0;
470
                DefaultMutableTreeNode n;
471
                Enumeration enumeration = m_Root.children();
472

    
473
                while (enumeration.hasMoreElements()) {
474
                        n = (DefaultMutableTreeNode) enumeration.nextElement();
475

    
476
                        if (n.getUserObject() instanceof TocItemBranch) {
477
                                ITocItem item = (ITocItem) n.getUserObject();
478
                                Dimension szAnt = item.getSize();
479
                                item.setSize(new Dimension(this.getWidth() - 40, szAnt.height));
480
                        }
481

    
482
                }
483

    
484
                // m_Tree.setSize(this.getSize());
485
                System.out.println("Ancho del tree=" + m_Tree.getWidth() + " "
486
                                + m_Tree.getComponentCount());
487
                System.out.println("Ancho del TOC=" + this.getWidth());
488

    
489
                // m_Tree.repaint();
490
        }
491

    
492
        /*
493
         * (non-Javadoc)
494
         *
495
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
496
         */
497
        public void componentShown(ComponentEvent e) {
498
        }
499

    
500
        /*
501
         * (non-Javadoc)
502
         *
503
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#legendChanged(com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent)
504
         */
505
        public void legendChanged(LegendChangedEvent e) {
506
                System.out.println("Refrescando TOC");
507
                refresh();
508
        }
509

    
510
        /*
511
         * (non-Javadoc)
512
         *
513
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
514
         */
515
        public void layerAdded(LayerCollectionEvent e) {
516
                refresh();
517
        }
518

    
519
        /*
520
         * (non-Javadoc)
521
         *
522
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
523
         */
524
        public void layerMoved(LayerPositionEvent e) {
525
                refresh();
526
        }
527

    
528
        /*
529
         * (non-Javadoc)
530
         *
531
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
532
         */
533
        public void layerRemoved(LayerCollectionEvent e) {
534
                m_ItemsExpanded.remove(e.getAffectedLayer().getName());
535
                refresh();
536
        }
537

    
538
        /*
539
         * (non-Javadoc)
540
         *
541
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
542
         */
543
        public void layerAdding(LayerCollectionEvent e) throws CancelationException {
544
        }
545

    
546
        /*
547
         * (non-Javadoc)
548
         *
549
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
550
         */
551
        public void layerMoving(LayerPositionEvent e) throws CancelationException {
552
        }
553

    
554
        /*
555
         * (non-Javadoc)
556
         *
557
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
558
         */
559
        public void layerRemoving(LayerCollectionEvent e)
560
                        throws CancelationException {
561
        }
562

    
563
        /*
564
         * (non-Javadoc)
565
         *
566
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
567
         */
568
        public void activationChanged(LayerCollectionEvent e)
569
                        throws CancelationException {
570
                repaint();
571
        }
572

    
573
        /*
574
         * (non-Javadoc)
575
         *
576
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
577
         */
578
        public void visibilityChanged(LayerCollectionEvent e)
579
                        throws CancelationException {
580
                repaint();
581
        }
582

    
583
        /*
584
         * (non-Javadoc)
585
         *
586
         * @see javax.swing.event.TreeExpansionListener#treeCollapsed(javax.swing.event.TreeExpansionEvent)
587
         */
588
        public void treeCollapsed(TreeExpansionEvent event) {
589
                TreePath path = event.getPath();
590
                DefaultMutableTreeNode n = (DefaultMutableTreeNode) path
591
                                .getLastPathComponent();
592

    
593
                if (n.getUserObject() instanceof ITocItem) {
594
                        ITocItem item = (ITocItem) n.getUserObject();
595
                        Boolean b = Boolean.FALSE;
596

    
597
                        // System.out.println("Collapsed: " + item.getLabel());
598
                        m_ItemsExpanded.put(item.getLabel(), b);
599
                }
600
        }
601

    
602
        /*
603
         * (non-Javadoc)
604
         *
605
         * @see javax.swing.event.TreeExpansionListener#treeExpanded(javax.swing.event.TreeExpansionEvent)
606
         */
607
        public void treeExpanded(TreeExpansionEvent event) {
608
                TreePath path = event.getPath();
609
                DefaultMutableTreeNode n = (DefaultMutableTreeNode) path
610
                                .getLastPathComponent();
611

    
612
                if (n.getUserObject() instanceof ITocItem) {
613
                        ITocItem item = (ITocItem) n.getUserObject();
614
                        Boolean b = Boolean.TRUE;
615

    
616
                        // System.out.println("Expanded: " + item.getLabel());
617
                        m_ItemsExpanded.put(item.getLabel(), b);
618
                }
619
        }
620

    
621
        /**
622
         * Obtiene el JScrollPane que contiene el TOC
623
         *
624
         * @return JScrollPane que contiene el TOC
625
         */
626
        public JScrollPane getJScrollPane() {
627
                return this.m_Scroller;
628
        }
629

    
630
        /**
631
         * DOCUMENT ME!
632
         *
633
         * @return DOCUMENT ME!
634
         */
635
        public DnDJTree getTree() {
636
                return m_Tree;
637
        }
638

    
639
        /**
640
         * Clase Listener que reacciona al pulsar sobre el checkbox de un nodo y
641
         * crea un popupmenu al pulsar el bot?n derecho.
642
         */
643
        class NodeSelectionListener extends MouseAdapter implements ActionListener {
644
                JTree tree;
645

    
646
                JDialog dlg;
647

    
648
                JColorChooser colorChooser;
649

    
650
                FPopupMenu popmenu = null;
651

    
652
                DefaultMutableTreeNode node;
653

    
654
                /**
655
                 * Crea un nuevo NodeSelectionListener.
656
                 *
657
                 * @param tree
658
                 *            DOCUMENT ME!
659
                 */
660
                NodeSelectionListener(JTree tree) {
661
                        this.tree = tree;
662
                }
663

    
664
                /**
665
                 * DOCUMENT ME!
666
                 *
667
                 * @param e
668
                 *            DOCUMENT ME!
669
                 */
670
                public void mouseClicked(MouseEvent e) {
671
                        int x = e.getX();
672
                        int y = e.getY();
673
                        int row = tree.getRowForLocation(x, y);
674
                        TreePath path = tree.getPathForRow(row);
675
                        LayerCollection layers = mapContext.getLayers();
676

    
677
                        // System.out.println(e.getSource());
678
                        if (path != null) {
679
                                if (e.getClickCount() == 1) {
680
                                        // this fixes a bug when double-clicking. JTree by default
681
                                        // expands the tree when double-clicking, so we capture a
682
                                        // different node in the second click than in the first
683
                                        node = (DefaultMutableTreeNode) path.getLastPathComponent();
684
                                }
685

    
686
                                // System.out.println("Evento de rat?n originado por " +
687
                                // e.getSource().getClass().toString());
688
                                if (node != null
689
                                                && node.getUserObject() instanceof TocItemBranch) {
690
                                        // double click with left button ON A BRANCH/NODE (layer)
691
                                        if (e.getClickCount() >= 2
692
                                                        && e.getButton() == MouseEvent.BUTTON1) {
693
                                                e.consume();
694
                                                PluginServices.getMDIManager().setWaitCursor();
695
                                                try {
696
                                                        TocItemBranch leaf = (TocItemBranch) node
697
                                                                        .getUserObject();
698

    
699
                                                        IContextMenuAction action = leaf
700
                                                                        .getDoubleClickAction();
701
                                                        if (action != null) {
702
                                                                /*
703
                                                                 * if there is an action associated with the
704
                                                                 * double-clicked element it will be fired for
705
                                                                 * it and FOR ALL OTHER COMPATIBLES THAT HAVE
706
                                                                 * BEEN ACTIVATED.
707
                                                                 */
708
                                                                ArrayList<FLayer> targetLayers = new ArrayList<FLayer>();
709

    
710
                                                                TocItemBranch owner = (TocItemBranch) node
711
                                                                                .getUserObject();
712

    
713
                                                                FLayer masterLayer = owner.getLayer();
714
                                                                targetLayers.add(masterLayer);
715
                                                                FLayer[] actives = mapContext.getLayers()
716
                                                                                .getActives();
717
                                                                for (int i = 0; i < actives.length; i++) {
718
                                                                        if (actives[i].getClass().equals(
719
                                                                                        masterLayer.getClass())) {
720
                                                                                if (actives[i] instanceof FLyrVect) {
721
                                                                                        FLyrVect vectorLayer = (FLyrVect) actives[i];
722
                                                                                        FLyrVect vectorMaster = (FLyrVect) masterLayer;
723
                                                                                        if (vectorLayer.getShapeType() == vectorMaster
724
                                                                                                        .getShapeType()) {
725
                                                                                                targetLayers.add(vectorLayer);
726
                                                                                        } else {
727
                                                                                                vectorLayer.setActive(false);
728
                                                                                        }
729
                                                                                }
730
                                                                                // TODO for the rest of layer types
731
                                                                                // (i.e. FLyrRaster)
732
                                                                        } else {
733
                                                                                actives[i].setActive(false);
734
                                                                        }
735
                                                                }
736
                                                                action.execute(leaf, targetLayers
737
                                                                                .toArray(new FLayer[0]));
738
                                                        }
739
                                                } catch (Exception ex) {
740
                                                        NotificationManager.addError(ex);
741
                                                } finally {
742
                                                        PluginServices.getMDIManager().restoreCursor();
743
                                                }
744
                                                return;
745
                                        }
746

    
747
                                        TocItemBranch elTema = (TocItemBranch) node.getUserObject();
748
                                        FLayer lyr = elTema.getLayer();
749
                                        lyr.getMapContext().beginAtomicEvent();
750

    
751
                                        if (((e.getModifiers() & InputEvent.SHIFT_MASK) != 0)
752
                                                        && (e.getButton() == MouseEvent.BUTTON1)) {
753
                                                FLayer[] activeLayers = layers.getActives();
754
                                                if (activeLayers.length > 0) {
755
                                                        selectInterval(layers, lyr);
756
                                                } else {
757
                                                        updateActive(lyr, !lyr.isActive());
758
                                                }
759

    
760
                                        } else {
761
                                                if (!((e.getModifiers() & InputEvent.CTRL_MASK) != 0)
762
                                                                && (e.getButton() == MouseEvent.BUTTON1)) {
763
                                                        layers.setAllActives(false);
764
                                                }
765
                                                if (e.getButton() == MouseEvent.BUTTON1) {
766
                                                        // lyr.setActive(true);
767
                                                        updateActive(lyr, !lyr.isActive());
768
                                                }
769
                                        }
770
                                        // Si pertenece a un grupo, lo ponemos activo tambi?n.
771
                                        // FLayer parentLayer = lyr.getParentLayer();
772

    
773
                                        /*
774
                                         * if (parentLayer != null) { parentLayer.setActive(true); }
775
                                         */
776
                                        Point layerNodeLocation = tree.getUI().getPathBounds(tree,
777
                                                        path).getLocation();
778

    
779
                                        // Rect?ngulo que representa el checkbox
780
                                        Rectangle checkBoxBounds = m_TocRenderer
781
                                                        .getCheckBoxBounds();
782
                                        checkBoxBounds.translate((int) layerNodeLocation.getX(),
783
                                                        (int) layerNodeLocation.getY());
784

    
785
                                        if (checkBoxBounds.contains(e.getPoint())) {
786
                                                updateVisible(lyr);
787
                                        }
788

    
789
                                        // }
790
                                        if (e.getButton() == MouseEvent.BUTTON3) {
791
                                                // Boton derecho sobre un nodo del arbol
792
                                                // if ((e.getModifiers() & InputEvent.META_MASK) != 0) {
793
                                                popmenu = new FPopupMenu(mapContext, node);
794
                                                tree.add(popmenu);
795

    
796
                                                popmenu.show(e.getComponent(), e.getX(), e.getY());
797

    
798
                                                // }
799
                                        }
800

    
801
                                        lyr.getMapContext().endAtomicEvent();
802
                                }
803

    
804
                                if (node != null && node.getUserObject() instanceof TocItemLeaf) {
805
                                        // double click with left button ON A LEAF (ISymbol)
806
                                        if (e.getClickCount() >= 2
807
                                                        && e.getButton() == MouseEvent.BUTTON1) {
808
                                                e.consume();
809

    
810
                                                PluginServices.getMDIManager().setWaitCursor();
811
                                                try {
812
                                                        TocItemLeaf leaf = (TocItemLeaf) node
813
                                                                        .getUserObject();
814
                                                        IContextMenuAction action = leaf
815
                                                                        .getDoubleClickAction();
816
                                                        if (action != null) {
817
                                                                /*
818
                                                                 * if there is an action associated with the
819
                                                                 * double-clicked element it will be fired for
820
                                                                 * it and FOR ALL OTHER COMPATIBLES THAT HAVE
821
                                                                 * BEEN ACTIVATED.
822
                                                                 */
823
                                                                ArrayList<FLayer> targetLayers = new ArrayList<FLayer>();
824

    
825
                                                                TocItemBranch owner = (TocItemBranch) ((DefaultMutableTreeNode) node
826
                                                                                .getParent()).getUserObject();
827

    
828
                                                                FLayer masterLayer = owner.getLayer();
829
                                                                targetLayers.add(masterLayer);
830
                                                                FLayer[] actives = mapContext.getLayers()
831
                                                                                .getActives();
832
                                                                for (int i = 0; i < actives.length; i++) {
833
                                                                        if (actives[i].getClass().equals(
834
                                                                                        masterLayer.getClass())) {
835
                                                                                if (actives[i] instanceof FLyrVect) {
836
                                                                                        FLyrVect vectorLayer = (FLyrVect) actives[i];
837
                                                                                        FLyrVect vectorMaster = (FLyrVect) masterLayer;
838
                                                                                        int masterLayerShapetypeOF_THE_LEGEND = ((IVectorLegend) vectorMaster
839
                                                                                                        .getLegend())
840
                                                                                                        .getShapeType();
841
                                                                                        int anotherVectorLayerShapetypeOF_THE_LEGEND = ((IVectorLegend) vectorLayer
842
                                                                                                        .getLegend())
843
                                                                                                        .getShapeType();
844
                                                                                        if (masterLayerShapetypeOF_THE_LEGEND == anotherVectorLayerShapetypeOF_THE_LEGEND) {
845
                                                                                                targetLayers.add(vectorLayer);
846
                                                                                        } else {
847
                                                                                                vectorLayer.setActive(false);
848
                                                                                        }
849
                                                                                }
850
                                                                                // TODO for the rest of layer types
851
                                                                                // (i.e. FLyrRaster)
852
                                                                        } else {
853
                                                                                actives[i].setActive(false);
854
                                                                        }
855
                                                                }
856
                                                                action.execute(leaf, targetLayers
857
                                                                                .toArray(new FLayer[0]));
858
                                                        }
859
                                                } catch (Exception ex) {
860
                                                        NotificationManager.addError(ex);
861
                                                } finally {
862
                                                        PluginServices.getMDIManager().restoreCursor();
863
                                                }
864
                                                return;
865
                                        }
866

    
867
                                        // Boton derecho sobre un Simbolo
868
                                        // TocItemLeaf auxLeaf = (TocItemLeaf) node.getUserObject();
869
                                        // FSymbol theSym = auxLeaf.getSymbol();
870
                                        if ((e.getModifiers() & InputEvent.META_MASK) != 0) {
871
                                                popmenu = new FPopupMenu(mapContext, node);
872
                                                tree.add(popmenu);
873
                                                popmenu.show(e.getComponent(), e.getX(), e.getY());
874
                                        }
875
                                }
876

    
877
                                ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
878

    
879
                                if (row == 0) {
880
                                        tree.revalidate();
881
                                        tree.repaint();
882
                                }
883

    
884
                                if (PluginServices.getMainFrame() != null) {
885
                                        PluginServices.getMainFrame().enableControls();
886
                                }
887
                        } else {
888
                                if (e.getButton() == MouseEvent.BUTTON3) {
889
                                        popmenu = new FPopupMenu(mapContext, null);
890
                                        tree.add(popmenu);
891
                                        popmenu.show(e.getComponent(), e.getX(), e.getY());
892
                                }
893

    
894
                        }
895
                }
896

    
897
                private void selectInterval(LayerCollection layers, FLayer lyr) {
898
                        FLayer[] activeLayers = layers.getActives();
899
                        // if (activeLayers[0].getParentLayer() instanceof FLayers &&
900
                        // activeLayers[0].getParentLayer().getParentLayer()!=null) {
901
                        // selectInterval((LayerCollection)activeLayers[0].getParentLayer(),lyr);
902
                        // }
903
                        for (int j = 0; j < layers.getLayersCount(); j++) {
904
                                FLayer layerAux = layers.getLayer(j);
905
                                // Si se cumple esta condici?n es porque la primera capa que nos
906
                                // encontramos en el TOC es la que estaba activa
907
                                if (activeLayers[0].equals(layerAux)) {
908
                                        boolean isSelected = false;
909
                                        for (int i = 0; i < layers.getLayersCount(); i++) {
910
                                                FLayer layer = layers.getLayer(i);
911
                                                if (!isSelected) {
912
                                                        isSelected = layer.isActive();
913
                                                } else {
914
                                                        updateActive(layer, true);
915
                                                        if (lyr.equals(layer)) {
916
                                                                isSelected = false;
917
                                                        }
918
                                                }
919
                                        }
920
                                        break;
921
                                } else
922
                                // Si se cumple esta condici?n es porque la primera capa que nos
923
                                // encontramos en el TOC es la que acabamos de seleccionar
924
                                if (lyr.equals(layerAux)) {
925
                                        boolean isSelected = false;
926
                                        for (int i = layers.getLayersCount() - 1; i >= 0; i--) {
927
                                                FLayer layer = layers.getLayer(i);
928
                                                if (!isSelected) {
929
                                                        isSelected = layer.isActive();
930
                                                } else {
931
                                                        updateActive(layer, true);
932
                                                        if (lyr.equals(layer)) {
933
                                                                isSelected = false;
934
                                                        }
935
                                                }
936
                                        }
937
                                        break;
938
                                }
939
                        }
940

    
941
                }
942

    
943
                /**
944
                 * DOCUMENT ME!
945
                 *
946
                 * @param lyr
947
                 *            DOCUMENT ME!
948
                 * @param active
949
                 *            DOCUMENT ME!
950
                 */
951
                private void updateActive(FLayer lyr, boolean active) {
952
                        lyr.setActive(active);
953
                        updateActiveChild(lyr);
954
                }
955

    
956
                /**
957
                 * DOCUMENT ME!
958
                 *
959
                 * @param lyr
960
                 *            DOCUMENT ME!
961
                 */
962
                private void updateActiveChild(FLayer lyr) {
963
                        if (lyr instanceof FLayers) { // Es la raiz de una rama o
964
                                // cualquier nodo intermedio.
965

    
966
                                FLayers layergroup = (FLayers) lyr;
967

    
968
                                for (int i = 0; i < layergroup.getLayersCount(); i++) {
969
                                        layergroup.getLayer(i).setActive(lyr.isActive());
970
                                        updateActiveChild(layergroup.getLayer(i));
971
                                }
972
                        }
973
                }
974

    
975
                /**
976
                 * Actualiza la visibilidad de la capas.
977
                 *
978
                 * @param lyr
979
                 *            Capa sobre la que se est? clickando.
980
                 */
981
                private void updateVisible(FLayer lyr) {
982
                        if (lyr.isAvailable()) {
983
                                lyr.setVisible(!lyr.visibleRequired());
984
                                updateVisibleChild(lyr);
985
                                updateVisibleParent(lyr);
986
                        }
987
                }
988

    
989
                /**
990
                 * Actualiza de forma recursiva la visibilidad de los hijos de la capa
991
                 * que se pasa como par?metro.
992
                 *
993
                 * @param lyr
994
                 *            Capa a actualizar.
995
                 */
996
                private void updateVisibleChild(FLayer lyr) {
997
                        if (lyr instanceof FLayers) { // Es la raiz de una rama o
998
                                                                                        // cualquier nodo intermedio.
999

    
1000
                                FLayers layergroup = (FLayers) lyr;
1001

    
1002
                                for (int i = 0; i < layergroup.getLayersCount(); i++) {
1003
                                        layergroup.getLayer(i).setVisible(lyr.visibleRequired());
1004
                                        updateVisibleChild(layergroup.getLayer(i));
1005
                                }
1006
                        }
1007
                }
1008

    
1009
                /**
1010
                 * Actualiza de forma recursiva la visibilidad del padre de la capa que
1011
                 * se pasa como par?metro.
1012
                 *
1013
                 * @param lyr
1014
                 *            Capa a actualizar.
1015
                 */
1016
                private void updateVisibleParent(FLayer lyr) {
1017
                        FLayers parent = lyr.getParentLayer();
1018

    
1019
                        if (parent != null) {
1020
                                boolean parentVisible = false;
1021

    
1022
                                for (int i = 0; i < parent.getLayersCount(); i++) {
1023
                                        if (parent.getLayer(i).visibleRequired()) {
1024
                                                parentVisible = true;
1025
                                        }
1026
                                }
1027

    
1028
                                parent.setVisible(parentVisible);
1029
                                updateVisibleParent(parent);
1030
                        }
1031
                }
1032

    
1033
                /**
1034
                 * DOCUMENT ME!
1035
                 *
1036
                 * @param arg0
1037
                 *            DOCUMENT ME!
1038
                 */
1039
                public void actionPerformed(ActionEvent arg0) {
1040
                }
1041

    
1042
                /**
1043
                 * DOCUMENT ME!
1044
                 *
1045
                 * @param arg0
1046
                 *            DOCUMENT ME!
1047
                 */
1048
                public void mouseReleased(MouseEvent arg0) {
1049
                        super.mouseReleased(arg0);
1050
                }
1051

    
1052
                /**
1053
                 * DOCUMENT ME!
1054
                 *
1055
                 * @param arg0
1056
                 *            DOCUMENT ME!
1057
                 */
1058
                public void mouseEntered(MouseEvent arg0) {
1059
                        // TODO Auto-generated method stub
1060
                        super.mouseEntered(arg0);
1061

    
1062
                        // FJP: COMENTO ESTO.
1063
                        // LO CORRECTO CREO QUE ES IMPLEMENTAR CORRECTAMENTE
1064
                        // LOS METODOS DE DRAG AND DROP
1065

    
1066
                        /*
1067
                         * if (m_Root.getChildCount()==0){ m_Tree.dropRoot(m_Root); }
1068
                         */
1069
                }
1070
        }
1071

    
1072
}