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 @ 41104
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.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.ProjectManager; |
41 |
import org.gvsig.app.project.documents.view.IContextMenuAction; |
42 |
import org.gvsig.app.project.documents.view.ViewManager; |
43 |
import org.gvsig.app.project.documents.view.toc.AbstractTocContextMenuAction; |
44 |
import org.gvsig.app.project.documents.view.toc.ITocItem; |
45 |
import org.gvsig.app.project.documents.view.toc.TocItemBranch; |
46 |
import org.gvsig.app.project.documents.view.toc.actions.ChangeNameTocMenuEntry; |
47 |
import org.gvsig.app.project.documents.view.toc.actions.ChangeSymbolTocMenuEntry; |
48 |
import org.gvsig.app.project.documents.view.toc.actions.CopyLayersTocMenuEntry; |
49 |
import org.gvsig.app.project.documents.view.toc.actions.CutLayersTocMenuEntry; |
50 |
import org.gvsig.app.project.documents.view.toc.actions.EliminarCapaTocMenuEntry; |
51 |
import org.gvsig.app.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry; |
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 |
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 IContextMenuAction[] getActionList() { |
150 |
ArrayList<IContextMenuAction> actionArrayList =
|
151 |
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 |
actionArrayList.add(action); |
184 |
} |
185 |
} |
186 |
|
187 |
} |
188 |
IContextMenuAction[] result =
|
189 |
(IContextMenuAction[]) Array.newInstance( |
190 |
IContextMenuAction.class, actionArrayList.size()); |
191 |
System.arraycopy(actionArrayList.toArray(), 0, result, 0, |
192 |
actionArrayList.size()); |
193 |
Arrays.sort(result, new CompareAction()); |
194 |
|
195 |
return result;
|
196 |
} |
197 |
return null; |
198 |
} |
199 |
|
200 |
public class CompareAction implements Comparator<IContextMenuAction> { |
201 |
|
202 |
public int compare(IContextMenuAction o1, IContextMenuAction o2) { |
203 |
// FIXME: flata formatear los enteros!!!!
|
204 |
NumberFormat formater = NumberFormat.getInstance(); |
205 |
formater.setMinimumIntegerDigits(3);
|
206 |
String key1 =
|
207 |
"" + formater.format(o1.getGroupOrder()) + o1.getGroup()
|
208 |
+ formater.format(o1.getOrder()); |
209 |
String key2 =
|
210 |
"" + formater.format(o2.getGroupOrder()) + o2.getGroup()
|
211 |
+ formater.format(o2.getOrder()); |
212 |
return key1.compareTo(key2);
|
213 |
} |
214 |
} |
215 |
|
216 |
private void createMenuElements(IContextMenuAction[] actions) { |
217 |
String group = null; |
218 |
for (int i = 0; i < actions.length; i++) { |
219 |
IContextMenuAction action = actions[i]; |
220 |
MenuItem item = new MenuItem(action.getText(), action); |
221 |
item.setEnabled(action.isEnabled(this.getNodeUserObject(),
|
222 |
this.selecteds));
|
223 |
if( action.getGroup()!=null ) { |
224 |
if ( !action.getGroup().equals(group)) {
|
225 |
if (group != null) { |
226 |
this.addSeparator();
|
227 |
} |
228 |
group = action.getGroup(); |
229 |
} |
230 |
} |
231 |
this.add(item);
|
232 |
} |
233 |
|
234 |
} |
235 |
|
236 |
public class MenuItem extends JMenuItem implements ActionListener { |
237 |
|
238 |
private static final long serialVersionUID = 6858724610206253411L; |
239 |
private IContextMenuAction action;
|
240 |
|
241 |
public MenuItem(String text, IContextMenuAction documentAction) { |
242 |
super(text);
|
243 |
this.action = documentAction;
|
244 |
String tip = this.action.getDescription(); |
245 |
if (tip != null && tip.length() > 0) { |
246 |
this.setToolTipText(tip);
|
247 |
} |
248 |
this.addActionListener(this); |
249 |
} |
250 |
|
251 |
public void actionPerformed(ActionEvent e) { |
252 |
this.action.execute(FPopupMenu.this.getNodeUserObject(),
|
253 |
FPopupMenu.this.selecteds); |
254 |
} |
255 |
} |
256 |
|
257 |
} |