root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / gui / FPopupMenu.java @ 24962
History | View | Annotate | Download (7.88 KB)
1 |
/*
|
---|---|
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 |
// Lo de fijar la fuente es porque en linux se ve?a mal si no se fija.
|
97 |
// TODO: Esto no funcionar? para idiomas como el chino. Hay que cambiarlo.
|
98 |
public final static Font theFont = new Font("SansSerif", Font.PLAIN, 10); |
99 |
|
100 |
public static void registerExtensionPoint() { |
101 |
|
102 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
103 |
extensionPoints.add(extPoint,"Terminate",new TerminateLayoutMenuEntry()); |
104 |
extensionPoints.add(extPoint,"Cancel",new CancelLayoutMenuEntry()); |
105 |
extensionPoints.add(extPoint,"Copy",new CopyLayoutMenuEntry()); |
106 |
extensionPoints.add(extPoint,"Cut",new CutLayoutMenuEntry()); |
107 |
extensionPoints.add(extPoint,"Paste",new PasteLayoutMenuEntry()); |
108 |
extensionPoints.add(extPoint,"Simplify",new SimplifyLayoutMenuEntry()); |
109 |
extensionPoints.add(extPoint,"Property",new PropertyLayoutMenuEntry()); |
110 |
extensionPoints.add(extPoint,"SelectAll",new SelectAllLayoutMenuEntry()); |
111 |
extensionPoints.add(extPoint,"Behind",new BehindLayoutMenuEntry()); |
112 |
extensionPoints.add(extPoint,"Before",new BeforeLayoutMenuEntry()); |
113 |
extensionPoints.add(extPoint,"Position",new PositionLayoutMenuEntry()); |
114 |
extensionPoints.add(extPoint,"Refresh",new RefreshLayoutMenuEntry()); |
115 |
|
116 |
} |
117 |
/**
|
118 |
* Creates a new FPopupMenu object.
|
119 |
*
|
120 |
* @param nodo DOCUMENT ME!
|
121 |
* @param vista DOCUMENT ME!
|
122 |
*/
|
123 |
public FPopupMenu(Layout layout) {
|
124 |
super();
|
125 |
this.initialize(layout);
|
126 |
} |
127 |
|
128 |
private void initialize(Layout layout) { |
129 |
this.layout = layout;
|
130 |
this.extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get(extPoint);
|
131 |
this.selecteds = this.layout.getLayoutContext().getFFrameSelected(); |
132 |
IContextMenuAction[] actions = this.getActionList(); |
133 |
this.createMenuElements(actions);
|
134 |
} |
135 |
|
136 |
private IContextMenuAction[] getActionList() { |
137 |
ArrayList actionArrayList = new ArrayList(); |
138 |
Iterator iter = this.extensionPoint.keySet().iterator(); |
139 |
AbstractLayoutContextMenuAction action; |
140 |
while (iter.hasNext()) {
|
141 |
action = null;
|
142 |
try {
|
143 |
action = (AbstractLayoutContextMenuAction)this.extensionPoint.create((String)iter.next()); |
144 |
} catch (InstantiationException e) { |
145 |
e.printStackTrace(); |
146 |
} catch (IllegalAccessException e) { |
147 |
e.printStackTrace(); |
148 |
} |
149 |
if (action != null) { |
150 |
action.setLayout(layout); |
151 |
if (action.isVisible(null,this.selecteds)) { |
152 |
actionArrayList.add(action); |
153 |
} |
154 |
} |
155 |
} |
156 |
IContextMenuAction[] result = (IContextMenuAction[])Array.newInstance(IContextMenuAction.class,actionArrayList.size()); |
157 |
System.arraycopy(actionArrayList.toArray(),0,result,0,actionArrayList.size()); |
158 |
Arrays.sort(result,new CompareAction()); |
159 |
|
160 |
return result;
|
161 |
} |
162 |
|
163 |
public class CompareAction implements Comparator{ |
164 |
public int compare(Object o1, Object o2) { |
165 |
return this.compare((IContextMenuAction)o1,(IContextMenuAction)o2); |
166 |
} |
167 |
|
168 |
public int compare(IContextMenuAction o1, IContextMenuAction o2) { |
169 |
NumberFormat formater = NumberFormat.getInstance(); |
170 |
formater.setMinimumIntegerDigits(3);
|
171 |
String key1= ""+formater.format(o1.getGroupOrder())+o1.getGroup()+formater.format(o1.getOrder()); |
172 |
String key2= ""+formater.format(o2.getGroupOrder())+o2.getGroup()+formater.format(o2.getOrder()); |
173 |
return key1.compareTo(key2);
|
174 |
} |
175 |
} |
176 |
|
177 |
private void createMenuElements(IContextMenuAction[] actions) { |
178 |
String group = null; |
179 |
for (int i=0;i < actions.length;i++) { |
180 |
IContextMenuAction action = actions[i]; |
181 |
MenuItem item = new MenuItem(action.getText(),action); |
182 |
item.setFont(theFont); |
183 |
item.setEnabled(action.isEnabled(null,this.selecteds)); |
184 |
if (!action.getGroup().equals(group)) {
|
185 |
if (group != null) this.addSeparator(); |
186 |
group = action.getGroup(); |
187 |
} |
188 |
this.add(item);
|
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
|
194 |
public class MenuItem extends JMenuItem implements ActionListener{ |
195 |
private IContextMenuAction action;
|
196 |
public MenuItem(String text,IContextMenuAction documentAction) { |
197 |
super(text);
|
198 |
this.action = documentAction;
|
199 |
String tip = this.action.getDescription(); |
200 |
if (tip != null && tip.length() > 0) { |
201 |
this.setToolTipText(tip);
|
202 |
} |
203 |
this.addActionListener(this); |
204 |
} |
205 |
|
206 |
public void actionPerformed(ActionEvent e) { |
207 |
this.action.execute(layout.getLayoutContext(), FPopupMenu.this.selecteds);
|
208 |
} |
209 |
} |
210 |
} |