root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / CADExtension.java @ 4551
History | View | Annotate | Download (14.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig; |
42 |
|
43 |
import java.awt.KeyEventPostProcessor; |
44 |
import java.awt.KeyboardFocusManager; |
45 |
import java.awt.event.ActionEvent; |
46 |
import java.awt.event.ActionListener; |
47 |
import java.awt.event.KeyEvent; |
48 |
import java.awt.event.MouseEvent; |
49 |
import java.util.HashMap; |
50 |
|
51 |
import javax.swing.AbstractAction; |
52 |
import javax.swing.JMenuItem; |
53 |
import javax.swing.JPopupMenu; |
54 |
|
55 |
import com.iver.andami.PluginServices; |
56 |
import com.iver.andami.plugins.Extension; |
57 |
import com.iver.cit.gvsig.fmap.MapControl; |
58 |
import com.iver.cit.gvsig.gui.View; |
59 |
import com.iver.cit.gvsig.gui.cad.CADTool; |
60 |
import com.iver.cit.gvsig.gui.cad.CADToolAdapter; |
61 |
import com.iver.cit.gvsig.gui.cad.tools.ArcCADTool; |
62 |
import com.iver.cit.gvsig.gui.cad.tools.CircleCADTool; |
63 |
import com.iver.cit.gvsig.gui.cad.tools.CopyCADTool; |
64 |
import com.iver.cit.gvsig.gui.cad.tools.EditVertexCADTool; |
65 |
import com.iver.cit.gvsig.gui.cad.tools.EllipseCADTool; |
66 |
import com.iver.cit.gvsig.gui.cad.tools.LineCADTool; |
67 |
import com.iver.cit.gvsig.gui.cad.tools.MoveCADTool; |
68 |
import com.iver.cit.gvsig.gui.cad.tools.PointCADTool; |
69 |
import com.iver.cit.gvsig.gui.cad.tools.PolygonCADTool; |
70 |
import com.iver.cit.gvsig.gui.cad.tools.PolylineCADTool; |
71 |
import com.iver.cit.gvsig.gui.cad.tools.RectangleCADTool; |
72 |
import com.iver.cit.gvsig.gui.cad.tools.RotateCADTool; |
73 |
import com.iver.cit.gvsig.gui.cad.tools.ScaleCADTool; |
74 |
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool; |
75 |
import com.iver.utiles.console.ResponseListener; |
76 |
/**
|
77 |
* Extensi?n dedicada a controlar las diferentes operaciones sobre el editado
|
78 |
* de una capa.
|
79 |
*
|
80 |
* @author Vicente Caballero Navarro
|
81 |
*/
|
82 |
public class CADExtension implements Extension { |
83 |
private static CADToolAdapter adapter=new CADToolAdapter(); |
84 |
private static EditionManager editionManager = new EditionManager(); |
85 |
private static HashMap namesCadTools = new HashMap(); |
86 |
private MapControl mapControl;
|
87 |
private static View view; |
88 |
public static CADToolAdapter getCADToolAdapter(){ |
89 |
return adapter;
|
90 |
} |
91 |
/**
|
92 |
* @see com.iver.andami.plugins.Extension#inicializar()
|
93 |
*/
|
94 |
public void inicializar() { |
95 |
SelectionCADTool selection=new SelectionCADTool();
|
96 |
// LineCADTool line = new LineCADTool();
|
97 |
// PointCADTool point = new PointCADTool();
|
98 |
// CircleCADTool circle=new CircleCADTool();
|
99 |
// RectangleCADTool rectangle=new RectangleCADTool();
|
100 |
// PolylineCADTool polyline=new PolylineCADTool();
|
101 |
// EllipseCADTool ellipse=new EllipseCADTool();
|
102 |
// ArcCADTool arc=new ArcCADTool();
|
103 |
// PolygonCADTool polygon=new PolygonCADTool();
|
104 |
CopyCADTool copy=new CopyCADTool();
|
105 |
MoveCADTool move=new MoveCADTool();
|
106 |
RotateCADTool rotate=new RotateCADTool();
|
107 |
ScaleCADTool scale=new ScaleCADTool();
|
108 |
EditVertexCADTool editvertex=new EditVertexCADTool();
|
109 |
addCADTool("selection", selection);
|
110 |
// addCADTool("line", line);
|
111 |
// addCADTool("point", point);
|
112 |
// addCADTool("circle",circle);
|
113 |
// addCADTool("rectangle", rectangle);
|
114 |
// addCADTool("polyline", polyline);
|
115 |
// addCADTool("ellipse", ellipse);
|
116 |
// addCADTool("arc", arc);
|
117 |
// addCADTool("polygon", polygon);
|
118 |
addCADTool("copy",copy);
|
119 |
addCADTool("move",move);
|
120 |
addCADTool("rotate",rotate);
|
121 |
addCADTool("scale",scale);
|
122 |
addCADTool("editvertex",editvertex);
|
123 |
|
124 |
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager(); |
125 |
kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* @see com.iver.andami.plugins.Extension#execute(java.lang.String)
|
130 |
*/
|
131 |
public void execute(String s) { |
132 |
view = (View) PluginServices.getMDIManager().getActiveView();
|
133 |
mapControl = (MapControl) view.getMapControl(); |
134 |
if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")) |
135 |
mapControl.addMapTool("cadtooladapter",adapter);
|
136 |
view.getMapControl().setTool("cadtooladapter");
|
137 |
view.addConsoleListener("cad", new ResponseListener() { |
138 |
public void acceptResponse(String response) { |
139 |
adapter.textEntered(response); |
140 |
// TODO:
|
141 |
// FocusManager fm=FocusManager.getCurrentManager();
|
142 |
// fm.focusPreviousComponent(mapControl);
|
143 |
/*if (popup.isShowing()){
|
144 |
popup.setVisible(false);
|
145 |
}*/
|
146 |
|
147 |
} |
148 |
}); |
149 |
// registerKeyStrokes();
|
150 |
/* view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "elimina");
|
151 |
view.getActionMap().put("elimina", new MyAction("eliminar"));
|
152 |
view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
|
153 |
view.getActionMap().put("escape", new MyAction("escape")); */
|
154 |
|
155 |
/* FLayers layers=mapControl.getMapContext().getLayers();
|
156 |
for (int i=0;i<layers.getLayersCount();i++){
|
157 |
if (layers.getLayer(i).isEditing() && layers.getLayer(i) instanceof FLyrVect){
|
158 |
adapter.setVectorialAdapter((VectorialEditableAdapter)((FLyrVect)layers.getLayer(i)).getSource());
|
159 |
adapter.setMapControl(mapControl);
|
160 |
|
161 |
}
|
162 |
} */
|
163 |
|
164 |
view.getMapControl().setTool("cadtooladapter");
|
165 |
|
166 |
if (s.equals("SPLINE")) { |
167 |
setCADTool("spline");
|
168 |
} else if (s.equals("COPY")) { |
169 |
setCADTool("copy");
|
170 |
} else if (s.equals("MOVE")) { |
171 |
setCADTool("move");
|
172 |
} else if (s.equals("EQUIDISTANCE")) { |
173 |
setCADTool("equidistance");
|
174 |
} else if (s.equals("MATRIZ")) { |
175 |
setCADTool("matriz");
|
176 |
} else if (s.equals("SYMMETRY")) { |
177 |
setCADTool("symmetry");
|
178 |
} else if (s.equals("ROTATION")) { |
179 |
setCADTool("rotate");
|
180 |
} else if (s.equals("STRETCHING")) { |
181 |
setCADTool("stretching");
|
182 |
} else if (s.equals("SCALE")) { |
183 |
setCADTool("scale");
|
184 |
} else if (s.equals("EXTEND")) { |
185 |
setCADTool("extend");
|
186 |
} else if (s.equals("TRIM")) { |
187 |
setCADTool("trim");
|
188 |
} else if (s.equals("UNIT")) { |
189 |
setCADTool("unit");
|
190 |
} else if (s.equals("EXPLOIT")) { |
191 |
setCADTool("exploit");
|
192 |
} else if (s.equals("CHAFLAN")) { |
193 |
setCADTool("chaflan");
|
194 |
} else if (s.equals("JOIN")) { |
195 |
setCADTool("join");
|
196 |
} else if (s.equals("SELCAD")) { |
197 |
setCADTool("selection");
|
198 |
// } else if (s.equals("POINT")) {
|
199 |
// setCADTool("point");
|
200 |
// } else if (s.equals("LINE")) {
|
201 |
// setCADTool("line");
|
202 |
// } else if (s.equals("POLYLINE")) {
|
203 |
// setCADTool("polyline");
|
204 |
// } else if (s.equals("CIRCLE")) {
|
205 |
// setCADTool("circle");
|
206 |
// } else if (s.equals("ARC")) {
|
207 |
// setCADTool("arc");
|
208 |
// } else if (s.equals("ELLIPSE")) {
|
209 |
// setCADTool("ellipse");
|
210 |
// } else if (s.equals("RECTANGLE")) {
|
211 |
// setCADTool("rectangle");
|
212 |
// } else if (s.equals("POLYGON")) {
|
213 |
// setCADTool("polygon");
|
214 |
} else if (s.equals("EDITVERTEX")) { |
215 |
setCADTool("editvertex");
|
216 |
} |
217 |
adapter.configureMenu(); |
218 |
//ViewControls.CANCELED=false;
|
219 |
} |
220 |
public static void addCADTool(String name, CADTool c){ |
221 |
namesCadTools.put(name, c); |
222 |
} |
223 |
public static void setCADTool(String text){ |
224 |
CADTool ct = (CADTool) namesCadTools.get(text); |
225 |
if (ct == null) throw new RuntimeException("No such cad tool"); |
226 |
adapter.setCadTool(ct); |
227 |
ct.init(); |
228 |
//PluginServices.getMainFrame().setSelectedTool("SELECT");
|
229 |
//PluginServices.getMainFrame().enableControls();
|
230 |
} |
231 |
/**
|
232 |
* @see com.iver.andami.plugins.Extension#isEnabled()
|
233 |
*/
|
234 |
public boolean isEnabled() { |
235 |
/* com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
|
236 |
.getActiveView();
|
237 |
|
238 |
if (f == null) {
|
239 |
return false;
|
240 |
}
|
241 |
|
242 |
if (f.getClass() == View.class) {
|
243 |
FLayer[] l = ((View) f).getModel().getMapContext().getLayers()
|
244 |
.getActives();
|
245 |
|
246 |
for (int i = 0; i < l.length; i++) {
|
247 |
if (l[i] instanceof FLyrVect && ((FLyrVect) l[i]).isEditing()) {
|
248 |
return true;
|
249 |
}
|
250 |
}
|
251 |
}
|
252 |
|
253 |
return false; */
|
254 |
if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
|
255 |
{ |
256 |
// Queremos que siempre que haya edici?n esto est? funcionando.
|
257 |
editionManager.setMapControl(mapControl); |
258 |
return true; |
259 |
} |
260 |
else
|
261 |
return false; |
262 |
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* @see com.iver.andami.plugins.Extension#isVisible()
|
267 |
*/
|
268 |
public boolean isVisible() { |
269 |
com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager() |
270 |
.getActiveView(); |
271 |
|
272 |
if (f == null) { |
273 |
return false; |
274 |
} |
275 |
|
276 |
if (f.getClass() == View.class) { |
277 |
return true; |
278 |
} else {
|
279 |
return false; |
280 |
} |
281 |
} |
282 |
public MapControl getMapControl() {
|
283 |
return this.mapControl; |
284 |
} |
285 |
class KeyAction extends AbstractAction{ |
286 |
|
287 |
private String key; |
288 |
|
289 |
public KeyAction(String key){ |
290 |
this.key = key;
|
291 |
} |
292 |
|
293 |
/* (non-Javadoc)
|
294 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
295 |
*/
|
296 |
public void actionPerformed(ActionEvent e) { |
297 |
view.focusConsole(key); |
298 |
} |
299 |
|
300 |
} |
301 |
|
302 |
class MyAction extends AbstractAction{ |
303 |
private String actionCommand; |
304 |
|
305 |
public MyAction(String command){ |
306 |
actionCommand = command; |
307 |
} |
308 |
|
309 |
/**
|
310 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
311 |
*/
|
312 |
public void actionPerformed(ActionEvent e) { |
313 |
adapter.keyPressed(actionCommand); |
314 |
} |
315 |
|
316 |
} |
317 |
|
318 |
/**
|
319 |
* @author fjp
|
320 |
*
|
321 |
* La idea es usar esto para recibir lo que el usuario escribe y enviarlo
|
322 |
* a la consola de la vista para que salga por all?.
|
323 |
*/
|
324 |
private class myKeyEventPostProcessor implements KeyEventPostProcessor |
325 |
{ |
326 |
|
327 |
public boolean postProcessKeyEvent(KeyEvent e) { |
328 |
// System.out.println("KeyEvent e = " + e);
|
329 |
if ((adapter==null) || (view == null)) return false; |
330 |
if (e.getID() != KeyEvent.KEY_RELEASED) return false; |
331 |
if (e.getKeyCode() == KeyEvent.VK_DELETE) |
332 |
adapter.keyPressed("eliminar");
|
333 |
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) |
334 |
adapter.keyPressed("escape");
|
335 |
else if (e.getKeyCode() == KeyEvent.VK_ENTER) |
336 |
// TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN JTEXTAREA
|
337 |
// QUE NO ES EL DE CONSOLA
|
338 |
view.focusConsole("");
|
339 |
else
|
340 |
{ |
341 |
if (e.getComponent().getName() != null) |
342 |
{ |
343 |
System.out.println("Evento de teclado desde el componente " + e.getComponent().getName()); |
344 |
if (!e.getComponent().getName().equals("CADConsole")) |
345 |
{ |
346 |
if ((e.getID() == KeyEvent.KEY_TYPED) && (!e.isActionKey())) |
347 |
{ |
348 |
if (Character.isLetterOrDigit(e.getKeyChar())) |
349 |
{ |
350 |
Character keyChar = new Character(e.getKeyChar()); |
351 |
view.focusConsole(keyChar+"");
|
352 |
} |
353 |
} |
354 |
} |
355 |
} |
356 |
} |
357 |
return false; |
358 |
} |
359 |
|
360 |
} |
361 |
|
362 |
/* private void registerKeyStrokes(){
|
363 |
for (char key = '0'; key <= '9'; key++){
|
364 |
Character keyChar = new Character(key);
|
365 |
mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
|
366 |
mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
|
367 |
}
|
368 |
for (char key = 'a'; key <= 'z'; key++){
|
369 |
Character keyChar = new Character(key);
|
370 |
mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
|
371 |
mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
|
372 |
}
|
373 |
for (char key = 'A'; key <= 'Z'; key++){
|
374 |
Character keyChar = new Character(key);
|
375 |
mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
|
376 |
mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
|
377 |
}
|
378 |
//this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
|
379 |
//this.getActionMap().put("enter", new MyAction("enter"));
|
380 |
Character keyChar = new Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
|
381 |
mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),keyChar);
|
382 |
mapControl.getActionMap().put(keyChar, new KeyAction(""));
|
383 |
|
384 |
// El espacio como si fuera INTRO
|
385 |
Character keyCharSpace = new Character(' ');
|
386 |
mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(' '), keyCharSpace);
|
387 |
mapControl.getActionMap().put(keyCharSpace, new KeyAction(""));
|
388 |
|
389 |
|
390 |
}
|
391 |
*/
|
392 |
private static JPopupMenu popup = new JPopupMenu(); |
393 |
public static void clearMenu(){ |
394 |
popup.removeAll(); |
395 |
} |
396 |
|
397 |
public static void addMenuEntry(String text){ |
398 |
JMenuItem menu = new JMenuItem(text); |
399 |
menu.setActionCommand(text); |
400 |
menu.setEnabled(true);
|
401 |
menu.setVisible(true);
|
402 |
menu.addActionListener(new ActionListener() { |
403 |
public void actionPerformed(ActionEvent e) { |
404 |
adapter.transition(e.getActionCommand()); |
405 |
} |
406 |
}); |
407 |
|
408 |
popup.add(menu); |
409 |
} |
410 |
public static void showPopup(MouseEvent e) { |
411 |
popup.show(e.getComponent(), |
412 |
e.getX(), e.getY()); |
413 |
} |
414 |
public static View getView() { |
415 |
return view;
|
416 |
} |
417 |
|
418 |
/**
|
419 |
* @return Returns the editionManager.
|
420 |
*/
|
421 |
public static EditionManager getEditionManager() { |
422 |
return editionManager;
|
423 |
} |
424 |
} |