Statistics
| Revision:

root / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / gui / FPopupMenu.java @ 11407

History | View | Annotate | Download (12.2 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.cit.gvsig.project.documents.view.toc.actions.ZoomPixelCursorTocMenuEntry;
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
            extensionPoints.add("View_TocActions","FSymbolChangeColor",new FSymbolChangeColorTocMenuEntry());
111
            extensionPoints.add("View_TocActions","ChangeName",new ChangeNameTocMenuEntry());
112
            extensionPoints.add("View_TocActions","FLyrVectEditProperties",new FLyrVectEditPropertiesTocMenuEntry());
113
            extensionPoints.add("View_TocActions","ZoomAlTema",new ZoomAlTemaTocMenuEntry());
114
            extensionPoints.add("View_TocActions","ZoomPixelCursor",new ZoomPixelCursorTocMenuEntry());
115
            extensionPoints.add("View_TocActions","EliminarCapa",new EliminarCapaTocMenuEntry());
116
            extensionPoints.add("View_TocActions","VerErroresCapa",new ShowLayerErrorsTocMenuEntry());
117
            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

    
122
               extensionPoints.add("View_TocActions","Copy",new CopyLayersTocMenuEntry());
123
               extensionPoints.add("View_TocActions","Cut",new CutLayersTocMenuEntry());
124
               extensionPoints.add("View_TocActions","Paste",new PasteLayersTocMenuEntry());
125

    
126

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

134

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

138

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

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

145

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

    
151

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

    
157
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
158

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

    
165
    }
166

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

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

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

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

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

    
212
                this.createMenuElements(actions);
213

    
214
                this.loadOldStileOptions();
215

    
216

    
217
    }
218

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

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

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

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

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

    
257
                return result;
258
    }
259

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

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

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

    
289
        }
290

    
291

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

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

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

    
339

    
340
        }
341

    
342

    
343
}