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

History | View | Annotate | Download (9.99 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.messages.NotificationManager;
40
import org.gvsig.app.project.ProjectManager;
41
import org.gvsig.app.project.documents.view.IContextMenuAction;
42
import org.gvsig.app.project.documents.view.IContextMenuActionWithIcon;
43
import org.gvsig.app.project.documents.view.ViewManager;
44
import org.gvsig.app.project.documents.view.toc.AbstractTocContextMenuAction;
45
import org.gvsig.app.project.documents.view.toc.ITocItem;
46
import org.gvsig.app.project.documents.view.toc.TocItemBranch;
47
import org.gvsig.app.project.documents.view.toc.actions.ChangeNameTocMenuEntry;
48
import org.gvsig.app.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry;
49
import org.gvsig.app.project.documents.view.toc.actions.CopyLayersTocMenuEntry;
50
import org.gvsig.app.project.documents.view.toc.actions.CutLayersTocMenuEntry;
51
import org.gvsig.app.project.documents.view.toc.actions.EliminarCapaTocMenuEntry;
52
import org.gvsig.app.project.documents.view.toc.actions.FirstLayerTocMenuEntry;
53
import org.gvsig.app.project.documents.view.toc.actions.LayersGroupTocMenuEntry;
54
import org.gvsig.app.project.documents.view.toc.actions.LayersUngroupTocMenuEntry;
55
import org.gvsig.app.project.documents.view.toc.actions.OldTocContextMenuAction;
56
import org.gvsig.app.project.documents.view.toc.actions.PasteLayersTocMenuEntry;
57
import org.gvsig.app.project.documents.view.toc.actions.ReloadLayerTocMenuEntry;
58
import org.gvsig.app.project.documents.view.toc.actions.ShowLayerErrorsTocMenuEntry;
59
import org.gvsig.app.project.documents.view.toc.actions.ZoomAlTemaTocMenuEntry;
60
import org.gvsig.fmap.mapcontext.MapContext;
61
import org.gvsig.fmap.mapcontext.layers.FLayer;
62
import org.gvsig.tools.ToolsLocator;
63
import org.gvsig.tools.extensionpoint.ExtensionPoint;
64
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
65

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

    
78
public class FPopupMenu extends JPopupMenu {
79

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

    
86
    public static void registerExtensionPoint() {
87
            ProjectManager projectManager = ProjectManager.getInstance(); 
88
            ViewManager viewManager = (ViewManager) projectManager.getDocumentManager(ViewManager.TYPENAME);
89

    
90
            viewManager.addTOCContextAction("FSymbolChangeColor", new ChangeSymbolTocMenuEntry());
91
        viewManager.addTOCContextAction("ChangeName", new ChangeNameTocMenuEntry());
92
//        viewManager.addTOCContextAction("FLyrVectEditProperties", new FLyrVectEditPropertiesTocMenuEntry());
93
        viewManager.addTOCContextAction("ZoomAlTema", new ZoomAlTemaTocMenuEntry());
94
        viewManager.addTOCContextAction("EliminarCapa", new EliminarCapaTocMenuEntry());
95
        viewManager.addTOCContextAction("VerErroresCapa", new ShowLayerErrorsTocMenuEntry());
96
        viewManager.addTOCContextAction("ReloadLayer", new ReloadLayerTocMenuEntry());
97
        viewManager.addTOCContextAction("LayersGroup", new LayersGroupTocMenuEntry());
98
        viewManager.addTOCContextAction("LayersUngroup", new LayersUngroupTocMenuEntry());
99
        viewManager.addTOCContextAction("FirstLayer", new FirstLayerTocMenuEntry());
100

    
101
        viewManager.addTOCContextAction("Copy", new CopyLayersTocMenuEntry());
102
        viewManager.addTOCContextAction("Cut", new CutLayersTocMenuEntry());
103
        viewManager.addTOCContextAction("Paste", new PasteLayersTocMenuEntry());
104
    }
105

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

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

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

    
127
        List<IContextMenuAction> actions = this.getActionList();
128
        if (actions == null) {
129
            return;
130
        }
131
        this.createMenuElements(actions);
132
    }
133

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

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

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

    
149
    private List<IContextMenuAction> getActionList() {
150
        List<IContextMenuAction> actions =  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
                        actions.add(action);
183
                    }
184
                }
185

    
186
            }
187
            Collections.sort(actions, new CompareAction());
188
            return actions;
189
        }
190
        return null;
191
    }
192

    
193
    public class CompareAction implements Comparator<IContextMenuAction> {
194

    
195
        public int compare(IContextMenuAction o1, IContextMenuAction o2) {
196
            // FIXME: flata formatear los enteros!!!!
197
            NumberFormat formater = NumberFormat.getInstance();
198
            formater.setMinimumIntegerDigits(6);
199
            String key1 =
200
                "" + formater.format(o1.getGroupOrder()) + o1.getGroup()
201
                    + formater.format(o1.getOrder());
202
            String key2 =
203
                "" + formater.format(o2.getGroupOrder()) + o2.getGroup()
204
                    + formater.format(o2.getOrder());
205
            return key1.compareTo(key2);
206
        }
207
    }
208

    
209
    private void createMenuElements(List<IContextMenuAction> actions) {
210
        String group = null;
211
        for (IContextMenuAction action : actions) {
212
            MenuItem item = new MenuItem(action.getText(), action);
213
            item.setEnabled(action.isEnabled(this.getNodeUserObject(),
214
                this.selecteds));
215
            if( action.getGroup()!=null ) {
216
                    if ( !action.getGroup().equals(group)) {
217
                        if (group != null) {
218
                            this.addSeparator();
219
                        }
220
                        group = action.getGroup();
221
                    }
222
            }
223
            this.add(item);
224
        }
225

    
226
    }
227

    
228
    public class MenuItem extends JMenuItem implements ActionListener {
229

    
230
        private static final long serialVersionUID = 6858724610206253411L;
231
        private IContextMenuAction action;
232

    
233
        public MenuItem(String text, IContextMenuAction documentAction) {
234
            super(text);
235
            this.action = documentAction;
236
            if( this.action instanceof IContextMenuActionWithIcon ) {
237
                this.setIcon(((IContextMenuActionWithIcon)this.action).getIcon());
238
            }
239
            String tip = this.action.getDescription();
240
            if (tip != null && tip.length() > 0) {
241
                this.setToolTipText(tip);
242
            }
243
            this.addActionListener(this);
244
        }
245

    
246
        public void actionPerformed(ActionEvent e) {
247
            this.action.execute(FPopupMenu.this.getNodeUserObject(),
248
                FPopupMenu.this.selecteds);
249
        }
250
    }
251

    
252
}