Statistics
| Revision:

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

History | View | Annotate | Download (31.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.Font;
48
import java.awt.event.ActionEvent;
49
import java.awt.geom.Rectangle2D;
50
import java.util.ArrayList;
51

    
52
import javax.swing.JColorChooser;
53
import javax.swing.JDialog;
54
import javax.swing.JMenuItem;
55
import javax.swing.JPopupMenu;
56
import javax.swing.table.DefaultTableModel;
57
import javax.swing.tree.DefaultMutableTreeNode;
58

    
59
import org.cresques.io.GeoRasterFile;
60
import org.cresques.io.raster.RasterFilterStackManager;
61
import org.cresques.ui.raster.BandSetupPanel;
62
import org.cresques.ui.raster.EnhancedPanel;
63
import org.cresques.ui.raster.FilterRasterDialogPanel;
64
import org.cresques.ui.raster.RasterTransparencyPanel;
65

    
66
import com.iver.andami.PluginServices;
67
import com.iver.cit.gvsig.fmap.DriverException;
68
import com.iver.cit.gvsig.fmap.FMap;
69
import com.iver.cit.gvsig.fmap.MapControl;
70
import com.iver.cit.gvsig.fmap.ViewPort;
71
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
72
import com.iver.cit.gvsig.fmap.layers.CancelationException;
73
import com.iver.cit.gvsig.fmap.layers.FLayer;
74
import com.iver.cit.gvsig.fmap.layers.FLayers;
75
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
76
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
77
import com.iver.cit.gvsig.fmap.layers.RasterAdapter;
78
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
79
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
80
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
81
import com.iver.cit.gvsig.fmap.tools.Behavior.MouseMovementBehavior;
82
import com.iver.cit.gvsig.fmap.tools.Behavior.PointBehavior;
83
import com.iver.cit.gvsig.gui.View;
84
import com.iver.cit.gvsig.gui.Panels.AdjustTransparencyPanel;
85
import com.iver.cit.gvsig.gui.Panels.PropertiesRasterDialog;
86
import com.iver.cit.gvsig.gui.thememanager.FThemeManagerWindow;
87
import com.iver.cit.gvsig.gui.toolListeners.StatusBarListener;
88
import com.iver.cit.gvsig.gui.toolListeners.ZoomPixelCursorListener;
89

    
90
/**
91
 * Realiza el cambio de color si se pulsa OK
92
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
93
 */
94
class FSymbolChangeColorTocMenuEntry extends TocMenuEntry {
95
    private JMenuItem color;    
96
        public void initialize(FPopupMenu m) {
97
                super.initialize(m);
98
                boolean bShow = false;
99
                if (isTocItemBranch())
100
                {
101
                        FLayer lyr = getNodeLayer();
102
                    if ((lyr instanceof ClassifiableVectorial))
103
                    {
104
                        ClassifiableVectorial lyrVect = (ClassifiableVectorial) lyr;
105
                        if (lyrVect.getLegend() instanceof SingleSymbolLegend)
106
                            bShow = true;
107
                    }
108
                }
109
                else
110
                    bShow = true;
111
                if (bShow)
112
                {
113
                color = new JMenuItem(PluginServices.getText(this, "Cambio_Color"));
114
                color.setFont(FPopupMenu.theFont);
115
                getMenu().add(color);        
116
                getMenu().setEnabled(true);
117
                //Cambio color
118
                color.addActionListener(this);
119
                }
120
        }
121
        
122
        /* (non-Javadoc)
123
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
124
         */
125
        public void actionPerformed(ActionEvent e) {
126
                ITocItem tocItem = (ITocItem) getNodeUserObject();
127
        
128
        Color newColor = JColorChooser.showDialog(null,                
129
                PluginServices.getText(this, "Elegir_Color"),
130
                null);
131
        if (newColor != null)
132
        {
133
                        if (isTocItemBranch())
134
                        {
135
                                FLayer lyr = getNodeLayer();
136
                            if ((lyr instanceof ClassifiableVectorial))
137
                            {
138
                                ClassifiableVectorial lyrVect = (ClassifiableVectorial) lyr;
139
                                if (lyrVect.getLegend() instanceof SingleSymbolLegend)
140
                                {
141
                                    SingleSymbolLegend leg = (SingleSymbolLegend) lyrVect.getLegend();
142
                        leg.getDefaultSymbol().setColor(newColor);
143
                    } 
144
                            }
145
                        }
146
                        else
147
                        {
148
                    TocItemLeaf leaf = (TocItemLeaf) tocItem;
149
                    FSymbol sym = leaf.getSymbol();
150
                    sym.setColor(newColor);
151
                                                
152
                        }
153
                // TRUCO PARA REFRESCAR.
154
                getMapContext().invalidate();                    
155

    
156
        }
157
        
158
        }
159
}
160
/**
161
 * Realiza un cambio de nombre en la capa seleccionada
162
 * 
163
 * @author Vicente Caballero Navarro
164
 */
165
class ChangeNameTocMenuEntry extends TocMenuEntry {
166
    private JMenuItem changename;    
167
        public void initialize(FPopupMenu m) {
168
                super.initialize(m);
169
                boolean bShow = false;
170
                if (isTocItemBranch())
171
                {
172
                        changename = new JMenuItem(PluginServices.getText(this, "cambio_nombre"));
173
                changename.setFont(FPopupMenu.theFont);
174
                getMenu().add(changename);        
175
                getMenu().setEnabled(true);
176
                //Cambio nombre
177
                changename.addActionListener(this);
178
                    
179
                }
180
        }
181
        
182
        /* (non-Javadoc)
183
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
184
         */
185
        public void actionPerformed(ActionEvent e) {
186
                ITocItem tocItem = (ITocItem) getNodeUserObject();
187
                if (isTocItemBranch())
188
                        {
189
                                FLayer lyr = getNodeLayer();
190
                                ChangeName chn=new ChangeName(lyr.getName());
191
                                PluginServices.getMDIManager().addView(chn);
192
                                 getNodeLayer().setName(chn.getName());
193
                        }
194
                // TRUCO PARA REFRESCAR.
195
                ///getMapContext().invalidate();                    
196

    
197
   }
198
}
199
/**
200
 * Muestra el men? de propiedades del tema.
201
 * 
202
 * @author jmorell
203
 *
204
 * TODO To change the template for this generated type comment go to
205
 * Window - Preferences - Java - Code Style - Code Templates
206
 */
207
class FLyrVectEditPropertiesTocMenuEntry extends TocMenuEntry {
208
    private JMenuItem prop;
209
        public void initialize(FPopupMenu m) {
210
                super.initialize(m);
211
                FLayer lyr = null;
212
                if (isTocItemBranch()) {
213
                        getMenu().addSeparator();
214
                        lyr = getNodeLayer();
215
                        if (lyr instanceof ClassifiableVectorial) {
216
                            prop = new JMenuItem(PluginServices.getText(this, "Cambio_Estilo"));
217
                                getMenu().add(prop);
218
                                prop.setFont(FPopupMenu.theFont);
219
                                //Cambio estilo
220
                                prop.addActionListener(this);
221
                        }
222
                }
223
        }
224
        /* (non-Javadoc)
225
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
226
         */
227
        public void actionPerformed(ActionEvent e) {
228
                /*FThemeManagerWindow fThemeManagerWindow = new FThemeManagerWindow();
229
                try {
230
                        fThemeManagerWindow.setMapContext(getMapContext());
231
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
232
                        e1.printStackTrace();
233
                }*/
234
                FLayer[] actives = getMapContext().getLayers().getActives();
235
                FThemeManagerWindow fThemeManagerWindow;
236
            if (actives.length==1) {
237
                    System.out.println("openPliegoThemeProperties(): Una sola capa. Abrimos el panel para la capa " + actives[0]);
238
                    fThemeManagerWindow = new FThemeManagerWindow();
239
                    try {
240
                            fThemeManagerWindow.setMapContext(getMapContext());
241
                    } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
242
                            e1.printStackTrace();
243
                    }
244
            //PluginServices.getMDIManager().addView(fThemeManagerWindow);
245
                    if (PluginServices.getMainFrame() == null) {
246
                            JDialog dlg = new JDialog();
247
                            fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
248
                            dlg.getContentPane().add(fThemeManagerWindow);
249
                            dlg.setModal(false);                        
250
                            dlg.pack();
251
                            dlg.show();
252
                    } else {
253
                            PluginServices.getMDIManager().addView(fThemeManagerWindow);
254
                    }
255
            } else {
256
                for (int i = 0; i < actives.length; i++){
257
                        System.out.println("openPliegoThemeProperties(): Muchas capas. Abrimos el panel para la capa " + actives[i]);
258
                        fThemeManagerWindow = new FThemeManagerWindow(actives[i], getMapContext());
259
                //PluginServices.getMDIManager().addView(fThemeManagerWindow);
260
                        if (PluginServices.getMainFrame() == null) {
261
                                JDialog dlg = new JDialog();
262
                                fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
263
                                dlg.getContentPane().add(fThemeManagerWindow);
264
                                dlg.setModal(false);                        
265
                                dlg.pack();
266
                                dlg.show();
267
                        } else {
268
                                PluginServices.getMDIManager().addView(fThemeManagerWindow);
269
                        }
270
                }
271
            }
272
                /*if (PluginServices.getMainFrame() == null) {
273
                        JDialog dlg = new JDialog();
274
                        fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
275
                        dlg.getContentPane().add(fThemeManagerWindow);
276
                        dlg.setModal(false);                        
277
                        dlg.pack();
278
                        dlg.show();
279
                } else {
280
                        PluginServices.getMDIManager().addView(fThemeManagerWindow);
281
                }*/
282
        }
283
}
284

    
285
class FLyrRasterAdjustTransparencyTocMenuEntry extends TocMenuEntry {
286
        private JMenuItem transparency;
287
        FLayer lyr = null;
288
        public void initialize(FPopupMenu m) {
289
                super.initialize(m);
290
                
291
                if (isTocItemBranch()) {
292
                        lyr = getNodeLayer();
293
                    // Opcciones para capas raster
294
                    if ((lyr instanceof FLyrRaster)) {
295
                        transparency = new JMenuItem(PluginServices.getText(this, "adjust_transparency"));
296
                            getMenu().add( transparency );
297
                            transparency.setFont(FPopupMenu.theFont);
298
                            getMenu().setEnabled(true);
299
                            //getMenu().addSeparator();
300
                    //Cambio color
301
                    transparency.addActionListener(this);
302
                     }
303
                }
304
        }
305
        /* (non-Javadoc)
306
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
307
         */
308
        public void actionPerformed(ActionEvent e) {
309
               lyr = getNodeLayer();
310
        AdjustTransparencyPanel transPanel = new AdjustTransparencyPanel((FLyrDefault) lyr);
311
                transPanel.openJDialog();
312
        //getMapContext().invalidate();                    
313
        }
314
}
315

    
316
class FLyrRasterAdjustPropertiesTocMenuEntry extends TocMenuEntry {
317
        private JMenuItem                                 properties;
318
        FLayer                                                         lyr = null;
319
        private PropertiesRasterDialog        propsDialog = null;
320
        private BandSetupPanel                         bandSetup = null;
321

    
322
        
323
        public void initialize(FPopupMenu m) {
324
                super.initialize(m);
325
                
326
                if (isTocItemBranch()) {
327
                        lyr = getNodeLayer();
328
                    // Opcciones para capas raster
329
                    if ((lyr instanceof FLyrRaster)) {
330
                            properties = new JMenuItem(PluginServices.getText(this, "propiedades_raster"));
331
                            getMenu().add( properties );
332
                            properties.setFont(FPopupMenu.theFont);
333
                            getMenu().setEnabled(true);
334
                            //getMenu().addSeparator();
335
                    //Cambio color
336
                            properties.addActionListener(this);
337
                     }
338
                }
339
        }
340
        
341
        /**
342
         * Gestiona la apertura del dialogo de propiedades de raster cuando se pulsa
343
         * la opci?n asignando a este las propiedades iniciales. 
344
         */
345
        public void actionPerformed(ActionEvent e) {
346
               lyr = getNodeLayer();
347
                   
348
               if(lyr instanceof FLyrRaster){
349
                       RasterFilterStackManager         stackManager = null;
350
       
351
                       stackManager = new RasterFilterStackManager(((FLyrRaster)lyr).getSource().getFilterStack());
352
                                              
353
                       int[][] rangeR = stackManager.getTransparecyR();
354
                       int[][] rangeG = stackManager.getTransparecyG();
355
                       int[][] rangeB = stackManager.getTransparecyB();
356
                       
357
                       propsDialog = new PropertiesRasterDialog(((FLyrRaster)lyr), rangeR, rangeG, rangeB);
358
                                              
359
                        int alpha = 255-((FLyrRaster)lyr).getSource().getTransparency();
360
                        
361
                        bandSetup = ((FilterRasterDialogPanel)propsDialog.getContentPane()).getBandSetup();
362
                        
363
                        //Asignaci?n del alpha actual de la imagen al dialogo
364
                        RasterTransparencyPanel rasterTrans = ((FilterRasterDialogPanel)propsDialog.getContentPane()).getTransparencyPanel();
365
                        rasterTrans.setOpacity(alpha);
366
                                
367
                        //Asignaci?n del porcentaje de recorte actual de la imagen al dialogo
368
                        EnhancedPanel pEnhan =  ((FilterRasterDialogPanel)propsDialog.getContentPane()).getEnhancedPanel();
369
                        
370
                        propsDialog.setRasterFilterStackManager(stackManager);
371
                        
372
                        ArrayList stackList = stackManager.getStringsFromStack();
373
                        boolean remove = false;
374
                        String hideBands = null;
375
                        for(int i=0;i<stackList.size();i++){
376
                                //System.out.println("*=>"+((String)stackList.get(i)));
377
                                if(((String)stackList.get(i)).startsWith("filter.tail.value")){
378
                                        int tail = (int)(Double.parseDouble(stackManager.getValue((String)stackList.get(i)))*200);
379
                                        pEnhan.setPercentTail(tail);
380
                                }
381
                                if(((String)stackList.get(i)).startsWith("filter.tail.remove"))
382
                                        remove = Boolean.valueOf(stackManager.getValue((String)stackList.get(i))).booleanValue();
383
                                
384
                                if(((String)stackList.get(i)).startsWith("filter.enhanced.remove"))
385
                                        remove = Boolean.valueOf(stackManager.getValue((String)stackList.get(i))).booleanValue();
386
                                
387
                                if(((String)stackList.get(i)).startsWith("filter.removebands.bands"))
388
                                        hideBands = stackManager.getValue((String)stackList.get(i));
389
                        }
390
                        
391
                        if(stackManager.isActive(stackManager.getTypeFilter("enhanced"))){
392
                                if(remove)
393
                                        pEnhan.setSelectedFilter(1);
394
                                else
395
                                        pEnhan.setSelectedFilter(2);
396
                        }else
397
                                pEnhan.setSelectedFilter(0);
398
                                                
399
                        GeoRasterFile[] files = ((FLyrRaster)lyr).getSource().getFiles();
400
                        bandSetup.addFiles(files);
401
                        for(int i=0; i< files.length;i++)
402
                                propsDialog.addNumBands(files[i].getBandCount());
403
                        
404
                        RasterAdapter rad = ((FLyrRaster)lyr).getSource();
405
                        int posR = 0, posG = 0, posB = 0;
406
                        
407
                        for(int i=0;i<rad.getPosFile(GeoRasterFile.RED_BAND);i++)
408
                                posR += files[i].getBandCount();
409
                        posR += rad.getBand(GeoRasterFile.RED_BAND);
410
                        
411
                        for(int i=0;i<rad.getPosFile(GeoRasterFile.GREEN_BAND);i++)
412
                                posG += files[i].getBandCount();
413
                        posG += rad.getBand(GeoRasterFile.GREEN_BAND);
414
                        
415
                        for(int i=0;i<rad.getPosFile(GeoRasterFile.BLUE_BAND);i++)
416
                                posB += files[i].getBandCount();
417
                        posB += rad.getBand(GeoRasterFile.BLUE_BAND);
418
                                                
419
                        bandSetup.assignBand(posR, GeoRasterFile.RED_BAND);
420
                        bandSetup.assignBand(posG, GeoRasterFile.GREEN_BAND);
421
                        bandSetup.assignBand(posB, GeoRasterFile.BLUE_BAND);        
422
                        
423
                        //Selecci?n de 1, 2 o 3 bandas para visualizar
424
                        if(hideBands != null){
425
                                int pos = 0;
426
                                if(hideBands.length() == 1)
427
                                        pos = 2;
428
                                else if(hideBands.length() == 2)
429
                                        pos = 1;
430
                                bandSetup.getFileList().getJComboBox().setSelectedIndex(pos);
431
                                
432
                                //Reseteamos los controles de la tabla
433
                                
434
                                for(int i=0;i<bandSetup.getRGBTable().getModel().getRowCount();i++)
435
                                        for(int j=0;j<3;j++)
436
                                                bandSetup.getRGBTable().getModel().setValueAt(new Boolean(false), i, j);
437
                                
438
                                if(hideBands.equals("GB") || hideBands.equals("G") || hideBands.equals("B") )
439
                                        bandSetup.getRGBTable().getModel().setValueAt(new Boolean(true), posR, 0);
440
                                
441
                                if(hideBands.equals("RB") || hideBands.equals("R") || hideBands.equals("B") )
442
                                        bandSetup.getRGBTable().getModel().setValueAt(new Boolean(true), posG, 1);
443
                                
444
                                if(hideBands.equals("RG") || hideBands.equals("R") || hideBands.equals("G") )
445
                                        bandSetup.getRGBTable().getModel().setValueAt(new Boolean(true), posB, 2);
446
                        }
447
                        
448
                        propsDialog.readStat();
449
                        
450
                       PluginServices.getMDIManager().addView(propsDialog);
451
        }
452
        }
453
}
454

    
455
class ZoomAlTemaTocMenuEntry extends TocMenuEntry {
456
    private JMenuItem zoom;
457
        public void initialize(FPopupMenu m) {
458
                super.initialize(m);
459
                if (isTocItemBranch()) {
460
                        zoom = new JMenuItem(PluginServices.getText(this, "Zoom_al_Tema"));
461
                        getMenu().add(zoom);
462
                        zoom.setFont(FPopupMenu.theFont);
463
            
464
                zoom.addActionListener(this);
465
                }
466
        }
467
        /* (non-Javadoc)
468
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
469
         */
470
        public void actionPerformed(ActionEvent e) {
471
            /*FLayer lyr = getNodeLayer();
472
        try {
473
                getMapContext().getViewPort().setExtent(lyr.getFullExtent());
474
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
475
                        e1.printStackTrace();
476
                }*/
477
                // 050209, jmorell: Para que haga un zoom a un grupo de capas seleccionadas.
478
                FLayer[] actives = getMapContext().getLayers().getActives();
479
                if (actives.length==1) {
480
                try {
481
                        getMapContext().getViewPort().setExtent(actives[0].getFullExtent());
482
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
483
                                e1.printStackTrace();
484
                        }
485
                } else {
486
                        try {
487
                                Rectangle2D maxExtent = setMaxExtent(actives);
488
                            getMapContext().getViewPort().setExtent(maxExtent);
489
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
490
                                e1.printStackTrace();
491
                        }
492
                }
493
        }
494
        
495
        private Rectangle2D setMaxExtent(FLayer[] actives) throws DriverException {
496
                Rectangle2D extRef = actives[0].getFullExtent();
497
                double minXRef = extRef.getMinX();
498
                double maxYRef = extRef.getMaxY();
499
                double maxXRef = extRef.getMaxX();
500
                double minYRef = extRef.getMinY();
501
                for (int i=0;i<actives.length;i++) {
502
                Rectangle2D extVar = actives[i].getFullExtent();
503
                    double minXVar = extVar.getMinX();
504
                    double maxYVar = extVar.getMaxY();
505
                    double maxXVar = extVar.getMaxX();
506
                    double minYVar = extVar.getMinY();
507
                    if (minXVar<=minXRef) minXRef=minXVar;
508
                    if (maxYVar>=maxYRef) maxYRef=maxYVar;
509
                    if (maxXVar>=maxXRef) maxXRef=maxXVar;
510
                    if (minYVar<=minYRef) minYRef=minYVar;
511
                    extRef.setRect(minXRef, minYRef, maxXRef-minXRef, maxYRef-minYRef);
512
                }
513
                return extRef;
514
        }
515
}
516

    
517
class EliminarCapaTocMenuEntry extends TocMenuEntry {
518
    private JMenuItem removeLayer;
519
        public void initialize(FPopupMenu m) {
520
                super.initialize(m);
521
                if (getNodeUserObject() instanceof TocItemBranch) {
522
                        removeLayer = new JMenuItem(PluginServices.getText(this, "eliminar_capa"));
523
                        removeLayer.setFont(FPopupMenu.theFont);
524
                        removeLayer.addActionListener(this);
525
                        getMenu().addSeparator();
526
                        getMenu().add(removeLayer);
527
                        getMenu().addSeparator();
528
                }
529
        }
530
        /* (non-Javadoc)
531
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
532
         */
533
        public void actionPerformed(ActionEvent e) {
534
            // 050209, jmorell: Para poder borrar todas las capas seleccionadas desde el FPopUpMenu.
535
                //                                        Es necesario pulsar May?sculas a la vez que el bot?n derecho.
536
            FMap fMap = getMapContext();
537
            FLayer[] actives = fMap.getLayers().getActives();
538
            for (int i = actives.length-1; i>=0; i--){
539
                try {
540
                                actives[i].getParentLayer().removeLayer(actives[i]);
541
                                FLayers lyrs=getMapContext().getLayers();
542
                                lyrs.addLayer(actives[i]);
543
                                actives[i].getParentLayer().removeLayer(actives[i]);
544
                                
545
                                if (getMapContext().getLayers().getLayersCount()==0)
546
                        PluginServices.getMainFrame().enableControls();
547
                    } catch (CancelationException e1) {
548
                            e1.printStackTrace();
549
                    }
550
            }
551
                // 050209, jmorell: As? solo borra una capa (sobre la que pulsas).
552
            /*FLayer lyr = getNodeLayer();
553
        try {
554
                getMapContext().getLayers().removeLayer(lyr);
555
                if (getMapContext().getLayers().getLayersCount()==0)PluginServices.getMainFrame().enableControls();
556
                } catch (CancelationException e1) {
557
                        e1.printStackTrace();
558
                }*/
559
    }
560
}
561
/**
562
 * Realiza una agrupaci?n de capas, a partir de las capas que se encuentren activas.
563
 * 
564
 * @author Vicente Caballero Navarro
565
 */
566
class LayersGroupTocMenuEntry extends TocMenuEntry {
567
    private JMenuItem agrupar;    
568
        public void initialize(FPopupMenu m) {
569
                super.initialize(m);
570
                    agrupar = new JMenuItem(PluginServices.getText(this, "agrupar_capas"));
571
                agrupar.setFont(FPopupMenu.theFont);
572
                getMenu().add(agrupar);        
573
                getMenu().setEnabled(true);
574
                agrupar.addActionListener(this);
575
        }
576
        
577
        /* (non-Javadoc)
578
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
579
         */
580
        public void actionPerformed(ActionEvent e) {
581
                ITocItem tocItem = (ITocItem) getNodeUserObject();
582
        ChangeName changename=new ChangeName(null);
583
                PluginServices.getMDIManager().addView(changename);
584
                String nombre=changename.getName();
585
                
586
        if (nombre != null){
587
                        if (isTocItemBranch()){
588
                                FLayer[] actives = getMapContext().getLayers().getActives();
589
                                
590
                                if (actives.length>1) {
591
                                FLayers layerGroup=new FLayers(getMapContext(),getNodeLayer().getParentLayer());
592
                                        for (int i=0;i<actives.length;i++){
593
                                                actives[i].getParentLayer().removeLayer(actives[i]);
594
                                                layerGroup.addLayer(actives[i]);
595
                                        }
596
                                        layerGroup.setName(nombre);
597
                                        View theView = (View) PluginServices.getMDIManager().getActiveView();
598
                                        theView.getMapControl().getMapContext().getLayers()
599
                                           .addLayer(layerGroup);        
600
                            
601
                            }
602
                                
603
                        }
604
                        // TRUCO PARA REFRESCAR.
605
                getMapContext().invalidate();                    
606
}
607
}
608
}
609
/**
610
 * Realiza una desagrupaci?n de capas, a partir de las capas que se encuentren activas.
611
 * 
612
 * @author Vicente Caballero Navarro
613
 */
614
class LayersUngroupTocMenuEntry extends TocMenuEntry {
615
    private JMenuItem desagrupar;    
616
        public void initialize(FPopupMenu m) {
617
                super.initialize(m);
618
                boolean isFLayers=true;
619
                if (isTocItemBranch()){
620
                        FLayer lyr = getNodeLayer();
621
                        if (!(lyr instanceof FLayers)){
622
                                isFLayers=false;
623
                        }
624
                }
625
                if (isFLayers){
626
                        desagrupar = new JMenuItem(PluginServices.getText(this, "desagrupar_capas"));
627
                desagrupar.setFont(FPopupMenu.theFont);
628
                getMenu().add(desagrupar);        
629
                getMenu().setEnabled(true);
630
                desagrupar.addActionListener(this);
631
                }
632
        }
633
        
634
        /* (non-Javadoc)
635
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
636
         */
637
        public void actionPerformed(ActionEvent e) {
638
                ITocItem tocItem = (ITocItem) getNodeUserObject();
639
                        if (isTocItemBranch()){
640
                                FLayers agrupa = (FLayers)getNodeLayer();
641
                                FLayers parent=agrupa.getParentLayer();
642
                                if (parent!=null){
643
                                        for (int j=0;j<agrupa.getLayersCount();j++){
644
                                                parent.addLayer(agrupa.getLayer(j));
645
                                        }
646
                                        parent.removeLayer(agrupa);
647
                                }
648
                        }
649
                // TRUCO PARA REFRESCAR.
650
                getMapContext().invalidate();                    
651

    
652
        
653
        
654
        }
655
}
656
/**
657
 * Cambia la posici?n actual del layer a la primera posici?n.
658
 * 
659
 * @author Vicente Caballero Navarro
660
 */
661
class FirstLayerTocMenuEntry extends TocMenuEntry {
662
    private JMenuItem first;    
663
        public void initialize(FPopupMenu m) {
664
                super.initialize(m);
665
                    first = new JMenuItem(PluginServices.getText(this, "colocar_delante"));
666
                first.setFont(FPopupMenu.theFont);
667
                getMenu().add(first);        
668
                getMenu().setEnabled(true);
669
                first.addActionListener(this);
670
        }
671
        
672
        /* (non-Javadoc)
673
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
674
         */
675
        public void actionPerformed(ActionEvent e) {
676
                ITocItem tocItem = (ITocItem) getNodeUserObject();
677
      
678
              if (isTocItemBranch()){
679
                                FLayer layer=((TocItemBranch)tocItem).getLayer();
680
                                FLayers layers=layer.getParentLayer();
681
                                for (int i=0;i<layers.getLayersCount();i++){
682
                                        if(layers.getLayer(i).equals(layer)){
683
                                                layers.removeLayer(i);
684
                                                layers.addLayer(layer);
685
                                        }
686
                                }
687
                                
688
                                
689
                        }
690
                        // TRUCO PARA REFRESCAR.
691
                getMapContext().invalidate();                    
692
        
693
        }
694
}
695
/** 
696
 * @author Nacho Brodin <brodin_ign@gva.es>
697
 *
698
 * Entrada de men? para la activaci?n de la funcionalidad de zoom a un
699
 * pixel.
700
 */
701
class ZoomPixelTocMenuEntry extends TocMenuEntry {
702
        public static final int ZOOM_TO_IMAGE_CENTER = 0x1;
703
        public static final int ZOOM_TO_VIEW_CENTER = 0x2;
704
        private JMenuItem properties;
705
        FLayer lyr = null;
706
        public int zoomType = ZOOM_TO_VIEW_CENTER;
707
        
708
        public void initialize(FPopupMenu m) {
709
                super.initialize(m);
710
                
711
                if (isTocItemBranch()) {
712
                        lyr = getNodeLayer();
713
                    // Opcciones para capas raster
714
                    if ((lyr instanceof FLyrRaster)) {
715
                            properties = new JMenuItem(PluginServices.getText(this, "Zoom_pixel"));
716
                            getMenu().add( properties );
717
                            properties.setFont(FPopupMenu.theFont);
718
                            getMenu().setEnabled(true);
719
                            
720
                            properties.addActionListener(this);
721
                     }
722
                }
723
        }
724
        
725
        public void actionPerformed(ActionEvent e) {
726

    
727
                FLayer[] actives = getMapContext().getLayers().getActives();
728
                if (actives.length==1) {
729
                try {
730
                        Rectangle2D r = actives[0].getFullExtent();
731
              
732
                        ArrayList attr = ((FLyrRaster)actives[0]).getSource().getAttributes();
733
                        int width = 0, height = 0;
734
                               for (int i=0; i<attr.size(); i++) {
735
                                    Object[] a = (Object []) attr.get(i);
736
                                    if (a[0].toString().equals("Width"))
737
                                            width = ((Integer)a[1]).intValue();
738
                                    if (a[0].toString().equals("Height"))
739
                                            height = ((Integer)a[1]).intValue();                                                                
740
                            }
741
                        ViewPort v = getMapContext().getViewPort();
742
                        
743
                        if (zoomType == ZOOM_TO_IMAGE_CENTER) {
744
                                //A centro de imagen
745
                                /*double centroX = width/2;
746
                                        double centroY = height/2;
747
                                        int minX = (int)Math.round(centroX - (v.getImageWidth()/2));
748
                                        int minY = (int)Math.round(centroY - (v.getImageHeight()/2));
749
                                        int maxX = (int)Math.round(minX + v.getImageWidth());
750
                                        int maxY = (int)Math.round(minY + v.getImageHeight());
751
                                        double wcOriginWidth = r.getMaxX() - r.getMinX();
752
                                        double wcOriginHeight = r.getMaxY() - r.getMinY();
753
                                        
754
                                        double wcDstMinX = r.getMinX()+((minX*wcOriginWidth)/width);
755
                                        double wcDstMinY = r.getMinY()+((minY*wcOriginHeight)/height);
756
                                        double wcDstMaxX = r.getMinX()+((maxX*wcOriginWidth)/width);
757
                                        double wcDstMaxY = r.getMinY()+((maxY*wcOriginHeight)/height);
758
                                        
759
                                        double wcDstWidth = wcDstMaxX - wcDstMinX;
760
                                        double wcDstHeight = wcDstMaxY - wcDstMinY;*/
761
                        } else if (zoomType == ZOOM_TO_VIEW_CENTER) {
762
                                //A centro de vista
763
                                double wcOriginCenterX = v.getExtent().getMinX()+((v.getExtent().getMaxX()-v.getExtent().getMinX())/2);
764
                                double wcOriginCenterY = v.getExtent().getMinY()+((v.getExtent().getMaxY()-v.getExtent().getMinY())/2);
765
                                                                
766
                                //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
767
                                        double relacionPixelWcWidth =  (r.getMaxX() - r.getMinX())/width;
768
                                        double relacionPixelWcHeight = (r.getMaxY() - r.getMinY())/height;
769
                                        double desplazamientoX = r.getMinX(); 
770
                                        double desplazamientoY = r.getMinY();
771
                                        
772
                                        double wcOriginX = wcOriginCenterX - ((v.getImageWidth()*relacionPixelWcWidth)/2);
773
                                        double wcOriginY = wcOriginCenterY - ((v.getImageHeight()*relacionPixelWcHeight)/2);
774
                                        
775
                                        double wcDstMinX = wcOriginX; 
776
                                        double wcDstMinY = wcOriginY; 
777
                                        double wcDstMaxX = wcDstMinX + (v.getImageWidth()*relacionPixelWcWidth);
778
                                        double wcDstMaxY = wcDstMinY + (v.getImageHeight()*relacionPixelWcHeight);
779
                                        
780
                                        double wcDstWidth = wcDstMaxX - wcDstMinX;
781
                                        double wcDstHeight = wcDstMaxY - wcDstMinY;
782
                                                                        
783
                                r = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
784
                                getMapContext().getViewPort().setExtent(r);
785
                        }
786
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
787
                                e1.printStackTrace();
788
                        }
789
                } 
790
                //View vista = (View) PluginServices.getMDIManager().getActiveView();
791
                //MapControl mapCtrl = vista.getMapControl();        
792
        }
793
}
794

    
795
/** 
796
 * @author Nacho Brodin <brodin_ign@gva.es>
797
 *
798
 * Entrada de men? para la activaci?n de la funcionalidad de zoom a un
799
 * pixel centrado en el cursor.
800
 */
801
class ZoomPixelCursorTocMenuEntry extends TocMenuEntry {
802
        public static final int ZOOM_TO_IMAGE_CENTER = 0x1;
803
        public static final int ZOOM_TO_VIEW_CENTER = 0x2;
804
        private JMenuItem properties;
805
        FLayer lyr = null;
806
        public int zoomType = ZOOM_TO_VIEW_CENTER;
807
        
808
        public void initialize(FPopupMenu m) {
809
                super.initialize(m);
810
                
811
                if (isTocItemBranch()) {
812
                        lyr = getNodeLayer();
813
                    // Opcciones para capas raster
814
                    if ((lyr instanceof FLyrRaster)) {
815
                            properties = new JMenuItem(PluginServices.getText(this, "Zoom_pixel"));
816
                            getMenu().add( properties );
817
                            properties.setFont(FPopupMenu.theFont);
818
                            getMenu().setEnabled(true);
819
                            properties.addActionListener(this);
820
                     }
821
                }
822
        }
823
        
824
        public void actionPerformed(ActionEvent e) {
825

    
826
                FLayer[] actives = getMapContext().getLayers().getActives();
827
                if (actives.length==1) {
828
             
829
                lyr = getNodeLayer();
830
                    View vista = (View) PluginServices.getMDIManager().getActiveView();
831
                    MapControl mapCtrl = vista.getMapControl();
832
                    mapCtrl.setTool("zoom_pixel_cursor");
833
                             
834
                } 
835
                //View vista = (View) PluginServices.getMDIManager().getActiveView();
836
                //MapControl mapCtrl = vista.getMapControl();        
837
        }
838
}
839

    
840
/**
841
 * 
842
 * @author Nacho Brodin <brodin_ign@gva.es>
843
 *
844
 * Entrada de men? para la activaci?n de la funcionalidad de salvar
845
 * a raster una parte de la vista.
846
 */
847
class SaveRasterTocMenuEntry extends TocMenuEntry{
848
        private JMenuItem properties;
849
        FLayer lyr = null;
850
        
851
        public void initialize(FPopupMenu m) {
852
                super.initialize(m);
853
                
854
                if (isTocItemBranch()) {
855
                        lyr = getNodeLayer();
856
                    // Opcciones para capas raster
857
                    if ((lyr instanceof FLyrRaster)) {
858
                            properties = new JMenuItem(PluginServices.getText(this, "salvar_raster"));
859
                            getMenu().add( properties );
860
                            properties.setFont(FPopupMenu.theFont);
861
                            getMenu().setEnabled(true);
862
                            //getMenu().addSeparator();
863
                    //Cambio color
864
                            properties.addActionListener(this);
865
                     }
866
                }
867
        }
868
        
869
        public void actionPerformed(ActionEvent e) {
870
                lyr = getNodeLayer();
871
                View vista = (View) PluginServices.getMDIManager().getActiveView();
872
                MapControl mapCtrl = vista.getMapControl();
873
                mapCtrl.setTool("saveRaster");
874
        }
875
}
876

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

    
889
public class FPopupMenu extends JPopupMenu {
890
        private static ArrayList menuEntrys = new ArrayList();
891
    public DefaultMutableTreeNode nodo;
892
    protected FMap mapContext;
893
    //private JMenuItem capa;
894
    // Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
895
    // TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
896
    public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10);
897
    
898
    static {
899
            FPopupMenu.addEntry(new FSymbolChangeColorTocMenuEntry());
900
            FPopupMenu.addEntry(new ChangeNameTocMenuEntry());
901
            menuEntrys.add(new FLyrVectEditPropertiesTocMenuEntry());
902
            //FPopupMenu.addEntry(new FLyrRasterAdjustTransparencyTocMenuEntry());
903
            FPopupMenu.addEntry(new ZoomAlTemaTocMenuEntry());
904
            //FPopupMenu.addEntry(new ZoomPixelTocMenuEntry());
905
            FPopupMenu.addEntry(new ZoomPixelCursorTocMenuEntry());
906
            FPopupMenu.addEntry(new EliminarCapaTocMenuEntry());
907
            menuEntrys.add(new LayersGroupTocMenuEntry());
908
                menuEntrys.add(new LayersUngroupTocMenuEntry());
909
                menuEntrys.add(new FirstLayerTocMenuEntry());
910
            FPopupMenu.addEntry(new FLyrRasterAdjustPropertiesTocMenuEntry());
911
    }
912

    
913
    public static void addEntry(TocMenuEntry entry) {
914
            menuEntrys.add(entry);
915
    }
916
    
917
    /**
918
     * Creates a new FPopupMenu object.
919
     *
920
     * @param nodo DOCUMENT ME!
921
     * @param vista DOCUMENT ME!
922
     */
923
    public FPopupMenu(FMap mc, DefaultMutableTreeNode node) {
924
        //super();
925
        this.mapContext = mc;
926
        this.nodo = node;
927
 
928
        //salir = new MenuItem("Salir");
929

    
930
        for (int i=0; i<menuEntrys.size(); i++) {
931
                ((TocMenuEntry) menuEntrys.get(i)).initialize(this);
932
        }
933
    }
934
    
935
    public FMap getMapContext() { return mapContext; }
936
        
937
}