Statistics
| Revision:

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

History | View | Annotate | Download (35.5 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.tree.DefaultMutableTreeNode;
57

    
58
import org.cresques.io.GeoRasterFile;
59
import org.cresques.io.raster.RasterFilter;
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
import org.cresques.ui.raster.SharpeningPanel;
66

    
67
import com.iver.andami.PluginServices;
68
import com.iver.cit.gvsig.ProjectExtension;
69
import com.iver.cit.gvsig.fmap.DriverException;
70
import com.iver.cit.gvsig.fmap.FMap;
71
import com.iver.cit.gvsig.fmap.MapControl;
72
import com.iver.cit.gvsig.fmap.ViewPort;
73
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
74
import com.iver.cit.gvsig.fmap.layers.CancelationException;
75
import com.iver.cit.gvsig.fmap.layers.FLayer;
76
import com.iver.cit.gvsig.fmap.layers.FLayers;
77
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
78
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
79
import com.iver.cit.gvsig.fmap.layers.RasterAdapter;
80
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
81
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
82
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
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.project.Project;
88
import com.iver.cit.gvsig.project.ProjectTable;
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
                FLayer[] actives = getMapContext().getLayers().getActives();
137
                for (int i=0;i<actives.length;i++){
138
                    if ((actives[i] instanceof ClassifiableVectorial))
139
                    {
140
                        ClassifiableVectorial lyrVect = (ClassifiableVectorial) actives[i];
141
                        if (lyrVect.getLegend() instanceof SingleSymbolLegend)
142
                        {
143
                            SingleSymbolLegend leg = (SingleSymbolLegend) lyrVect.getLegend();
144
                            leg.getDefaultSymbol().setColor(newColor);
145
                        } 
146
                    }
147
                }
148
                        }
149
                        else
150
                        {
151
                    TocItemLeaf leaf = (TocItemLeaf) tocItem;
152
                    FSymbol sym = leaf.getSymbol();
153
                    sym.setColor(newColor);
154
                                                
155
                        }
156
                // TRUCO PARA REFRESCAR.
157
                getMapContext().invalidate();                    
158

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

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

    
299
class FLyrRasterAdjustTransparencyTocMenuEntry extends TocMenuEntry {
300
        private JMenuItem transparency;
301
        FLayer lyr = null;
302
        public void initialize(FPopupMenu m) {
303
                super.initialize(m);
304
                
305
                if (isTocItemBranch()) {
306
                        lyr = getNodeLayer();
307
                    // Opcciones para capas raster
308
                    if ((lyr instanceof FLyrRaster)) {
309
                        transparency = new JMenuItem(PluginServices.getText(this, "adjust_transparency"));
310
                            getMenu().add( transparency );
311
                            transparency.setFont(FPopupMenu.theFont);
312
                            getMenu().setEnabled(true);
313
                            //getMenu().addSeparator();
314
                    //Cambio color
315
                    transparency.addActionListener(this);
316
                     }
317
                }
318
        }
319
        /* (non-Javadoc)
320
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
321
         */
322
        public void actionPerformed(ActionEvent e) {
323
               lyr = getNodeLayer();
324
        AdjustTransparencyPanel transPanel = new AdjustTransparencyPanel((FLyrDefault) lyr);
325
                transPanel.openJDialog();
326
        //getMapContext().invalidate();                    
327
        }
328
}
329

    
330
class FLyrRasterAdjustPropertiesTocMenuEntry extends TocMenuEntry {
331
        private JMenuItem                                 properties;
332
        FLayer                                                         lyr = null;
333
        private PropertiesRasterDialog        propsDialog = null;
334
        private BandSetupPanel                         bandSetup = null;
335
        
336
        
337
        public void initialize(FPopupMenu m) {
338
                super.initialize(m);
339
                
340
                if (isTocItemBranch()) {
341
                        lyr = getNodeLayer();
342
                    // Opcciones para capas raster
343
                    if ((lyr instanceof FLyrRaster)) {
344
                            properties = new JMenuItem(PluginServices.getText(this, "propiedades_raster"));
345
                            getMenu().add( properties );
346
                            properties.setFont(FPopupMenu.theFont);
347
                            getMenu().setEnabled(true);
348
                            //getMenu().addSeparator();
349
                    //Cambio color
350
                            properties.addActionListener(this);
351
                     }
352
                }
353
        }
354
        
355
        /**
356
         * Gestiona la apertura del dialogo de propiedades de raster cuando se pulsa
357
         * la opci?n asignando a este las propiedades iniciales. 
358
         */
359
        public void actionPerformed(ActionEvent e) {
360
               lyr = getNodeLayer();
361
                   
362
               if(lyr instanceof FLyrRaster){
363
                       RasterFilterStackManager         stackManager = null;
364
       
365
                       stackManager = new RasterFilterStackManager(((FLyrRaster)lyr).getSource().getFilterStack());
366
                                                                     
367
                       propsDialog = new PropertiesRasterDialog(((FLyrRaster)lyr), stackManager.getTransparencyRGB());
368
                                              
369
                        int alpha = 255-((FLyrRaster)lyr).getSource().getTransparency();
370
                        
371
                        bandSetup = ((FilterRasterDialogPanel)propsDialog.getContentPane()).getBandSetup();
372
                        
373
                        //Asignaci?n del alpha actual de la imagen al dialogo
374
                        RasterTransparencyPanel rasterTrans = ((FilterRasterDialogPanel)propsDialog.getContentPane()).getTransparencyPanel();
375
                        rasterTrans.setOpacity(alpha);
376
                                
377
                        //Asignaci?n del porcentaje de recorte actual de la imagen al dialogo
378
                        EnhancedPanel pEnhan =  ((FilterRasterDialogPanel)propsDialog.getContentPane()).getEnhancedPanel();
379
                        
380
                        propsDialog.setRasterFilterStackManager(stackManager);
381
                        
382
                        ArrayList stackList = stackManager.getStringsFromStack();
383
                        boolean remove = false;
384
                        String hideBands = null;
385
                        for(int i=0;i<stackList.size();i++){
386
                                //System.out.println("*=>"+((String)stackList.get(i)));
387
                                if(((String)stackList.get(i)).startsWith("filter.tail.value")){
388
                                        int tail = (int)(Double.parseDouble(stackManager.getValue((String)stackList.get(i)))*200);
389
                                        pEnhan.setPercentTail(tail);
390
                                }
391
                                if(((String)stackList.get(i)).startsWith("filter.tail.remove"))
392
                                        remove = Boolean.valueOf(stackManager.getValue((String)stackList.get(i))).booleanValue();
393
                                
394
                                if(((String)stackList.get(i)).startsWith("filter.enhanced.remove"))
395
                                        remove = Boolean.valueOf(stackManager.getValue((String)stackList.get(i))).booleanValue();
396
                                
397
                                if(((String)stackList.get(i)).startsWith("filter.removebands.bands"))
398
                                        hideBands = stackManager.getValue((String)stackList.get(i));
399
                        }
400
                        
401
                        if(stackManager.isActive(stackManager.getTypeFilter("enhanced"))){
402
                                if(remove)
403
                                        pEnhan.setSelectedFilter(1);
404
                                else
405
                                        pEnhan.setSelectedFilter(2);
406
                        }else
407
                                pEnhan.setSelectedFilter(0);
408
                                                
409
                        GeoRasterFile[] files = ((FLyrRaster)lyr).getSource().getFiles();
410
                        propsDialog.addFiles(files);
411
                        for(int i=0; i< files.length;i++)
412
                                propsDialog.addNumBands(files[i].getBandCount());
413
                        
414
                        RasterAdapter rad = ((FLyrRaster)lyr).getSource();
415
                        int posR = 0, posG = 0, posB = 0;
416
                        
417
                        for(int i=0;i<rad.getPosFile(GeoRasterFile.RED_BAND);i++)
418
                                posR += files[i].getBandCount();
419
                        posR += rad.getBand(GeoRasterFile.RED_BAND);
420
                        
421
                        for(int i=0;i<rad.getPosFile(GeoRasterFile.GREEN_BAND);i++)
422
                                posG += files[i].getBandCount();
423
                        posG += rad.getBand(GeoRasterFile.GREEN_BAND);
424
                        
425
                        for(int i=0;i<rad.getPosFile(GeoRasterFile.BLUE_BAND);i++)
426
                                posB += files[i].getBandCount();
427
                        posB += rad.getBand(GeoRasterFile.BLUE_BAND);
428
                                                
429
                        bandSetup.assignBand(posR, GeoRasterFile.RED_BAND);
430
                        bandSetup.assignBand(posG, GeoRasterFile.GREEN_BAND);
431
                        bandSetup.assignBand(posB, GeoRasterFile.BLUE_BAND);        
432
                        
433
                        //Selecci?n de 1, 2 o 3 bandas para visualizar
434
                        if(hideBands != null){
435
                                int pos = 2;
436
                                if(hideBands.length() == 1)
437
                                        pos = 1;
438
                                else if(hideBands.length() == 2)
439
                                        pos = 0;
440
                                bandSetup.getFileList().getJComboBox().setSelectedIndex(pos);
441
                                
442
                                //Reseteamos los controles de la tabla
443
                                
444
                                for(int i=0;i<bandSetup.getRGBTable().getModel().getRowCount();i++)
445
                                        for(int j=0;j<3;j++)
446
                                                bandSetup.getRGBTable().getModel().setValueAt(new Boolean(false), i, j);
447
                                
448
                                if(hideBands.equals("GB") || hideBands.equals("G") || hideBands.equals("B") )
449
                                        bandSetup.getRGBTable().getModel().setValueAt(new Boolean(true), posR, 0);
450
                                
451
                                if(hideBands.equals("RB") || hideBands.equals("R") || hideBands.equals("B") )
452
                                        bandSetup.getRGBTable().getModel().setValueAt(new Boolean(true), posG, 1);
453
                                
454
                                if(hideBands.equals("RG") || hideBands.equals("R") || hideBands.equals("G") )
455
                                        bandSetup.getRGBTable().getModel().setValueAt(new Boolean(true), posB, 2);
456
                        }
457
                        
458
                        propsDialog.readStat();
459
                        
460
                        //Modificaci?n del estado de los controles del pansharpening
461
                        if(stackManager.isActive(stackManager.getTypeFilter("sharpening"))){
462
                                SharpeningPanel pSharp =  ((FilterRasterDialogPanel)propsDialog.getContentPane()).getSharpeningPanel();
463
                                pSharp.getCbActiveSharpening().setSelected(true);
464
                                pSharp.setTableEnabled(true);
465
                                
466
                                RasterFilter sharp = stackManager.getFilter("sharpening");
467
                                String pancrName = (String)sharp.getParam("pancrName");
468
                                for(int i=0;i<pSharp.getNBands();i++){
469
                                        if(pSharp.getBandName(i).endsWith(pancrName))
470
                                                pSharp.selectRow(i);
471
                                }
472
                                if(((String)sharp.getParam("method")).equals("hsl")){
473
                                        pSharp.getRbHSL().setSelected(true);
474
                                        Double coef = ((Double)sharp.getParam("coef"));
475
                                        pSharp.getJTextField().setText(coef.toString());
476
                                        pSharp.getJSlider().setValue((int)((coef.doubleValue() * 100)/0.5));
477
                                }
478
                                
479
                                if(((String)sharp.getParam("method")).equals("brovey")){
480
                                        pSharp.getRbBrovey().setSelected(true);
481
                                        Integer coefBrovey = ((Integer)sharp.getParam("coefBrovey"));
482
                                        pSharp.getJTextField1().setText(coefBrovey.toString());
483
                                        pSharp.getJSlider1().setValue((int)((coefBrovey.intValue() * 100)/50));
484
                                }
485
                        }
486
                        
487
                       PluginServices.getMDIManager().addView(propsDialog);
488
        }
489
        }
490
}
491

    
492
class ZoomAlTemaTocMenuEntry extends TocMenuEntry {
493
    private JMenuItem zoom;
494
        public void initialize(FPopupMenu m) {
495
                super.initialize(m);
496
                if (isTocItemBranch()) {
497
                        zoom = new JMenuItem(PluginServices.getText(this, "Zoom_al_Tema"));
498
                        getMenu().add(zoom);
499
                        zoom.setFont(FPopupMenu.theFont);
500
            
501
                zoom.addActionListener(this);
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
            /*FLayer lyr = getNodeLayer();
509
        try {
510
                getMapContext().getViewPort().setExtent(lyr.getFullExtent());
511
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
512
                        e1.printStackTrace();
513
                }*/
514
                // 050209, jmorell: Para que haga un zoom a un grupo de capas seleccionadas.
515
                FLayer[] actives = getMapContext().getLayers().getActives();
516
                if (actives.length==1) {
517
                try {
518
                        getMapContext().getViewPort().setExtent(actives[0].getFullExtent());
519
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
520
                                e1.printStackTrace();
521
                        }
522
                } else {
523
                        try {
524
                                Rectangle2D maxExtent = setMaxExtent(actives);
525
                            getMapContext().getViewPort().setExtent(maxExtent);
526
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
527
                                e1.printStackTrace();
528
                        }
529
                }
530
        }
531
        
532
        private Rectangle2D setMaxExtent(FLayer[] actives) throws DriverException {
533
                Rectangle2D extRef = actives[0].getFullExtent();
534
                double minXRef = extRef.getMinX();
535
                double maxYRef = extRef.getMaxY();
536
                double maxXRef = extRef.getMaxX();
537
                double minYRef = extRef.getMinY();
538
                for (int i=0;i<actives.length;i++) {
539
                Rectangle2D extVar = actives[i].getFullExtent();
540
                    double minXVar = extVar.getMinX();
541
                    double maxYVar = extVar.getMaxY();
542
                    double maxXVar = extVar.getMaxX();
543
                    double minYVar = extVar.getMinY();
544
                    if (minXVar<=minXRef) minXRef=minXVar;
545
                    if (maxYVar>=maxYRef) maxYRef=maxYVar;
546
                    if (maxXVar>=maxXRef) maxXRef=maxXVar;
547
                    if (minYVar<=minYRef) minYRef=minYVar;
548
                    extRef.setRect(minXRef, minYRef, maxXRef-minXRef, maxYRef-minYRef);
549
                }
550
                return extRef;
551
        }
552
}
553

    
554
class EliminarCapaTocMenuEntry extends TocMenuEntry {
555
    private JMenuItem removeLayer;
556
        public void initialize(FPopupMenu m) {
557
                super.initialize(m);
558
                if (getNodeUserObject() instanceof TocItemBranch) {
559
                        removeLayer = new JMenuItem(PluginServices.getText(this, "eliminar_capa"));
560
                        removeLayer.setFont(FPopupMenu.theFont);
561
                        removeLayer.addActionListener(this);
562
                        getMenu().addSeparator();
563
                        getMenu().add(removeLayer);
564
                        getMenu().addSeparator();
565
                }
566
        }
567
        /* (non-Javadoc)
568
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
569
         */
570
        public void actionPerformed(ActionEvent e) {
571
            // 050209, jmorell: Para poder borrar todas las capas seleccionadas desde el FPopUpMenu.
572
                //                                        Es necesario pulsar May?sculas a la vez que el bot?n derecho.
573
            FMap fMap = getMapContext();
574
            FLayer[] actives = fMap.getLayers().getActives();
575
            for (int i = actives.length-1; i>=0; i--){
576
                try {
577
                                actives[i].getParentLayer().removeLayer(actives[i]);
578
                                FLayers lyrs=getMapContext().getLayers();
579
                                lyrs.addLayer(actives[i]);
580
                                actives[i].getParentLayer().removeLayer(actives[i]);
581
                                
582
                if (actives[i] instanceof AlphanumericData){
583
                    Project project = ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject();
584
                    ProjectTable pt = project.getTable((AlphanumericData) actives[i]);
585
                    
586
                    ArrayList tables = project.getTables();
587
                    for (int j = 0; j < tables.size(); j++) {
588
                        if (tables.get(j) == pt){
589
                            project.delTable(j);
590
                            break;
591
                        }
592
                    }
593
                    
594
                    PluginServices.getMDIManager().closeSingletonView(pt);
595
                }
596
                
597
                                if (getMapContext().getLayers().getLayersCount()==0)
598
                        PluginServices.getMainFrame().enableControls();
599
                    } catch (CancelationException e1) {
600
                            e1.printStackTrace();
601
                    }
602
            }
603
                // 050209, jmorell: As? solo borra una capa (sobre la que pulsas).
604
            /*FLayer lyr = getNodeLayer();
605
        try {
606
                getMapContext().getLayers().removeLayer(lyr);
607
                if (getMapContext().getLayers().getLayersCount()==0)PluginServices.getMainFrame().enableControls();
608
                } catch (CancelationException e1) {
609
                        e1.printStackTrace();
610
                }*/
611
    }
612
}
613
/**
614
 * Realiza una agrupaci?n de capas, a partir de las capas que se encuentren activas.
615
 * 
616
 * @author Vicente Caballero Navarro
617
 */
618
class LayersGroupTocMenuEntry extends TocMenuEntry {
619
    private JMenuItem agrupar;    
620
        public void initialize(FPopupMenu m) {
621
                super.initialize(m);
622
        if (isTocItemBranch()) {
623
            agrupar = new JMenuItem(PluginServices.getText(this, "agrupar_capas"));
624
            agrupar.setFont(FPopupMenu.theFont);
625
            getMenu().add(agrupar);        
626
            getMenu().setEnabled(true);
627
            if (getMapContext().getLayers().getActives().length > 1) {
628
                agrupar.setEnabled(true);
629
            } else {
630
                agrupar.setEnabled(false);
631
            }
632
            agrupar.addActionListener(this);
633
        }
634
        }
635
        
636
        /* (non-Javadoc)
637
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
638
         */
639
        public void actionPerformed(ActionEvent e) {
640
                //ITocItem tocItem = (ITocItem) getNodeUserObject();
641
        ChangeName changename=new ChangeName(null);
642
                PluginServices.getMDIManager().addView(changename);
643
                String nombre=changename.getName();
644
                
645
        if (nombre != null){
646
                        if (isTocItemBranch()){
647
                                FLayer[] actives = getMapContext().getLayers().getActives();
648
                                
649
                                if (actives.length>1) {
650
                                                                        
651
                                        FLayers layerGroup=new FLayers(getMapContext(),getNodeLayer().getParentLayer());
652
                                        View theView = (View) PluginServices.getMDIManager().getActiveView();
653
                                        theView.getMapControl().getMapContext().getLayers()
654
                                           .addLayer(layerGroup);   
655
                                        for (int i=0;i<actives.length;i++){
656
                                                
657
                                                if(actives[i] instanceof FLyrRaster)
658
                                                        ((FLyrRaster)actives[i]).setRemoveRasterFlag(false);
659
                                                
660
                                                actives[i].getParentLayer().removeLayer(actives[i]);
661
                                                layerGroup.addLayer(actives[i]);
662
                                        }
663
                                        layerGroup.setName(nombre);
664
                                             
665
                            
666
                            }
667
                                
668
                        }
669
                        // TRUCO PARA REFRESCAR.
670
                getMapContext().invalidate();                    
671
}
672
}
673
}
674
/**
675
 * Realiza una desagrupaci?n de capas, a partir de las capas que se encuentren activas.
676
 * 
677
 * @author Vicente Caballero Navarro
678
 */
679
class LayersUngroupTocMenuEntry extends TocMenuEntry {
680
    private JMenuItem desagrupar;    
681
        public void initialize(FPopupMenu m) {
682
                super.initialize(m);
683
        if (isTocItemBranch()) {
684
                    boolean isFLayers=true;
685
                    if (isTocItemBranch()){
686
                            FLayer lyr = getNodeLayer();
687
                            if (!(lyr instanceof FLayers)){
688
                                    isFLayers=false;
689
                            }
690
                    }
691
                    if (isFLayers){
692
                            desagrupar = new JMenuItem(PluginServices.getText(this, "desagrupar_capas"));
693
                    desagrupar.setFont(FPopupMenu.theFont);
694
                    getMenu().add(desagrupar);        
695
                    getMenu().setEnabled(true);
696
                    desagrupar.addActionListener(this);
697
                    }
698
        }
699
        }
700
        
701
        /* (non-Javadoc)
702
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
703
         */
704
        public void actionPerformed(ActionEvent e) {
705
                //ITocItem tocItem = (ITocItem) getNodeUserObject();
706
                        if (isTocItemBranch()){
707
                                FLayers agrupa = (FLayers)getNodeLayer();
708
                                FLayers parent=agrupa.getParentLayer();
709
                                if (parent!=null){
710
                                        for (int j=0;j<agrupa.getLayersCount();j++){
711
                                                parent.addLayer(agrupa.getLayer(j));
712
                                        }
713
                                        parent.removeLayer(agrupa);
714
                                }
715
                        }
716
                // TRUCO PARA REFRESCAR.
717
                getMapContext().invalidate();                    
718

    
719
        
720
        
721
        }
722
}
723
/**
724
 * Cambia la posici?n actual del layer a la primera posici?n.
725
 * 
726
 * @author Vicente Caballero Navarro
727
 */
728
class FirstLayerTocMenuEntry extends TocMenuEntry {
729
    private JMenuItem first;    
730
        public void initialize(FPopupMenu m) {
731
                super.initialize(m);
732
        if (isTocItemBranch()) {
733
                    first = new JMenuItem(PluginServices.getText(this, "colocar_delante"));
734
                first.setFont(FPopupMenu.theFont);
735
                getMenu().add(first);        
736
                getMenu().setEnabled(true);
737
                first.addActionListener(this);
738
        }
739
        }
740
        
741
        /* (non-Javadoc)
742
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
743
         */
744
        public void actionPerformed(ActionEvent e) {
745
                ITocItem tocItem = (ITocItem) getNodeUserObject();
746
      
747
              if (isTocItemBranch()){
748
            if (getMapContext().getLayers().getActives().length == 1) {
749
                FLayer layer=((TocItemBranch)tocItem).getLayer();
750
                FLayers layers=layer.getParentLayer();
751
                for (int i=0;i<layers.getLayersCount();i++){
752
                    if(layers.getLayer(i).equals(layer)){
753
                        layers.removeLayer(i);
754
                        layers.addLayer(layer);
755
                    }
756
                }
757
            } else if (getMapContext().getLayers().getActives().length > 1) {
758
                FLayer[] actives = getMapContext().getLayers().getActives();
759
                FLayers layers=actives[0].getParentLayer();
760
                //FLayers layerGroup=new FLayers(getMapContext(),getNodeLayer().getParentLayer());
761
                for (int i=0;i<actives.length;i++){
762
                    for (int j=0;j<layers.getLayersCount();j++){
763
                        if(layers.getLayer(j).equals(actives[i])){
764
                            layers.removeLayer(j);
765
                            layers.addLayer(actives[i]);
766
                        }
767
                    }
768
                }
769
            }
770
                }
771
                // TRUCO PARA REFRESCAR.
772
        getMapContext().invalidate();                    
773
        }
774
}
775
/** 
776
 * @author Nacho Brodin <brodin_ign@gva.es>
777
 *
778
 * Entrada de men? para la activaci?n de la funcionalidad de zoom a un
779
 * pixel.
780
 */
781
class ZoomPixelTocMenuEntry extends TocMenuEntry {
782
        public static final int ZOOM_TO_IMAGE_CENTER = 0x1;
783
        public static final int ZOOM_TO_VIEW_CENTER = 0x2;
784
        private JMenuItem properties;
785
        FLayer lyr = null;
786
        public int zoomType = ZOOM_TO_VIEW_CENTER;
787
        
788
        public void initialize(FPopupMenu m) {
789
                super.initialize(m);
790
                
791
                if (isTocItemBranch()) {
792
                        lyr = getNodeLayer();
793
                    // Opcciones para capas raster
794
                    if ((lyr instanceof FLyrRaster)) {
795
                            properties = new JMenuItem(PluginServices.getText(this, "Zoom_pixel"));
796
                            getMenu().add( properties );
797
                            properties.setFont(FPopupMenu.theFont);
798
                            getMenu().setEnabled(true);
799
                            
800
                            properties.addActionListener(this);
801
                     }
802
                }
803
        }
804
        
805
        public void actionPerformed(ActionEvent e) {
806

    
807
                FLayer[] actives = getMapContext().getLayers().getActives();
808
                if (actives.length==1) {
809
                try {
810
                        Rectangle2D r = actives[0].getFullExtent();
811
              
812
                        ArrayList attr = ((FLyrRaster)actives[0]).getSource().getAttributes();
813
                        int width = 0, height = 0;
814
                               for (int i=0; i<attr.size(); i++) {
815
                                    Object[] a = (Object []) attr.get(i);
816
                                    if (a[0].toString().equals("Width"))
817
                                            width = ((Integer)a[1]).intValue();
818
                                    if (a[0].toString().equals("Height"))
819
                                            height = ((Integer)a[1]).intValue();                                                                
820
                            }
821
                        ViewPort v = getMapContext().getViewPort();
822
                        
823
                        if (zoomType == ZOOM_TO_IMAGE_CENTER) {
824
                                //A centro de imagen
825
                                /*double centroX = width/2;
826
                                        double centroY = height/2;
827
                                        int minX = (int)Math.round(centroX - (v.getImageWidth()/2));
828
                                        int minY = (int)Math.round(centroY - (v.getImageHeight()/2));
829
                                        int maxX = (int)Math.round(minX + v.getImageWidth());
830
                                        int maxY = (int)Math.round(minY + v.getImageHeight());
831
                                        double wcOriginWidth = r.getMaxX() - r.getMinX();
832
                                        double wcOriginHeight = r.getMaxY() - r.getMinY();
833
                                        
834
                                        double wcDstMinX = r.getMinX()+((minX*wcOriginWidth)/width);
835
                                        double wcDstMinY = r.getMinY()+((minY*wcOriginHeight)/height);
836
                                        double wcDstMaxX = r.getMinX()+((maxX*wcOriginWidth)/width);
837
                                        double wcDstMaxY = r.getMinY()+((maxY*wcOriginHeight)/height);
838
                                        
839
                                        double wcDstWidth = wcDstMaxX - wcDstMinX;
840
                                        double wcDstHeight = wcDstMaxY - wcDstMinY;*/
841
                        } else if (zoomType == ZOOM_TO_VIEW_CENTER) {
842
                                //A centro de vista
843
                                double wcOriginCenterX = v.getExtent().getMinX()+((v.getExtent().getMaxX()-v.getExtent().getMinX())/2);
844
                                double wcOriginCenterY = v.getExtent().getMinY()+((v.getExtent().getMaxY()-v.getExtent().getMinY())/2);
845
                                                                
846
                                //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
847
                                        double relacionPixelWcWidth =  (r.getMaxX() - r.getMinX())/width;
848
                                        double relacionPixelWcHeight = (r.getMaxY() - r.getMinY())/height;
849
                                        //double desplazamientoX = r.getMinX(); 
850
                                        //double desplazamientoY = r.getMinY();
851
                                        
852
                                        double wcOriginX = wcOriginCenterX - ((v.getImageWidth()*relacionPixelWcWidth)/2);
853
                                        double wcOriginY = wcOriginCenterY - ((v.getImageHeight()*relacionPixelWcHeight)/2);
854
                                        
855
                                        double wcDstMinX = wcOriginX; 
856
                                        double wcDstMinY = wcOriginY; 
857
                                        double wcDstMaxX = wcDstMinX + (v.getImageWidth()*relacionPixelWcWidth);
858
                                        double wcDstMaxY = wcDstMinY + (v.getImageHeight()*relacionPixelWcHeight);
859
                                        
860
                                        double wcDstWidth = wcDstMaxX - wcDstMinX;
861
                                        double wcDstHeight = wcDstMaxY - wcDstMinY;
862
                                                                        
863
                                r = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
864
                                getMapContext().getViewPort().setExtent(r);
865
                        }
866
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
867
                                e1.printStackTrace();
868
                        }
869
                } 
870
                //View vista = (View) PluginServices.getMDIManager().getActiveView();
871
                //MapControl mapCtrl = vista.getMapControl();        
872
        }
873
}
874

    
875
/** 
876
 * @author Nacho Brodin <brodin_ign@gva.es>
877
 *
878
 * Entrada de men? para la activaci?n de la funcionalidad de zoom a un
879
 * pixel centrado en el cursor.
880
 */
881
class ZoomPixelCursorTocMenuEntry extends TocMenuEntry {
882
        public static final int ZOOM_TO_IMAGE_CENTER = 0x1;
883
        public static final int ZOOM_TO_VIEW_CENTER = 0x2;
884
        private JMenuItem properties;
885
        FLayer lyr = null;
886
        public int zoomType = ZOOM_TO_VIEW_CENTER;
887
        
888
        public void initialize(FPopupMenu m) {
889
                super.initialize(m);
890
                
891
                if (isTocItemBranch()) {
892
                        lyr = getNodeLayer();
893
                    // Opcciones para capas raster
894
                    if ((lyr instanceof FLyrRaster)) {
895
                            properties = new JMenuItem(PluginServices.getText(this, "Zoom_pixel"));
896
                            getMenu().add( properties );
897
                            properties.setFont(FPopupMenu.theFont);
898
                            getMenu().setEnabled(true);
899
                            properties.addActionListener(this);
900
                     }
901
                }
902
        }
903
        
904
        public void actionPerformed(ActionEvent e) {
905

    
906
                FLayer[] actives = getMapContext().getLayers().getActives();
907
                if (actives.length==1) {
908
             
909
                lyr = getNodeLayer();
910
                    View vista = (View) PluginServices.getMDIManager().getActiveView();
911
                    MapControl mapCtrl = vista.getMapControl();
912
                    mapCtrl.setTool("zoom_pixel_cursor");
913
                             
914
                } 
915
                //View vista = (View) PluginServices.getMDIManager().getActiveView();
916
                //MapControl mapCtrl = vista.getMapControl();        
917
        }
918
}
919

    
920
/**
921
 * 
922
 * @author Nacho Brodin <brodin_ign@gva.es>
923
 *
924
 * Entrada de men? para la activaci?n de la funcionalidad de salvar
925
 * a raster una parte de la vista.
926
 */
927
class SaveRasterTocMenuEntry extends TocMenuEntry{
928
        private JMenuItem properties;
929
        FLayer lyr = null;
930
        
931
        public void initialize(FPopupMenu m) {
932
                super.initialize(m);
933
                
934
                if (isTocItemBranch()) {
935
                        lyr = getNodeLayer();
936
                    // Opcciones para capas raster
937
                    if ((lyr instanceof FLyrRaster)) {
938
                            properties = new JMenuItem(PluginServices.getText(this, "salvar_raster"));
939
                            getMenu().add( properties );
940
                            properties.setFont(FPopupMenu.theFont);
941
                            getMenu().setEnabled(true);
942
                            //getMenu().addSeparator();
943
                    //Cambio color
944
                            properties.addActionListener(this);
945
                     }
946
                }
947
        }
948
        
949
        public void actionPerformed(ActionEvent e) {
950
                lyr = getNodeLayer();
951
                View vista = (View) PluginServices.getMDIManager().getActiveView();
952
                MapControl mapCtrl = vista.getMapControl();
953
                mapCtrl.setTool("saveRaster");
954
        }
955
}
956

    
957
/**
958
 * Menu de bot?n derecho para el TOC.
959
 * Se pueden a?adir entradas facilmente desde una extensi?n,
960
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
961
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
962
 * (Las entradas actuales est?n hechas de esa manera).
963
 *
964
 * @author vcn To change the template for this generated type comment go to
965
 *         Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and
966
 *         Comments
967
 */
968

    
969
public class FPopupMenu extends JPopupMenu {
970
        private static ArrayList menuEntrys = new ArrayList();
971
    public DefaultMutableTreeNode nodo;
972
    protected FMap mapContext;
973
    //private JMenuItem capa;
974
    // Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
975
    // TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
976
    public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10);
977
    
978
    static {
979
            FPopupMenu.addEntry(new FSymbolChangeColorTocMenuEntry());
980
            FPopupMenu.addEntry(new ChangeNameTocMenuEntry());
981
            menuEntrys.add(new FLyrVectEditPropertiesTocMenuEntry());
982
            //FPopupMenu.addEntry(new FLyrRasterAdjustTransparencyTocMenuEntry());
983
            FPopupMenu.addEntry(new ZoomAlTemaTocMenuEntry());
984
            //FPopupMenu.addEntry(new ZoomPixelTocMenuEntry());
985
            FPopupMenu.addEntry(new ZoomPixelCursorTocMenuEntry());
986
            FPopupMenu.addEntry(new EliminarCapaTocMenuEntry());
987
            menuEntrys.add(new LayersGroupTocMenuEntry());
988
                menuEntrys.add(new LayersUngroupTocMenuEntry());
989
                menuEntrys.add(new FirstLayerTocMenuEntry());
990
            FPopupMenu.addEntry(new FLyrRasterAdjustPropertiesTocMenuEntry());
991
    }
992

    
993
    public static void addEntry(TocMenuEntry entry) {
994
            menuEntrys.add(entry);
995
    }
996
    
997
    /**
998
     * Creates a new FPopupMenu object.
999
     *
1000
     * @param nodo DOCUMENT ME!
1001
     * @param vista DOCUMENT ME!
1002
     */
1003
    public FPopupMenu(FMap mc, DefaultMutableTreeNode node) {
1004
        //super();
1005
        this.mapContext = mc;
1006
        this.nodo = node;
1007
 
1008
        //salir = new MenuItem("Salir");
1009

    
1010
        for (int i=0; i<menuEntrys.size(); i++) {
1011
                ((TocMenuEntry) menuEntrys.get(i)).initialize(this);
1012
        }
1013
    }
1014
    
1015
    public FMap getMapContext() { return mapContext; }
1016
        
1017
}