Statistics
| Revision:

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

History | View | Annotate | Download (12.7 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
import java.util.List;
56

    
57
import javax.swing.JMenuItem;
58
import javax.swing.JPopupMenu;
59
import javax.swing.tree.DefaultMutableTreeNode;
60

    
61
import org.gvsig.fmap.swing.toc.TOC;
62
import org.gvsig.fmap.swing.toc.TOCFactory;
63
import org.gvsig.fmap.swing.toc.TOCLocator;
64
import org.gvsig.fmap.swing.toc.TOCManager;
65
import org.gvsig.fmap.swing.toc.action.TOCAction;
66
import org.gvsig.fmap.swing.toc.action.TOCActionAdapter;
67
import org.gvsig.tools.service.ServiceException;
68

    
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.cit.gvsig.TOCImplementationExtension;
71
import com.iver.cit.gvsig.fmap.MapContext;
72
import com.iver.cit.gvsig.fmap.ViewPort;
73
import com.iver.cit.gvsig.fmap.layers.FLayer;
74
import com.iver.cit.gvsig.project.documents.IContextMenuAction;
75
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
76
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
77
import com.iver.cit.gvsig.project.documents.view.toc.TocItemBranch;
78
import com.iver.cit.gvsig.project.documents.view.toc.TocMenuEntry;
79
import com.iver.cit.gvsig.project.documents.view.toc.actions.ChangeNameTocMenuEntry;
80
import com.iver.cit.gvsig.project.documents.view.toc.actions.CopyLayersTocMenuEntry;
81
import com.iver.cit.gvsig.project.documents.view.toc.actions.CutLayersTocMenuEntry;
82
import com.iver.cit.gvsig.project.documents.view.toc.actions.EliminarCapaTocMenuEntry;
83
import com.iver.cit.gvsig.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
84
import com.iver.cit.gvsig.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
85
import com.iver.cit.gvsig.project.documents.view.toc.actions.FirstLayerTocMenuEntry;
86
import com.iver.cit.gvsig.project.documents.view.toc.actions.LayersGroupTocMenuEntry;
87
import com.iver.cit.gvsig.project.documents.view.toc.actions.LayersUngroupTocMenuEntry;
88
import com.iver.cit.gvsig.project.documents.view.toc.actions.OldTocContextMenuAction;
89
import com.iver.cit.gvsig.project.documents.view.toc.actions.PasteLayersTocMenuEntry;
90
import com.iver.cit.gvsig.project.documents.view.toc.actions.ReloadLayerTocMenuEntry;
91
import com.iver.cit.gvsig.project.documents.view.toc.actions.ShowLayerErrorsTocMenuEntry;
92
import com.iver.cit.gvsig.project.documents.view.toc.actions.ZoomAlTemaTocMenuEntry;
93
import com.iver.cit.gvsig.project.documents.view.toc.util.TOCMenuItemComparator;
94
import com.iver.utiles.extensionPoints.ExtensionPoint;
95
import com.iver.utiles.extensionPoints.ExtensionPoints;
96
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
97

    
98
/**
99
 * Menu de bot?n derecho para el TOC.
100
 * Se pueden a?adir entradas facilmente desde una extensi?n,
101
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
102
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
103
 * (Las entradas actuales est?n hechas de esa manera).
104
 *
105
 * @author vcn To change the template for this generated type comment go to
106
 *         Window>Preferences>Java>Code Generation>Code and
107
 *         Comments
108
 */
109

    
110
public class FPopupMenu extends JPopupMenu {
111
        //private static ArrayList menuEntrys = new ArrayList();
112
    private DefaultMutableTreeNode nodo;
113
    protected MapContext mapContext;
114
    private TOC toc = null;
115
    private ExtensionPoint extensionPoint;
116
    private FLayer[] selecteds;
117
    //private JMenuItem capa;
118
    // Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
119
    // TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
120
    public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10);
121
    
122
    /**
123
     * @deprecated
124
     */
125
    public static void addEntry(TocMenuEntry entry) {
126

    
127
            OldTocContextMenuAction action = new OldTocContextMenuAction();
128
            action.setEntry(entry);
129
            System.out.println("Add old TocMenuEntry: " + entry);
130
            TOCManager tm = TOCLocator.getInstance().getTOCManager();
131
                tm.addServiceFactory(action);
132
    }
133

    
134
    /**
135
     * @deprecated
136
     */
137
    
138
    /*
139
    public static Object getEntry(String className, boolean[][] bb) {
140
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
141
            OldTocContextMenuAction action = null;
142
            try {
143
                        action = (OldTocContextMenuAction)((ExtensionPoint)extensionPoints.get("View_TocActions")).create(className);
144
                } catch (InstantiationException e) {
145
                        // TODO Auto-generated catch block
146
                        e.printStackTrace();
147
                } catch (IllegalAccessException e) {
148
                        // TODO Auto-generated catch block
149
                        e.printStackTrace();
150
                } catch (ClassCastException e) {
151
                        action = null;
152
                }
153
            if (action != null) {
154
                    return action.getEntry();
155
            } else {
156
                    return null;
157
            }
158
    }
159
    */
160

    
161
    /**
162
     * Creates a new FPopupMenu object.
163
     *
164
     * @param nodo DOCUMENT ME!
165
     * @param vista DOCUMENT ME!
166
     */
167
    public FPopupMenu(TOC _toc, MapContext mc, DefaultMutableTreeNode node) {
168
        super();
169
        toc = _toc;
170
        this.initialize(mc,node);
171
    }
172

    
173
    private void initialize(MapContext mc, DefaultMutableTreeNode node) {
174
        this.mapContext = mc;
175
        this.nodo = node;
176

    
177
        //salir = new MenuItem("Salir");
178
                extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get("View_TocActions");
179
                this.selecteds = this.mapContext.getLayers().getActives();
180

    
181
                List<TOCAction> actions = this.getActionList();
182
                if (actions == null){
183
                        return;
184
                }
185
                this.createMenuElements(actions);
186

    
187
                this.loadOldStileOptions();
188
    }
189

    
190
    public MapContext getMapContext() { return mapContext; }
191

    
192
        public ITocItem getNodeUserObject() {
193
                if (nodo == null) return null;
194
                return (ITocItem)nodo.getUserObject();
195
        }
196

    
197
        public DefaultMutableTreeNode getNode() {
198
                return this.nodo;
199
        }
200

    
201
    private List<TOCAction> getActionList() {
202
            
203
            TOCManager tm = TOCLocator.getInstance().getTOCManager();
204
                List<TOCAction> result_list = null;
205
                List<TOCAction> result_list_sort = null;
206
                
207
                try {
208
                        result_list = tm.getActions(toc);
209
                } catch (ServiceException se) {
210
                        NotificationManager.addError("While getting actions for fpopupmenu", se);
211
                        return null;
212
                }
213
                
214

    
215
            
216
            AbstractTocContextMenuAction action;
217
            boolean contains=false;
218
            ITocItem tocItem=(ITocItem)this.getNodeUserObject();
219
            if (tocItem instanceof TocItemBranch){
220
                    for (int i=0;i<this.selecteds.length;i++){
221
                            if (this.selecteds[i].equals(((TocItemBranch)tocItem).getLayer()))
222
                                    contains=true;
223
                    }
224
            }else{
225
                    contains=true;
226
            }
227
            
228
            if (contains){
229

    
230
                    result_list_sort = new ArrayList<TOCAction>();
231
                    TOCAction[] ta_array = (TOCAction[]) result_list.toArray(new TOCAction[0]);
232
                    Arrays.sort(ta_array, new TOCMenuItemComparator());
233
                    int len = ta_array.length;
234
                    for (int i=0; i<len; i++) {
235
                            result_list_sort.add(ta_array[i]);
236
                    }
237
                    return result_list_sort;
238

    
239
                    
240
                    //                    Iterator iter = this.extensionPoint.keySet().iterator();
241
//                    while (iter.hasNext()) {
242
//                            action = null;
243
//                            try {
244
//                                    action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
245
//                            } catch (InstantiationException e) {
246
//                                    // TODO Auto-generated catch block
247
//                                    e.printStackTrace();
248
//                            } catch (IllegalAccessException e) {
249
//                                    // TODO Auto-generated catch block
250
//                                    e.printStackTrace();
251
//                            }
252
//                            if (action != null && !(action instanceof OldTocContextMenuAction)) {
253
//                                    action.setMapContext(this.mapContext);
254
//                                    if (action.isVisible((ITocItem)this.getNodeUserObject(),this.selecteds)) {
255
//                                            actionArrayList.add(action);
256
//                                    }
257
//                            }
258
//
259
//                    }
260
//                    IContextMenuAction[] result = (IContextMenuAction[])Array.newInstance(IContextMenuAction.class,actionArrayList.size());
261
//                    System.arraycopy(actionArrayList.toArray(),0,result,0,actionArrayList.size());
262
//                    Arrays.sort(result,new CompareAction());
263
//                    return result;
264
            }
265
            return null;
266

    
267
    }
268

    
269
    /*
270
        public class CompareAction implements Comparator{
271
                public int compare(Object o1, Object o2) {
272
                        return this.compare((IContextMenuAction)o1,(IContextMenuAction)o2);
273
                }
274

275
                public int compare(IContextMenuAction o1, IContextMenuAction o2) {
276
                        //FIXME: flata formatear los enteros!!!!
277
                        NumberFormat formater = NumberFormat.getInstance();
278
                        formater.setMinimumIntegerDigits(3);
279
                        String key1= ""+formater.format(o1.getGroupOrder())+o1.getGroup()+formater.format(o1.getOrder());
280
                        String key2= ""+formater.format(o2.getGroupOrder())+o2.getGroup()+formater.format(o2.getOrder());
281
                        return key1.compareTo(key2);
282
                }
283
        }
284
        */
285

    
286
        private void createMenuElements(List<TOCAction> actions) {
287
                
288
                String group = null;
289
                String item_group = null;
290
                TOCAction action = null;
291
                
292
                for (int i=0;i < actions.size();i++) {
293
                        
294
                        action = actions.get(i);
295
                        
296
                        if (!action.isVisible() ||
297
                                        (action instanceof TOCActionAdapter &&
298
                                                        ((TOCActionAdapter) action).getContextMenuAction() instanceof OldTocContextMenuAction)) {
299
                                if (((action instanceof TOCActionAdapter) && ((TOCActionAdapter) action).getContextMenuAction() instanceof OldTocContextMenuAction)) {
300
                                        OldTocContextMenuAction oldAct = (OldTocContextMenuAction) ((TOCActionAdapter) action).getContextMenuAction();
301
                                        oldAct.getEntry().initialize(this);
302
                                }
303
                                continue;
304
                        }
305
                        
306
                        JMenuItem item = new JMenuItem(action);
307
                        item.setFont(theFont);
308
                        item.setEnabled(action.isEnabled());
309
                        item_group = action.getValue(TOCAction.GROUP).toString();
310
                        if (group == null || item_group.compareToIgnoreCase(group) != 0) {
311
                                if (group != null) this.addSeparator();
312
                                group = item_group;
313
                        }
314
                        this.add(item);
315
                }
316
                
317
                // comprobamos si el ultimo elemento es un seprardor
318
                // not here
319
                /*
320
                if (this.getComponentCount()>0 && this.getComponent(this.getComponentCount()-1) instanceof Separator) {
321
                        //Si lo es lo eliminamos
322
                        this.remove(this.getComponentCount()-1);
323
                }
324
                */
325

    
326

    
327
        }
328

    
329

    
330
        /*
331
        public class MenuItem extends JMenuItem implements ActionListener{
332
                private IContextMenuAction action;
333
                public MenuItem(String text,IContextMenuAction documentAction) {
334
                        super(text);
335
                        this.action = documentAction;
336
                        String tip = this.action.getDescription();
337
                        if (tip != null && tip.length() > 0) {
338
                                this.setToolTipText(tip);
339
                        }
340
                        this.addActionListener(this);
341
                }
342

343
                public void actionPerformed(ActionEvent e) {
344
                        this.action.execute(FPopupMenu.this.getNodeUserObject(), FPopupMenu.this.selecteds);
345
                }
346
        }
347
        */
348

    
349
        private void loadOldStileOptions() {
350
                boolean first = true;
351
                Iterator iter = this.extensionPoint.keySet().iterator();
352
                AbstractTocContextMenuAction action;
353
                while (iter.hasNext()) {
354
                        action = null;
355
                        try {
356
                                action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
357
                        } catch (InstantiationException e) {
358
                                // TODO Auto-generated catch block
359
                                e.printStackTrace();
360
                        } catch (IllegalAccessException e) {
361
                                // TODO Auto-generated catch block
362
                                e.printStackTrace();
363
                        }
364
                        if (action != null && (action instanceof OldTocContextMenuAction)) {
365
                                if (first) {
366
                                        this.addSeparator();
367
                                        first = false;
368
                                }
369
                                action.setMapContext(this.mapContext);
370
                                ((OldTocContextMenuAction)action).initializeElement(this);
371
                        }
372
                }
373
                //comprobamos si el ultimo elemento es un seprardor
374
                if (this.getComponentCount()>0 && this.getComponent(this.getComponentCount()-1) instanceof Separator) {
375
                        //Si lo es lo eliminamos
376
                        this.remove(this.getComponentCount()-1);
377
                }
378

    
379

    
380
        }
381

    
382

    
383
}