Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / CADExtension.java @ 15637

History | View | Annotate | Download (14.5 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
                registerIcons();
163
        }
164

    
165
        private void registerIcons(){
166
                PluginServices.getIconTheme().registerDefault(
167
                                "edition-geometry-copy",
168
                                this.getClass().getClassLoader().getResource("images/Copy.png")
169
                        );
170

    
171
                PluginServices.getIconTheme().registerDefault(
172
                                "edition-geometry-symmetry",
173
                                this.getClass().getClassLoader().getResource("images/Symmetry.png")
174
                        );
175

    
176
                PluginServices.getIconTheme().registerDefault(
177
                                "edition-geometry-rotate",
178
                                this.getClass().getClassLoader().getResource("images/Rotation.png")
179
                        );
180

    
181
                PluginServices.getIconTheme().registerDefault(
182
                                "edition-geometry-scale",
183
                                this.getClass().getClassLoader().getResource("images/Scale.png")
184
                        );
185
                
186
                
187

    
188

    
189
        }
190

    
191
        /**
192
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
193
         */
194
        public void execute(String s) {
195
                initFocus();
196

    
197
                if (s.equals("_spline") || s.equals("_copy")
198
                                || s.equals("_equidistance") || s.equals("_matriz")
199
                                || s.equals("_symmetry") || s.equals("_rotate")
200
                                || s.equals("_stretch") || s.equals("_scale")
201
                                || s.equals("_extend") || s.equals("_trim")
202
                                || s.equals("_unit")
203
                                || s.equals("_chaflan") || s.equals("_join")) {
204
                        setCADTool(s, true);
205
                }
206
                CADToolAdapter cta=getCADToolAdapter();
207
                cta.configureMenu();
208
        }
209

    
210
        public static void addCADTool(String name, CADTool c) {
211
                CADToolAdapter.addCADTool(name, c);
212
        }
213

    
214
        public static void setCADTool(String text, boolean showCommand) {
215
                CADToolAdapter cta=getCADToolAdapter();
216
                CADTool ct=cta.getCADTool(text);
217

    
218
                if (ct == null)
219
                        throw new RuntimeException("No such cad tool");
220
                cta.initializeFlatness();
221
                cta.initializeGrid();
222
                cta.setCadTool(ct);
223
                ct.init();
224
                if (showCommand) {
225
                        if (PluginServices.getMDIManager().getActiveWindow() instanceof View) {
226
                                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
227
                                vista.getConsolePanel().addText("\n" + ct.getName(),
228
                                        JConsole.COMMAND);
229
                                cta.askQuestion();
230
                        }
231
                }
232
                // PluginServices.getMainFrame().setSelectedTool("SELECT");
233
                // PluginServices.getMainFrame().enableControls();
234
        }
235

    
236
        public static CADTool getCADTool() {
237
                CADToolAdapter cta=getCADToolAdapter();
238
                return cta.getCadTool();
239
        }
240

    
241
        /**
242
         * @see com.iver.andami.plugins.IExtension#isEnabled()
243
         */
244
        public boolean isEnabled() {
245
                // initFocus();
246
                return true;
247
        }
248

    
249
        /**
250
         * @see com.iver.andami.plugins.IExtension#isVisible()
251
         */
252
        public boolean isVisible() {
253
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
254
                        view = (View) PluginServices.getMDIManager().getActiveWindow();
255
                        mapControl = view.getMapControl();
256
                        FLayer[] layers = mapControl.getMapContext().getLayers()
257
                                        .getActives();
258
                        if (!(layers[0] instanceof FLyrAnnotation)) {
259
                                return true;
260
                        }
261
                }
262
                return false;
263
        }
264

    
265
        public MapControl getMapControl() {
266
                return getEditionManager().getMapControl();
267
        }
268

    
269
        class KeyAction extends AbstractAction {
270

    
271
                private String key;
272

    
273
                public KeyAction(String key) {
274
                        this.key = key;
275
                }
276

    
277
                /*
278
                 * (non-Javadoc)
279
                 *
280
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
281
                 */
282
                public void actionPerformed(ActionEvent e) {
283
                        view.focusConsole(key);
284
                }
285

    
286
        }
287

    
288
        class MyAction extends AbstractAction {
289
                private String actionCommand;
290

    
291
                public MyAction(String command) {
292
                        actionCommand = command;
293
                }
294

    
295
                /**
296
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
297
                 */
298
                public void actionPerformed(ActionEvent e) {
299
                        CADToolAdapter cta=getCADToolAdapter();
300
                        cta.keyPressed(actionCommand);
301
                }
302

    
303
        }
304

    
305
        /**
306
         * @author fjp
307
         *
308
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
309
         * la consola de la vista para que salga por all?.
310
         */
311
        private class myKeyEventPostProcessor implements KeyEventPostProcessor {
312

    
313
                public boolean postProcessKeyEvent(KeyEvent e) {
314
                        // System.out.println("KeyEvent e = " + e);
315
                        CADToolAdapter cta=getCADToolAdapter();
316
                        if ((cta == null) || (view == null))
317
                                return false;
318

    
319
                        if (cta.getMapControl() == null){
320
                                return false;
321
                        }
322

    
323
                        if (e.getID() != KeyEvent.KEY_RELEASED)
324
                                return false;
325
                        if (!(e.getComponent() instanceof JTextComponent)) {
326
                                if (e.getKeyCode() == KeyEvent.VK_DELETE)
327
                                        cta.keyPressed("eliminar");
328
                                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
329
                                        cta.keyPressed("escape");
330
                                else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
331
                                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
332
                                        // JTEXTAREA
333
                                        // QUE NO ES EL DE CONSOLA
334
                                        if (!(e.getSource() instanceof JTable))
335
                                                view.focusConsole("");
336
                                }
337
                                else if ((!e.isActionKey() && e.getKeyCode()!=KeyEvent.VK_TAB)) {
338
                                                //if (Character.isLetterOrDigit(e.getKeyChar())) {
339
                                                        Character keyChar = new Character(e.getKeyChar());
340
                                                        if (e.getComponent().getName() != null) {
341
                                                                System.out
342
                                                                                .println("Evento de teclado desde el componente "
343
                                                                                                + e.getComponent().getName());
344
                                                                if (!e.getComponent().getName().equals(
345
                                                                                "CADConsole")) {
346
                                                                        view.focusConsole(keyChar + "");
347
                                                                }
348
                                                        } else {
349
                                                                if (!(e.getComponent() instanceof JTextComponent)) {
350
                                                                        view.focusConsole(keyChar + "");
351
                                                                }
352
                                                        }
353
                                                //}
354
                                        }
355
                                }
356
                        return false;
357
                }
358

    
359
        }
360

    
361
        /*
362
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
363
         * key++){ Character keyChar = new Character(key);
364
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
365
         * keyChar); mapControl.getActionMap().put(keyChar, new
366
         * KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
367
         * Character keyChar = new Character(key);
368
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
369
         * keyChar); mapControl.getActionMap().put(keyChar, new
370
         * KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
371
         * Character keyChar = new Character(key);
372
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
373
         * keyChar); mapControl.getActionMap().put(keyChar, new
374
         * KeyAction(keyChar+"")); }
375
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
376
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
377
         * Character keyChar = new
378
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
379
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
380
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
381
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
382
         * Character(' ');
383
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
384
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
385
         * KeyAction(""));
386
         *
387
         *  }
388
         */
389
        private static JPopupMenu popup = new JPopupMenu();
390

    
391
        public static void clearMenu() {
392
                popup.removeAll();
393
        }
394

    
395
        public static void addMenuEntry(String text) {
396
                JMenuItem menu = new JMenuItem(text);
397
                menu.setActionCommand(text);
398
                menu.setEnabled(true);
399
                menu.setVisible(true);
400
                menu.addActionListener(new ActionListener() {
401
                        public void actionPerformed(ActionEvent e) {
402
                                CADToolAdapter cta=getCADToolAdapter();
403
                                cta.transition(e.getActionCommand());
404
                        }
405
                });
406

    
407
                popup.add(menu);
408
        }
409

    
410
        public static void showPopup(MouseEvent e) {
411
                popup.show(e.getComponent(), e.getX(), e.getY());
412
        }
413

    
414
        public static View getView() {
415
                return view;
416
        }
417

    
418
        public static void clearView() {
419
                view = null;
420
        }
421

    
422
        /**
423
         * @return Returns the editionManager.
424
         */
425
        public static EditionManager getEditionManager() {
426
                CADToolAdapter cta=getCADToolAdapter();
427
                return cta.getEditionManager();
428
        }
429

    
430
        public static CADTool[] getCADTools() {
431
                return CADToolAdapter.getCADTools();
432
        }
433

    
434
        public static void initFocus() {
435
                view = (View) PluginServices.getMDIManager().getActiveWindow();
436
                MapControl mapControl = view.getMapControl();
437
                CADToolAdapter cta=getCADToolAdapter();
438
                if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")){
439
                        // StatusBarListener sbl=new StatusBarListener(view.getMapControl());
440
                        // mapControl.addMapTool("cadtooladapter",  new Behavior[]{adapter,new MouseMovementBehavior(sbl)});
441
                        mapControl.addMapTool("cadtooladapter", cta);
442
                }
443
                // view.getMapControl().setTool("cadtooladapter");
444
                JEditTextArea jeta=view.getConsolePanel().getTxt();
445
                jeta.requestFocusInWindow();
446
                jeta.setCaretPosition(jeta.getText().length());
447

    
448
                view.addConsoleListener("cad", new ResponseListener() {
449
                        public void acceptResponse(String response) {
450
                                CADToolAdapter cta=getCADToolAdapter();
451
                                cta.textEntered(response);
452
                                // TODO:
453
                                // FocusManager fm=FocusManager.getCurrentManager();
454
                                // fm.focusPreviousComponent(mapControl);
455
                                /*
456
                                 * if (popup.isShowing()){ popup.setVisible(false); }
457
                                 */
458

    
459
                        }
460
                });
461
                cta.getEditionManager().setMapControl(mapControl);
462
                view.getMapControl().setTool("cadtooladapter");
463
        }
464

    
465
        public IPreference[] getPreferencesPages() {
466
                IPreference[] preferences=new IPreference[1];
467
                preferences[0]=editingPage;
468
                return preferences;
469
        }
470
}