svn-gvsig-desktop / tags / v10_RC2c / extensions / extCAD / src / com / iver / cit / gvsig / CADExtension.java @ 8745
History | View | Annotate | Download (13.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 |
import javax.swing.JTable; |
55 |
import javax.swing.KeyStroke; |
56 |
import javax.swing.text.JTextComponent; |
57 |
|
58 |
import com.iver.andami.PluginServices; |
59 |
import com.iver.andami.plugins.Extension; |
60 |
import com.iver.cit.gvsig.fmap.MapControl; |
61 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
62 |
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation; |
63 |
import com.iver.cit.gvsig.gui.View; |
64 |
import com.iver.cit.gvsig.gui.accelerators.ForceCursorAccelerator; |
65 |
import com.iver.cit.gvsig.gui.accelerators.GridAccelerator; |
66 |
import com.iver.cit.gvsig.gui.accelerators.OrtoAccelerator; |
67 |
import com.iver.cit.gvsig.gui.accelerators.RefentAccelerator; |
68 |
import com.iver.cit.gvsig.gui.cad.CADTool; |
69 |
import com.iver.cit.gvsig.gui.cad.CADToolAdapter; |
70 |
import com.iver.cit.gvsig.gui.cad.tools.CopyCADTool; |
71 |
import com.iver.cit.gvsig.gui.cad.tools.RotateCADTool; |
72 |
import com.iver.cit.gvsig.gui.cad.tools.ScaleCADTool; |
73 |
import com.iver.cit.gvsig.gui.cad.tools.SymmetryCADTool; |
74 |
import com.iver.cit.gvsig.gui.popupmenu.PopupEditionProperties; |
75 |
import com.iver.cit.gvsig.gui.toc.FPopupMenu; |
76 |
import com.iver.utiles.console.JConsole; |
77 |
import com.iver.utiles.console.ResponseListener; |
78 |
import com.iver.utiles.console.jedit.JEditTextArea; |
79 |
|
80 |
/**
|
81 |
* Extensi?n dedicada a controlar las diferentes operaciones sobre el editado de
|
82 |
* una capa.
|
83 |
*
|
84 |
* @author Vicente Caballero Navarro
|
85 |
*/
|
86 |
public class CADExtension extends Extension { |
87 |
private static HashMap adapters=new HashMap(); |
88 |
|
89 |
private static View view; |
90 |
|
91 |
private MapControl mapControl;
|
92 |
private static CADToolAdapter adapter=null; |
93 |
|
94 |
public static CADToolAdapter getCADToolAdapter() { |
95 |
com.iver.andami.ui.mdiManager.IWindow view=PluginServices.getMDIManager().getActiveWindow(); |
96 |
if (view instanceof View) { |
97 |
View v=(View)view; |
98 |
if (!adapters.containsKey(v.getModel())) {
|
99 |
adapters.put(v.getModel(),new CADToolAdapter());
|
100 |
} |
101 |
adapter=(CADToolAdapter)adapters.get(v.getModel()); |
102 |
return adapter;
|
103 |
} |
104 |
return adapter;
|
105 |
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
110 |
*/
|
111 |
public void initialize() { |
112 |
|
113 |
// Registramos los Popup menus:
|
114 |
|
115 |
// Adds an entry to the TOC's floating menu
|
116 |
FPopupMenu.addEntry(new PopupEditionProperties());
|
117 |
|
118 |
|
119 |
|
120 |
// Fijamos que los s?mbolos de dibujo tengan outline
|
121 |
// TODO: Esto se debe configurar en el cuadro de di?logo de preferencias
|
122 |
CADTool.drawingSymbol.setOutlined(true);
|
123 |
CADTool.drawingSymbol.setOutlineColor(CADTool.drawingSymbol.getColor().darker()); |
124 |
CADTool.modifySymbol.setOutlined(true);
|
125 |
CADTool.modifySymbol.setOutlineColor(CADTool.modifySymbol.getColor().darker()); |
126 |
CADTool.selectSymbol.setOutlined(true);
|
127 |
CADTool.selectSymbol.setOutlineColor(CADTool.selectSymbol.getColor().darker()); |
128 |
|
129 |
|
130 |
CopyCADTool copy = new CopyCADTool();
|
131 |
RotateCADTool rotate = new RotateCADTool();
|
132 |
ScaleCADTool scale = new ScaleCADTool();
|
133 |
SymmetryCADTool symmetry=new SymmetryCADTool();
|
134 |
addCADTool("_copy", copy);
|
135 |
addCADTool("_rotate", rotate);
|
136 |
addCADTool("_scale", scale);
|
137 |
addCADTool("_symmetry", symmetry);
|
138 |
// Registramos las teclas de acceso r?pido que vamos a usar.
|
139 |
|
140 |
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0); |
141 |
RefentAccelerator accRef = new RefentAccelerator();
|
142 |
PluginServices.registerKeyStroke(key, accRef); |
143 |
|
144 |
key = KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0); |
145 |
OrtoAccelerator accOrto = new OrtoAccelerator();
|
146 |
PluginServices.registerKeyStroke(key, accOrto); |
147 |
|
148 |
key = KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0); |
149 |
GridAccelerator accGrid = new GridAccelerator();
|
150 |
PluginServices.registerKeyStroke(key, accGrid); |
151 |
|
152 |
key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0); |
153 |
ForceCursorAccelerator accForce = new ForceCursorAccelerator();
|
154 |
PluginServices.registerKeyStroke(key, accForce); |
155 |
|
156 |
|
157 |
KeyboardFocusManager kfm = KeyboardFocusManager |
158 |
.getCurrentKeyboardFocusManager(); |
159 |
kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
164 |
*/
|
165 |
public void execute(String s) { |
166 |
initFocus(); |
167 |
|
168 |
if (s.equals("_spline") || s.equals("_copy") |
169 |
|| s.equals("_equidistance") || s.equals("_matriz") |
170 |
|| s.equals("_symmetry") || s.equals("_rotate") |
171 |
|| s.equals("_stretch") || s.equals("_scale") |
172 |
|| s.equals("_extend") || s.equals("_trim") |
173 |
|| s.equals("_unit")
|
174 |
|| s.equals("_chaflan") || s.equals("_join")) { |
175 |
setCADTool(s, true);
|
176 |
} |
177 |
CADToolAdapter cta=getCADToolAdapter(); |
178 |
cta.configureMenu(); |
179 |
} |
180 |
|
181 |
public static void addCADTool(String name, CADTool c) { |
182 |
CADToolAdapter.addCADTool(name, c); |
183 |
} |
184 |
|
185 |
public static void setCADTool(String text, boolean showCommand) { |
186 |
CADToolAdapter cta=getCADToolAdapter(); |
187 |
CADTool ct=cta.getCADTool(text); |
188 |
|
189 |
if (ct == null) |
190 |
throw new RuntimeException("No such cad tool"); |
191 |
cta.initializeFlatness(); |
192 |
cta.initializeGrid(); |
193 |
cta.setCadTool(ct); |
194 |
ct.init(); |
195 |
if (showCommand) {
|
196 |
if (PluginServices.getMDIManager().getActiveWindow() instanceof View) { |
197 |
View vista = (View) PluginServices.getMDIManager().getActiveWindow(); |
198 |
vista.getConsolePanel().addText("\n" + ct.getName(),
|
199 |
JConsole.COMMAND); |
200 |
cta.askQuestion(); |
201 |
} |
202 |
} |
203 |
// PluginServices.getMainFrame().setSelectedTool("SELECT");
|
204 |
// PluginServices.getMainFrame().enableControls();
|
205 |
} |
206 |
|
207 |
public static CADTool getCADTool() { |
208 |
CADToolAdapter cta=getCADToolAdapter(); |
209 |
return cta.getCadTool();
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
214 |
*/
|
215 |
public boolean isEnabled() { |
216 |
// initFocus();
|
217 |
return true; |
218 |
} |
219 |
|
220 |
/**
|
221 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
222 |
*/
|
223 |
public boolean isVisible() { |
224 |
if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
|
225 |
view = (View) PluginServices.getMDIManager().getActiveWindow();
|
226 |
mapControl = view.getMapControl(); |
227 |
FLayer[] layers = mapControl.getMapContext().getLayers()
|
228 |
.getActives(); |
229 |
if (!(layers[0] instanceof FLyrAnnotation)) { |
230 |
return true; |
231 |
} |
232 |
} |
233 |
return false; |
234 |
} |
235 |
|
236 |
public MapControl getMapControl() {
|
237 |
return getEditionManager().getMapControl();
|
238 |
} |
239 |
|
240 |
class KeyAction extends AbstractAction { |
241 |
|
242 |
private String key; |
243 |
|
244 |
public KeyAction(String key) { |
245 |
this.key = key;
|
246 |
} |
247 |
|
248 |
/*
|
249 |
* (non-Javadoc)
|
250 |
*
|
251 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
252 |
*/
|
253 |
public void actionPerformed(ActionEvent e) { |
254 |
view.focusConsole(key); |
255 |
} |
256 |
|
257 |
} |
258 |
|
259 |
class MyAction extends AbstractAction { |
260 |
private String actionCommand; |
261 |
|
262 |
public MyAction(String command) { |
263 |
actionCommand = command; |
264 |
} |
265 |
|
266 |
/**
|
267 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
268 |
*/
|
269 |
public void actionPerformed(ActionEvent e) { |
270 |
CADToolAdapter cta=getCADToolAdapter(); |
271 |
cta.keyPressed(actionCommand); |
272 |
} |
273 |
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* @author fjp
|
278 |
*
|
279 |
* La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
|
280 |
* la consola de la vista para que salga por all?.
|
281 |
*/
|
282 |
private class myKeyEventPostProcessor implements KeyEventPostProcessor { |
283 |
|
284 |
public boolean postProcessKeyEvent(KeyEvent e) { |
285 |
// System.out.println("KeyEvent e = " + e);
|
286 |
CADToolAdapter cta=getCADToolAdapter(); |
287 |
if ((cta == null) || (view == null)) |
288 |
return false; |
289 |
|
290 |
if (cta.getMapControl() == null){ |
291 |
return false; |
292 |
} |
293 |
|
294 |
if (e.getID() != KeyEvent.KEY_RELEASED) |
295 |
return false; |
296 |
if (!(e.getComponent() instanceof JTextComponent)) { |
297 |
if (e.getKeyCode() == KeyEvent.VK_DELETE) |
298 |
cta.keyPressed("eliminar");
|
299 |
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) |
300 |
cta.keyPressed("escape");
|
301 |
else if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
302 |
// TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
|
303 |
// JTEXTAREA
|
304 |
// QUE NO ES EL DE CONSOLA
|
305 |
if (!(e.getSource() instanceof JTable)) |
306 |
view.focusConsole("");
|
307 |
} |
308 |
else if ((!e.isActionKey())) { |
309 |
//if (Character.isLetterOrDigit(e.getKeyChar())) {
|
310 |
Character keyChar = new Character(e.getKeyChar()); |
311 |
if (e.getComponent().getName() != null) { |
312 |
System.out
|
313 |
.println("Evento de teclado desde el componente "
|
314 |
+ e.getComponent().getName()); |
315 |
if (!e.getComponent().getName().equals(
|
316 |
"CADConsole")) {
|
317 |
view.focusConsole(keyChar + "");
|
318 |
} |
319 |
} else {
|
320 |
if (!(e.getComponent() instanceof JTextComponent)) { |
321 |
view.focusConsole(keyChar + "");
|
322 |
} |
323 |
} |
324 |
//}
|
325 |
} |
326 |
} |
327 |
return false; |
328 |
} |
329 |
|
330 |
} |
331 |
|
332 |
/*
|
333 |
* private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
|
334 |
* key++){ Character keyChar = new Character(key);
|
335 |
* mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
|
336 |
* keyChar); mapControl.getActionMap().put(keyChar, new
|
337 |
* KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
|
338 |
* Character keyChar = new Character(key);
|
339 |
* mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
|
340 |
* keyChar); mapControl.getActionMap().put(keyChar, new
|
341 |
* KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
|
342 |
* Character keyChar = new Character(key);
|
343 |
* mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
|
344 |
* keyChar); mapControl.getActionMap().put(keyChar, new
|
345 |
* KeyAction(keyChar+"")); }
|
346 |
* //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
|
347 |
* 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
|
348 |
* Character keyChar = new
|
349 |
* Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
|
350 |
* mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
|
351 |
* 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
|
352 |
* // El espacio como si fuera INTRO Character keyCharSpace = new
|
353 |
* Character(' ');
|
354 |
* mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
|
355 |
* '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
|
356 |
* KeyAction(""));
|
357 |
*
|
358 |
* }
|
359 |
*/
|
360 |
private static JPopupMenu popup = new JPopupMenu(); |
361 |
|
362 |
public static void clearMenu() { |
363 |
popup.removeAll(); |
364 |
} |
365 |
|
366 |
public static void addMenuEntry(String text) { |
367 |
JMenuItem menu = new JMenuItem(text); |
368 |
menu.setActionCommand(text); |
369 |
menu.setEnabled(true);
|
370 |
menu.setVisible(true);
|
371 |
menu.addActionListener(new ActionListener() { |
372 |
public void actionPerformed(ActionEvent e) { |
373 |
CADToolAdapter cta=getCADToolAdapter(); |
374 |
cta.transition(e.getActionCommand()); |
375 |
} |
376 |
}); |
377 |
|
378 |
popup.add(menu); |
379 |
} |
380 |
|
381 |
public static void showPopup(MouseEvent e) { |
382 |
popup.show(e.getComponent(), e.getX(), e.getY()); |
383 |
} |
384 |
|
385 |
public static View getView() { |
386 |
return view;
|
387 |
} |
388 |
|
389 |
public static void clearView() { |
390 |
view = null;
|
391 |
} |
392 |
|
393 |
/**
|
394 |
* @return Returns the editionManager.
|
395 |
*/
|
396 |
public static EditionManager getEditionManager() { |
397 |
CADToolAdapter cta=getCADToolAdapter(); |
398 |
return cta.getEditionManager();
|
399 |
} |
400 |
|
401 |
public static CADTool[] getCADTools() { |
402 |
return CADToolAdapter.getCADTools();
|
403 |
} |
404 |
|
405 |
public static void initFocus() { |
406 |
view = (View) PluginServices.getMDIManager().getActiveWindow();
|
407 |
MapControl mapControl = view.getMapControl(); |
408 |
CADToolAdapter cta=getCADToolAdapter(); |
409 |
if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")){ |
410 |
// StatusBarListener sbl=new StatusBarListener(view.getMapControl());
|
411 |
// mapControl.addMapTool("cadtooladapter", new Behavior[]{adapter,new MouseMovementBehavior(sbl)});
|
412 |
mapControl.addMapTool("cadtooladapter", cta);
|
413 |
} |
414 |
// view.getMapControl().setTool("cadtooladapter");
|
415 |
JEditTextArea jeta=view.getConsolePanel().getTxt(); |
416 |
jeta.requestFocusInWindow(); |
417 |
jeta.setCaretPosition(jeta.getText().length()); |
418 |
|
419 |
view.addConsoleListener("cad", new ResponseListener() { |
420 |
public void acceptResponse(String response) { |
421 |
CADToolAdapter cta=getCADToolAdapter(); |
422 |
cta.textEntered(response); |
423 |
// TODO:
|
424 |
// FocusManager fm=FocusManager.getCurrentManager();
|
425 |
// fm.focusPreviousComponent(mapControl);
|
426 |
/*
|
427 |
* if (popup.isShowing()){ popup.setVisible(false); }
|
428 |
*/
|
429 |
|
430 |
} |
431 |
}); |
432 |
cta.getEditionManager().setMapControl(mapControl); |
433 |
view.getMapControl().setTool("cadtooladapter");
|
434 |
} |
435 |
} |