Statistics
| Revision:

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

History | View | Annotate | Download (12.3 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.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
import com.iver.cit.gvsig.project.documents.view.toc.TocMenuEntry;
66
import com.iver.cit.gvsig.project.documents.view.toc.actions.ChangeNameTocMenuEntry;
67
import com.iver.cit.gvsig.project.documents.view.toc.actions.CopyLayersTocMenuEntry;
68
import com.iver.cit.gvsig.project.documents.view.toc.actions.CutLayersTocMenuEntry;
69
import com.iver.cit.gvsig.project.documents.view.toc.actions.EliminarCapaTocMenuEntry;
70
import com.iver.cit.gvsig.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
71
import com.iver.cit.gvsig.project.documents.view.toc.actions.FSymbolChangeColorTocMenuEntry;
72
import com.iver.cit.gvsig.project.documents.view.toc.actions.FirstLayerTocMenuEntry;
73
import com.iver.cit.gvsig.project.documents.view.toc.actions.LayersGroupTocMenuEntry;
74
import com.iver.cit.gvsig.project.documents.view.toc.actions.LayersUngroupTocMenuEntry;
75
import com.iver.cit.gvsig.project.documents.view.toc.actions.OldTocContextMenuAction;
76
import com.iver.cit.gvsig.project.documents.view.toc.actions.PasteLayersTocMenuEntry;
77
import com.iver.cit.gvsig.project.documents.view.toc.actions.ReloadLayerTocMenuEntry;
78
import com.iver.cit.gvsig.project.documents.view.toc.actions.ShowLayerErrorsTocMenuEntry;
79
import com.iver.cit.gvsig.project.documents.view.toc.actions.ZoomAlTemaTocMenuEntry;
80
import com.iver.utiles.extensionPoints.ExtensionPoint;
81
import com.iver.utiles.extensionPoints.ExtensionPoints;
82
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
83

    
84
/**
85
 * Menu de bot?n derecho para el TOC.
86
 * Se pueden a?adir entradas facilmente desde una extensi?n,
87
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
88
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
89
 * (Las entradas actuales est?n hechas de esa manera).
90
 *
91
 * @author vcn To change the template for this generated type comment go to
92
 *         Window>Preferences>Java>Code Generation>Code and
93
 *         Comments
94
 */
95

    
96
public class FPopupMenu extends JPopupMenu {
97
        //private static ArrayList menuEntrys = new ArrayList();
98
    private DefaultMutableTreeNode nodo;
99
    protected MapContext mapContext;
100
    private ExtensionPoint extensionPoint;
101
    private FLayer[] selecteds;
102
    //private JMenuItem capa;
103
    // Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
104
    // TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
105
    public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10);
106

    
107
    public static void registerExtensionPoint() {
108
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
109
            extensionPoints.add("View_TocActions","FSymbolChangeColor",new FSymbolChangeColorTocMenuEntry());
110
            extensionPoints.add("View_TocActions","ChangeName",new ChangeNameTocMenuEntry());
111
            extensionPoints.add("View_TocActions","FLyrVectEditProperties",new FLyrVectEditPropertiesTocMenuEntry());
112
            extensionPoints.add("View_TocActions","ZoomAlTema",new ZoomAlTemaTocMenuEntry());
113
            //extensionPoints.add("View_TocActions","ZoomPixelCursor",new ZoomPixelCursorTocMenuEntry());
114
            extensionPoints.add("View_TocActions","EliminarCapa",new EliminarCapaTocMenuEntry());
115
            extensionPoints.add("View_TocActions","VerErroresCapa",new ShowLayerErrorsTocMenuEntry());
116
            extensionPoints.add("View_TocActions","ReloadLayer",new ReloadLayerTocMenuEntry());
117
            extensionPoints.add("View_TocActions","LayersGroup",new LayersGroupTocMenuEntry());
118
               extensionPoints.add("View_TocActions","LayersUngroup",new LayersUngroupTocMenuEntry());
119
               extensionPoints.add("View_TocActions","FirstLayer",new FirstLayerTocMenuEntry());
120
               
121
               extensionPoints.add("View_TocActions","Copy",new CopyLayersTocMenuEntry());
122
               extensionPoints.add("View_TocActions","Cut",new CutLayersTocMenuEntry());
123
               extensionPoints.add("View_TocActions","Paste",new PasteLayersTocMenuEntry());
124
               //extensionPoints.add("View_TocActions","RasterProperties",new FLyrRasterAdjustPropertiesTocMenuEntry());
125
               //extensionPoints.add("View_TocActions","RasterProperties",new RasterPropertiesTocMenuEntry());
126
               
127

    
128
    }
129
    static {
130
            /* Cambiados
131
            FPopupMenu.addEntry(new FSymbolChangeColorTocMenuEntry());
132
            FPopupMenu.addEntry(new ChangeNameTocMenuEntry());
133
            FPopupMenu.addEntry(new FLyrVectEditPropertiesTocMenuEntry());
134

135

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

139

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

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

146

147
            FPopupMenu.addEntry(new FirstLayerTocMenuEntry());
148
                                                        */
149
            //FPopupMenu.addEntry(new FLyrRasterAdjustPropertiesTocMenuEntry());
150
    }
151

    
152

    
153
    /**
154
     * @deprecated
155
     */
156
    public static void addEntry(TocMenuEntry entry) {
157

    
158
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
159

    
160
            OldTocContextMenuAction action = new OldTocContextMenuAction();
161
            action.setEntry(entry);
162
            String name =entry.getClass().getName();
163
            name = name.substring(name.lastIndexOf(".")+1);
164
            extensionPoints.add("View_TocActions",name,action);
165

    
166
    }
167

    
168
    /**
169
     * @deprecated
170
     */
171
    public static Object getEntry(String className) {
172
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
173
            OldTocContextMenuAction action = null;
174
            try {
175
                        action = (OldTocContextMenuAction)((ExtensionPoint)extensionPoints.get("View_TocActions")).create(className);
176
                } catch (InstantiationException e) {
177
                        // TODO Auto-generated catch block
178
                        e.printStackTrace();
179
                } catch (IllegalAccessException e) {
180
                        // TODO Auto-generated catch block
181
                        e.printStackTrace();
182
                } catch (ClassCastException e) {
183
                        action = null;
184
                }
185
            if (action != null) {
186
                    return action.getEntry();
187
            } else {
188
                    return null;
189
            }
190
    }
191

    
192
    /**
193
     * Creates a new FPopupMenu object.
194
     *
195
     * @param nodo DOCUMENT ME!
196
     * @param vista DOCUMENT ME!
197
     */
198
    public FPopupMenu(MapContext mc, DefaultMutableTreeNode node) {
199
        super();
200
        this.initialize(mc,node);
201
    }
202

    
203
    private void initialize(MapContext mc, DefaultMutableTreeNode node) {
204
        this.mapContext = mc;
205
        this.nodo = node;
206

    
207
        //salir = new MenuItem("Salir");
208
                this.extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get("View_TocActions");
209
                this.selecteds = this.mapContext.getLayers().getActives();
210

    
211
                IContextMenuAction[] actions = this.getActionList();
212

    
213
                this.createMenuElements(actions);
214

    
215
                this.loadOldStileOptions();
216

    
217

    
218
    }
219

    
220
    public MapContext getMapContext() { return mapContext; }
221

    
222
        public ITocItem getNodeUserObject() {
223
                if (nodo == null) return null;
224
                return (ITocItem)nodo.getUserObject();
225
        }
226

    
227
        public DefaultMutableTreeNode getNode() {
228
                return this.nodo;
229
        }
230

    
231
    private IContextMenuAction[] getActionList() {
232
                ArrayList actionArrayList = new ArrayList();
233
                Iterator iter = this.extensionPoint.keySet().iterator();
234
                AbstractTocContextMenuAction action;
235
                while (iter.hasNext()) {
236
                        action = null;
237
                        try {
238
                                action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
239
                        } catch (InstantiationException e) {
240
                                // TODO Auto-generated catch block
241
                                e.printStackTrace();
242
                        } catch (IllegalAccessException e) {
243
                                // TODO Auto-generated catch block
244
                                e.printStackTrace();
245
                        }
246
                        if (action != null && !(action instanceof OldTocContextMenuAction)) {
247
                                action.setMapContext(this.mapContext);
248
                                if (action.isVisible((ITocItem)this.getNodeUserObject(),this.selecteds)) {
249
                                        actionArrayList.add(action);
250
                                }
251
                        }
252

    
253
                }
254
                IContextMenuAction[] result = (IContextMenuAction[])Array.newInstance(IContextMenuAction.class,actionArrayList.size());
255
                System.arraycopy(actionArrayList.toArray(),0,result,0,actionArrayList.size());
256
                Arrays.sort(result,new CompareAction());
257

    
258
                return result;
259
    }
260

    
261
        public class CompareAction implements Comparator{
262
                public int compare(Object o1, Object o2) {
263
                        return this.compare((IContextMenuAction)o1,(IContextMenuAction)o2);
264
                }
265

    
266
                public int compare(IContextMenuAction o1, IContextMenuAction o2) {
267
                        //FIXME: flata formatear los enteros!!!!
268
                        NumberFormat formater = NumberFormat.getInstance();
269
                        formater.setMinimumIntegerDigits(3);
270
                        String key1= ""+formater.format(o1.getGroupOrder())+o1.getGroup()+formater.format(o1.getOrder());
271
                        String key2= ""+formater.format(o2.getGroupOrder())+o2.getGroup()+formater.format(o2.getOrder());
272
                        return key1.compareTo(key2);
273
                }
274
        }
275

    
276
        private void createMenuElements(IContextMenuAction[] actions) {
277
                String group = null;
278
                for (int i=0;i < actions.length;i++) {
279
                        IContextMenuAction action = actions[i];
280
                        MenuItem item = new MenuItem(action.getText(),action);
281
                        item.setFont(theFont);
282
                        item.setEnabled(action.isEnabled(this.getNodeUserObject(),this.selecteds));
283
                        if (!action.getGroup().equals(group)) {
284
                                if (group != null) this.addSeparator();
285
                                group = action.getGroup();
286
                        }
287
                        this.add(item);
288
                }
289

    
290
        }
291

    
292

    
293
        public class MenuItem extends JMenuItem implements ActionListener{
294
                private IContextMenuAction action;
295
                public MenuItem(String text,IContextMenuAction documentAction) {
296
                        super(text);
297
                        this.action = documentAction;
298
                        String tip = this.action.getDescription();
299
                        if (tip != null && tip.length() > 0) {
300
                                this.setToolTipText(tip);
301
                        }
302
                        this.addActionListener(this);
303
                }
304

    
305
                public void actionPerformed(ActionEvent e) {
306
                        this.action.execute(FPopupMenu.this.getNodeUserObject(), FPopupMenu.this.selecteds);
307
                }
308
        }
309

    
310
        private void loadOldStileOptions() {
311
                boolean first = true;
312
                Iterator iter = this.extensionPoint.keySet().iterator();
313
                AbstractTocContextMenuAction action;
314
                while (iter.hasNext()) {
315
                        action = null;
316
                        try {
317
                                action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
318
                        } catch (InstantiationException e) {
319
                                // TODO Auto-generated catch block
320
                                e.printStackTrace();
321
                        } catch (IllegalAccessException e) {
322
                                // TODO Auto-generated catch block
323
                                e.printStackTrace();
324
                        }
325
                        if (action != null && (action instanceof OldTocContextMenuAction)) {
326
                                if (first) {
327
                                        this.addSeparator();
328
                                        first = false;
329
                                }
330
                                action.setMapContext(this.mapContext);
331
                                ((OldTocContextMenuAction)action).initializeElement(this);
332
                        }
333
                }
334
                //comprobamos si el ultimo elemento es un seprardor
335
                if (this.getComponent(this.getComponentCount()-1) instanceof Separator) {
336
                        //Si lo es lo eliminamos
337
                        this.remove(this.getComponentCount()-1);
338
                }
339

    
340

    
341
        }
342

    
343

    
344
}