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 @ 44601

History | View | Annotate | Download (10.2 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40558 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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.text.NumberFormat;
29
import java.util.ArrayList;
30 42514 jjdelcerro
import java.util.Collections;
31 40435 jjdelcerro
import java.util.Comparator;
32
import java.util.Iterator;
33 42514 jjdelcerro
import java.util.List;
34 40435 jjdelcerro
35
import javax.swing.JMenuItem;
36
import javax.swing.JPopupMenu;
37
import javax.swing.tree.DefaultMutableTreeNode;
38
39 42548 dmartinezizquierdo
import org.gvsig.andami.PluginServices;
40 40435 jjdelcerro
import org.gvsig.andami.messages.NotificationManager;
41 41104 jjdelcerro
import org.gvsig.app.project.ProjectManager;
42 40435 jjdelcerro
import org.gvsig.app.project.documents.view.IContextMenuAction;
43 42505 jjdelcerro
import org.gvsig.app.project.documents.view.IContextMenuActionWithIcon;
44 41104 jjdelcerro
import org.gvsig.app.project.documents.view.ViewManager;
45 40435 jjdelcerro
import org.gvsig.app.project.documents.view.toc.AbstractTocContextMenuAction;
46
import org.gvsig.app.project.documents.view.toc.ITocItem;
47
import org.gvsig.app.project.documents.view.toc.TocItemBranch;
48
import org.gvsig.app.project.documents.view.toc.actions.ChangeNameTocMenuEntry;
49
import org.gvsig.app.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
50
import org.gvsig.app.project.documents.view.toc.actions.CopyLayersTocMenuEntry;
51
import org.gvsig.app.project.documents.view.toc.actions.CutLayersTocMenuEntry;
52
import org.gvsig.app.project.documents.view.toc.actions.EliminarCapaTocMenuEntry;
53
import org.gvsig.app.project.documents.view.toc.actions.FirstLayerTocMenuEntry;
54
import org.gvsig.app.project.documents.view.toc.actions.LayersGroupTocMenuEntry;
55
import org.gvsig.app.project.documents.view.toc.actions.LayersUngroupTocMenuEntry;
56
import org.gvsig.app.project.documents.view.toc.actions.OldTocContextMenuAction;
57
import org.gvsig.app.project.documents.view.toc.actions.PasteLayersTocMenuEntry;
58
import org.gvsig.app.project.documents.view.toc.actions.ReloadLayerTocMenuEntry;
59
import org.gvsig.app.project.documents.view.toc.actions.ShowLayerErrorsTocMenuEntry;
60
import org.gvsig.app.project.documents.view.toc.actions.ZoomAlTemaTocMenuEntry;
61
import org.gvsig.fmap.mapcontext.MapContext;
62
import org.gvsig.fmap.mapcontext.layers.FLayer;
63
import org.gvsig.tools.ToolsLocator;
64
import org.gvsig.tools.extensionpoint.ExtensionPoint;
65
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
66
67
/**
68
 * Menu de bot?n derecho para el TOC.
69
 * Se pueden a?adir entradas facilmente desde una extensi?n,
70
 * creando una clase derivando de TocMenuEntry, y a?adiendola en
71
 * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
72
 * (Las entradas actuales est?n hechas de esa manera).
73 42548 dmartinezizquierdo
 *
74 40435 jjdelcerro
 * @author vcn To change the template for this generated type comment go to
75
 *         Window>Preferences>Java>Code Generation>Code and
76
 *         Comments
77
 */
78
79
public class FPopupMenu extends JPopupMenu {
80
81
    private static final long serialVersionUID = -7420923229618207434L;
82
    private DefaultMutableTreeNode nodo;
83
    protected MapContext mapContext;
84
    private ExtensionPoint extensionPoint;
85
    private FLayer[] selecteds;
86
87
    public static void registerExtensionPoint() {
88 42548 dmartinezizquierdo
            ProjectManager projectManager = ProjectManager.getInstance();
89 41104 jjdelcerro
            ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME);
90 40435 jjdelcerro
91 41104 jjdelcerro
            viewManager.addTOCContextAction("FSymbolChangeColor", new ChangeSymbolTocMenuEntry());
92
        viewManager.addTOCContextAction("ChangeName", new ChangeNameTocMenuEntry());
93 42175 jjdelcerro
//        viewManager.addTOCContextAction("FLyrVectEditProperties", new FLyrVectEditPropertiesTocMenuEntry());
94 41104 jjdelcerro
        viewManager.addTOCContextAction("ZoomAlTema", new ZoomAlTemaTocMenuEntry());
95
        viewManager.addTOCContextAction("EliminarCapa", new EliminarCapaTocMenuEntry());
96
        viewManager.addTOCContextAction("VerErroresCapa", new ShowLayerErrorsTocMenuEntry());
97
        viewManager.addTOCContextAction("ReloadLayer", new ReloadLayerTocMenuEntry());
98
        viewManager.addTOCContextAction("LayersGroup", new LayersGroupTocMenuEntry());
99
        viewManager.addTOCContextAction("LayersUngroup", new LayersUngroupTocMenuEntry());
100
        viewManager.addTOCContextAction("FirstLayer", new FirstLayerTocMenuEntry());
101
102
        viewManager.addTOCContextAction("Copy", new CopyLayersTocMenuEntry());
103
        viewManager.addTOCContextAction("Cut", new CutLayersTocMenuEntry());
104
        viewManager.addTOCContextAction("Paste", new PasteLayersTocMenuEntry());
105 40435 jjdelcerro
    }
106
107
    /**
108
     * Creates a new FPopupMenu object.
109 42548 dmartinezizquierdo
     *
110 40435 jjdelcerro
     * @param nodo
111
     *            DOCUMENT ME!
112
     * @param vista
113
     *            DOCUMENT ME!
114
     */
115
    public FPopupMenu(MapContext mc, DefaultMutableTreeNode node) {
116
        super();
117
        this.initialize(mc, node);
118
    }
119
120
    private void initialize(MapContext mc, DefaultMutableTreeNode node) {
121
        this.mapContext = mc;
122
        this.nodo = node;
123
124
        this.extensionPoint =
125
            ToolsLocator.getExtensionPointManager().get("View_TocActions");
126
        this.selecteds = this.mapContext.getLayers().getActives();
127 44601 omartinez
128 42514 jjdelcerro
        List<IContextMenuAction> actions = this.getActionList();
129 40435 jjdelcerro
        if (actions == null) {
130
            return;
131
        }
132
        this.createMenuElements(actions);
133
    }
134
135
    public MapContext getMapContext() {
136
        return mapContext;
137
    }
138
139
    public ITocItem getNodeUserObject() {
140
        if (nodo == null) {
141
            return null;
142
        }
143
        return (ITocItem) nodo.getUserObject();
144
    }
145
146
    public DefaultMutableTreeNode getNode() {
147
        return this.nodo;
148
    }
149
150 42514 jjdelcerro
    private List<IContextMenuAction> getActionList() {
151
        List<IContextMenuAction> actions =  new ArrayList<IContextMenuAction>();
152 40435 jjdelcerro
        @SuppressWarnings("unchecked")
153
        Iterator<Extension> iter = this.extensionPoint.iterator();
154 41081 jjdelcerro
        IContextMenuAction action;
155 40435 jjdelcerro
        boolean contains = false;
156
        ITocItem tocItem = this.getNodeUserObject();
157
        if (tocItem instanceof TocItemBranch) {
158
            for (int i = 0; i < this.selecteds.length; i++) {
159
                if (this.selecteds[i].equals(((TocItemBranch) tocItem)
160
                    .getLayer())) {
161
                    contains = true;
162
                    break;
163
                }
164
            }
165
        } else {
166
            contains = true;
167
        }
168
        if (contains) {
169
            while (iter.hasNext()) {
170
                action = null;
171
                try {
172 41081 jjdelcerro
                    action = (IContextMenuAction) iter.next().create();
173 40435 jjdelcerro
                } catch (Exception e) {
174
                    NotificationManager.addError(e);
175
                }
176 41081 jjdelcerro
                if (action != null ) {
177 42548 dmartinezizquierdo
                        if( action instanceof AbstractTocContextMenuAction
178 41081 jjdelcerro
                                && !(action instanceof OldTocContextMenuAction)) {
179
                                ((AbstractTocContextMenuAction)action).setMapContext(this.mapContext);
180
                        }
181 40435 jjdelcerro
                    if (action.isVisible(this.getNodeUserObject(),
182
                        this.selecteds)) {
183 42514 jjdelcerro
                        actions.add(action);
184 40435 jjdelcerro
                    }
185
                }
186
187
            }
188 42514 jjdelcerro
            Collections.sort(actions, new CompareAction());
189
            return actions;
190 40435 jjdelcerro
        }
191
        return null;
192
    }
193
194
    public class CompareAction implements Comparator<IContextMenuAction> {
195
196
        public int compare(IContextMenuAction o1, IContextMenuAction o2) {
197
            // FIXME: flata formatear los enteros!!!!
198
            NumberFormat formater = NumberFormat.getInstance();
199 42514 jjdelcerro
            formater.setMinimumIntegerDigits(6);
200 40435 jjdelcerro
            String key1 =
201
                "" + formater.format(o1.getGroupOrder()) + o1.getGroup()
202
                    + formater.format(o1.getOrder());
203
            String key2 =
204
                "" + formater.format(o2.getGroupOrder()) + o2.getGroup()
205
                    + formater.format(o2.getOrder());
206
            return key1.compareTo(key2);
207
        }
208
    }
209
210 42514 jjdelcerro
    private void createMenuElements(List<IContextMenuAction> actions) {
211 40435 jjdelcerro
        String group = null;
212 42514 jjdelcerro
        for (IContextMenuAction action : actions) {
213 40435 jjdelcerro
            MenuItem item = new MenuItem(action.getText(), action);
214
            item.setEnabled(action.isEnabled(this.getNodeUserObject(),
215
                this.selecteds));
216 41081 jjdelcerro
            if( action.getGroup()!=null ) {
217
                    if ( !action.getGroup().equals(group)) {
218
                        if (group != null) {
219
                            this.addSeparator();
220
                        }
221
                        group = action.getGroup();
222
                    }
223 40435 jjdelcerro
            }
224
            this.add(item);
225
        }
226
227
    }
228
229
    public class MenuItem extends JMenuItem implements ActionListener {
230
231
        private static final long serialVersionUID = 6858724610206253411L;
232
        private IContextMenuAction action;
233
234
        public MenuItem(String text, IContextMenuAction documentAction) {
235
            super(text);
236
            this.action = documentAction;
237 42505 jjdelcerro
            if( this.action instanceof IContextMenuActionWithIcon ) {
238
                this.setIcon(((IContextMenuActionWithIcon)this.action).getIcon());
239
            }
240 40435 jjdelcerro
            String tip = this.action.getDescription();
241
            if (tip != null && tip.length() > 0) {
242
                this.setToolTipText(tip);
243
            }
244
            this.addActionListener(this);
245
        }
246
247
        public void actionPerformed(ActionEvent e) {
248
            this.action.execute(FPopupMenu.this.getNodeUserObject(),
249
                FPopupMenu.this.selecteds);
250 42548 dmartinezizquierdo
            if (PluginServices.getMainFrame() != null) {
251
                PluginServices.getMainFrame().enableControls();
252
            }
253 40435 jjdelcerro
        }
254
    }
255
256
}