Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / CADExtension.java @ 11434

History | View | Annotate | Download (13.8 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.andami.preferences.IPreference;
61
import com.iver.andami.preferences.IPreferenceExtension;
62
import com.iver.cit.gvsig.fmap.MapControl;
63
import com.iver.cit.gvsig.fmap.layers.FLayer;
64
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
65
import com.iver.cit.gvsig.gui.accelerators.ForceCursorAccelerator;
66
import com.iver.cit.gvsig.gui.accelerators.GridAccelerator;
67
import com.iver.cit.gvsig.gui.accelerators.OrtoAccelerator;
68
import com.iver.cit.gvsig.gui.accelerators.RefentAccelerator;
69
import com.iver.cit.gvsig.gui.cad.CADTool;
70
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
71
import com.iver.cit.gvsig.gui.cad.tools.CopyCADTool;
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.SymmetryCADTool;
75
import com.iver.cit.gvsig.gui.preferences.EditingPage;
76
import com.iver.cit.gvsig.project.documents.view.gui.View;
77
import com.iver.cit.gvsig.project.documents.view.snapping.Snapping;
78
import com.iver.cit.gvsig.project.documents.view.toc.MenuEntry;
79
import com.iver.utiles.console.JConsole;
80
import com.iver.utiles.console.ResponseListener;
81
import com.iver.utiles.console.jedit.JEditTextArea;
82

    
83
/**
84
 * Extensi?n dedicada a controlar las diferentes operaciones sobre el editado de
85
 * una capa.
86
 *
87
 * @author Vicente Caballero Navarro
88
 */
89
public class CADExtension extends Extension implements IPreferenceExtension{
90
        private static HashMap adapters=new HashMap();
91

    
92
        private static View view;
93

    
94
        private MapControl mapControl;
95
        private static CADToolAdapter adapter=null;
96
        private EditingPage editingPage=new EditingPage();
97

    
98
        public static CADToolAdapter getCADToolAdapter() {
99
                com.iver.andami.ui.mdiManager.IWindow view=PluginServices.getMDIManager().getActiveWindow();
100
                if (view instanceof View) {
101
                        View v=(View)view;
102
                        if (!adapters.containsKey(v.getModel())) {
103
                                adapters.put(v.getModel(),new CADToolAdapter());
104
                        }
105
                        adapter=(CADToolAdapter)adapters.get(v.getModel());
106
                        return adapter;
107
                }
108
                return adapter;
109

    
110
        }
111

    
112
        /**
113
         * @see com.iver.andami.plugins.IExtension#initialize()
114
         */
115
        public void initialize() {
116

    
117
                // Registramos los Popup menus:
118
        MenuEntry.register();
119
        Snapping.register();
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
// jaume, this should be unnecessary
123
//                CADTool.drawingSymbol.setOutlined(true);
124
//                CADTool.drawingSymbol.setOutlineColor(CADTool.drawingSymbol.getColor().darker());
125
//                CADTool.modifySymbol.setOutlined(true);
126
//                CADTool.modifySymbol.setOutlineColor(CADTool.modifySymbol.getColor().darker());
127
//                CADTool.selectSymbol.setOutlined(true);
128
//                CADTool.selectSymbol.setOutlineColor(CADTool.selectSymbol.getColor().darker());
129

    
130

    
131
                CopyCADTool copy = new CopyCADTool();
132
                RotateCADTool rotate = new RotateCADTool();
133
                ScaleCADTool scale = new ScaleCADTool();
134
                SymmetryCADTool symmetry=new SymmetryCADTool();
135
                addCADTool("_copy", copy);
136
                addCADTool("_rotate", rotate);
137
                addCADTool("_scale", scale);
138
                addCADTool("_symmetry", symmetry);
139
        // Registramos las teclas de acceso r?pido que vamos a usar.
140

    
141
                KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
142
                RefentAccelerator accRef = new RefentAccelerator();
143
                PluginServices.registerKeyStroke(key, accRef);
144

    
145
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0);
146
                OrtoAccelerator accOrto = new OrtoAccelerator();
147
                PluginServices.registerKeyStroke(key, accOrto);
148

    
149
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0);
150
                GridAccelerator accGrid = new GridAccelerator();
151
                PluginServices.registerKeyStroke(key, accGrid);
152

    
153
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
154
                ForceCursorAccelerator accForce = new ForceCursorAccelerator();
155
                PluginServices.registerKeyStroke(key, accForce);
156

    
157

    
158
                KeyboardFocusManager kfm = KeyboardFocusManager
159
                                .getCurrentKeyboardFocusManager();
160
                kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
161
        }
162

    
163
        /**
164
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
165
         */
166
        public void execute(String s) {
167
                initFocus();
168

    
169
                if (s.equals("_spline") || s.equals("_copy")
170
                                || s.equals("_equidistance") || s.equals("_matriz")
171
                                || s.equals("_symmetry") || s.equals("_rotate")
172
                                || s.equals("_stretch") || s.equals("_scale")
173
                                || s.equals("_extend") || s.equals("_trim")
174
                                || s.equals("_unit")
175
                                || s.equals("_chaflan") || s.equals("_join")) {
176
                        setCADTool(s, true);
177
                }
178
                CADToolAdapter cta=getCADToolAdapter();
179
                cta.configureMenu();
180
        }
181

    
182
        public static void addCADTool(String name, CADTool c) {
183
                CADToolAdapter.addCADTool(name, c);
184
        }
185

    
186
        public static void setCADTool(String text, boolean showCommand) {
187
                CADToolAdapter cta=getCADToolAdapter();
188
                CADTool ct=cta.getCADTool(text);
189

    
190
                if (ct == null)
191
                        throw new RuntimeException("No such cad tool");
192
                cta.initializeFlatness();
193
                cta.initializeGrid();
194
                cta.setCadTool(ct);
195
                ct.init();
196
                if (showCommand) {
197
                        if (PluginServices.getMDIManager().getActiveWindow() instanceof View) {
198
                                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
199
                                vista.getConsolePanel().addText("\n" + ct.getName(),
200
                                        JConsole.COMMAND);
201
                                cta.askQuestion();
202
                        }
203
                }
204
                // PluginServices.getMainFrame().setSelectedTool("SELECT");
205
                // PluginServices.getMainFrame().enableControls();
206
        }
207

    
208
        public static CADTool getCADTool() {
209
                CADToolAdapter cta=getCADToolAdapter();
210
                return cta.getCadTool();
211
        }
212

    
213
        /**
214
         * @see com.iver.andami.plugins.IExtension#isEnabled()
215
         */
216
        public boolean isEnabled() {
217
                // initFocus();
218
                return true;
219
        }
220

    
221
        /**
222
         * @see com.iver.andami.plugins.IExtension#isVisible()
223
         */
224
        public boolean isVisible() {
225
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
226
                        view = (View) PluginServices.getMDIManager().getActiveWindow();
227
                        mapControl = view.getMapControl();
228
                        FLayer[] layers = mapControl.getMapContext().getLayers()
229
                                        .getActives();
230
                        if (!(layers[0] instanceof FLyrAnnotation)) {
231
                                return true;
232
                        }
233
                }
234
                return false;
235
        }
236

    
237
        public MapControl getMapControl() {
238
                return getEditionManager().getMapControl();
239
        }
240

    
241
        class KeyAction extends AbstractAction {
242

    
243
                private String key;
244

    
245
                public KeyAction(String key) {
246
                        this.key = key;
247
                }
248

    
249
                /*
250
                 * (non-Javadoc)
251
                 *
252
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
253
                 */
254
                public void actionPerformed(ActionEvent e) {
255
                        view.focusConsole(key);
256
                }
257

    
258
        }
259

    
260
        class MyAction extends AbstractAction {
261
                private String actionCommand;
262

    
263
                public MyAction(String command) {
264
                        actionCommand = command;
265
                }
266

    
267
                /**
268
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
269
                 */
270
                public void actionPerformed(ActionEvent e) {
271
                        CADToolAdapter cta=getCADToolAdapter();
272
                        cta.keyPressed(actionCommand);
273
                }
274

    
275
        }
276

    
277
        /**
278
         * @author fjp
279
         *
280
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
281
         * la consola de la vista para que salga por all?.
282
         */
283
        private class myKeyEventPostProcessor implements KeyEventPostProcessor {
284

    
285
                public boolean postProcessKeyEvent(KeyEvent e) {
286
                        // System.out.println("KeyEvent e = " + e);
287
                        CADToolAdapter cta=getCADToolAdapter();
288
                        if ((cta == null) || (view == null))
289
                                return false;
290

    
291
                        if (cta.getMapControl() == null){
292
                                return false;
293
                        }
294

    
295
                        if (e.getID() != KeyEvent.KEY_RELEASED)
296
                                return false;
297
                        if (!(e.getComponent() instanceof JTextComponent)) {
298
                                if (e.getKeyCode() == KeyEvent.VK_DELETE)
299
                                        cta.keyPressed("eliminar");
300
                                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
301
                                        cta.keyPressed("escape");
302
                                else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
303
                                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
304
                                        // JTEXTAREA
305
                                        // QUE NO ES EL DE CONSOLA
306
                                        if (!(e.getSource() instanceof JTable))
307
                                                view.focusConsole("");
308
                                }
309
                                else if ((!e.isActionKey())) {
310
                                                //if (Character.isLetterOrDigit(e.getKeyChar())) {
311
                                                        Character keyChar = new Character(e.getKeyChar());
312
                                                        if (e.getComponent().getName() != null) {
313
                                                                System.out
314
                                                                                .println("Evento de teclado desde el componente "
315
                                                                                                + e.getComponent().getName());
316
                                                                if (!e.getComponent().getName().equals(
317
                                                                                "CADConsole")) {
318
                                                                        view.focusConsole(keyChar + "");
319
                                                                }
320
                                                        } else {
321
                                                                if (!(e.getComponent() instanceof JTextComponent)) {
322
                                                                        view.focusConsole(keyChar + "");
323
                                                                }
324
                                                        }
325
                                                //}
326
                                        }
327
                                }
328
                        return false;
329
                }
330

    
331
        }
332

    
333
        /*
334
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
335
         * key++){ Character keyChar = new Character(key);
336
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
337
         * keyChar); mapControl.getActionMap().put(keyChar, new
338
         * KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
339
         * Character keyChar = new Character(key);
340
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
341
         * keyChar); mapControl.getActionMap().put(keyChar, new
342
         * KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
343
         * Character keyChar = new Character(key);
344
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
345
         * keyChar); mapControl.getActionMap().put(keyChar, new
346
         * KeyAction(keyChar+"")); }
347
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
348
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
349
         * Character keyChar = new
350
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
351
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
352
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
353
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
354
         * Character(' ');
355
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
356
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
357
         * KeyAction(""));
358
         *
359
         *  }
360
         */
361
        private static JPopupMenu popup = new JPopupMenu();
362

    
363
        public static void clearMenu() {
364
                popup.removeAll();
365
        }
366

    
367
        public static void addMenuEntry(String text) {
368
                JMenuItem menu = new JMenuItem(text);
369
                menu.setActionCommand(text);
370
                menu.setEnabled(true);
371
                menu.setVisible(true);
372
                menu.addActionListener(new ActionListener() {
373
                        public void actionPerformed(ActionEvent e) {
374
                                CADToolAdapter cta=getCADToolAdapter();
375
                                cta.transition(e.getActionCommand());
376
                        }
377
                });
378

    
379
                popup.add(menu);
380
        }
381

    
382
        public static void showPopup(MouseEvent e) {
383
                popup.show(e.getComponent(), e.getX(), e.getY());
384
        }
385

    
386
        public static View getView() {
387
                return view;
388
        }
389

    
390
        public static void clearView() {
391
                view = null;
392
        }
393

    
394
        /**
395
         * @return Returns the editionManager.
396
         */
397
        public static EditionManager getEditionManager() {
398
                CADToolAdapter cta=getCADToolAdapter();
399
                return cta.getEditionManager();
400
        }
401

    
402
        public static CADTool[] getCADTools() {
403
                return CADToolAdapter.getCADTools();
404
        }
405

    
406
        public static void initFocus() {
407
                view = (View) PluginServices.getMDIManager().getActiveWindow();
408
                MapControl mapControl = view.getMapControl();
409
                CADToolAdapter cta=getCADToolAdapter();
410
                if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")){
411
                        // StatusBarListener sbl=new StatusBarListener(view.getMapControl());
412
                        // mapControl.addMapTool("cadtooladapter",  new Behavior[]{adapter,new MouseMovementBehavior(sbl)});
413
                        mapControl.addMapTool("cadtooladapter", cta);
414
                }
415
                // view.getMapControl().setTool("cadtooladapter");
416
                JEditTextArea jeta=view.getConsolePanel().getTxt();
417
                jeta.requestFocusInWindow();
418
                jeta.setCaretPosition(jeta.getText().length());
419

    
420
                view.addConsoleListener("cad", new ResponseListener() {
421
                        public void acceptResponse(String response) {
422
                                CADToolAdapter cta=getCADToolAdapter();
423
                                cta.textEntered(response);
424
                                // TODO:
425
                                // FocusManager fm=FocusManager.getCurrentManager();
426
                                // fm.focusPreviousComponent(mapControl);
427
                                /*
428
                                 * if (popup.isShowing()){ popup.setVisible(false); }
429
                                 */
430

    
431
                        }
432
                });
433
                cta.getEditionManager().setMapControl(mapControl);
434
                view.getMapControl().setTool("cadtooladapter");
435
        }
436

    
437
        public IPreference[] getPreferencesPages() {
438
                IPreference[] preferences=new IPreference[1];
439
                preferences[0]=editingPage;
440
                return preferences;
441
        }
442
}