Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / gui / FPopupMenu.java @ 7304

History | View | Annotate | Download (11.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.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.EliminarCapaTocMenuEntry;
68
import com.iver.cit.gvsig.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
69
import com.iver.cit.gvsig.project.documents.view.toc.actions.FSymbolChangeColorTocMenuEntry;
70
import com.iver.cit.gvsig.project.documents.view.toc.actions.FirstLayerTocMenuEntry;
71
import com.iver.cit.gvsig.project.documents.view.toc.actions.LayersGroupTocMenuEntry;
72
import com.iver.cit.gvsig.project.documents.view.toc.actions.LayersUngroupTocMenuEntry;
73
import com.iver.cit.gvsig.project.documents.view.toc.actions.OldTocContextMenuAction;
74
import com.iver.cit.gvsig.project.documents.view.toc.actions.ReloadLayerTocMenuEntry;
75
import com.iver.cit.gvsig.project.documents.view.toc.actions.ZoomAlTemaTocMenuEntry;
76
import com.iver.cit.gvsig.project.documents.view.toc.actions.ZoomPixelCursorTocMenuEntry;
77
import com.iver.utiles.extensionPoints.ExtensionPoint;
78
import com.iver.utiles.extensionPoints.ExtensionPoints;
79
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
80

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

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

    
104
    public static void registerExtensionPoint() {
105
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
106
            extensionPoints.add("View_TocActions","FSymbolChangeColor",new FSymbolChangeColorTocMenuEntry());
107
            extensionPoints.add("View_TocActions","ChangeName",new ChangeNameTocMenuEntry());
108
            extensionPoints.add("View_TocActions","FLyrVectEditProperties",new FLyrVectEditPropertiesTocMenuEntry());
109
            extensionPoints.add("View_TocActions","ZoomAlTema",new ZoomAlTemaTocMenuEntry());
110
            extensionPoints.add("View_TocActions","ZoomPixelCursor",new ZoomPixelCursorTocMenuEntry());
111
            extensionPoints.add("View_TocActions","EliminarCapa",new EliminarCapaTocMenuEntry());
112
            extensionPoints.add("View_TocActions","ReloadLayer",new ReloadLayerTocMenuEntry());
113
            extensionPoints.add("View_TocActions","LayersGroup",new LayersGroupTocMenuEntry());
114
               extensionPoints.add("View_TocActions","LayersUngroup",new LayersUngroupTocMenuEntry());
115
               extensionPoints.add("View_TocActions","FirstLayer",new FirstLayerTocMenuEntry());
116

    
117

    
118
    }
119
    static {
120
            /* Cambiados
121
            FPopupMenu.addEntry(new FSymbolChangeColorTocMenuEntry());
122
            FPopupMenu.addEntry(new ChangeNameTocMenuEntry());
123
            FPopupMenu.addEntry(new FLyrVectEditPropertiesTocMenuEntry());
124

125

126
            FPopupMenu.addEntry(new ZoomAlTemaTocMenuEntry());
127
            FPopupMenu.addEntry(new ZoomPixelCursorTocMenuEntry());
128

129

130
            FPopupMenu.addEntry(new EliminarCapaTocMenuEntry());
131
            FPopupMenu.addEntry(new ReloadLayerTocMenuEntry());
132

133
            FPopupMenu.addEntry(new LayersGroupTocMenuEntry());
134
        FPopupMenu.addEntry(new LayersUngroupTocMenuEntry());
135

136

137
            FPopupMenu.addEntry(new FirstLayerTocMenuEntry());
138
                                                        */
139
            FPopupMenu.addEntry(new FLyrRasterAdjustPropertiesTocMenuEntry());
140
    }
141

    
142

    
143
    /**
144
     * @deprecated
145
     */
146
    public static void addEntry(TocMenuEntry entry) {
147

    
148
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
149

    
150
            OldTocContextMenuAction action = new OldTocContextMenuAction();
151
            action.setEntry(entry);
152
            String name =entry.getClass().getName();
153
            name = name.substring(name.lastIndexOf(".")+1);
154
            extensionPoints.add("View_TocActions",name,action);
155

    
156
    }
157

    
158
    /**
159
     * @deprecated
160
     */
161
    public static Object getEntry(String className) {
162
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
163
            OldTocContextMenuAction action = null;
164
            try {
165
                        action = (OldTocContextMenuAction)((ExtensionPoint)extensionPoints.get("View_TocActions")).create(className);
166
                } catch (InstantiationException e) {
167
                        // TODO Auto-generated catch block
168
                        e.printStackTrace();
169
                } catch (IllegalAccessException e) {
170
                        // TODO Auto-generated catch block
171
                        e.printStackTrace();
172
                } catch (ClassCastException e) {
173
                        action = null;
174
                }
175
            if (action != null) {
176
                    return action.getEntry();
177
            } else {
178
                    return null;
179
            }
180
    }
181

    
182
    /**
183
     * Creates a new FPopupMenu object.
184
     *
185
     * @param nodo DOCUMENT ME!
186
     * @param vista DOCUMENT ME!
187
     */
188
    public FPopupMenu(MapContext mc, DefaultMutableTreeNode node) {
189
        super();
190
        this.initialize(mc,node);
191
    }
192

    
193
    private void initialize(MapContext mc, DefaultMutableTreeNode node) {
194
        this.mapContext = mc;
195
        this.nodo = node;
196

    
197
        //salir = new MenuItem("Salir");
198
                this.extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get("View_TocActions");
199
                this.selecteds = this.mapContext.getLayers().getActives();
200

    
201
                IContextMenuAction[] actions = this.getActionList();
202

    
203
                this.createMenuElements(actions);
204

    
205
                this.loadOldStileOptions();
206

    
207

    
208
    }
209

    
210
    public MapContext getMapContext() { return mapContext; }
211

    
212
        public ITocItem getNodeUserObject() {
213
                if (nodo == null) return null;
214
                return (ITocItem)nodo.getUserObject();
215
        }
216

    
217
        public DefaultMutableTreeNode getNode() {
218
                return this.nodo;
219
        }
220

    
221
    private IContextMenuAction[] getActionList() {
222
                ArrayList actionArrayList = new ArrayList();
223
                Iterator iter = this.extensionPoint.keySet().iterator();
224
                AbstractTocContextMenuAction action;
225
                while (iter.hasNext()) {
226
                        action = null;
227
                        try {
228
                                action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
229
                        } catch (InstantiationException e) {
230
                                // TODO Auto-generated catch block
231
                                e.printStackTrace();
232
                        } catch (IllegalAccessException e) {
233
                                // TODO Auto-generated catch block
234
                                e.printStackTrace();
235
                        }
236
                        if (action != null && !(action instanceof OldTocContextMenuAction)) {
237
                                action.setMapContext(this.mapContext);
238
                                if (action.isVisible((ITocItem)this.getNodeUserObject(),this.selecteds)) {
239
                                        actionArrayList.add(action);
240
                                }
241
                        }
242

    
243
                }
244
                IContextMenuAction[] result = (IContextMenuAction[])Array.newInstance(IContextMenuAction.class,actionArrayList.size());
245
                System.arraycopy(actionArrayList.toArray(),0,result,0,actionArrayList.size());
246
                Arrays.sort(result,new CompareAction());
247

    
248
                return result;
249
    }
250

    
251
        public class CompareAction implements Comparator{
252
                public int compare(Object o1, Object o2) {
253
                        return this.compare((IContextMenuAction)o1,(IContextMenuAction)o2);
254
                }
255

    
256
                public int compare(IContextMenuAction o1, IContextMenuAction o2) {
257
                        //FIXME: flata formatear los enteros!!!!
258
                        NumberFormat formater = NumberFormat.getInstance();
259
                        formater.setMinimumIntegerDigits(3);
260
                        String key1= ""+formater.format(o1.getGroupOrder())+o1.getGroup()+formater.format(o1.getOrder());
261
                        String key2= ""+formater.format(o2.getGroupOrder())+o2.getGroup()+formater.format(o2.getOrder());
262
                        return key1.compareTo(key2);
263
                }
264
        }
265

    
266
        private void createMenuElements(IContextMenuAction[] actions) {
267
                String group = null;
268
                for (int i=0;i < actions.length;i++) {
269
                        IContextMenuAction action = actions[i];
270
                        MenuItem item = new MenuItem(action.getText(),action);
271
                        item.setFont(theFont);
272
                        item.setEnabled(action.isEnabled(this.getNodeUserObject(),this.selecteds));
273
                        if (!action.getGroup().equals(group)) {
274
                                if (group != null) this.addSeparator();
275
                                group = action.getGroup();
276
                        }
277
                        this.add(item);
278
                }
279

    
280
        }
281

    
282

    
283
        public class MenuItem extends JMenuItem implements ActionListener{
284
                private IContextMenuAction action;
285
                public MenuItem(String text,IContextMenuAction documentAction) {
286
                        super(text);
287
                        this.action = documentAction;
288
                        String tip = this.action.getDescription();
289
                        if (tip != null && tip.length() > 0) {
290
                                this.setToolTipText(tip);
291
                        }
292
                        this.addActionListener(this);
293
                }
294

    
295
                public void actionPerformed(ActionEvent e) {
296
                        this.action.execute(FPopupMenu.this.getNodeUserObject(), FPopupMenu.this.selecteds);
297
                }
298
        }
299

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

    
330

    
331
        }
332

    
333

    
334
}