Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toc / FPopupMenu.java @ 6543

History | View | Annotate | Download (30.7 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

    
517
                                if (actives.length>1) {
518

    
519
                                        FLayers layerGroup=new FLayers(getMapContext(),getNodeLayer().getParentLayer());
520
                                        View theView = (View) PluginServices.getMDIManager().getActiveView();
521
                                        theView.getMapControl().getMapContext().getLayers()
522
                                           .addLayer(layerGroup);
523
                                        for (int i=0;i<actives.length;i++){
524

    
525
                                                if(actives[i] instanceof FLyrRaster)
526
                                                        ((FLyrRaster)actives[i]).setRemoveRasterFlag(false);
527

    
528
                                                actives[i].getParentLayer().removeLayer(actives[i]);
529
                                                layerGroup.addLayer(actives[i]);
530
                                        }
531
                                        layerGroup.setName(nombre);
532

    
533

    
534
                            }
535

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

    
569
        /* (non-Javadoc)
570
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
571
         */
572
        public void actionPerformed(ActionEvent e) {
573
                //ITocItem tocItem = (ITocItem) getNodeUserObject();
574
                        if (isTocItemBranch()){
575
                                FLayers agrupa = (FLayers)getNodeLayer();
576
                                FLayers parent=agrupa.getParentLayer();
577
                                if (parent!=null){
578
                                        for (int j=0;j<agrupa.getLayersCount();j++){
579
                                                parent.addLayer(agrupa.getLayer(j));
580
                                        }
581
                                        parent.removeLayer(agrupa);
582
                                }
583
                        }
584
                // TRUCO PARA REFRESCAR.
585
                getMapContext().invalidate();
586

    
587

    
588

    
589
        }
590
}
591
/**
592
 * Cambia la posici?n actual del layer a la primera posici?n.
593
 *
594
 * @author Vicente Caballero Navarro
595
 */
596
class FirstLayerTocMenuEntry extends TocMenuEntry {
597
    private JMenuItem first;
598
        public void initialize(FPopupMenu m) {
599
                super.initialize(m);
600
        if (isTocItemBranch()) {
601
                    first = new JMenuItem(PluginServices.getText(this, "colocar_delante"));
602
                first.setFont(FPopupMenu.theFont);
603
                getMenu().add(first);
604
                getMenu().setEnabled(true);
605
                first.addActionListener(this);
606
        }
607
        }
608

    
609
        /* (non-Javadoc)
610
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
611
         */
612
        public void actionPerformed(ActionEvent e) {
613
                ITocItem tocItem = (ITocItem) getNodeUserObject();
614

    
615
              if (isTocItemBranch()){
616
            if (getMapContext().getLayers().getActives().length == 1) {
617
                FLayer layer=((TocItemBranch)tocItem).getLayer();
618
                FLayers layers=layer.getParentLayer();
619
                for (int i=0;i<layers.getLayersCount();i++){
620
                    if(layers.getLayer(i).equals(layer)){
621
                        layers.removeLayer(i);
622
                        layers.addLayer(layer);
623
                    }
624
                }
625
            } else if (getMapContext().getLayers().getActives().length > 1) {
626
                FLayer[] actives = getMapContext().getLayers().getActives();
627
                FLayers layers=actives[0].getParentLayer();
628
                //FLayers layerGroup=new FLayers(getMapContext(),getNodeLayer().getParentLayer());
629
                for (int i=0;i<actives.length;i++){
630
                    for (int j=0;j<layers.getLayersCount();j++){
631
                        if(layers.getLayer(j).equals(actives[i])){
632
                            layers.removeLayer(j);
633
                            layers.addLayer(actives[i]);
634
                        }
635
                    }
636
                }
637
            }
638
                }
639
                // TRUCO PARA REFRESCAR.
640
        getMapContext().invalidate();
641
        }
642
}
643
/**
644
 * @author Nacho Brodin <brodin_ign@gva.es>
645
 *
646
 * Entrada de men? para la activaci?n de la funcionalidad de zoom a un
647
 * pixel.
648
 */
649
class ZoomPixelTocMenuEntry extends TocMenuEntry {
650
        public static final int ZOOM_TO_IMAGE_CENTER = 0x1;
651
        public static final int ZOOM_TO_VIEW_CENTER = 0x2;
652
        private JMenuItem properties;
653
        FLayer lyr = null;
654
        public int zoomType = ZOOM_TO_VIEW_CENTER;
655

    
656
        public void initialize(FPopupMenu m) {
657
                super.initialize(m);
658

    
659
                if (isTocItemBranch()) {
660
                        lyr = getNodeLayer();
661
                    // Opcciones para capas raster
662
                    if ((lyr instanceof FLyrRaster)) {
663
                            properties = new JMenuItem(PluginServices.getText(this, "Zoom_pixel"));
664
                            getMenu().add( properties );
665
                            properties.setFont(FPopupMenu.theFont);
666
                            getMenu().setEnabled(true);
667

    
668
                            properties.addActionListener(this);
669
                     }
670
                }
671
        }
672

    
673
        public void actionPerformed(ActionEvent e) {
674

    
675
                FLayer[] actives = getMapContext().getLayers().getActives();
676
                if (actives.length==1) {
677
                try {
678
                        Rectangle2D r = actives[0].getFullExtent();
679

    
680
                        ArrayList attr = ((FLyrRaster)actives[0]).getSource().getAttributes();
681
                        int width = 0, height = 0;
682
                               for (int i=0; i<attr.size(); i++) {
683
                                    Object[] a = (Object []) attr.get(i);
684
                                    if (a[0].toString().equals("Width"))
685
                                            width = ((Integer)a[1]).intValue();
686
                                    if (a[0].toString().equals("Height"))
687
                                            height = ((Integer)a[1]).intValue();
688
                            }
689
                        ViewPort v = getMapContext().getViewPort();
690

    
691
                        if (zoomType == ZOOM_TO_IMAGE_CENTER) {
692
                                //A centro de imagen
693
                                /*double centroX = width/2;
694
                                        double centroY = height/2;
695
                                        int minX = (int)Math.round(centroX - (v.getImageWidth()/2));
696
                                        int minY = (int)Math.round(centroY - (v.getImageHeight()/2));
697
                                        int maxX = (int)Math.round(minX + v.getImageWidth());
698
                                        int maxY = (int)Math.round(minY + v.getImageHeight());
699
                                        double wcOriginWidth = r.getMaxX() - r.getMinX();
700
                                        double wcOriginHeight = r.getMaxY() - r.getMinY();
701

702
                                        double wcDstMinX = r.getMinX()+((minX*wcOriginWidth)/width);
703
                                        double wcDstMinY = r.getMinY()+((minY*wcOriginHeight)/height);
704
                                        double wcDstMaxX = r.getMinX()+((maxX*wcOriginWidth)/width);
705
                                        double wcDstMaxY = r.getMinY()+((maxY*wcOriginHeight)/height);
706

707
                                        double wcDstWidth = wcDstMaxX - wcDstMinX;
708
                                        double wcDstHeight = wcDstMaxY - wcDstMinY;*/
709
                        } else if (zoomType == ZOOM_TO_VIEW_CENTER) {
710
                                //A centro de vista
711
                                double wcOriginCenterX = v.getExtent().getMinX()+((v.getExtent().getMaxX()-v.getExtent().getMinX())/2);
712
                                double wcOriginCenterY = v.getExtent().getMinY()+((v.getExtent().getMaxY()-v.getExtent().getMinY())/2);
713

    
714
                                //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
715
                                        double relacionPixelWcWidth =  (r.getMaxX() - r.getMinX())/width;
716
                                        double relacionPixelWcHeight = (r.getMaxY() - r.getMinY())/height;
717
                                        //double desplazamientoX = r.getMinX();
718
                                        //double desplazamientoY = r.getMinY();
719

    
720
                                        double wcOriginX = wcOriginCenterX - ((v.getImageWidth()*relacionPixelWcWidth)/2);
721
                                        double wcOriginY = wcOriginCenterY - ((v.getImageHeight()*relacionPixelWcHeight)/2);
722

    
723
                                        double wcDstMinX = wcOriginX;
724
                                        double wcDstMinY = wcOriginY;
725
                                        double wcDstMaxX = wcDstMinX + (v.getImageWidth()*relacionPixelWcWidth);
726
                                        double wcDstMaxY = wcDstMinY + (v.getImageHeight()*relacionPixelWcHeight);
727

    
728
                                        double wcDstWidth = wcDstMaxX - wcDstMinX;
729
                                        double wcDstHeight = wcDstMaxY - wcDstMinY;
730

    
731
                                r = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
732
                                getMapContext().getViewPort().setExtent(r);
733
                        }
734
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
735
                                e1.printStackTrace();
736
                        }
737
                }
738
                //View vista = (View) PluginServices.getMDIManager().getActiveView();
739
                //MapControl mapCtrl = vista.getMapControl();
740
        }
741
}
742

    
743
/**
744
 * @author Nacho Brodin <brodin_ign@gva.es>
745
 *
746
 * Entrada de men? para la activaci?n de la funcionalidad de zoom a un
747
 * pixel centrado en el cursor.
748
 */
749
class ZoomPixelCursorTocMenuEntry extends TocMenuEntry {
750
        public static final int ZOOM_TO_IMAGE_CENTER = 0x1;
751
        public static final int ZOOM_TO_VIEW_CENTER = 0x2;
752
        private JMenuItem properties;
753
        FLayer lyr = null;
754
        public int zoomType = ZOOM_TO_VIEW_CENTER;
755

    
756
        public void initialize(FPopupMenu m) {
757
                super.initialize(m);
758

    
759
                if (isTocItemBranch()) {
760
                        lyr = getNodeLayer();
761
                    // Opcciones para capas raster
762
                    if ((lyr instanceof FLyrRaster)) {
763
                            properties = new JMenuItem(PluginServices.getText(this, "Zoom_pixel"));
764
                            getMenu().add( properties );
765
                            properties.setFont(FPopupMenu.theFont);
766
                            getMenu().setEnabled(true);
767
                            properties.addActionListener(this);
768
                     }
769
                }
770
        }
771

    
772
        public void actionPerformed(ActionEvent e) {
773

    
774
                FLayer[] actives = getMapContext().getLayers().getActives();
775
                if (actives.length==1) {
776

    
777
                lyr = getNodeLayer();
778
                    View vista = (View) PluginServices.getMDIManager().getActiveView();
779
                    MapControl mapCtrl = vista.getMapControl();
780
                    mapCtrl.setTool("zoom_pixel_cursor");
781

    
782
                }
783
                //View vista = (View) PluginServices.getMDIManager().getActiveView();
784
                //MapControl mapCtrl = vista.getMapControl();
785
        }
786
}
787

    
788
/**
789
 *
790
 * @author Nacho Brodin <brodin_ign@gva.es>
791
 *
792
 * Entrada de men? para la activaci?n de la funcionalidad de salvar
793
 * a raster una parte de la vista.
794
 */
795
class SaveRasterTocMenuEntry extends TocMenuEntry{
796
        private JMenuItem properties;
797
        FLayer lyr = null;
798

    
799
        public void initialize(FPopupMenu m) {
800
                super.initialize(m);
801

    
802
                if (isTocItemBranch()) {
803
                        lyr = getNodeLayer();
804
                    // Opcciones para capas raster
805
                    if ((lyr instanceof FLyrRaster)) {
806
                            properties = new JMenuItem(PluginServices.getText(this, "salvar_raster"));
807
                            getMenu().add( properties );
808
                            properties.setFont(FPopupMenu.theFont);
809
                            getMenu().setEnabled(true);
810
                            //getMenu().addSeparator();
811
                    //Cambio color
812
                            properties.addActionListener(this);
813
                     }
814
                }
815
        }
816

    
817
        public void actionPerformed(ActionEvent e) {
818
                lyr = getNodeLayer();
819
                View vista = (View) PluginServices.getMDIManager().getActiveView();
820
                MapControl mapCtrl = vista.getMapControl();
821
                mapCtrl.setTool("saveRaster");
822
        }
823
}
824

    
825
/**
826
*
827
* @author Jose Manuel Viv? (Chema)
828
*
829
* Entrada de men? para recargar una capa.
830
*/
831
class ReloadLayerTocMenuEntry extends TocMenuEntry{
832
        private JMenuItem properties;
833
        FLayer lyr = null;
834

    
835
        public void initialize(FPopupMenu m) {
836
                super.initialize(m);
837

    
838
                if (isTocItemBranch()) {
839
                        lyr = getNodeLayer();
840
                    // Opcciones para capas raster
841
                    
842
                        properties = new JMenuItem(PluginServices.getText(this, "recargar"));
843
                        getMenu().add( properties );
844
                        properties.setFont(FPopupMenu.theFont);                        
845
                                                
846
                        properties.setEnabled((!lyr.isAvailable()));
847
                        
848
                        properties.addActionListener(this);
849
                        
850
                }
851
        }
852

    
853
        public void actionPerformed(ActionEvent e) {
854
                lyr = getNodeLayer();
855
                try {
856
                        lyr.reload();
857
                } catch (Exception ex) {
858
                        ex.printStackTrace();
859
                }
860
        }
861
}
862

    
863

    
864

    
865

    
866
/**
867
 * Menu de bot?n derecho para el TOC.
868
 * Se pueden a?adir entradas facilmente desde una extensi?n,
869
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
870
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
871
 * (Las entradas actuales est?n hechas de esa manera).
872
 *
873
 * @author vcn To change the template for this generated type comment go to
874
 *         Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and
875
 *         Comments
876
 */
877

    
878
public class FPopupMenu extends JPopupMenu {
879
        private static ArrayList menuEntrys = new ArrayList();
880
    public DefaultMutableTreeNode nodo;
881
    protected FMap mapContext;
882
    //private JMenuItem capa;
883
    // Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
884
    // TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
885
    public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10);
886

    
887
    static {
888
            FPopupMenu.addEntry(new FSymbolChangeColorTocMenuEntry());
889
            FPopupMenu.addEntry(new ChangeNameTocMenuEntry());
890
            menuEntrys.add(new FLyrVectEditPropertiesTocMenuEntry());
891
            //FPopupMenu.addEntry(new FLyrRasterAdjustTransparencyTocMenuEntry());
892
            FPopupMenu.addEntry(new ZoomAlTemaTocMenuEntry());
893
            //FPopupMenu.addEntry(new ZoomPixelTocMenuEntry());
894
            FPopupMenu.addEntry(new ZoomPixelCursorTocMenuEntry());
895
            FPopupMenu.addEntry(new EliminarCapaTocMenuEntry());
896
            FPopupMenu.addEntry(new ReloadLayerTocMenuEntry());
897
            menuEntrys.add(new LayersGroupTocMenuEntry());
898
                menuEntrys.add(new LayersUngroupTocMenuEntry());
899
                menuEntrys.add(new FirstLayerTocMenuEntry());
900
            FPopupMenu.addEntry(new FLyrRasterAdjustPropertiesTocMenuEntry());
901
    }
902

    
903
    public static void addEntry(TocMenuEntry entry) {
904
            menuEntrys.add(entry);
905
    }
906

    
907
    public static Object getEntry(String className) {
908
            for(int i=0;i<menuEntrys.size();i++){
909
                    if(menuEntrys.get(i).getClass().getName().endsWith(className)){
910
                            return menuEntrys.get(i);
911
                    }
912
            }
913
            return null;
914
    }
915

    
916
    /**
917
     * Creates a new FPopupMenu object.
918
     *
919
     * @param nodo DOCUMENT ME!
920
     * @param vista DOCUMENT ME!
921
     */
922
    public FPopupMenu(FMap mc, DefaultMutableTreeNode node) {
923
        //super();
924
        this.mapContext = mc;
925
        this.nodo = node;
926

    
927
        //salir = new MenuItem("Salir");
928

    
929
        for (int i=0; i<menuEntrys.size(); i++) {
930
                ((TocMenuEntry) menuEntrys.get(i)).initialize(this);
931
        }
932
    }
933

    
934
    public FMap getMapContext() { return mapContext; }
935

    
936
}