Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / gui / FPopupMenu.java @ 35756

History | View | Annotate | Download (12.9 KB)

1 7304 caballero
/*
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.project.documents.view.toc.gui;
45
46
import java.awt.Font;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.lang.reflect.Array;
50
import java.text.NumberFormat;
51
import java.util.ArrayList;
52
import java.util.Arrays;
53
import java.util.Comparator;
54
import java.util.Iterator;
55
56
import javax.swing.JMenuItem;
57
import javax.swing.JPopupMenu;
58
import javax.swing.tree.DefaultMutableTreeNode;
59
60
import com.iver.cit.gvsig.fmap.MapContext;
61
import com.iver.cit.gvsig.fmap.layers.FLayer;
62
import com.iver.cit.gvsig.project.documents.IContextMenuAction;
63
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
64
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
65 29056 vcaballero
import com.iver.cit.gvsig.project.documents.view.toc.TocItemBranch;
66 7304 caballero
import com.iver.cit.gvsig.project.documents.view.toc.TocMenuEntry;
67
import com.iver.cit.gvsig.project.documents.view.toc.actions.ChangeNameTocMenuEntry;
68 7746 jmvivo
import com.iver.cit.gvsig.project.documents.view.toc.actions.CopyLayersTocMenuEntry;
69
import com.iver.cit.gvsig.project.documents.view.toc.actions.CutLayersTocMenuEntry;
70 7304 caballero
import com.iver.cit.gvsig.project.documents.view.toc.actions.EliminarCapaTocMenuEntry;
71
import com.iver.cit.gvsig.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
72 13604 jaume
import com.iver.cit.gvsig.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
73 7304 caballero
import com.iver.cit.gvsig.project.documents.view.toc.actions.FirstLayerTocMenuEntry;
74
import com.iver.cit.gvsig.project.documents.view.toc.actions.LayersGroupTocMenuEntry;
75
import com.iver.cit.gvsig.project.documents.view.toc.actions.LayersUngroupTocMenuEntry;
76
import com.iver.cit.gvsig.project.documents.view.toc.actions.OldTocContextMenuAction;
77 7746 jmvivo
import com.iver.cit.gvsig.project.documents.view.toc.actions.PasteLayersTocMenuEntry;
78 7304 caballero
import com.iver.cit.gvsig.project.documents.view.toc.actions.ReloadLayerTocMenuEntry;
79 7476 azabala
import com.iver.cit.gvsig.project.documents.view.toc.actions.ShowLayerErrorsTocMenuEntry;
80 7304 caballero
import com.iver.cit.gvsig.project.documents.view.toc.actions.ZoomAlTemaTocMenuEntry;
81
import com.iver.utiles.extensionPoints.ExtensionPoint;
82
import com.iver.utiles.extensionPoints.ExtensionPoints;
83
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
84
85
/**
86
 * Menu de bot?n derecho para el TOC.
87
 * Se pueden a?adir entradas facilmente desde una extensi?n,
88
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
89
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
90
 * (Las entradas actuales est?n hechas de esa manera).
91
 *
92
 * @author vcn To change the template for this generated type comment go to
93
 *         Window>Preferences>Java>Code Generation>Code and
94
 *         Comments
95
 */
96
97
public class FPopupMenu extends JPopupMenu {
98
        //private static ArrayList menuEntrys = new ArrayList();
99
    private DefaultMutableTreeNode nodo;
100
    protected MapContext mapContext;
101
    private ExtensionPoint extensionPoint;
102
    private FLayer[] selecteds;
103
    //private JMenuItem capa;
104
    // Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
105
    // TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
106
    public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10);
107
108
    public static void registerExtensionPoint() {
109
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
110 13604 jaume
            extensionPoints.add("View_TocActions","FSymbolChangeColor",new ChangeSymbolTocMenuEntry());
111 7304 caballero
            extensionPoints.add("View_TocActions","ChangeName",new ChangeNameTocMenuEntry());
112
            extensionPoints.add("View_TocActions","FLyrVectEditProperties",new FLyrVectEditPropertiesTocMenuEntry());
113
            extensionPoints.add("View_TocActions","ZoomAlTema",new ZoomAlTemaTocMenuEntry());
114 13068 bsanchez
            //extensionPoints.add("View_TocActions","ZoomPixelCursor",new ZoomPixelCursorTocMenuEntry());
115 7304 caballero
            extensionPoints.add("View_TocActions","EliminarCapa",new EliminarCapaTocMenuEntry());
116 7476 azabala
            extensionPoints.add("View_TocActions","VerErroresCapa",new ShowLayerErrorsTocMenuEntry());
117 7304 caballero
            extensionPoints.add("View_TocActions","ReloadLayer",new ReloadLayerTocMenuEntry());
118
            extensionPoints.add("View_TocActions","LayersGroup",new LayersGroupTocMenuEntry());
119
               extensionPoints.add("View_TocActions","LayersUngroup",new LayersUngroupTocMenuEntry());
120
               extensionPoints.add("View_TocActions","FirstLayer",new FirstLayerTocMenuEntry());
121 13710 caballero
122 7746 jmvivo
               extensionPoints.add("View_TocActions","Copy",new CopyLayersTocMenuEntry());
123
               extensionPoints.add("View_TocActions","Cut",new CutLayersTocMenuEntry());
124
               extensionPoints.add("View_TocActions","Paste",new PasteLayersTocMenuEntry());
125 13068 bsanchez
               //extensionPoints.add("View_TocActions","RasterProperties",new FLyrRasterAdjustPropertiesTocMenuEntry());
126 9727 nacho
               //extensionPoints.add("View_TocActions","RasterProperties",new RasterPropertiesTocMenuEntry());
127 7304 caballero
128 13710 caballero
129 7304 caballero
    }
130
    static {
131
            /* Cambiados
132
            FPopupMenu.addEntry(new FSymbolChangeColorTocMenuEntry());
133
            FPopupMenu.addEntry(new ChangeNameTocMenuEntry());
134
            FPopupMenu.addEntry(new FLyrVectEditPropertiesTocMenuEntry());
135

136

137
            FPopupMenu.addEntry(new ZoomAlTemaTocMenuEntry());
138
            FPopupMenu.addEntry(new ZoomPixelCursorTocMenuEntry());
139

140

141
            FPopupMenu.addEntry(new EliminarCapaTocMenuEntry());
142
            FPopupMenu.addEntry(new ReloadLayerTocMenuEntry());
143

144
            FPopupMenu.addEntry(new LayersGroupTocMenuEntry());
145
        FPopupMenu.addEntry(new LayersUngroupTocMenuEntry());
146

147

148
            FPopupMenu.addEntry(new FirstLayerTocMenuEntry());
149
                                                        */
150 9727 nacho
            //FPopupMenu.addEntry(new FLyrRasterAdjustPropertiesTocMenuEntry());
151 7304 caballero
    }
152
153
154
    /**
155
     * @deprecated
156
     */
157
    public static void addEntry(TocMenuEntry entry) {
158
159
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
160
161
            OldTocContextMenuAction action = new OldTocContextMenuAction();
162
            action.setEntry(entry);
163
            String name =entry.getClass().getName();
164
            name = name.substring(name.lastIndexOf(".")+1);
165
            extensionPoints.add("View_TocActions",name,action);
166
167
    }
168
169
    /**
170
     * @deprecated
171
     */
172
    public static Object getEntry(String className) {
173
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
174
            OldTocContextMenuAction action = null;
175
            try {
176
                        action = (OldTocContextMenuAction)((ExtensionPoint)extensionPoints.get("View_TocActions")).create(className);
177
                } catch (InstantiationException e) {
178
                        // TODO Auto-generated catch block
179
                        e.printStackTrace();
180
                } catch (IllegalAccessException e) {
181
                        // TODO Auto-generated catch block
182
                        e.printStackTrace();
183
                } catch (ClassCastException e) {
184
                        action = null;
185
                }
186
            if (action != null) {
187
                    return action.getEntry();
188
            } else {
189
                    return null;
190
            }
191
    }
192
193
    /**
194
     * Creates a new FPopupMenu object.
195
     *
196
     * @param nodo DOCUMENT ME!
197
     * @param vista DOCUMENT ME!
198
     */
199
    public FPopupMenu(MapContext mc, DefaultMutableTreeNode node) {
200
        super();
201
        this.initialize(mc,node);
202
    }
203
204
    private void initialize(MapContext mc, DefaultMutableTreeNode node) {
205
        this.mapContext = mc;
206
        this.nodo = node;
207
208
        //salir = new MenuItem("Salir");
209
                this.extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get("View_TocActions");
210
                this.selecteds = this.mapContext.getLayers().getActives();
211
212
                IContextMenuAction[] actions = this.getActionList();
213 29056 vcaballero
                if (actions == null){
214
                        return;
215
                }
216 7304 caballero
                this.createMenuElements(actions);
217
218
                this.loadOldStileOptions();
219
220
221
    }
222
223
    public MapContext getMapContext() { return mapContext; }
224
225
        public ITocItem getNodeUserObject() {
226
                if (nodo == null) return null;
227
                return (ITocItem)nodo.getUserObject();
228
        }
229
230
        public DefaultMutableTreeNode getNode() {
231
                return this.nodo;
232
        }
233
234
    private IContextMenuAction[] getActionList() {
235 29056 vcaballero
            ArrayList actionArrayList = new ArrayList();
236
            Iterator iter = this.extensionPoint.keySet().iterator();
237
            AbstractTocContextMenuAction action;
238
            boolean contains=false;
239
            ITocItem tocItem=(ITocItem)this.getNodeUserObject();
240
            if (tocItem instanceof TocItemBranch){
241
                    for (int i=0;i<this.selecteds.length;i++){
242
                            if (this.selecteds[i].equals(((TocItemBranch)tocItem).getLayer()))
243
                                    contains=true;
244
                    }
245
            }else{
246
                    contains=true;
247
            }
248
            if (contains){
249
                    while (iter.hasNext()) {
250
                            action = null;
251
                            try {
252
                                    action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
253
                            } catch (InstantiationException e) {
254
                                    // TODO Auto-generated catch block
255
                                    e.printStackTrace();
256
                            } catch (IllegalAccessException e) {
257
                                    // TODO Auto-generated catch block
258
                                    e.printStackTrace();
259
                            }
260
                            if (action != null && !(action instanceof OldTocContextMenuAction)) {
261
                                    action.setMapContext(this.mapContext);
262
                                    if (action.isVisible((ITocItem)this.getNodeUserObject(),this.selecteds)) {
263
                                            actionArrayList.add(action);
264
                                    }
265
                            }
266 7304 caballero
267 29056 vcaballero
                    }
268
                    IContextMenuAction[] result = (IContextMenuAction[])Array.newInstance(IContextMenuAction.class,actionArrayList.size());
269
                    System.arraycopy(actionArrayList.toArray(),0,result,0,actionArrayList.size());
270
                    Arrays.sort(result,new CompareAction());
271
                    return result;
272
            }
273
            return null;
274 7304 caballero
275
    }
276
277
        public class CompareAction implements Comparator{
278
                public int compare(Object o1, Object o2) {
279
                        return this.compare((IContextMenuAction)o1,(IContextMenuAction)o2);
280
                }
281
282
                public int compare(IContextMenuAction o1, IContextMenuAction o2) {
283
                        //FIXME: flata formatear los enteros!!!!
284
                        NumberFormat formater = NumberFormat.getInstance();
285
                        formater.setMinimumIntegerDigits(3);
286
                        String key1= ""+formater.format(o1.getGroupOrder())+o1.getGroup()+formater.format(o1.getOrder());
287
                        String key2= ""+formater.format(o2.getGroupOrder())+o2.getGroup()+formater.format(o2.getOrder());
288
                        return key1.compareTo(key2);
289
                }
290
        }
291
292
        private void createMenuElements(IContextMenuAction[] actions) {
293
                String group = null;
294
                for (int i=0;i < actions.length;i++) {
295
                        IContextMenuAction action = actions[i];
296
                        MenuItem item = new MenuItem(action.getText(),action);
297
                        item.setFont(theFont);
298
                        item.setEnabled(action.isEnabled(this.getNodeUserObject(),this.selecteds));
299
                        if (!action.getGroup().equals(group)) {
300
                                if (group != null) this.addSeparator();
301
                                group = action.getGroup();
302
                        }
303
                        this.add(item);
304
                }
305
306
        }
307
308
309
        public class MenuItem extends JMenuItem implements ActionListener{
310
                private IContextMenuAction action;
311
                public MenuItem(String text,IContextMenuAction documentAction) {
312
                        super(text);
313
                        this.action = documentAction;
314
                        String tip = this.action.getDescription();
315
                        if (tip != null && tip.length() > 0) {
316
                                this.setToolTipText(tip);
317
                        }
318
                        this.addActionListener(this);
319
                }
320
321
                public void actionPerformed(ActionEvent e) {
322
                        this.action.execute(FPopupMenu.this.getNodeUserObject(), FPopupMenu.this.selecteds);
323
                }
324
        }
325
326
        private void loadOldStileOptions() {
327
                boolean first = true;
328
                Iterator iter = this.extensionPoint.keySet().iterator();
329
                AbstractTocContextMenuAction action;
330
                while (iter.hasNext()) {
331
                        action = null;
332
                        try {
333
                                action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
334
                        } catch (InstantiationException e) {
335
                                // TODO Auto-generated catch block
336
                                e.printStackTrace();
337
                        } catch (IllegalAccessException e) {
338
                                // TODO Auto-generated catch block
339
                                e.printStackTrace();
340
                        }
341
                        if (action != null && (action instanceof OldTocContextMenuAction)) {
342
                                if (first) {
343
                                        this.addSeparator();
344
                                        first = false;
345
                                }
346
                                action.setMapContext(this.mapContext);
347
                                ((OldTocContextMenuAction)action).initializeElement(this);
348
                        }
349
                }
350
                //comprobamos si el ultimo elemento es un seprardor
351 13710 caballero
                if (this.getComponentCount()>0 && this.getComponent(this.getComponentCount()-1) instanceof Separator) {
352 7304 caballero
                        //Si lo es lo eliminamos
353
                        this.remove(this.getComponentCount()-1);
354
                }
355
356
357
        }
358
359
360
}