Statistics
| Revision:

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

History | View | Annotate | Download (15.2 KB)

1 312 fernando
/*
2
 * Created on 02-mar-2004
3 1117 luisw
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5 312 fernando
 *
6 1103 fjp
 * 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 312 fernando
package com.iver.cit.gvsig.gui.toc;
45
46 1337 fjp
import java.awt.Color;
47 312 fernando
import java.awt.Font;
48
import java.awt.event.ActionEvent;
49 1198 luisw
import java.awt.event.ActionListener;
50 1670 jmorell
import java.awt.geom.Rectangle2D;
51 571 luisw
import java.util.ArrayList;
52 312 fernando
53
import javax.swing.JColorChooser;
54
import javax.swing.JDialog;
55
import javax.swing.JFrame;
56
import javax.swing.JMenuItem;
57
import javax.swing.JPopupMenu;
58 1670 jmorell
import javax.swing.JTabbedPane;
59 312 fernando
import javax.swing.tree.DefaultMutableTreeNode;
60
61 600 fjp
import com.iver.andami.PluginServices;
62 1337 fjp
import com.iver.cit.gvsig.fmap.DriverException;
63 493 fjp
import com.iver.cit.gvsig.fmap.FMap;
64 387 fjp
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
65 434 fjp
import com.iver.cit.gvsig.fmap.layers.CancelationException;
66 387 fjp
import com.iver.cit.gvsig.fmap.layers.FLayer;
67
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
68
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
69 575 fjp
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
70 1337 fjp
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
71
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
72
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
73 312 fernando
import com.iver.cit.gvsig.gui.Panels.AdjustTransparencyPanel;
74 458 fjp
import com.iver.cit.gvsig.gui.legendmanager.FLegendManagerWindow;
75 1670 jmorell
import com.iver.cit.gvsig.gui.thememanager.FThemeManagerWindow;
76 312 fernando
77 1198 luisw
/**
78
 * Realiza el cambio de color si se pulsa OK
79
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
80
 */
81 571 luisw
class FSymbolChangeColorTocMenuEntry extends TocMenuEntry {
82 1337 fjp
    private JMenuItem color;
83 571 luisw
        public void initialize(FPopupMenu m) {
84 1117 luisw
                super.initialize(m);
85 1337 fjp
                boolean bShow = false;
86
                if (isTocItemBranch())
87
                {
88
                        FLayer lyr = getNodeLayer();
89
                    if ((lyr instanceof ClassifiableVectorial))
90
                    {
91
                        ClassifiableVectorial lyrVect = (ClassifiableVectorial) lyr;
92
                        if (lyrVect.getLegend() instanceof SingleSymbolLegend)
93
                            bShow = true;
94
                    }
95
                }
96
                else
97
                    bShow = true;
98
                if (bShow)
99
                {
100 600 fjp
                color = new JMenuItem(PluginServices.getText(this, "Cambio_Color"));
101 571 luisw
                color.setFont(FPopupMenu.theFont);
102 1117 luisw
                getMenu().add(color);
103 1670 jmorell
                getMenu().setEnabled(true);
104 312 fernando
                //Cambio color
105 1117 luisw
                color.addActionListener(this);
106 312 fernando
                }
107 571 luisw
        }
108 1117 luisw
109 1095 fjp
        /* (non-Javadoc)
110
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
111
         */
112 1117 luisw
        public void actionPerformed(ActionEvent e) {
113 1337 fjp
                ITocItem tocItem = (ITocItem) getNodeUserObject();
114
115
        Color newColor = JColorChooser.showDialog(null,
116
                PluginServices.getText(this, "Elegir_Color"),
117
                null);
118
        if (newColor != null)
119
        {
120
                        if (isTocItemBranch())
121
                        {
122
                                FLayer lyr = getNodeLayer();
123
                            if ((lyr instanceof ClassifiableVectorial))
124
                            {
125
                                ClassifiableVectorial lyrVect = (ClassifiableVectorial) lyr;
126
                                if (lyrVect.getLegend() instanceof SingleSymbolLegend)
127
                                {
128
                                    SingleSymbolLegend leg = (SingleSymbolLegend) lyrVect.getLegend();
129
                        leg.getDefaultSymbol().setColor(newColor);
130
                    }
131
                            }
132
                        }
133
                        else
134
                        {
135
                    TocItemLeaf leaf = (TocItemLeaf) tocItem;
136
                    FSymbol sym = leaf.getSymbol();
137
                    sym.setColor(newColor);
138
139
                        }
140
                // TRUCO PARA REFRESCAR.
141
                getMapContext().invalidate();
142
143
        }
144
145 1095 fjp
        }
146 571 luisw
}
147 312 fernando
148 571 luisw
class FLyrVectChangeStyleTocMenuEntry extends TocMenuEntry {
149
    private JMenuItem estilo;
150
        public void initialize(FPopupMenu m) {
151 1117 luisw
                super.initialize(m);
152 571 luisw
                FLayer lyr = null;
153 312 fernando
154 1117 luisw
                if (isTocItemBranch()) {
155
                        lyr = getNodeLayer();
156 575 fjp
                        if (lyr instanceof ClassifiableVectorial) {
157 600 fjp
                            estilo = new JMenuItem(PluginServices.getText(this, "Cambio_Estilo"));
158 1117 luisw
                                getMenu().add(estilo);
159 571 luisw
                                estilo.setFont(FPopupMenu.theFont);
160
                                //Cambio estilo
161 1117 luisw
                                estilo.addActionListener(this);
162 571 luisw
                        }
163
                }
164
        }
165 1095 fjp
        /* (non-Javadoc)
166
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
167
         */
168 1117 luisw
        public void actionPerformed(ActionEvent e) {
169 1095 fjp
                FLegendManagerWindow m_LegendEditor = new FLegendManagerWindow();
170
                try {
171 1117 luisw
                        m_LegendEditor.setMapContext(getMapContext());
172 1095 fjp
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
173
                        e1.printStackTrace();
174
                }
175
                if (PluginServices.getMainFrame() == null) {
176
                        JDialog dlg = new JDialog();
177
178
                        m_LegendEditor.setPreferredSize(m_LegendEditor.getSize());
179
                        dlg.getContentPane().add(m_LegendEditor);
180
                        dlg.setModal(false);
181
                        dlg.pack();
182
                        dlg.show();
183
184
                } else
185
                        PluginServices.getMDIManager().addView(m_LegendEditor);
186
        }
187 571 luisw
}
188 458 fjp
189 1670 jmorell
/**
190
 * Muestra el men? de propiedades del tema.
191
 *
192
 * @author jmorell
193
 *
194
 * TODO To change the template for this generated type comment go to
195
 * Window - Preferences - Java - Code Style - Code Templates
196
 */
197
class FLyrVectEditPropertiesTocMenuEntry extends TocMenuEntry {
198
    private JMenuItem prop;
199
        public void initialize(FPopupMenu m) {
200
                super.initialize(m);
201
                FLayer lyr = null;
202
                if (isTocItemBranch()) {
203
                        getMenu().addSeparator();
204
                        lyr = getNodeLayer();
205
                        if (lyr instanceof ClassifiableVectorial) {
206
                            prop = new JMenuItem("Propiedades Pliego");
207
                                getMenu().add(prop);
208
                                prop.setFont(FPopupMenu.theFont);
209
                                //Cambio estilo
210
                                prop.addActionListener(this);
211
                        }
212
                }
213
        }
214
        /* (non-Javadoc)
215
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
216
         */
217
        public void actionPerformed(ActionEvent e) {
218
                /*FThemeManagerWindow fThemeManagerWindow = new FThemeManagerWindow();
219
                try {
220
                        fThemeManagerWindow.setMapContext(getMapContext());
221
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
222
                        e1.printStackTrace();
223
                }*/
224
                FLayer[] actives = getMapContext().getLayers().getActives();
225
                FThemeManagerWindow fThemeManagerWindow;
226
            if (actives.length==1) {
227
                    System.out.println("openPliegoThemeProperties(): Una sola capa. Abrimos el panel para la capa " + actives[0]);
228
                    fThemeManagerWindow = new FThemeManagerWindow();
229
                    try {
230
                            fThemeManagerWindow.setMapContext(getMapContext());
231
                    } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
232
                            e1.printStackTrace();
233
                    }
234
            //PluginServices.getMDIManager().addView(fThemeManagerWindow);
235
                    if (PluginServices.getMainFrame() == null) {
236
                            JDialog dlg = new JDialog();
237
                            fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
238
                            dlg.getContentPane().add(fThemeManagerWindow);
239
                            dlg.setModal(false);
240
                            dlg.pack();
241
                            dlg.show();
242
                    } else {
243
                            PluginServices.getMDIManager().addView(fThemeManagerWindow);
244
                    }
245
            } else {
246
                for (int i = 0; i < actives.length; i++){
247
                        System.out.println("openPliegoThemeProperties(): Muchas capas. Abrimos el panel para la capa " + actives[i]);
248
                        fThemeManagerWindow = new FThemeManagerWindow(actives[i], getMapContext());
249
                //PluginServices.getMDIManager().addView(fThemeManagerWindow);
250
                        if (PluginServices.getMainFrame() == null) {
251
                                JDialog dlg = new JDialog();
252
                                fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
253
                                dlg.getContentPane().add(fThemeManagerWindow);
254
                                dlg.setModal(false);
255
                                dlg.pack();
256
                                dlg.show();
257
                        } else {
258
                                PluginServices.getMDIManager().addView(fThemeManagerWindow);
259
                        }
260
                }
261
            }
262
                /*if (PluginServices.getMainFrame() == null) {
263
                        JDialog dlg = new JDialog();
264
                        fThemeManagerWindow.setPreferredSize(fThemeManagerWindow.getSize());
265
                        dlg.getContentPane().add(fThemeManagerWindow);
266
                        dlg.setModal(false);
267
                        dlg.pack();
268
                        dlg.show();
269
                } else {
270
                        PluginServices.getMDIManager().addView(fThemeManagerWindow);
271
                }*/
272
        }
273
}
274 1095 fjp
275 571 luisw
class FLyrRasterAdjustTransparencyTocMenuEntry extends TocMenuEntry {
276
        private JMenuItem transparency;
277 1117 luisw
        FLayer lyr = null;
278 571 luisw
        public void initialize(FPopupMenu m) {
279 1117 luisw
                super.initialize(m);
280 571 luisw
281 1117 luisw
                if (isTocItemBranch()) {
282
                        lyr = getNodeLayer();
283 312 fernando
                    // Opcciones para capas raster
284
                    if ((lyr instanceof FLyrRaster)) {
285 600 fjp
                        transparency = new JMenuItem(PluginServices.getText(this, "adjust_transparency"));
286 1117 luisw
                            getMenu().add( transparency );
287 571 luisw
                            transparency.setFont(FPopupMenu.theFont);
288 1670 jmorell
                            getMenu().setEnabled(true);
289 1117 luisw
                            getMenu().addSeparator();
290 312 fernando
                    //Cambio color
291 1117 luisw
                    transparency.addActionListener(this);
292 312 fernando
                     }
293 571 luisw
                }
294
        }
295 1095 fjp
        /* (non-Javadoc)
296
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
297
         */
298 1117 luisw
        public void actionPerformed(ActionEvent e) {
299
               lyr = getNodeLayer();
300 1095 fjp
        AdjustTransparencyPanel transPanel = new AdjustTransparencyPanel((FLyrDefault) lyr);
301
                transPanel.openJDialog();
302 1454 luisw
        //getMapContext().invalidate();
303 1095 fjp
        }
304 571 luisw
}
305
306
class ZoomAlTemaTocMenuEntry extends TocMenuEntry {
307
    private JMenuItem zoom;
308
        public void initialize(FPopupMenu m) {
309 1117 luisw
                super.initialize(m);
310
                if (isTocItemBranch()) {
311 600 fjp
                        zoom = new JMenuItem(PluginServices.getText(this, "Zoom_al_Tema"));
312 1117 luisw
                        getMenu().add(zoom);
313 571 luisw
                        zoom.setFont(FPopupMenu.theFont);
314 312 fernando
315 1117 luisw
                zoom.addActionListener(this);
316 571 luisw
                }
317
        }
318 1095 fjp
        /* (non-Javadoc)
319
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
320
         */
321 1117 luisw
        public void actionPerformed(ActionEvent e) {
322 1670 jmorell
            /*FLayer lyr = getNodeLayer();
323 1095 fjp
        try {
324 1117 luisw
                getMapContext().getViewPort().setExtent(lyr.getFullExtent());
325 1095 fjp
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
326
                        e1.printStackTrace();
327 1670 jmorell
                }*/
328
                // 050209, jmorell: Para que haga un zoom a un grupo de capas seleccionadas.
329
                FLayer[] actives = getMapContext().getLayers().getActives();
330
                if (actives.length==1) {
331
                try {
332
                        getMapContext().getViewPort().setExtent(actives[0].getFullExtent());
333
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
334
                                e1.printStackTrace();
335
                        }
336
                } else {
337
                        try {
338
                                Rectangle2D maxExtent = setMaxExtent(actives);
339
                            getMapContext().getViewPort().setExtent(maxExtent);
340
                        } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
341
                                e1.printStackTrace();
342
                        }
343 1095 fjp
                }
344
        }
345 1670 jmorell
346
        private Rectangle2D setMaxExtent(FLayer[] actives) throws DriverException {
347
                Rectangle2D extRef = actives[0].getFullExtent();
348
                double minXRef = extRef.getMinX();
349
                double maxYRef = extRef.getMaxY();
350
                double maxXRef = extRef.getMaxX();
351
                double minYRef = extRef.getMinY();
352
                for (int i=0;i<actives.length;i++) {
353
                Rectangle2D extVar = actives[i].getFullExtent();
354
                    double minXVar = extVar.getMinX();
355
                    double maxYVar = extVar.getMaxY();
356
                    double maxXVar = extVar.getMaxX();
357
                    double minYVar = extVar.getMinY();
358
                    if (minXVar<=minXRef) minXRef=minXVar;
359
                    if (maxYVar>=maxYRef) maxYRef=maxYVar;
360
                    if (maxXVar>=maxXRef) maxXRef=maxXVar;
361
                    if (minYVar<=minYRef) minYRef=minYVar;
362
                    extRef.setRect(minXRef, minYRef, maxXRef-minXRef, maxYRef-minYRef);
363
                }
364
                return extRef;
365
        }
366 571 luisw
}
367
368
class EliminarCapaTocMenuEntry extends TocMenuEntry {
369
    private JMenuItem removeLayer;
370
        public void initialize(FPopupMenu m) {
371 1117 luisw
                super.initialize(m);
372
                if (getNodeUserObject() instanceof TocItemBranch) {
373
                        getMenu().addSeparator();
374 600 fjp
                        removeLayer = new JMenuItem(PluginServices.getText(this, "eliminar_capa"));
375 1117 luisw
                        getMenu().add(removeLayer);
376 571 luisw
                        removeLayer.setFont(FPopupMenu.theFont);
377 1117 luisw
                        removeLayer.addActionListener(this);
378 571 luisw
                }
379
        }
380 1095 fjp
        /* (non-Javadoc)
381
         * @see com.iver.cit.gvsig.gui.toc.TocMenuEntry#execute(com.iver.cit.gvsig.gui.toc.ITocItem)
382
         */
383 1117 luisw
        public void actionPerformed(ActionEvent e) {
384 1670 jmorell
            // 050209, jmorell: Para poder borrar todas las capas seleccionadas desde el FPopUpMenu.
385
                //                                        Es necesario pulsar May?sculas a la vez que el bot?n derecho.
386
            FMap fMap = getMapContext();
387
            FLayer[] actives = fMap.getLayers().getActives();
388
            for (int i = 0; i < actives.length; i++){
389
                try {
390
                    getMapContext().getLayers().removeLayer(actives[i]);
391
                    if (getMapContext().getLayers().getLayersCount()==0)PluginServices.getMainFrame().enableControls();
392
                    } catch (CancelationException e1) {
393
                            e1.printStackTrace();
394
                    }
395
            }
396
                // 050209, jmorell: As? solo borra una capa (sobre la que pulsas).
397
            /*FLayer lyr = getNodeLayer();
398 1095 fjp
        try {
399 1117 luisw
                getMapContext().getLayers().removeLayer(lyr);
400 1227 vcaballero
                if (getMapContext().getLayers().getLayersCount()==0)PluginServices.getMainFrame().enableControls();
401 1095 fjp
                } catch (CancelationException e1) {
402
                        e1.printStackTrace();
403 1670 jmorell
                }*/
404 1095 fjp
    }
405 571 luisw
}
406
407
/**
408
 * Menu de bot?n derecho para el TOC.
409
 * Se pueden a?adir entradas facilmente desde una extensi?n,
410
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
411
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
412
 * (Las entradas actuales est?n hechas de esa manera).
413
 *
414
 * @author vcn To change the template for this generated type comment go to
415
 *         Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and
416
 *         Comments
417
 */
418
419
public class FPopupMenu extends JPopupMenu {
420
        private static ArrayList menuEntrys = null;
421
    public DefaultMutableTreeNode nodo;
422 1090 fjp
    protected FMap mapContext;
423 571 luisw
    //private JMenuItem capa;
424
    // Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
425 575 fjp
    // TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
426 571 luisw
    public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10);
427
428
    static {
429
            menuEntrys = new ArrayList();
430
            menuEntrys.add(new FSymbolChangeColorTocMenuEntry());
431
            menuEntrys.add(new FLyrVectChangeStyleTocMenuEntry());
432
            menuEntrys.add(new FLyrRasterAdjustTransparencyTocMenuEntry());
433
            menuEntrys.add(new ZoomAlTemaTocMenuEntry());
434
            menuEntrys.add(new EliminarCapaTocMenuEntry());
435 1670 jmorell
            //050201 jmorell: Acceso al men? propiedades.
436
            menuEntrys.add(new FLyrVectEditPropertiesTocMenuEntry());
437 571 luisw
    }
438
439
    public static void addEntry(TocMenuEntry entry) {
440
            menuEntrys.add(entry);
441
    }
442
443
    /**
444
     * Creates a new FPopupMenu object.
445
     *
446
     * @param nodo DOCUMENT ME!
447
     * @param vista DOCUMENT ME!
448
     */
449
    public FPopupMenu(FMap mc, DefaultMutableTreeNode node) {
450
        //super();
451
        this.mapContext = mc;
452
        this.nodo = node;
453
454
        //salir = new MenuItem("Salir");
455
456
        for (int i=0; i<menuEntrys.size(); i++) {
457
                ((TocMenuEntry) menuEntrys.get(i)).initialize(this);
458 312 fernando
        }
459
    }
460 571 luisw
461
    public FMap getMapContext() { return mapContext; }
462 312 fernando
463
}