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
/**
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.text.NumberFormat;
29
import java.util.ArrayList;
30
import java.util.Collections;
31
import java.util.Comparator;
32
import java.util.Iterator;
33
import java.util.List;
34

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

    
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.andami.messages.NotificationManager;
41
import org.gvsig.app.project.ProjectManager;
42
import org.gvsig.app.project.documents.view.IContextMenuAction;
43
import org.gvsig.app.project.documents.view.IContextMenuActionWithIcon;
44
import org.gvsig.app.project.documents.view.ViewManager;
45
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
 *
74
 * @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
            ProjectManager projectManager = ProjectManager.getInstance();
89
            ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME);
90

    
91
            viewManager.addTOCContextAction("FSymbolChangeColor", new ChangeSymbolTocMenuEntry());
92
        viewManager.addTOCContextAction("ChangeName", new ChangeNameTocMenuEntry());
93
//        viewManager.addTOCContextAction("FLyrVectEditProperties", new FLyrVectEditPropertiesTocMenuEntry());
94
        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
    }
106

    
107
    /**
108
     * Creates a new FPopupMenu object.
109
     *
110
     * @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
        
128
        List<IContextMenuAction> actions = this.getActionList();
129
        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
    private List<IContextMenuAction> getActionList() {
151
        List<IContextMenuAction> actions =  new ArrayList<IContextMenuAction>();
152
        @SuppressWarnings("unchecked")
153
        Iterator<Extension> iter = this.extensionPoint.iterator();
154
        IContextMenuAction action;
155
        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
                    action = (IContextMenuAction) iter.next().create();
173
                } catch (Exception e) {
174
                    NotificationManager.addError(e);
175
                }
176
                if (action != null ) {
177
                        if( action instanceof AbstractTocContextMenuAction
178
                                && !(action instanceof OldTocContextMenuAction)) {
179
                                ((AbstractTocContextMenuAction)action).setMapContext(this.mapContext);
180
                        }
181
                    if (action.isVisible(this.getNodeUserObject(),
182
                        this.selecteds)) {
183
                        actions.add(action);
184
                    }
185
                }
186

    
187
            }
188
            Collections.sort(actions, new CompareAction());
189
            return actions;
190
        }
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
            formater.setMinimumIntegerDigits(6);
200
            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
    private void createMenuElements(List<IContextMenuAction> actions) {
211
        String group = null;
212
        for (IContextMenuAction action : actions) {
213
            MenuItem item = new MenuItem(action.getText(), action);
214
            item.setEnabled(action.isEnabled(this.getNodeUserObject(),
215
                this.selecteds));
216
            if( action.getGroup()!=null ) {
217
                    if ( !action.getGroup().equals(group)) {
218
                        if (group != null) {
219
                            this.addSeparator();
220
                        }
221
                        group = action.getGroup();
222
                    }
223
            }
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
            if( this.action instanceof IContextMenuActionWithIcon ) {
238
                this.setIcon(((IContextMenuActionWithIcon)this.action).getIcon());
239
            }
240
            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
            if (PluginServices.getMainFrame() != null) {
251
                PluginServices.getMainFrame().enableControls();
252
            }
253
        }
254
    }
255

    
256
}