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