root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / gui / FPopupMenu.java @ 38063
History | View | Annotate | Download (7.63 KB)
1 | 9811 | caballero | /*
|
---|---|---|---|
2 | * Created on 02-mar-2004
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | *
|
||
6 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | *
|
||
13 | * This program is distributed in the hope that it will be useful,
|
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | *
|
||
18 | * You should have received a copy of the GNU General Public License
|
||
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
21 | *
|
||
22 | * For more information, contact:
|
||
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | *
|
||
34 | * or
|
||
35 | *
|
||
36 | * IVER T.I. S.A
|
||
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | *
|
||
41 | * +34 963163400
|
||
42 | * dac@iver.es
|
||
43 | */
|
||
44 | package com.iver.cit.gvsig.project.documents.layout.gui; |
||
45 | |||
46 | import java.awt.Font; |
||
47 | import java.awt.event.ActionEvent; |
||
48 | import java.awt.event.ActionListener; |
||
49 | import java.lang.reflect.Array; |
||
50 | import java.text.NumberFormat; |
||
51 | import java.util.ArrayList; |
||
52 | import java.util.Arrays; |
||
53 | import java.util.Comparator; |
||
54 | import java.util.Iterator; |
||
55 | |||
56 | import javax.swing.JMenuItem; |
||
57 | import javax.swing.JPopupMenu; |
||
58 | |||
59 | import com.iver.cit.gvsig.project.documents.IContextMenuAction; |
||
60 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.AbstractLayoutContextMenuAction; |
||
61 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.BeforeLayoutMenuEntry; |
||
62 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.BehindLayoutMenuEntry; |
||
63 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.CancelLayoutMenuEntry; |
||
64 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.CopyLayoutMenuEntry; |
||
65 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.CutLayoutMenuEntry; |
||
66 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.PasteLayoutMenuEntry; |
||
67 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.PositionLayoutMenuEntry; |
||
68 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.PropertyLayoutMenuEntry; |
||
69 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.RefreshLayoutMenuEntry; |
||
70 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.SelectAllLayoutMenuEntry; |
||
71 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.SimplifyLayoutMenuEntry; |
||
72 | import com.iver.cit.gvsig.project.documents.layout.contextmenu.gui.TerminateLayoutMenuEntry; |
||
73 | import com.iver.cit.gvsig.project.documents.layout.fframes.IFFrame; |
||
74 | import com.iver.utiles.extensionPoints.ExtensionPoint; |
||
75 | import com.iver.utiles.extensionPoints.ExtensionPoints; |
||
76 | import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
||
77 | |||
78 | /**
|
||
79 | * Menu de bot?n derecho para el Layout.
|
||
80 | * Se pueden a?adir entradas facilmente desde una extensi?n,
|
||
81 | * creando una clase derivando de LayoutMenuEntry, y a?adiendola en
|
||
82 | * est?tico (o en tiempo de carga de la extensi?n) a FPopupMenu.
|
||
83 | * (Las entradas actuales est?n hechas de esa manera).
|
||
84 | *
|
||
85 | * @author Vicente Caballero Navarro
|
||
86 | *
|
||
87 | */
|
||
88 | |||
89 | public class FPopupMenu extends JPopupMenu { |
||
90 | //private static ArrayList menuEntrys = new ArrayList();
|
||
91 | protected Layout layout;
|
||
92 | private ExtensionPoint extensionPoint;
|
||
93 | private IFFrame[] selecteds; |
||
94 | private static String extPoint="Layout_PopupActions"; |
||
95 | //private JMenuItem capa;
|
||
96 | |||
97 | public static void registerExtensionPoint() { |
||
98 | |||
99 | ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
||
100 | extensionPoints.add(extPoint,"Terminate",new TerminateLayoutMenuEntry()); |
||
101 | extensionPoints.add(extPoint,"Cancel",new CancelLayoutMenuEntry()); |
||
102 | extensionPoints.add(extPoint,"Copy",new CopyLayoutMenuEntry()); |
||
103 | extensionPoints.add(extPoint,"Cut",new CutLayoutMenuEntry()); |
||
104 | extensionPoints.add(extPoint,"Paste",new PasteLayoutMenuEntry()); |
||
105 | extensionPoints.add(extPoint,"Simplify",new SimplifyLayoutMenuEntry()); |
||
106 | extensionPoints.add(extPoint,"Property",new PropertyLayoutMenuEntry()); |
||
107 | extensionPoints.add(extPoint,"SelectAll",new SelectAllLayoutMenuEntry()); |
||
108 | extensionPoints.add(extPoint,"Behind",new BehindLayoutMenuEntry()); |
||
109 | extensionPoints.add(extPoint,"Before",new BeforeLayoutMenuEntry()); |
||
110 | extensionPoints.add(extPoint,"Position",new PositionLayoutMenuEntry()); |
||
111 | extensionPoints.add(extPoint,"Refresh",new RefreshLayoutMenuEntry()); |
||
112 | |||
113 | } |
||
114 | /**
|
||
115 | * Creates a new FPopupMenu object.
|
||
116 | *
|
||
117 | * @param nodo DOCUMENT ME!
|
||
118 | * @param vista DOCUMENT ME!
|
||
119 | */
|
||
120 | public FPopupMenu(Layout layout) {
|
||
121 | super();
|
||
122 | this.initialize(layout);
|
||
123 | } |
||
124 | |||
125 | private void initialize(Layout layout) { |
||
126 | this.layout = layout;
|
||
127 | this.extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get(extPoint);
|
||
128 | this.selecteds = this.layout.getLayoutContext().getFFrameSelected(); |
||
129 | IContextMenuAction[] actions = this.getActionList(); |
||
130 | this.createMenuElements(actions);
|
||
131 | } |
||
132 | |||
133 | private IContextMenuAction[] getActionList() { |
||
134 | ArrayList actionArrayList = new ArrayList(); |
||
135 | Iterator iter = this.extensionPoint.keySet().iterator(); |
||
136 | AbstractLayoutContextMenuAction action; |
||
137 | while (iter.hasNext()) {
|
||
138 | action = null;
|
||
139 | try {
|
||
140 | action = (AbstractLayoutContextMenuAction)this.extensionPoint.create((String)iter.next()); |
||
141 | } catch (InstantiationException e) { |
||
142 | e.printStackTrace(); |
||
143 | } catch (IllegalAccessException e) { |
||
144 | e.printStackTrace(); |
||
145 | } |
||
146 | if (action != null) { |
||
147 | action.setLayout(layout); |
||
148 | if (action.isVisible(null,this.selecteds)) { |
||
149 | actionArrayList.add(action); |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | IContextMenuAction[] result = (IContextMenuAction[])Array.newInstance(IContextMenuAction.class,actionArrayList.size()); |
||
154 | System.arraycopy(actionArrayList.toArray(),0,result,0,actionArrayList.size()); |
||
155 | Arrays.sort(result,new CompareAction()); |
||
156 | |||
157 | return result;
|
||
158 | } |
||
159 | |||
160 | public class CompareAction implements Comparator{ |
||
161 | public int compare(Object o1, Object o2) { |
||
162 | return this.compare((IContextMenuAction)o1,(IContextMenuAction)o2); |
||
163 | } |
||
164 | |||
165 | public int compare(IContextMenuAction o1, IContextMenuAction o2) { |
||
166 | NumberFormat formater = NumberFormat.getInstance(); |
||
167 | formater.setMinimumIntegerDigits(3);
|
||
168 | String key1= ""+formater.format(o1.getGroupOrder())+o1.getGroup()+formater.format(o1.getOrder()); |
||
169 | String key2= ""+formater.format(o2.getGroupOrder())+o2.getGroup()+formater.format(o2.getOrder()); |
||
170 | return key1.compareTo(key2);
|
||
171 | } |
||
172 | } |
||
173 | |||
174 | private void createMenuElements(IContextMenuAction[] actions) { |
||
175 | String group = null; |
||
176 | for (int i=0;i < actions.length;i++) { |
||
177 | IContextMenuAction action = actions[i]; |
||
178 | MenuItem item = new MenuItem(action.getText(),action); |
||
179 | item.setEnabled(action.isEnabled(null,this.selecteds)); |
||
180 | if (!action.getGroup().equals(group)) {
|
||
181 | if (group != null) this.addSeparator(); |
||
182 | group = action.getGroup(); |
||
183 | } |
||
184 | this.add(item);
|
||
185 | } |
||
186 | |||
187 | } |
||
188 | |||
189 | |||
190 | public class MenuItem extends JMenuItem implements ActionListener{ |
||
191 | private IContextMenuAction action;
|
||
192 | public MenuItem(String text,IContextMenuAction documentAction) { |
||
193 | super(text);
|
||
194 | this.action = documentAction;
|
||
195 | String tip = this.action.getDescription(); |
||
196 | if (tip != null && tip.length() > 0) { |
||
197 | this.setToolTipText(tip);
|
||
198 | } |
||
199 | this.addActionListener(this); |
||
200 | } |
||
201 | |||
202 | public void actionPerformed(ActionEvent e) { |
||
203 | this.action.execute(layout.getLayoutContext(), FPopupMenu.this.selecteds);
|
||
204 | } |
||
205 | } |
||
206 | } |