Statistics
| Revision:

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

History | View | Annotate | Download (10.9 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.util.ArrayList;
47
import java.util.Arrays;
48
import java.util.Iterator;
49
import java.util.List;
50

    
51
import javax.swing.JMenuItem;
52
import javax.swing.JPopupMenu;
53
import javax.swing.tree.DefaultMutableTreeNode;
54

    
55
import org.gvsig.fmap.swing.toc.TOC;
56
import org.gvsig.fmap.swing.toc.TOCLocator;
57
import org.gvsig.fmap.swing.toc.TOCManager;
58
import org.gvsig.fmap.swing.toc.action.TOCAction;
59
import org.gvsig.fmap.swing.toc.action.TOCActionAdapter;
60
import org.gvsig.tools.service.ServiceException;
61

    
62
import com.iver.andami.messages.NotificationManager;
63
import com.iver.cit.gvsig.fmap.MapContext;
64
import com.iver.cit.gvsig.fmap.layers.FLayer;
65
import com.iver.cit.gvsig.project.documents.view.toc.AbstractTocContextMenuAction;
66
import com.iver.cit.gvsig.project.documents.view.toc.ITocItem;
67
import com.iver.cit.gvsig.project.documents.view.toc.TocItemBranch;
68
import com.iver.cit.gvsig.project.documents.view.toc.TocMenuEntry;
69
import com.iver.cit.gvsig.project.documents.view.toc.actions.OldTocContextMenuAction;
70
import com.iver.cit.gvsig.project.documents.view.toc.util.TOCMenuItemComparator;
71
import com.iver.utiles.extensionPoints.ExtensionPoint;
72
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
73

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

    
86
public class FPopupMenu extends JPopupMenu {
87
        //private static ArrayList menuEntrys = new ArrayList();
88
    private DefaultMutableTreeNode nodo;
89
    protected MapContext mapContext;
90
    private TOC toc = null;
91
    private ExtensionPoint extensionPoint;
92
    private FLayer[] selecteds;
93
    //private JMenuItem capa;
94

    
95

    
96
    /**
97
     * @deprecated
98
     */
99
    public static void addEntry(TocMenuEntry entry) {
100

    
101
            OldTocContextMenuAction action = new OldTocContextMenuAction();
102
            action.setEntry(entry);
103
            System.out.println("Add old TocMenuEntry: " + entry);
104
            TOCManager tm = TOCLocator.getInstance().getTOCManager();
105
                tm.addServiceFactory(action);
106
    }
107

    
108
    /**
109
     * @deprecated
110
     */
111

    
112
    /*
113
    public static Object getEntry(String className, boolean[][] bb) {
114
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
115
            OldTocContextMenuAction action = null;
116
            try {
117
                        action = (OldTocContextMenuAction)((ExtensionPoint)extensionPoints.get("View_TocActions")).create(className);
118
                } catch (InstantiationException e) {
119
                        // TODO Auto-generated catch block
120
                        e.printStackTrace();
121
                } catch (IllegalAccessException e) {
122
                        // TODO Auto-generated catch block
123
                        e.printStackTrace();
124
                } catch (ClassCastException e) {
125
                        action = null;
126
                }
127
            if (action != null) {
128
                    return action.getEntry();
129
            } else {
130
                    return null;
131
            }
132
    }
133
    */
134

    
135
    /**
136
     * Creates a new FPopupMenu object.
137
     *
138
     * @param nodo DOCUMENT ME!
139
     * @param vista DOCUMENT ME!
140
     */
141
    public FPopupMenu(TOC _toc, MapContext mc, DefaultMutableTreeNode node) {
142
        super();
143
        toc = _toc;
144
        this.initialize(mc,node);
145
    }
146

    
147
    private void initialize(MapContext mc, DefaultMutableTreeNode node) {
148
        this.mapContext = mc;
149
        this.nodo = node;
150

    
151
        //salir = new MenuItem("Salir");
152
                extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get("View_TocActions");
153
                this.selecteds = this.mapContext.getLayers().getActives();
154

    
155
                List<TOCAction> actions = this.getActionList();
156
                if (actions == null){
157
                        return;
158
                }
159
                this.createMenuElements(actions);
160

    
161
                this.loadOldStileOptions();
162
    }
163

    
164
    public MapContext getMapContext() { return mapContext; }
165

    
166
        public ITocItem getNodeUserObject() {
167
                if (nodo == null) {
168
                    return null;
169
                }
170
                return (ITocItem)nodo.getUserObject();
171
        }
172

    
173
        public DefaultMutableTreeNode getNode() {
174
                return this.nodo;
175
        }
176

    
177
    private List<TOCAction> getActionList() {
178

    
179
            TOCManager tm = TOCLocator.getInstance().getTOCManager();
180
                List<TOCAction> result_list = null;
181
                List<TOCAction> result_list_sort = null;
182

    
183
                try {
184
                        result_list = tm.getActions(toc);
185
                } catch (ServiceException se) {
186
                        NotificationManager.addError("While getting actions for fpopupmenu", se);
187
                        return null;
188
                }
189

    
190

    
191

    
192
            AbstractTocContextMenuAction action;
193
            boolean contains=false;
194
            ITocItem tocItem=(ITocItem)this.getNodeUserObject();
195
            if (tocItem instanceof TocItemBranch){
196
                    for (int i=0;i<this.selecteds.length;i++){
197
                            if (this.selecteds[i].equals(((TocItemBranch)tocItem).getLayer())) {
198
                            contains=true;
199
                        }
200
                    }
201
            }else{
202
                    contains=true;
203
            }
204

    
205
            if (contains){
206

    
207
                    result_list_sort = new ArrayList<TOCAction>();
208
                    TOCAction[] ta_array = (TOCAction[]) result_list.toArray(new TOCAction[0]);
209
                    Arrays.sort(ta_array, new TOCMenuItemComparator());
210
                    int len = ta_array.length;
211
                    for (int i=0; i<len; i++) {
212
                            result_list_sort.add(ta_array[i]);
213
                    }
214
                    return result_list_sort;
215

    
216

    
217
                    //                    Iterator iter = this.extensionPoint.keySet().iterator();
218
//                    while (iter.hasNext()) {
219
//                            action = null;
220
//                            try {
221
//                                    action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
222
//                            } catch (InstantiationException e) {
223
//                                    // TODO Auto-generated catch block
224
//                                    e.printStackTrace();
225
//                            } catch (IllegalAccessException e) {
226
//                                    // TODO Auto-generated catch block
227
//                                    e.printStackTrace();
228
//                            }
229
//                            if (action != null && !(action instanceof OldTocContextMenuAction)) {
230
//                                    action.setMapContext(this.mapContext);
231
//                                    if (action.isVisible((ITocItem)this.getNodeUserObject(),this.selecteds)) {
232
//                                            actionArrayList.add(action);
233
//                                    }
234
//                            }
235
//
236
//                    }
237
//                    IContextMenuAction[] result = (IContextMenuAction[])Array.newInstance(IContextMenuAction.class,actionArrayList.size());
238
//                    System.arraycopy(actionArrayList.toArray(),0,result,0,actionArrayList.size());
239
//                    Arrays.sort(result,new CompareAction());
240
//                    return result;
241
            }
242
            return null;
243

    
244
    }
245

    
246
    /*
247
        public class CompareAction implements Comparator{
248
                public int compare(Object o1, Object o2) {
249
                        return this.compare((IContextMenuAction)o1,(IContextMenuAction)o2);
250
                }
251

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

    
263
        private void createMenuElements(List<TOCAction> actions) {
264

    
265
                String group = null;
266
                String item_group = null;
267
                TOCAction action = null;
268

    
269
                for (int i=0;i < actions.size();i++) {
270

    
271
                        action = actions.get(i);
272

    
273
                        if (!action.isVisible() ||
274
                                        (action instanceof TOCActionAdapter &&
275
                                                        ((TOCActionAdapter) action).getContextMenuAction() instanceof OldTocContextMenuAction)) {
276
                                if (((action instanceof TOCActionAdapter) && ((TOCActionAdapter) action).getContextMenuAction() instanceof OldTocContextMenuAction)) {
277
                                        OldTocContextMenuAction oldAct = (OldTocContextMenuAction) ((TOCActionAdapter) action).getContextMenuAction();
278
                                        oldAct.getEntry().initialize(this);
279
                                }
280
                                continue;
281
                        }
282

    
283
                        JMenuItem item = new JMenuItem(action);
284
            // item.setFont(theFont);
285
                        item.setEnabled(action.isEnabled());
286
                        item_group = action.getValue(TOCAction.GROUP).toString();
287
                        if (group == null || item_group.compareToIgnoreCase(group) != 0) {
288
                                if (group != null) {
289
                                    this.addSeparator();
290
                                }
291
                                group = item_group;
292
                        }
293
                        this.add(item);
294
                }
295

    
296
                // comprobamos si el ultimo elemento es un seprardor
297
                // not here
298
                /*
299
                if (this.getComponentCount()>0 && this.getComponent(this.getComponentCount()-1) instanceof Separator) {
300
                        //Si lo es lo eliminamos
301
                        this.remove(this.getComponentCount()-1);
302
                }
303
                */
304

    
305

    
306
        }
307

    
308

    
309
        /*
310
        public class MenuItem extends JMenuItem implements ActionListener{
311
                private IContextMenuAction action;
312
                public MenuItem(String text,IContextMenuAction documentAction) {
313
                        super(text);
314
                        this.action = documentAction;
315
                        String tip = this.action.getDescription();
316
                        if (tip != null && tip.length() > 0) {
317
                                this.setToolTipText(tip);
318
                        }
319
                        this.addActionListener(this);
320
                }
321

322
                public void actionPerformed(ActionEvent e) {
323
                        this.action.execute(FPopupMenu.this.getNodeUserObject(), FPopupMenu.this.selecteds);
324
                }
325
        }
326
        */
327

    
328
        private void loadOldStileOptions() {
329
                boolean first = true;
330
                Iterator iter = this.extensionPoint.keySet().iterator();
331
                AbstractTocContextMenuAction action;
332
                while (iter.hasNext()) {
333
                        action = null;
334
                        try {
335
                                action = (AbstractTocContextMenuAction)this.extensionPoint.create((String)iter.next());
336
                        } catch (InstantiationException e) {
337
                                // TODO Auto-generated catch block
338
                                e.printStackTrace();
339
                        } catch (IllegalAccessException e) {
340
                                // TODO Auto-generated catch block
341
                                e.printStackTrace();
342
                        }
343
                        if (action != null && (action instanceof OldTocContextMenuAction)) {
344
                                if (first) {
345
                                        this.addSeparator();
346
                                        first = false;
347
                                }
348
                                action.setMapContext(this.mapContext);
349
                                ((OldTocContextMenuAction)action).initializeElement(this);
350
                        }
351
                }
352
                //comprobamos si el ultimo elemento es un seprardor
353
                if (this.getComponentCount()>0 && this.getComponent(this.getComponentCount()-1) instanceof Separator) {
354
                        //Si lo es lo eliminamos
355
                        this.remove(this.getComponentCount()-1);
356
                }
357

    
358

    
359
        }
360

    
361

    
362
}