Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toc / TOC.java @ 2792

History | View | Annotate | Download (27 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 com.iver.cit.gvsig.gui.toc;
48

    
49
import com.iver.andami.PluginServices;
50

    
51
import com.iver.cit.gvsig.fmap.AtomicEvent;
52
import com.iver.cit.gvsig.fmap.AtomicEventListener;
53
import com.iver.cit.gvsig.fmap.DriverException;
54
import com.iver.cit.gvsig.fmap.FMap;
55
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
56
import com.iver.cit.gvsig.fmap.layers.CancelationException;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
import com.iver.cit.gvsig.fmap.layers.FLayers;
59
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
60
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener;
61
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
62
import com.iver.cit.gvsig.fmap.layers.LegendListener;
63
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable;
64
import com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection;
65
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
66
import com.iver.cit.gvsig.fmap.rendering.Legend;
67
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
68
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
69

    
70
import java.awt.BorderLayout;
71
import java.awt.Dimension;
72
import java.awt.Point;
73
import java.awt.Rectangle;
74
import java.awt.event.ActionEvent;
75
import java.awt.event.ActionListener;
76
import java.awt.event.ComponentEvent;
77
import java.awt.event.ComponentListener;
78
import java.awt.event.InputEvent;
79
import java.awt.event.MouseAdapter;
80
import java.awt.event.MouseEvent;
81

    
82
import java.util.Enumeration;
83
import java.util.HashMap;
84

    
85
import javax.swing.BorderFactory;
86
import javax.swing.JColorChooser;
87
import javax.swing.JComponent;
88
import javax.swing.JDialog;
89
import javax.swing.JScrollPane;
90
import javax.swing.JTree;
91
import javax.swing.SwingUtilities;
92
import javax.swing.UIManager;
93
import javax.swing.event.TreeExpansionEvent;
94
import javax.swing.event.TreeExpansionListener;
95
import javax.swing.tree.DefaultMutableTreeNode;
96
import javax.swing.tree.DefaultTreeModel;
97
import javax.swing.tree.TreePath;
98
import javax.swing.tree.TreeSelectionModel;
99

    
100

    
101
/**
102
 * DOCUMENT ME!
103
 *
104
 * @author fjp To change the template for this generated type comment go to
105
 *         Window>Preferences>Java>Code Generation>Code and
106
 *         Comments
107
 */
108
public class TOC extends JComponent implements ITocOrderListener,
109
    LegendListener, LayerCollectionListener, TreeExpansionListener,
110
    ComponentListener {
111
    private FMap mapContext;
112
    private DnDJTree m_Tree;
113
    private DefaultTreeModel m_TreeModel;
114
    private DefaultMutableTreeNode m_Root;
115
    private TOCRenderer m_TocRenderer;
116
    private JScrollPane m_Scroller;
117

    
118
    //private ArrayList m_Listeners;
119
    private HashMap m_ItemsExpanded = new HashMap();
120
    private NodeSelectionListener nodeSelectionListener = null;
121

    
122
    /**
123
     * Crea un nuevo TOC.
124
     */
125
    public TOC() {
126
        this.setLayout(new BorderLayout());
127
        this.setMinimumSize(new Dimension(100, 80));
128
        this.setPreferredSize(new Dimension(100, 80));
129

    
130
        //Construct the tree.
131
        m_Root = new DefaultMutableTreeNode(java.lang.Object.class);
132
        m_TreeModel = new DefaultTreeModel(m_Root);
133
        m_Tree = new DnDJTree(m_TreeModel);
134

    
135
        m_TocRenderer = new TOCRenderer();
136
        m_Tree.setCellRenderer(m_TocRenderer);
137

    
138
        m_Tree.setRootVisible(false);
139

    
140
        // m_Tree.setExpandsSelectedPaths(true);
141
        // m_Tree.setAutoscrolls(true);
142
        m_Tree.setShowsRootHandles(true);
143

    
144
        //Posibilidad de seleccionar de forma aleatoria nodos de la leyenda.
145
        m_Tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
146
        nodeSelectionListener=new NodeSelectionListener(m_Tree);
147
        m_Tree.addMouseListener(nodeSelectionListener);
148
        m_Tree.setBackground(UIManager.getColor("Button.background"));
149
        m_Tree.setBorder(BorderFactory.createEtchedBorder());
150

    
151
        this.addComponentListener(this);
152

    
153
        m_Tree.addTreeExpansionListener(this);
154

    
155
        m_Tree.addOrderListener(this);
156

    
157
        m_Tree.setRowHeight(0); // Para que lo determine el renderer
158

    
159
        m_Scroller = new JScrollPane(m_Tree);
160
        m_Scroller.setBorder(BorderFactory.createEmptyBorder());
161

    
162
        // scrollPane.setPreferredSize(new Dimension(80,80));
163
        //Add everything to this panel.
164
        /* GridBagLayout gridbag = new GridBagLayout();
165
           GridBagConstraints c = new GridBagConstraints();
166
           setLayout(gridbag);
167
           c.fill = GridBagConstraints.BOTH;
168
           c.weightx = 1.0;
169
           gridbag.setConstraints(check,c); */
170
        add(m_Scroller); //, BorderLayout.WEST);
171

    
172
        //                refresh();
173
    }
174

    
175
    /**
176
     * Elimina los listeners que actuan sobre el TOC, lo ?nico que deja hacer
177
     * es desplegar la leyenda de las capas.
178
     */
179
    public void removeListeners() {
180
        m_Tree.removeMouseListener(nodeSelectionListener);
181
        m_Tree.invalidateListeners();
182
    }
183

    
184
    /**
185
     * Inserta el FMap.
186
     *
187
     * @param mc FMap.
188
     */
189
    public void setMapContext(FMap mc) {
190
        mapContext = mc;
191
        mapContext.addAtomicEventListener(new AtomicEventListener() {
192
                /**
193
                 * @see com.iver.cit.gvsig.fmap.AtomicEventListener#atomicEvent(com.iver.cit.gvsig.fmap.AtomicEvent)
194
                 */
195
                public void atomicEvent(AtomicEvent e) {
196
                    if ((e.getLayerCollectionEvents().length > 0) ||
197
                            (e.getLegendEvents().length > 0)) {
198
                        SwingUtilities.invokeLater(new Runnable() {
199
                                public void run() {
200
                                    refresh();
201
                                }
202
                            });
203
                    }
204

    
205
                    if (e.getLayerEvents().length > 0) {
206
                        repaint();
207
                    }
208

    
209
                    if (e.getExtentEvents().length > 0) {
210
                        repaint();
211
                    }
212
                }
213
            });
214

    
215
        SwingUtilities.invokeLater(new Runnable() {
216
                public void run() {
217
                    refresh();
218
                }
219
            });
220
    }
221

    
222
    /**
223
     * DOCUMENT ME!
224
     */
225
    private void setExpandedNodes() {
226
        //int i = 0;
227
        // Las claves sobrantes de m_ItemsExpanded (provocadas
228
        // por layerRemove, se quitan en el evento layerRemoved
229
        // de este TOC
230
        DefaultMutableTreeNode n;
231
        Enumeration enumeration = m_Root.children();
232

    
233
        while (enumeration.hasMoreElements()) {
234
            n = (DefaultMutableTreeNode) enumeration.nextElement();
235

    
236
            TreePath path = new TreePath(m_TreeModel.getPathToRoot(n));
237
            ITocItem item = (ITocItem) n.getUserObject();
238
            Boolean b = (Boolean) m_ItemsExpanded.get(item.getLabel());
239

    
240
            if (b == null) // No estaba en el hash todav?a: valor por defecto
241
             {
242
                // System.out.println("Primera expansi?n de " + item.getLabel());
243
                m_Tree.expandPath(path);
244

    
245
                return;
246
            }
247

    
248
            if (b.booleanValue()) {
249
                // System.out.println("Expansi?n de " + item.getLabel());
250
                m_Tree.expandPath(path);
251
            } else {
252
                // System.out.println("Colapso de " + item.getLabel());
253
                m_Tree.collapsePath(path);
254
            }
255
        }
256
    }
257

    
258
    /* (non-Javadoc)
259
     * @see com.iver.cit.opensig.gui.IToc#refresh()
260
     */
261
    public void refresh() {
262
        LayerCollection theLayers = mapContext.getLayers();
263
        m_Root.removeAllChildren();
264
        m_Root.setAllowsChildren(true);
265
        System.out.println("Refresh del toc");
266
        doRefresh(theLayers, m_Root);
267

    
268
        m_TreeModel.reload();
269

    
270
        setExpandedNodes();
271
    }
272

    
273
    /**
274
     * DOCUMENT ME!
275
     *
276
     * @param theLayers DOCUMENT ME!
277
     * @param parentNode DOCUMENT ME!
278
     */
279
    private void doRefresh(LayerCollection theLayers,
280
        DefaultMutableTreeNode parentNode) {
281
        Dimension sizeLeaf = new Dimension(m_Tree.getWidth(), 15);
282
        Dimension sizeBranch = new Dimension(m_Tree.getWidth(), 25);
283

    
284
        for (int i = theLayers.getLayersCount() - 1; i >= 0; i--) {
285
            FLayer lyr = theLayers.getLayer(i);
286
            TocItemBranch elTema = new TocItemBranch(lyr);
287
            elTema.setSize(sizeBranch);
288

    
289
            DefaultMutableTreeNode nodeLayer = new DefaultMutableTreeNode(elTema);
290

    
291
            m_TreeModel.insertNodeInto(nodeLayer, parentNode,
292
                parentNode.getChildCount());
293

    
294
            //TreePath path = new TreePath(m_TreeModel.getPathToRoot(nodeLayer));
295
            // m_Tree.makeVisible(path);
296
            if (lyr instanceof LayerCollection) {
297
                LayerCollection group = (LayerCollection) lyr;
298
                doRefresh(group, nodeLayer);
299
            } else {
300
                if (lyr instanceof Classifiable) {
301
                    Classifiable aux = (Classifiable) lyr;
302
                    Legend legendInfo = aux.getLegend();
303

    
304
                    try {
305
                        if (legendInfo instanceof ClassifiedLegendInfo) {
306
                            ClassifiedLegendInfo cl = (ClassifiedLegendInfo) legendInfo;
307
                            String[] descriptions = cl.getDescriptions();
308
                            FSymbol[] symbols = cl.getSymbols();
309

    
310
                            for (int j = 0; j < descriptions.length; j++) {
311
                                TocItemLeaf itemLeaf;
312
                                itemLeaf = new TocItemLeaf(symbols[j],
313
                                        descriptions[j], aux.getShapeType());
314
                                itemLeaf.setSize(sizeLeaf);
315

    
316
                                DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(itemLeaf);
317
                                m_TreeModel.insertNodeInto(nodeValue,
318
                                    nodeLayer, nodeLayer.getChildCount());
319

    
320
                                //TreePath pathSymbol = new TreePath(m_TreeModel.getPathToRoot(
321
                                //                        nodeValue));
322
                                // m_Tree.makeVisible(pathSymbol);                                                        
323
                            }
324
                        }
325

    
326
                        if (legendInfo instanceof SingleSymbolLegend &&
327
                                (legendInfo.getDefaultSymbol() != null)) {
328
                            TocItemLeaf itemLeaf;
329
                            itemLeaf = new TocItemLeaf(legendInfo.getDefaultSymbol(),
330
                                    "Default", aux.getShapeType());
331
                            itemLeaf.setSize(sizeLeaf);
332

    
333
                            DefaultMutableTreeNode nodeValue = new DefaultMutableTreeNode(itemLeaf);
334
                            m_TreeModel.insertNodeInto(nodeValue, nodeLayer,
335
                                nodeLayer.getChildCount());
336

    
337
                            //TreePath pathSymbol = new TreePath(m_TreeModel.getPathToRoot(
338
                            //                        nodeValue));
339
                            // m_Tree.makeVisible(pathSymbol);
340
                        }
341
                    } catch (DriverException e) {
342
                        // TODO Auto-generated catch block
343
                        e.printStackTrace();
344
                    }
345
                }
346
            } // if instanceof layers
347
        }
348
    }
349

    
350
    /**
351
     * @see com.iver.cit.opensig.gui.toc.ITocOrderListener#orderChanged(int,
352
     *      int)
353
     */
354
    public void orderChanged(int oldPos, int newPos, FLayers lpd) {
355
        //LayerCollection layers = mapContext.getLayers();
356
        // El orden es el contrario, hay que traducir.
357
        // El orden es el contrario, hay que traducir.
358
        ///oldPos = layers.getLayersCount() - 1 - oldPos;
359
        ///newPos = layers.getLayersCount() - 1 - newPos;
360
        try {
361
            lpd.moveTo(oldPos, newPos);
362
        } catch (CancelationException e) {
363
            // TODO Auto-generated catch block
364
            e.printStackTrace();
365
        }
366

    
367
        // No hace falta un refresh, lo hace mediante eventos.
368
        //refresh();
369
        mapContext.invalidate();
370
    }
371

    
372
    /**
373
     * DOCUMENT ME!
374
     *
375
     * @param lpo DOCUMENT ME!
376
     * @param lpd DOCUMENT ME!
377
     * @param ls DOCUMENT ME!
378
     */
379
    public void parentChanged(FLayers lpo, FLayers lpd, FLayer ls) {
380
        lpo.removeLayer(ls);
381
        lpd.addLayer(ls);
382

    
383
        /*if (lpo.getLayersCount()==0){
384
           lpo.getParentLayer().removeLayer(lpo);
385
           }*/
386
        mapContext.invalidate();
387
    }
388

    
389
    /* (non-Javadoc)
390
     * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
391
     */
392
    public void componentHidden(ComponentEvent e) {
393
    }
394

    
395
    /* (non-Javadoc)
396
     * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
397
     */
398
    public void componentMoved(ComponentEvent e) {
399
    }
400

    
401
    /* (non-Javadoc)
402
     * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
403
     */
404
    public void componentResized(ComponentEvent e) {
405
        System.out.println("Cambiando tama?o.");
406

    
407
        int i = 0;
408
        DefaultMutableTreeNode n;
409
        Enumeration enumeration = m_Root.children();
410

    
411
        while (enumeration.hasMoreElements()) {
412
            n = (DefaultMutableTreeNode) enumeration.nextElement();
413

    
414
            if (n.getUserObject() instanceof TocItemBranch) {
415
                ITocItem item = (ITocItem) n.getUserObject();
416
                Dimension szAnt = item.getSize();
417
                item.setSize(new Dimension(this.getWidth() - 40, szAnt.height));
418
            }
419

    
420
            System.out.println("i=" + i++);
421
        }
422

    
423
        // m_Tree.setSize(this.getSize());
424
        System.out.println("Ancho del tree=" + m_Tree.getWidth() + " " +
425
            m_Tree.getComponentCount());
426
        System.out.println("Ancho del TOC=" + this.getWidth());
427

    
428
        // m_Tree.repaint();
429
    }
430

    
431
    /* (non-Javadoc)
432
     * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
433
     */
434
    public void componentShown(ComponentEvent e) {
435
    }
436

    
437
    /* (non-Javadoc)
438
     * @see com.iver.cit.gvsig.fmap.layers.LayerListener#legendChanged(com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent)
439
     */
440
    public void legendChanged(LegendChangedEvent e) {
441
        System.out.println("Refrescando TOC");
442
        refresh();
443
    }
444

    
445
    /* (non-Javadoc)
446
     * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
447
     */
448
    public void layerAdded(LayerCollectionEvent e) {
449
        refresh();
450
    }
451

    
452
    /* (non-Javadoc)
453
     * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
454
     */
455
    public void layerMoved(LayerPositionEvent e) {
456
        refresh();
457
    }
458

    
459
    /* (non-Javadoc)
460
     * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
461
     */
462
    public void layerRemoved(LayerCollectionEvent e) {
463
        m_ItemsExpanded.remove(e.getAffectedLayer().getName());
464
        refresh();
465
    }
466

    
467
    /* (non-Javadoc)
468
     * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
469
     */
470
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
471
    }
472

    
473
    /* (non-Javadoc)
474
     * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
475
     */
476
    public void layerMoving(LayerPositionEvent e) throws CancelationException {
477
    }
478

    
479
    /* (non-Javadoc)
480
     * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
481
     */
482
    public void layerRemoving(LayerCollectionEvent e)
483
        throws CancelationException {
484
    }
485

    
486
    /* (non-Javadoc)
487
     * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
488
     */
489
    public void activationChanged(LayerCollectionEvent e)
490
        throws CancelationException {
491
        repaint();
492
    }
493

    
494
    /* (non-Javadoc)
495
     * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
496
     */
497
    public void visibilityChanged(LayerCollectionEvent e)
498
        throws CancelationException {
499
        repaint();
500
    }
501

    
502
    /* (non-Javadoc)
503
     * @see javax.swing.event.TreeExpansionListener#treeCollapsed(javax.swing.event.TreeExpansionEvent)
504
     */
505
    public void treeCollapsed(TreeExpansionEvent event) {
506
        TreePath path = event.getPath();
507
        DefaultMutableTreeNode n = (DefaultMutableTreeNode) path.getLastPathComponent();
508

    
509
        if (n.getUserObject() instanceof ITocItem) {
510
            ITocItem item = (ITocItem) n.getUserObject();
511
            Boolean b = Boolean.FALSE;
512

    
513
            // System.out.println("Collapsed: " + item.getLabel());
514
            m_ItemsExpanded.put(item.getLabel(), b);
515
        }
516
    }
517

    
518
    /* (non-Javadoc)
519
     * @see javax.swing.event.TreeExpansionListener#treeExpanded(javax.swing.event.TreeExpansionEvent)
520
     */
521
    public void treeExpanded(TreeExpansionEvent event) {
522
        TreePath path = event.getPath();
523
        DefaultMutableTreeNode n = (DefaultMutableTreeNode) path.getLastPathComponent();
524

    
525
        if (n.getUserObject() instanceof ITocItem) {
526
            ITocItem item = (ITocItem) n.getUserObject();
527
            Boolean b = Boolean.TRUE;
528

    
529
            // System.out.println("Expanded: " + item.getLabel());
530
            m_ItemsExpanded.put(item.getLabel(), b);
531
        }
532
    }
533

    
534
    /**
535
     * Obtiene el JScrollPane que contiene el TOC
536
     *
537
     * @return JScrollPane que contiene el TOC
538
     */
539
    public JScrollPane getJScrollPane() {
540
        return this.m_Scroller;
541
    }
542

    
543
    /**
544
     * DOCUMENT ME!
545
     *
546
     * @return DOCUMENT ME!
547
     */
548
    public DnDJTree getTree() {
549
        return m_Tree;
550
    }
551

    
552
    /**
553
     * Clase Listener que reacciona al pulsar sobre el checkbox de un nodo y
554
     * crea un popupmenu al pulsar el bot?n derecho.
555
     */
556
    class NodeSelectionListener extends MouseAdapter implements ActionListener {
557
        JTree tree;
558
        JDialog dlg;
559
        JColorChooser colorChooser;
560
        FPopupMenu popmenu = null;
561

    
562
        /**
563
         * Crea un nuevo NodeSelectionListener.
564
         *
565
         * @param tree DOCUMENT ME!
566
         */
567
        NodeSelectionListener(JTree tree) {
568
            this.tree = tree;
569
        }
570

    
571
        /**
572
         * DOCUMENT ME!
573
         *
574
         * @param e DOCUMENT ME!
575
         */
576
        public void mouseClicked(MouseEvent e) {
577
            int x = e.getX();
578
            int y = e.getY();
579
            int row = tree.getRowForLocation(x, y);
580
            TreePath path = tree.getPathForRow(row);
581
            LayerCollection layers = mapContext.getLayers();
582

    
583
            // System.out.println(e.getSource());
584
            if (path != null) {
585
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
586

    
587
                // System.out.println("Evento de rat?n originado por " + e.getSource().getClass().toString());
588
                if (node.getUserObject() instanceof TocItemBranch) {
589
                    TocItemBranch elTema = (TocItemBranch) node.getUserObject();
590
                    FLayer lyr = elTema.getLayer();
591
                    lyr.getFMap().beginAtomicEvent();
592

    
593
                    //if (e.getButton()==MouseEvent.BUTTON1){
594
                    // Si tenemos may?sculas apretadas es que queremos activar m?s temas.
595
                    // Si no, primero desactivamos temas
596
                    if (!((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) &&
597
                            (e.getButton() == MouseEvent.BUTTON1)) {
598
                        layers.setAllActives(false);
599
                    }
600

    
601
                    if (e.getButton() == MouseEvent.BUTTON1) {
602
                        //lyr.setActive(true);
603
                        updateActive(lyr, !lyr.isActive());
604
                    } else {
605
                        /*if (!((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) && e.getButton()==MouseEvent.BUTTON3) {
606
                           layers.setAllActives(false);
607
                           }
608
                           updateActive(lyr,true);
609
                         */
610
                    }
611

    
612
                    // Si pertenece a un grupo, lo ponemos activo tambi?n.
613
                    //FLayer parentLayer = lyr.getParentLayer();
614

    
615
                    /*if (parentLayer != null) {
616
                       parentLayer.setActive(true);
617
                       }*/
618
                    Point layerNodeLocation = tree.getUI()
619
                                                  .getPathBounds(tree, path)
620
                                                  .getLocation();
621

    
622
                    //Rect?ngulo que representa el checkbox
623
                    Rectangle checkBoxBounds = m_TocRenderer.getCheckBoxBounds();
624
                    checkBoxBounds.translate((int) layerNodeLocation.getX(),
625
                        (int) layerNodeLocation.getY());
626

    
627
                    if (checkBoxBounds.contains(e.getPoint())) {
628
                        updateVisible(lyr);
629
                    }
630

    
631
                    //}
632
                    if ((e.getButton() == MouseEvent.BUTTON3) &&
633
                            (layers.getActives().length > 0)) {
634
                        //Boton derecho sobre un nodo del arbol
635
                        //if ((e.getModifiers() & InputEvent.META_MASK) != 0) {
636
                        popmenu = new FPopupMenu(mapContext, node);
637
                        tree.add(popmenu);
638

    
639
                        //System.out.println("boton derecho");
640
                        popmenu.show(e.getComponent(), e.getX(), e.getY());
641

    
642
                        //}
643
                    }
644

    
645
                    lyr.getFMap().endAtomicEvent();
646
                }
647

    
648
                if (node.getUserObject() instanceof TocItemLeaf) {
649
                    //Boton derecho sobre un Simbolo
650
                    //TocItemLeaf auxLeaf = (TocItemLeaf) node.getUserObject();
651
                    //FSymbol theSym = auxLeaf.getSymbol();
652
                    if ((e.getModifiers() & InputEvent.META_MASK) != 0) {
653
                        popmenu = new FPopupMenu(mapContext, node);
654
                        tree.add(popmenu);
655
                        popmenu.show(e.getComponent(), e.getX(), e.getY());
656
                    }
657
                }
658

    
659
                ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
660

    
661
                if (row == 0) {
662
                    tree.revalidate();
663
                    tree.repaint();
664
                }
665

    
666
                if (PluginServices.getMainFrame() != null) {
667
                    PluginServices.getMainFrame().enableControls();
668
                }
669
            }
670
        }
671

    
672
        /**
673
         * DOCUMENT ME!
674
         *
675
         * @param lyr DOCUMENT ME!
676
         * @param active DOCUMENT ME!
677
         */
678
        private void updateActive(FLayer lyr, boolean active) {
679
            lyr.setActive(active);
680
            updateActiveChild(lyr);
681
        }
682

    
683
        /**
684
         * DOCUMENT ME!
685
         *
686
         * @param lyr DOCUMENT ME!
687
         */
688
        private void updateActiveChild(FLayer lyr) {
689
            if (lyr instanceof FLayers) { //Es la raiz de una rama o cualquier nodo intermedio.
690

    
691
                FLayers layergroup = (FLayers) lyr;
692

    
693
                for (int i = 0; i < layergroup.getLayersCount(); i++) {
694
                    layergroup.getLayer(i).setActive(lyr.isActive());
695
                    updateActiveChild(layergroup.getLayer(i));
696
                }
697
            }
698
        }
699

    
700
        /**
701
         * Actualiza la visibilidad de la capas.
702
         *
703
         * @param lyr Capa sobre la que se est? clickando.
704
         */
705
        private void updateVisible(FLayer lyr) {
706
            lyr.setVisible(!lyr.isVisible());
707
            updateVisibleChild(lyr);
708
            updateVisibleParent(lyr);
709
        }
710

    
711
        /**
712
         * Actualiza de forma recursiva la visibilidad de los hijos de la capa
713
         * que se pasa como par?metro.
714
         *
715
         * @param lyr Capa a actualizar.
716
         */
717
        private void updateVisibleChild(FLayer lyr) {
718
            if (lyr instanceof FLayers) { //Es la raiz de una rama o cualquier nodo intermedio.
719

    
720
                FLayers layergroup = (FLayers) lyr;
721

    
722
                for (int i = 0; i < layergroup.getLayersCount(); i++) {
723
                    layergroup.getLayer(i).setVisible(lyr.isVisible());
724
                    updateVisibleChild(layergroup.getLayer(i));
725
                }
726
            }
727
        }
728

    
729
        /**
730
         * Actualiza de forma recursiva la visibilidad del padre de la capa que
731
         * se pasa como par?metro.
732
         *
733
         * @param lyr Capa a actualizar.
734
         */
735
        private void updateVisibleParent(FLayer lyr) {
736
            FLayers parent = (FLayers) lyr.getParentLayer();
737

    
738
            if (parent != null) {
739
                boolean parentVisible = false;
740

    
741
                for (int i = 0; i < parent.getLayersCount(); i++) {
742
                    if (parent.getLayer(i).isVisible()) {
743
                        parentVisible = true;
744
                    }
745
                }
746

    
747
                parent.setVisible(parentVisible);
748
                updateVisibleParent(parent);
749
            }
750
        }
751

    
752
        /**
753
         * DOCUMENT ME!
754
         *
755
         * @param arg0 DOCUMENT ME!
756
         */
757
        public void actionPerformed(ActionEvent arg0) {
758
        }
759

    
760
        /**
761
         * DOCUMENT ME!
762
         *
763
         * @param arg0 DOCUMENT ME!
764
         */
765
        public void mouseReleased(MouseEvent arg0) {
766
            super.mouseReleased(arg0);
767
        }
768

    
769
        /**
770
         * DOCUMENT ME!
771
         *
772
         * @param arg0 DOCUMENT ME!
773
         */
774
        public void mouseEntered(MouseEvent arg0) {
775
            // TODO Auto-generated method stub
776
            super.mouseEntered(arg0);
777

    
778
            // FJP: COMENTO ESTO.
779
            // LO CORRECTO CREO QUE ES IMPLEMENTAR CORRECTAMENTE
780
            // LOS METODOS DE DRAG AND DROP
781

    
782
            /* if (m_Root.getChildCount()==0){
783
               m_Tree.dropRoot(m_Root);
784
               }*/
785
        }
786
    }
787
}