Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toc / gui / FPopupMenu.java @ 41081

History | View | Annotate | Download (9.89 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.view.toc.gui;
25

    
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.lang.reflect.Array;
29
import java.text.NumberFormat;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.Comparator;
33
import java.util.Iterator;
34

    
35
import javax.swing.JMenuItem;
36
import javax.swing.JPopupMenu;
37
import javax.swing.tree.DefaultMutableTreeNode;
38

    
39
import org.gvsig.andami.messages.NotificationManager;
40
import org.gvsig.app.project.documents.view.IContextMenuAction;
41
import org.gvsig.app.project.documents.view.toc.AbstractTocContextMenuAction;
42
import org.gvsig.app.project.documents.view.toc.ITocItem;
43
import org.gvsig.app.project.documents.view.toc.TocItemBranch;
44
import org.gvsig.app.project.documents.view.toc.actions.ChangeNameTocMenuEntry;
45
import org.gvsig.app.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
46
import org.gvsig.app.project.documents.view.toc.actions.CopyLayersTocMenuEntry;
47
import org.gvsig.app.project.documents.view.toc.actions.CutLayersTocMenuEntry;
48
import org.gvsig.app.project.documents.view.toc.actions.EliminarCapaTocMenuEntry;
49
import org.gvsig.app.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
50
import org.gvsig.app.project.documents.view.toc.actions.FirstLayerTocMenuEntry;
51
import org.gvsig.app.project.documents.view.toc.actions.LayersGroupTocMenuEntry;
52
import org.gvsig.app.project.documents.view.toc.actions.LayersUngroupTocMenuEntry;
53
import org.gvsig.app.project.documents.view.toc.actions.OldTocContextMenuAction;
54
import org.gvsig.app.project.documents.view.toc.actions.PasteLayersTocMenuEntry;
55
import org.gvsig.app.project.documents.view.toc.actions.ReloadLayerTocMenuEntry;
56
import org.gvsig.app.project.documents.view.toc.actions.ShowLayerErrorsTocMenuEntry;
57
import org.gvsig.app.project.documents.view.toc.actions.ZoomAlTemaTocMenuEntry;
58
import org.gvsig.fmap.mapcontext.MapContext;
59
import org.gvsig.fmap.mapcontext.layers.FLayer;
60
import org.gvsig.tools.ToolsLocator;
61
import org.gvsig.tools.extensionpoint.ExtensionPoint;
62
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
63

    
64
/**
65
 * Menu de bot?n derecho para el TOC.
66
 * Se pueden a?adir entradas facilmente desde una extensi?n,
67
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
68
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
69
 * (Las entradas actuales est?n hechas de esa manera).
70
 * 
71
 * @author vcn To change the template for this generated type comment go to
72
 *         Window>Preferences>Java>Code Generation>Code and
73
 *         Comments
74
 */
75

    
76
public class FPopupMenu extends JPopupMenu {
77

    
78
    private static final long serialVersionUID = -7420923229618207434L;
79
    private DefaultMutableTreeNode nodo;
80
    protected MapContext mapContext;
81
    private ExtensionPoint extensionPoint;
82
    private FLayer[] selecteds;
83

    
84
    public static void registerExtensionPoint() {
85
        ExtensionPoint exPoint =
86
            ToolsLocator.getExtensionPointManager().add("View_TocActions", "");
87
        exPoint
88
            .append("FSymbolChangeColor", "", new ChangeSymbolTocMenuEntry());
89
        exPoint.append("ChangeName", "", new ChangeNameTocMenuEntry());
90
        exPoint.append("FLyrVectEditProperties", "",
91
            new FLyrVectEditPropertiesTocMenuEntry());
92
        exPoint.append("ZoomAlTema", "", new ZoomAlTemaTocMenuEntry());
93
        exPoint.append("EliminarCapa", "", new EliminarCapaTocMenuEntry());
94
        exPoint.append("VerErroresCapa", "", new ShowLayerErrorsTocMenuEntry());
95
        exPoint.append("ReloadLayer", "", new ReloadLayerTocMenuEntry());
96
        exPoint.append("LayersGroup", "", new LayersGroupTocMenuEntry());
97
        exPoint.append("LayersUngroup", "", new LayersUngroupTocMenuEntry());
98
        exPoint.append("FirstLayer", "", new FirstLayerTocMenuEntry());
99

    
100
        exPoint.append("Copy", "", new CopyLayersTocMenuEntry());
101
        exPoint.append("Cut", "", new CutLayersTocMenuEntry());
102
        exPoint.append("Paste", "", new PasteLayersTocMenuEntry());
103
    }
104

    
105
    /**
106
     * Creates a new FPopupMenu object.
107
     * 
108
     * @param nodo
109
     *            DOCUMENT ME!
110
     * @param vista
111
     *            DOCUMENT ME!
112
     */
113
    public FPopupMenu(MapContext mc, DefaultMutableTreeNode node) {
114
        super();
115
        this.initialize(mc, node);
116
    }
117

    
118
    private void initialize(MapContext mc, DefaultMutableTreeNode node) {
119
        this.mapContext = mc;
120
        this.nodo = node;
121

    
122
        this.extensionPoint =
123
            ToolsLocator.getExtensionPointManager().get("View_TocActions");
124
        this.selecteds = this.mapContext.getLayers().getActives();
125

    
126
        IContextMenuAction[] actions = this.getActionList();
127
        if (actions == null) {
128
            return;
129
        }
130
        this.createMenuElements(actions);
131
    }
132

    
133
    public MapContext getMapContext() {
134
        return mapContext;
135
    }
136

    
137
    public ITocItem getNodeUserObject() {
138
        if (nodo == null) {
139
            return null;
140
        }
141
        return (ITocItem) nodo.getUserObject();
142
    }
143

    
144
    public DefaultMutableTreeNode getNode() {
145
        return this.nodo;
146
    }
147

    
148
    private IContextMenuAction[] getActionList() {
149
        ArrayList<IContextMenuAction> actionArrayList =
150
            new ArrayList<IContextMenuAction>();
151
        @SuppressWarnings("unchecked")
152
        Iterator<Extension> iter = this.extensionPoint.iterator();
153
        IContextMenuAction action;
154
        boolean contains = false;
155
        ITocItem tocItem = this.getNodeUserObject();
156
        if (tocItem instanceof TocItemBranch) {
157
            for (int i = 0; i < this.selecteds.length; i++) {
158
                if (this.selecteds[i].equals(((TocItemBranch) tocItem)
159
                    .getLayer())) {
160
                    contains = true;
161
                    break;
162
                }
163
            }
164
        } else {
165
            contains = true;
166
        }
167
        if (contains) {
168
            while (iter.hasNext()) {
169
                action = null;
170
                try {
171
                    action = (IContextMenuAction) iter.next().create();
172
                } catch (Exception e) {
173
                    NotificationManager.addError(e);
174
                }
175
                if (action != null ) {
176
                        if( action instanceof AbstractTocContextMenuAction 
177
                                && !(action instanceof OldTocContextMenuAction)) {
178
                                ((AbstractTocContextMenuAction)action).setMapContext(this.mapContext);
179
                        }
180
                    if (action.isVisible(this.getNodeUserObject(),
181
                        this.selecteds)) {
182
                        actionArrayList.add(action);
183
                    }
184
                }
185

    
186
            }
187
            IContextMenuAction[] result =
188
                (IContextMenuAction[]) Array.newInstance(
189
                    IContextMenuAction.class, actionArrayList.size());
190
            System.arraycopy(actionArrayList.toArray(), 0, result, 0,
191
                actionArrayList.size());
192
            Arrays.sort(result, new CompareAction());
193

    
194
            return result;
195
        }
196
        return null;
197
    }
198

    
199
    public class CompareAction implements Comparator<IContextMenuAction> {
200

    
201
        public int compare(IContextMenuAction o1, IContextMenuAction o2) {
202
            // FIXME: flata formatear los enteros!!!!
203
            NumberFormat formater = NumberFormat.getInstance();
204
            formater.setMinimumIntegerDigits(3);
205
            String key1 =
206
                "" + formater.format(o1.getGroupOrder()) + o1.getGroup()
207
                    + formater.format(o1.getOrder());
208
            String key2 =
209
                "" + formater.format(o2.getGroupOrder()) + o2.getGroup()
210
                    + formater.format(o2.getOrder());
211
            return key1.compareTo(key2);
212
        }
213
    }
214

    
215
    private void createMenuElements(IContextMenuAction[] actions) {
216
        String group = null;
217
        for (int i = 0; i < actions.length; i++) {
218
            IContextMenuAction action = actions[i];
219
            MenuItem item = new MenuItem(action.getText(), action);
220
            item.setEnabled(action.isEnabled(this.getNodeUserObject(),
221
                this.selecteds));
222
            if( action.getGroup()!=null ) {
223
                    if ( !action.getGroup().equals(group)) {
224
                        if (group != null) {
225
                            this.addSeparator();
226
                        }
227
                        group = action.getGroup();
228
                    }
229
            }
230
            this.add(item);
231
        }
232

    
233
    }
234

    
235
    public class MenuItem extends JMenuItem implements ActionListener {
236

    
237
        private static final long serialVersionUID = 6858724610206253411L;
238
        private IContextMenuAction action;
239

    
240
        public MenuItem(String text, IContextMenuAction documentAction) {
241
            super(text);
242
            this.action = documentAction;
243
            String tip = this.action.getDescription();
244
            if (tip != null && tip.length() > 0) {
245
                this.setToolTipText(tip);
246
            }
247
            this.addActionListener(this);
248
        }
249

    
250
        public void actionPerformed(ActionEvent e) {
251
            this.action.execute(FPopupMenu.this.getNodeUserObject(),
252
                FPopupMenu.this.selecteds);
253
        }
254
    }
255

    
256
}