Statistics
| Revision:

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

History | View | Annotate | Download (31.2 KB)

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

    
46
import java.awt.Color;
47
import java.awt.Component;
48
import java.awt.Font;
49
import java.awt.event.ActionEvent;
50
import java.awt.geom.Rectangle2D;
51
import java.util.ArrayList;
52

    
53
import javax.swing.JColorChooser;
54
import javax.swing.JDialog;
55
import javax.swing.JMenuItem;
56
import javax.swing.JOptionPane;
57
import javax.swing.JPopupMenu;
58
import javax.swing.tree.DefaultMutableTreeNode;
59

    
60
import com.iver.andami.PluginServices;
61
import com.iver.cit.gvsig.ProjectExtension;
62
import com.iver.cit.gvsig.fmap.DriverException;
63
import com.iver.cit.gvsig.fmap.FMap;
64
import com.iver.cit.gvsig.fmap.MapControl;
65
import com.iver.cit.gvsig.fmap.ViewPort;
66
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
67
import com.iver.cit.gvsig.fmap.layers.CancelationException;
68
import com.iver.cit.gvsig.fmap.layers.FLayer;
69
import com.iver.cit.gvsig.fmap.layers.FLayers;
70
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
71
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
72
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
74
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
75
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
76
import com.iver.cit.gvsig.gui.View;
77
import com.iver.cit.gvsig.gui.panels.AdjustTransparencyPanel;
78
import com.iver.cit.gvsig.gui.thememanager.FThemeManagerWindow;
79
import com.iver.cit.gvsig.project.Project;
80
import com.iver.cit.gvsig.project.ProjectTable;
81

    
82
/**
83
 * Realiza el cambio de color si se pulsa OK
84
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
85
 */
86
class FSymbolChangeColorTocMenuEntry extends TocMenuEntry {
87
    private JMenuItem color;
88
        public void initialize(FPopupMenu m) {
89
                super.initialize(m);
90
                boolean bShow = false;
91
                if (isTocItemBranch())
92
                {
93
                        FLayer lyr = getNodeLayer();
94
                    if ((lyr instanceof ClassifiableVectorial))
95
                    {
96
                        ClassifiableVectorial lyrVect = (ClassifiableVectorial) lyr;
97
                        if (lyrVect.getLegend() instanceof SingleSymbolLegend)
98
                            bShow = true;
99
                    }
100
                } else if (isTocItemLeaf()) {
101
                    //bShow = true;
102

    
103
                }
104
                if (bShow)
105
                {
106
                color = new JMenuItem(PluginServices.getText(this, "Cambio_Color"));
107
                color.setFont(FPopupMenu.theFont);
108
                getMenu().add(color);
109
                getMenu().setEnabled(true);
110
                //Cambio color
111
                color.addActionListener(this);
112
                }
113
        }
114

    
115
        /* (non-Javadoc)
116
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
117
         */
118
        public void actionPerformed(ActionEvent e) {
119
                ITocItem tocItem = (ITocItem) getNodeUserObject();
120

    
121
                boolean showDialog=false;
122
                if (isTocItemBranch()) {
123
                        FLayer[] actives = getMapContext().getLayers().getActives();
124
                        if (actives.length > 0) {
125
                for (int i=0;i<actives.length;i++){
126
                    if ((actives[i] instanceof ClassifiableVectorial))
127
                    {
128
                        ClassifiableVectorial lyrVect = (ClassifiableVectorial) actives[i];
129
                        if (lyrVect.getLegend() instanceof SingleSymbolLegend && actives[i].isAvailable() )
130
                        {
131
                            showDialog=true;
132
                            break;
133
                        }
134
                    }
135
                }
136
                        }
137
                }
138

    
139
                if (!showDialog) return;
140

    
141
        Color newColor = JColorChooser.showDialog(null,
142
                PluginServices.getText(this, "Elegir_Color"),
143
                null);
144
        if (newColor != null)
145
        {
146
                        if (isTocItemBranch())
147
                        {
148
                                //FLayer lyr = getNodeLayer();
149
                FLayer[] actives = getMapContext().getLayers().getActives();
150
                for (int i=0;i<actives.length;i++){
151
                    if ((actives[i] instanceof ClassifiableVectorial))
152
                    {
153
                        ClassifiableVectorial lyrVect = (ClassifiableVectorial) actives[i];
154
                        if (lyrVect.getLegend() instanceof SingleSymbolLegend)
155
                        {
156
                            SingleSymbolLegend leg = (SingleSymbolLegend) lyrVect.getLegend();
157
                            leg.getDefaultSymbol().setColor(newColor);
158
                        }
159
                    }
160
                }
161
                        }
162
                        else
163
                        {
164
                    TocItemLeaf leaf = (TocItemLeaf) tocItem;
165
                    FSymbol sym = leaf.getSymbol();
166
                    sym.setColor(newColor);
167

    
168
                        }
169
                // TRUCO PARA REFRESCAR.
170
                getMapContext().invalidate();
171

    
172
        }
173

    
174
        }
175
}
176
/**
177
 * Realiza un cambio de nombre en la capa seleccionada
178
 *
179
 * @author Vicente Caballero Navarro
180
 */
181
class ChangeNameTocMenuEntry extends TocMenuEntry {
182
    private JMenuItem changename;
183
        public void initialize(FPopupMenu m) {
184
                super.initialize(m);
185
                //boolean bShow = false;
186
                if (isTocItemBranch())
187
                {
188
                        changename = new JMenuItem(PluginServices.getText(this, "cambio_nombre"));
189
                changename.setFont(FPopupMenu.theFont);
190
                getMenu().add(changename);
191
                getMenu().setEnabled(true);
192
            if (getMapContext().getLayers().getActives().length == 1) {
193
                changename.setEnabled(true);
194
            } else {
195
                changename.setEnabled(false);
196
            }
197
                //Cambio nombre
198
                changename.addActionListener(this);
199

    
200
                }
201
        }
202

    
203
        /* (non-Javadoc)
204
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
205
         */
206
        public void actionPerformed(ActionEvent e) {
207
                //ITocItem tocItem = (ITocItem) getNodeUserObject();
208
                if (isTocItemBranch())
209
                        {
210
                                FLayer lyr = getNodeLayer();
211
                                if (!lyr.isAvailable()) return;
212
                                ChangeName chn=new ChangeName(lyr.getName());
213
                                PluginServices.getMDIManager().addView(chn);
214
                                 getNodeLayer().setName(chn.getName());
215
                        }
216
                // TRUCO PARA REFRESCAR.
217
                ///getMapContext().invalidate();
218

    
219
   }
220
}
221
/**
222
 * Muestra el men? de propiedades del tema.
223
 *
224
 * @author jmorell
225
 *
226
 * TODO To change the template for this generated type comment go to
227
 * Window - Preferences - Java - Code Style - Code Templates
228
 */
229
class FLyrVectEditPropertiesTocMenuEntry extends TocMenuEntry {
230
    private JMenuItem prop;
231
        public void initialize(FPopupMenu m) {
232
                super.initialize(m);
233
                FLayer lyr = null;
234
                if (isTocItemBranch()) {
235
                        getMenu().addSeparator();
236
                        lyr = getNodeLayer();
237
            //if ((lyr instanceof ClassifiableVectorial) && (getMapContext().getLayers().getActives().length == 1)) {
238
            if ((lyr instanceof ClassifiableVectorial)) {
239
                    if (!((lyr instanceof FLyrVect) &&
240
                                    !((FLyrVect)lyr).isPropertiesMenuVisible())){
241
                            prop = new JMenuItem(PluginServices.getText(this, "propiedades"));
242
                            getMenu().add(prop);
243
                            prop.setFont(FPopupMenu.theFont);
244
                            if (getMapContext().getLayers().getActives().length == 1) {
245
                                    prop.setEnabled(true);
246
                            } else {
247
                                    prop.setEnabled(false);
248
                            }
249
                            //Cambio estilo
250
                            prop.addActionListener(this);
251
                    }
252
                        }
253
                }
254
        }
255
        /* (non-Javadoc)
256
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
257
         */
258
        public void actionPerformed(ActionEvent e) {
259
                /*FThemeManagerWindow fThemeManagerWindow = new FThemeManagerWindow();
260
                try {
261
                        fThemeManagerWindow.setMapContext(getMapContext());
262
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
263
                        e1.printStackTrace();
264
                }*/
265
                FLayer[] actives = getMapContext().getLayers().getActives();
266
                FThemeManagerWindow fThemeManagerWindow;
267
            if (actives.length==1) {
268
                    if (!actives[0].isAvailable()) return;
269
                    System.out.println("openPliegoThemeProperties(): Una sola capa. Abrimos el panel para la capa " + actives[0]);
270
                    fThemeManagerWindow = new FThemeManagerWindow();
271
                    try {
272
                            fThemeManagerWindow.setMapContext(getMapContext());
273
                    } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
274
                            e1.printStackTrace();
275
                    }
276
            //PluginServices.getMDIManager().addView(fThemeManagerWindow);
277
                    if (PluginServices.getMainFrame() == null) {
278
                            JDialog dlg = new JDialog();
279
                            fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
280
                            dlg.getContentPane().add(fThemeManagerWindow);
281
                            dlg.setModal(false);
282
                            dlg.pack();
283
                            dlg.show();
284
                    } else {
285
                            PluginServices.getMDIManager().addView(fThemeManagerWindow);
286
                    }
287
            } else {
288
                for (int i = 0; i < actives.length; i++){
289
                        if (actives[0].isAvailable()) {
290
                                System.out.println("openPliegoThemeProperties(): Muchas capas. Abrimos el panel para la capa " + actives[i]);
291
                                fThemeManagerWindow = new FThemeManagerWindow(actives[i], getMapContext());
292
                                //PluginServices.getMDIManager().addView(fThemeManagerWindow);
293
                                if (PluginServices.getMainFrame() == null) {
294
                                        JDialog dlg = new JDialog();
295
                                        fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
296
                                        dlg.getContentPane().add(fThemeManagerWindow);
297
                                        dlg.setModal(false);
298
                                        dlg.pack();
299
                                        dlg.show();
300
                                } else {
301
                                        PluginServices.getMDIManager().addView(fThemeManagerWindow);
302
                                }
303
                        }
304
                }
305
            }
306
                /*if (PluginServices.getMainFrame() == null) {
307
                        JDialog dlg = new JDialog();
308
                        fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
309
                        dlg.getContentPane().add(fThemeManagerWindow);
310
                        dlg.setModal(false);
311
                        dlg.pack();
312
                        dlg.show();
313
                } else {
314
                        PluginServices.getMDIManager().addView(fThemeManagerWindow);
315
                }*/
316
        }
317
}
318

    
319
class FLyrRasterAdjustTransparencyTocMenuEntry extends TocMenuEntry {
320
        private JMenuItem transparency;
321
        FLayer lyr = null;
322
        public void initialize(FPopupMenu m) {
323
                super.initialize(m);
324

    
325
                if (isTocItemBranch()) {
326
                        lyr = getNodeLayer();
327
                    // Opcciones para capas raster
328
                    if ((lyr instanceof FLyrRaster)) {
329
                        transparency = new JMenuItem(PluginServices.getText(this, "adjust_transparency"));
330
                            getMenu().add( transparency );
331
                            transparency.setFont(FPopupMenu.theFont);
332
                            getMenu().setEnabled(true);
333
                            //getMenu().addSeparator();
334
                    //Cambio color
335
                    transparency.addActionListener(this);
336
                     }
337
                }
338
        }
339
        /* (non-Javadoc)
340
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
341
         */
342
        public void actionPerformed(ActionEvent e) {
343
               lyr = getNodeLayer();
344
        AdjustTransparencyPanel transPanel = new AdjustTransparencyPanel((FLyrDefault) lyr);
345
                transPanel.openJDialog();
346
        //getMapContext().invalidate();
347
        }
348
}
349

    
350
class ZoomAlTemaTocMenuEntry extends TocMenuEntry {
351
    private JMenuItem zoom;
352
        public void initialize(FPopupMenu m) {
353
                super.initialize(m);
354
                if (isTocItemBranch()) {
355
                        zoom = new JMenuItem(PluginServices.getText(this, "Zoom_a_la_capa"));
356
                        getMenu().add(zoom);
357
                        zoom.setFont(FPopupMenu.theFont);
358

    
359
                zoom.addActionListener(this);
360
                }
361
        }
362
        /* (non-Javadoc)
363
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
364
         */
365
        public void actionPerformed(ActionEvent e) {
366
            /*FLayer lyr = getNodeLayer();
367
        try {
368
                getMapContext().getViewPort().setExtent(lyr.getFullExtent());
369
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
370
                        e1.printStackTrace();
371
                }*/
372
                // 050209, jmorell: Para que haga un zoom a un grupo de capas seleccionadas.
373
                FLayer[] actives = getMapContext().getLayers().getActives();
374
                if (actives.length==1) {
375
                try {
376
                        if (!actives[0].isAvailable()) return;
377
                        getMapContext().getViewPort().setExtent(actives[0].getFullExtent());
378
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
379
                                e1.printStackTrace();
380
                        }
381
                } else {
382
                        try {
383
                                Rectangle2D maxExtent = setMaxExtent(actives);
384
                            getMapContext().getViewPort().setExtent(maxExtent);
385
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
386
                                e1.printStackTrace();
387
                        }
388
                }
389
        }
390

    
391
        private Rectangle2D setMaxExtent(FLayer[] actives) throws DriverException {
392
                Rectangle2D extRef = actives[0].getFullExtent();
393
                double minXRef = extRef.getMinX();
394
                double maxYRef = extRef.getMaxY();
395
                double maxXRef = extRef.getMaxX();
396
                double minYRef = extRef.getMinY();
397
                for (int i=0;i<actives.length;i++) {
398
                        if (actives[i].isAvailable()) {
399
                                Rectangle2D extVar = actives[i].getFullExtent();
400
                                double minXVar = extVar.getMinX();
401
                                double maxYVar = extVar.getMaxY();
402
                                double maxXVar = extVar.getMaxX();
403
                                double minYVar = extVar.getMinY();
404
                                if (minXVar<=minXRef) minXRef=minXVar;
405
                                if (maxYVar>=maxYRef) maxYRef=maxYVar;
406
                                if (maxXVar>=maxXRef) maxXRef=maxXVar;
407
                                if (minYVar<=minYRef) minYRef=minYVar;
408
                                extRef.setRect(minXRef, minYRef, maxXRef-minXRef, maxYRef-minYRef);
409
                        }
410
                }
411
                return extRef;
412
        }
413
}
414

    
415
class EliminarCapaTocMenuEntry extends TocMenuEntry {
416
    private JMenuItem removeLayer;
417
        public void initialize(FPopupMenu m) {
418
                super.initialize(m);
419
                if (getNodeUserObject() instanceof TocItemBranch) {
420
                        removeLayer = new JMenuItem(PluginServices.getText(this, "eliminar_capa"));
421
                        removeLayer.setFont(FPopupMenu.theFont);
422
                        removeLayer.addActionListener(this);
423
                        getMenu().addSeparator();
424
                        getMenu().add(removeLayer);
425
                        getMenu().addSeparator();
426
                }
427
        }
428
        /* (non-Javadoc)
429
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
430
         */
431
        public void actionPerformed(ActionEvent e) {
432
            // 050209, jmorell: Para poder borrar todas las capas seleccionadas desde el FPopUpMenu.
433
                //                                        Es necesario pulsar May?sculas a la vez que el bot?n derecho.
434
            FMap fMap = getMapContext();
435
            FLayer[] actives = fMap.getLayers().getActives();
436
            int option=-1;
437
            if (actives.length>0) {
438
                    option=JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"desea_borrar_la_capa"));
439
            }
440
            if (option!=JOptionPane.OK_OPTION)
441
                    return;
442
            for (int i = actives.length-1; i>=0; i--){
443
                try {
444
                                actives[i].getParentLayer().removeLayer(actives[i]);
445
                                FLayers lyrs=getMapContext().getLayers();
446
                                lyrs.addLayer(actives[i]);
447
                                actives[i].getParentLayer().removeLayer(actives[i]);
448

    
449
                if (actives[i] instanceof AlphanumericData){
450
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
451
                    ProjectTable pt = project.getTable((AlphanumericData) actives[i]);
452

    
453
                    ArrayList tables = project.getTables();
454
                    for (int j = 0; j < tables.size(); j++) {
455
                        if (tables.get(j) == pt){
456
                            project.delTable(j);
457
                            break;
458
                        }
459
                    }
460

    
461
                    PluginServices.getMDIManager().closeSingletonView(pt);
462
                }
463

    
464
                                if (getMapContext().getLayers().getLayersCount()==0)
465
                        PluginServices.getMainFrame().enableControls();
466
                    } catch (CancelationException e1) {
467
                            e1.printStackTrace();
468
                    }
469
            }
470
                // 050209, jmorell: As? solo borra una capa (sobre la que pulsas).
471
            /*FLayer lyr = getNodeLayer();
472
        try {
473
                getMapContext().getLayers().removeLayer(lyr);
474
                if (getMapContext().getLayers().getLayersCount()==0)PluginServices.getMainFrame().enableControls();
475
                } catch (CancelationException e1) {
476
                        e1.printStackTrace();
477
                }*/
478
    }
479
}
480

    
481
/**
482
 * Realiza una agrupaci?n de capas, a partir de las capas que se encuentren activas.
483
 *
484
 * @author Vicente Caballero Navarro
485
 */
486
class LayersGroupTocMenuEntry extends TocMenuEntry {
487
    private JMenuItem agrupar;
488
        public void initialize(FPopupMenu m) {
489
                super.initialize(m);
490
        if (isTocItemBranch()) {
491
            agrupar = new JMenuItem(PluginServices.getText(this, "agrupar_capas"));
492
            agrupar.setFont(FPopupMenu.theFont);
493
            getMenu().add(agrupar);
494
            getMenu().setEnabled(true);
495
            if (getMapContext().getLayers().getActives().length > 1) {
496
                agrupar.setEnabled(true);
497
            } else {
498
                agrupar.setEnabled(false);
499
            }
500
            agrupar.addActionListener(this);
501
        }
502
        }
503

    
504
        /* (non-Javadoc)
505
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
506
         */
507
        public void actionPerformed(ActionEvent e) {
508
                //ITocItem tocItem = (ITocItem) getNodeUserObject();
509
        ChangeName changename=new ChangeName(null);
510
                PluginServices.getMDIManager().addView(changename);
511
                String nombre=changename.getName();
512

    
513
        if (nombre != null){
514
                        if (isTocItemBranch()){
515
                                FLayer[] actives = getMapContext().getLayers().getActives();
516
                                FLayers allLayers = getMapContext().getLayers();
517
                                FLayer[] layers=new FLayer[allLayers.getLayersCount()];
518
                                for (int i=0;i<allLayers.getLayersCount();i++) {
519
                                        layers[i]=allLayers.getLayer(i);
520
                                }
521
                                if (actives.length>1) {
522

    
523
                                        FLayers layerGroup=new FLayers(getMapContext(),getNodeLayer().getParentLayer());
524
                                        boolean first=true;
525
                                        for (int i=0;i<layers.length;i++) {
526
                                        //for (int i=0;i<actives.length;i++){
527
                                                FLayer layer=layers[i];
528
                                                if (layer.isActive()) {
529
                                                        if (first) {
530
                                                                getMapContext().getLayers().addLayer(i,layerGroup);
531
                                                                first=false;
532
                                                        }
533
                                                        if(layer instanceof FLyrRaster)
534
                                                                ((FLyrRaster)layer).setRemoveRasterFlag(false);
535

    
536
                                                        layer.getParentLayer().removeLayer(layer);
537
                                                        layerGroup.addLayer(layer);
538
                                                }
539
                                        }
540
                                        layerGroup.setName(nombre);
541

    
542

    
543
                            }
544

    
545
                        }
546
                        // TRUCO PARA REFRESCAR.
547
                getMapContext().invalidate();
548
}
549
}
550
}
551
/**
552
 * Realiza una desagrupaci?n de capas, a partir de las capas que se encuentren activas.
553
 *
554
 * @author Vicente Caballero Navarro
555
 */
556
class LayersUngroupTocMenuEntry extends TocMenuEntry {
557
    private JMenuItem desagrupar;
558
        public void initialize(FPopupMenu m) {
559
                super.initialize(m);
560
        if (isTocItemBranch()) {
561
                    boolean isFLayers=true;
562
                    if (isTocItemBranch()){
563
                            FLayer lyr = getNodeLayer();
564
                            if (!(lyr instanceof FLayers)){
565
                                    isFLayers=false;
566
                            }
567
                    }
568
                    if (isFLayers){
569
                            desagrupar = new JMenuItem(PluginServices.getText(this, "desagrupar_capas"));
570
                    desagrupar.setFont(FPopupMenu.theFont);
571
                    getMenu().add(desagrupar);
572
                    getMenu().setEnabled(true);
573
                    desagrupar.addActionListener(this);
574
                    }
575
        }
576
        }
577

    
578
        /* (non-Javadoc)
579
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
580
         */
581
        public void actionPerformed(ActionEvent e) {
582
                //ITocItem tocItem = (ITocItem) getNodeUserObject();
583
                        if (isTocItemBranch()){
584
                                FLayers agrupa = (FLayers)getNodeLayer();
585
                                FLayers parent=agrupa.getParentLayer();
586
                                if (parent!=null){
587
                                        FLayers allLayers=getMapContext().getLayers();
588
                                        for (int i=0;i<allLayers.getLayersCount();i++) {
589
                                                FLayer layer=allLayers.getLayer(i);
590
                                                if (layer.equals(agrupa)) {
591
                                                        for (int j=0;j<agrupa.getLayersCount();j++){
592
                                                                parent.addLayer(i+j,agrupa.getLayer(j));
593
                                                        }
594
                                                        parent.removeLayer(agrupa);
595
                                                }
596
                                        }
597
                                }
598
                        }
599
                // TRUCO PARA REFRESCAR.
600
                getMapContext().invalidate();
601

    
602

    
603

    
604
        }
605
}
606
/**
607
 * Cambia la posici?n actual del layer a la primera posici?n.
608
 *
609
 * @author Vicente Caballero Navarro
610
 */
611
class FirstLayerTocMenuEntry extends TocMenuEntry {
612
    private JMenuItem first;
613
        public void initialize(FPopupMenu m) {
614
                super.initialize(m);
615
        if (isTocItemBranch()) {
616
                    first = new JMenuItem(PluginServices.getText(this, "colocar_delante"));
617
                first.setFont(FPopupMenu.theFont);
618
                getMenu().add(first);
619
                getMenu().setEnabled(true);
620
                first.addActionListener(this);
621
        }
622
        }
623

    
624
        /* (non-Javadoc)
625
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
626
         */
627
        public void actionPerformed(ActionEvent e) {
628
                ITocItem tocItem = (ITocItem) getNodeUserObject();
629

    
630
              if (isTocItemBranch()){
631
            if (getMapContext().getLayers().getActives().length == 1) {
632
                FLayer layer=((TocItemBranch)tocItem).getLayer();
633
                FLayers layers=layer.getParentLayer();
634
                for (int i=0;i<layers.getLayersCount();i++){
635
                    if(layers.getLayer(i).equals(layer)){
636
                        layers.removeLayer(i);
637
                        layers.addLayer(layer);
638
                    }
639
                }
640
            } else if (getMapContext().getLayers().getActives().length > 1) {
641
                FLayer[] actives = getMapContext().getLayers().getActives();
642
                FLayers layers=actives[0].getParentLayer();
643
                //FLayers layerGroup=new FLayers(getMapContext(),getNodeLayer().getParentLayer());
644
                for (int i=0;i<actives.length;i++){
645
                    for (int j=0;j<layers.getLayersCount();j++){
646
                        if(layers.getLayer(j).equals(actives[i])){
647
                            layers.removeLayer(j);
648
                            layers.addLayer(actives[i]);
649
                        }
650
                    }
651
                }
652
            }
653
                }
654
                // TRUCO PARA REFRESCAR.
655
        getMapContext().invalidate();
656
        }
657
}
658
/**
659
 * @author Nacho Brodin <brodin_ign@gva.es>
660
 *
661
 * Entrada de men? para la activaci?n de la funcionalidad de zoom a un
662
 * pixel.
663
 */
664
class ZoomPixelTocMenuEntry extends TocMenuEntry {
665
        public static final int ZOOM_TO_IMAGE_CENTER = 0x1;
666
        public static final int ZOOM_TO_VIEW_CENTER = 0x2;
667
        private JMenuItem properties;
668
        FLayer lyr = null;
669
        public int zoomType = ZOOM_TO_VIEW_CENTER;
670

    
671
        public void initialize(FPopupMenu m) {
672
                super.initialize(m);
673

    
674
                if (isTocItemBranch()) {
675
                        lyr = getNodeLayer();
676
                    // Opcciones para capas raster
677
                    if ((lyr instanceof FLyrRaster)) {
678
                            properties = new JMenuItem(PluginServices.getText(this, "Zoom_pixel"));
679
                            getMenu().add( properties );
680
                            properties.setFont(FPopupMenu.theFont);
681
                            getMenu().setEnabled(true);
682

    
683
                            properties.addActionListener(this);
684
                     }
685
                }
686
        }
687

    
688
        public void actionPerformed(ActionEvent e) {
689

    
690
                FLayer[] actives = getMapContext().getLayers().getActives();
691
                if (actives.length==1) {
692
                try {
693
                        Rectangle2D r = actives[0].getFullExtent();
694

    
695
                        ArrayList attr = ((FLyrRaster)actives[0]).getSource().getAttributes();
696
                        int width = 0, height = 0;
697
                               for (int i=0; i<attr.size(); i++) {
698
                                    Object[] a = (Object []) attr.get(i);
699
                                    if (a[0].toString().equals("Width"))
700
                                            width = ((Integer)a[1]).intValue();
701
                                    if (a[0].toString().equals("Height"))
702
                                            height = ((Integer)a[1]).intValue();
703
                            }
704
                        ViewPort v = getMapContext().getViewPort();
705

    
706
                        if (zoomType == ZOOM_TO_IMAGE_CENTER) {
707
                                //A centro de imagen
708
                                /*double centroX = width/2;
709
                                        double centroY = height/2;
710
                                        int minX = (int)Math.round(centroX - (v.getImageWidth()/2));
711
                                        int minY = (int)Math.round(centroY - (v.getImageHeight()/2));
712
                                        int maxX = (int)Math.round(minX + v.getImageWidth());
713
                                        int maxY = (int)Math.round(minY + v.getImageHeight());
714
                                        double wcOriginWidth = r.getMaxX() - r.getMinX();
715
                                        double wcOriginHeight = r.getMaxY() - r.getMinY();
716

717
                                        double wcDstMinX = r.getMinX()+((minX*wcOriginWidth)/width);
718
                                        double wcDstMinY = r.getMinY()+((minY*wcOriginHeight)/height);
719
                                        double wcDstMaxX = r.getMinX()+((maxX*wcOriginWidth)/width);
720
                                        double wcDstMaxY = r.getMinY()+((maxY*wcOriginHeight)/height);
721

722
                                        double wcDstWidth = wcDstMaxX - wcDstMinX;
723
                                        double wcDstHeight = wcDstMaxY - wcDstMinY;*/
724
                        } else if (zoomType == ZOOM_TO_VIEW_CENTER) {
725
                                //A centro de vista
726
                                double wcOriginCenterX = v.getExtent().getMinX()+((v.getExtent().getMaxX()-v.getExtent().getMinX())/2);
727
                                double wcOriginCenterY = v.getExtent().getMinY()+((v.getExtent().getMaxY()-v.getExtent().getMinY())/2);
728

    
729
                                //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
730
                                        double relacionPixelWcWidth =  (r.getMaxX() - r.getMinX())/width;
731
                                        double relacionPixelWcHeight = (r.getMaxY() - r.getMinY())/height;
732
                                        //double desplazamientoX = r.getMinX();
733
                                        //double desplazamientoY = r.getMinY();
734

    
735
                                        double wcOriginX = wcOriginCenterX - ((v.getImageWidth()*relacionPixelWcWidth)/2);
736
                                        double wcOriginY = wcOriginCenterY - ((v.getImageHeight()*relacionPixelWcHeight)/2);
737

    
738
                                        double wcDstMinX = wcOriginX;
739
                                        double wcDstMinY = wcOriginY;
740
                                        double wcDstMaxX = wcDstMinX + (v.getImageWidth()*relacionPixelWcWidth);
741
                                        double wcDstMaxY = wcDstMinY + (v.getImageHeight()*relacionPixelWcHeight);
742

    
743
                                        double wcDstWidth = wcDstMaxX - wcDstMinX;
744
                                        double wcDstHeight = wcDstMaxY - wcDstMinY;
745

    
746
                                r = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
747
                                getMapContext().getViewPort().setExtent(r);
748
                        }
749
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
750
                                e1.printStackTrace();
751
                        }
752
                }
753
                //View vista = (View) PluginServices.getMDIManager().getActiveView();
754
                //MapControl mapCtrl = vista.getMapControl();
755
        }
756
}
757

    
758
/**
759
 * @author Nacho Brodin <brodin_ign@gva.es>
760
 *
761
 * Entrada de men? para la activaci?n de la funcionalidad de zoom a un
762
 * pixel centrado en el cursor.
763
 */
764
class ZoomPixelCursorTocMenuEntry extends TocMenuEntry {
765
        public static final int ZOOM_TO_IMAGE_CENTER = 0x1;
766
        public static final int ZOOM_TO_VIEW_CENTER = 0x2;
767
        private JMenuItem properties;
768
        FLayer lyr = null;
769
        public int zoomType = ZOOM_TO_VIEW_CENTER;
770

    
771
        public void initialize(FPopupMenu m) {
772
                super.initialize(m);
773

    
774
                if (isTocItemBranch()) {
775
                        lyr = getNodeLayer();
776
                    // Opcciones para capas raster
777
                    if ((lyr instanceof FLyrRaster)) {
778
                            properties = new JMenuItem(PluginServices.getText(this, "Zoom_pixel"));
779
                            getMenu().add( properties );
780
                            properties.setFont(FPopupMenu.theFont);
781
                            getMenu().setEnabled(true);
782
                            properties.addActionListener(this);
783
                     }
784
                }
785
        }
786

    
787
        public void actionPerformed(ActionEvent e) {
788

    
789
                FLayer[] actives = getMapContext().getLayers().getActives();
790
                if (actives.length==1) {
791

    
792
                lyr = getNodeLayer();
793
                    View vista = (View) PluginServices.getMDIManager().getActiveView();
794
                    MapControl mapCtrl = vista.getMapControl();
795
                    mapCtrl.setTool("zoom_pixel_cursor");
796

    
797
                }
798
                //View vista = (View) PluginServices.getMDIManager().getActiveView();
799
                //MapControl mapCtrl = vista.getMapControl();
800
        }
801
}
802

    
803
/**
804
 *
805
 * @author Nacho Brodin <brodin_ign@gva.es>
806
 *
807
 * Entrada de men? para la activaci?n de la funcionalidad de salvar
808
 * a raster una parte de la vista.
809
 */
810
class SaveRasterTocMenuEntry extends TocMenuEntry{
811
        private JMenuItem properties;
812
        FLayer lyr = null;
813

    
814
        public void initialize(FPopupMenu m) {
815
                super.initialize(m);
816

    
817
                if (isTocItemBranch()) {
818
                        lyr = getNodeLayer();
819
                    // Opcciones para capas raster
820
                    if ((lyr instanceof FLyrRaster)) {
821
                            properties = new JMenuItem(PluginServices.getText(this, "salvar_raster"));
822
                            getMenu().add( properties );
823
                            properties.setFont(FPopupMenu.theFont);
824
                            getMenu().setEnabled(true);
825
                            //getMenu().addSeparator();
826
                    //Cambio color
827
                            properties.addActionListener(this);
828
                     }
829
                }
830
        }
831

    
832
        public void actionPerformed(ActionEvent e) {
833
                lyr = getNodeLayer();
834
                View vista = (View) PluginServices.getMDIManager().getActiveView();
835
                MapControl mapCtrl = vista.getMapControl();
836
                mapCtrl.setTool("saveRaster");
837
        }
838
}
839

    
840
/**
841
*
842
* @author Jose Manuel Viv? (Chema)
843
*
844
* Entrada de men? para recargar una capa.
845
*/
846
class ReloadLayerTocMenuEntry extends TocMenuEntry{
847
        private JMenuItem properties;
848
        FLayer lyr = null;
849

    
850
        public void initialize(FPopupMenu m) {
851
                super.initialize(m);
852

    
853
                if (isTocItemBranch()) {
854
                        lyr = getNodeLayer();
855
                    // Opcciones para capas raster
856

    
857
                        properties = new JMenuItem(PluginServices.getText(this, "recargar"));
858
                        getMenu().add( properties );
859
                        properties.setFont(FPopupMenu.theFont);
860

    
861
                        properties.setEnabled((!lyr.isAvailable()));
862

    
863
                        properties.addActionListener(this);
864

    
865
                }
866
        }
867

    
868
        public void actionPerformed(ActionEvent e) {
869
                lyr = getNodeLayer();
870
                try {
871
                        lyr.reload();
872
                } catch (Exception ex) {
873
                        ex.printStackTrace();
874
                }
875
        }
876
}
877

    
878

    
879

    
880

    
881
/**
882
 * Menu de bot?n derecho para el TOC.
883
 * Se pueden a?adir entradas facilmente desde una extensi?n,
884
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
885
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
886
 * (Las entradas actuales est?n hechas de esa manera).
887
 *
888
 * @author vcn To change the template for this generated type comment go to
889
 *         Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and
890
 *         Comments
891
 */
892

    
893
public class FPopupMenu extends JPopupMenu {
894
        private static ArrayList menuEntrys = new ArrayList();
895
    public DefaultMutableTreeNode nodo;
896
    protected FMap mapContext;
897
    //private JMenuItem capa;
898
    // Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
899
    // TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
900
    public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10);
901

    
902
    static {
903
            FPopupMenu.addEntry(new FSymbolChangeColorTocMenuEntry());
904
            FPopupMenu.addEntry(new ChangeNameTocMenuEntry());
905
            menuEntrys.add(new FLyrVectEditPropertiesTocMenuEntry());
906
            //FPopupMenu.addEntry(new FLyrRasterAdjustTransparencyTocMenuEntry());
907
            FPopupMenu.addEntry(new ZoomAlTemaTocMenuEntry());
908
            //FPopupMenu.addEntry(new ZoomPixelTocMenuEntry());
909
            FPopupMenu.addEntry(new ZoomPixelCursorTocMenuEntry());
910
            FPopupMenu.addEntry(new EliminarCapaTocMenuEntry());
911
            FPopupMenu.addEntry(new ReloadLayerTocMenuEntry());
912
            menuEntrys.add(new LayersGroupTocMenuEntry());
913
                menuEntrys.add(new LayersUngroupTocMenuEntry());
914
                menuEntrys.add(new FirstLayerTocMenuEntry());
915
            FPopupMenu.addEntry(new FLyrRasterAdjustPropertiesTocMenuEntry());
916
    }
917

    
918
    public static void addEntry(TocMenuEntry entry) {
919
            menuEntrys.add(entry);
920
    }
921

    
922
    public static Object getEntry(String className) {
923
            for(int i=0;i<menuEntrys.size();i++){
924
                    if(menuEntrys.get(i).getClass().getName().endsWith(className)){
925
                            return menuEntrys.get(i);
926
                    }
927
            }
928
            return null;
929
    }
930

    
931
    /**
932
     * Creates a new FPopupMenu object.
933
     *
934
     * @param nodo DOCUMENT ME!
935
     * @param vista DOCUMENT ME!
936
     */
937
    public FPopupMenu(FMap mc, DefaultMutableTreeNode node) {
938
        //super();
939
        this.mapContext = mc;
940
        this.nodo = node;
941

    
942
        //salir = new MenuItem("Salir");
943

    
944
        for (int i=0; i<menuEntrys.size(); i++) {
945
                ((TocMenuEntry) menuEntrys.get(i)).initialize(this);
946
        }
947
    }
948

    
949
    public FMap getMapContext() { return mapContext; }
950

    
951
}