Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / CADExtension.java @ 38560

History | View | Annotate | Download (16.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 org.gvsig.editing;
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 org.gvsig.andami.PluginServices;
59
import org.gvsig.andami.PluginsLocator;
60
import org.gvsig.andami.PluginsManager;
61
import org.gvsig.andami.plugins.Extension;
62
import org.gvsig.andami.preferences.IPreference;
63
import org.gvsig.andami.preferences.IPreferenceExtension;
64
import org.gvsig.andami.ui.mdiManager.IWindow;
65
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
66
import org.gvsig.editing.gui.accelerators.ForceCursorAccelerator;
67
import org.gvsig.editing.gui.accelerators.GridAccelerator;
68
import org.gvsig.editing.gui.accelerators.OrtoAccelerator;
69
import org.gvsig.editing.gui.accelerators.RefentAccelerator;
70
import org.gvsig.editing.gui.cad.CADTool;
71
import org.gvsig.editing.gui.cad.CADToolAdapter;
72
import org.gvsig.editing.gui.cad.tools.CopyCADTool;
73
import org.gvsig.editing.gui.cad.tools.RotateCADTool;
74
import org.gvsig.editing.gui.cad.tools.ScaleCADTool;
75
import org.gvsig.editing.gui.cad.tools.SymmetryCADTool;
76
import org.gvsig.editing.gui.preferences.EditingPage;
77
import org.gvsig.editing.project.documents.view.toc.MenuEntry;
78
import org.gvsig.fmap.mapcontext.layers.FLayer;
79
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
80
import org.gvsig.fmap.mapcontrol.MapControl;
81
import org.gvsig.fmap.mapcontrol.MapControlLocator;
82
import org.gvsig.fmap.mapcontrol.MapControlManager;
83
import org.gvsig.tools.dynobject.DynObject;
84
import org.gvsig.utils.console.JConsole;
85
import org.gvsig.utils.console.ResponseListener;
86
import org.gvsig.utils.console.jedit.JEditTextArea;
87

    
88

    
89
/**
90
 * Extensi?n dedicada a controlar las diferentes operaciones sobre el editado de
91
 * una capa.
92
 *
93
 * @author Vicente Caballero Navarro
94
 */
95
public class CADExtension extends Extension implements IPreferenceExtension{
96
        private static HashMap adapters=new HashMap();
97

    
98
        private static DefaultViewPanel view;
99

    
100
        private MapControl mapControl;
101
        private static CADToolAdapter adapter=null;
102
        private EditingPage editingPage=new EditingPage();
103

    
104

    
105
        public static CADToolAdapter getCADToolAdapter(FLayer layer) {
106
                IWindow[] windows=PluginServices.getMDIManager().getAllWindows();
107
                for (int i = 0; i < windows.length; i++) {
108
                        IWindow window=windows[i];
109
                        if (window instanceof DefaultViewPanel){
110
                                DefaultViewPanel v=(DefaultViewPanel)window;
111
                                if (v.getModel().getMapContext().equals(layer.getMapContext())){
112
                                        if (!adapters.containsKey(v.getModel())) {
113
                                                adapters.put(v.getModel(),new CADToolAdapter());
114
                                        }
115
                                        return (CADToolAdapter)adapters.get(v.getModel());
116
                                }
117
                        }
118
                }
119
                return null;
120
        }
121

    
122
        public static CADToolAdapter getCADToolAdapter() {
123
                org.gvsig.andami.ui.mdiManager.IWindow view=PluginServices.getMDIManager().getActiveWindow();
124
                if (view instanceof DefaultViewPanel) {
125
                        DefaultViewPanel v=(DefaultViewPanel)view;
126
                        if (!adapters.containsKey(v.getModel())) {
127
                                adapters.put(v.getModel(),new CADToolAdapter());
128
                        }
129
                        adapter=(CADToolAdapter)adapters.get(v.getModel());
130
                        return adapter;
131
                }
132
                return adapter;
133

    
134
        }
135

    
136
        /**
137
         * @see org.gvsig.andami.plugins.IExtension#initialize()
138
         */
139
        public void initialize() {
140

    
141
                // Registramos los Popup menus:
142
        MenuEntry.register();
143

    
144
                CopyCADTool copy = new CopyCADTool();
145
                RotateCADTool rotate = new RotateCADTool();
146
                ScaleCADTool scale = new ScaleCADTool();
147
                SymmetryCADTool symmetry=new SymmetryCADTool();
148
                addCADTool("_copy", copy);
149
                addCADTool("_rotate", rotate);
150
                addCADTool("_scale", scale);
151
                addCADTool("_symmetry", symmetry);
152
        // Registramos las teclas de acceso r?pido que vamos a usar.
153

    
154
                KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
155
                RefentAccelerator accRef = new RefentAccelerator();
156
                PluginServices.registerKeyStroke(key, accRef);
157

    
158
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0);
159
                OrtoAccelerator accOrto = new OrtoAccelerator();
160
                PluginServices.registerKeyStroke(key, accOrto);
161

    
162
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0);
163
                GridAccelerator accGrid = new GridAccelerator();
164
                PluginServices.registerKeyStroke(key, accGrid);
165

    
166
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
167
                ForceCursorAccelerator accForce = new ForceCursorAccelerator();
168
                PluginServices.registerKeyStroke(key, accForce);
169

    
170

    
171
                KeyboardFocusManager kfm = KeyboardFocusManager
172
                                .getCurrentKeyboardFocusManager();
173
                kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
174

    
175
                registerIcons();
176
        }
177

    
178
        @Override
179
    public void postInitialize() {
180
        //Register the edition symbology
181
        registerEditionSymbology();
182
    }
183

    
184
    /**
185
     * It registers the default symbology used in edition
186
     */
187
    private void registerEditionSymbology() {
188
        PluginsManager pluginsManager = PluginsLocator.getManager();
189
        DynObject dynObject = pluginsManager.getPlugin(getClass()).getPluginProperties();
190
       
191
        MapControlManager mapControlManager = MapControlLocator.getMapControlManager();
192
               
193
        if (dynObject.hasDynValue(EditingPage.DEFAULT_AXIS_REFERENCES_SYMBOL)){
194
            mapControlManager.setAxisReferenceSymbol(
195
                (ISymbol)dynObject.getDynValue(EditingPage.DEFAULT_AXIS_REFERENCES_SYMBOL));
196
        }        
197
     
198
        if (dynObject.hasDynValue(EditingPage.DEFAULT_RECTANGLE_SELECTION_SYMBOL)) {                      
199
            mapControlManager.setGeometrySelectionSymbol(
200
                (ISymbol)dynObject.getDynValue(EditingPage.DEFAULT_RECTANGLE_SELECTION_SYMBOL));
201
        }
202
        
203
        if (dynObject.hasDynValue(EditingPage.DEFAULT_HANDLER_SYMBOL)) {           
204
            mapControlManager.setHandlerSymbol(
205
                (ISymbol)dynObject.getDynValue(EditingPage.DEFAULT_HANDLER_SYMBOL));
206
        }        
207
    }
208

    
209
    private void registerIcons(){
210
                PluginServices.getIconTheme().registerDefault(
211
                                "edition-geometry-copy",
212
                                this.getClass().getClassLoader().getResource("images/Copy.png")
213
                        );
214

    
215
                PluginServices.getIconTheme().registerDefault(
216
                                "edition-geometry-symmetry",
217
                                this.getClass().getClassLoader().getResource("images/Symmetry.png")
218
                        );
219

    
220
                PluginServices.getIconTheme().registerDefault(
221
                                "edition-geometry-rotate",
222
                                this.getClass().getClassLoader().getResource("images/Rotation.png")
223
                        );
224

    
225
                PluginServices.getIconTheme().registerDefault(
226
                                "edition-geometry-scale",
227
                                this.getClass().getClassLoader()
228
                                                .getResource("images/Scale.png"));
229

    
230
                PluginServices.getIconTheme().registerDefault(
231
                                "edition-properties",
232
                                CADExtension.class.getClassLoader().getResource(
233
                                                "images/EditingProperties.png"));
234

    
235
                PluginServices.getIconTheme().registerDefault(
236
                                "field-expresion",
237
                                CADExtension.class.getClassLoader().getResource(
238
                                                "images/FieldExpresion.png"));
239
                PluginServices.getIconTheme().registerDefault(
240
                                "flatness-icon",
241
                                CADExtension.class.getClassLoader().getResource(
242
                                                "images/Flatness.png"));
243
                PluginServices.getIconTheme().registerDefault(
244
                                "grid-icon",
245
                                CADExtension.class.getClassLoader().getResource(
246
                                                "images/Grid.png"));
247
                PluginServices.getIconTheme().registerDefault(
248
                                "snapper-icon",
249
                                CADExtension.class.getClassLoader().getResource(
250
                                                "images/Snapper.png"));
251
        }
252

    
253
        /**
254
         * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
255
         */
256
        public void execute(String s) {
257
                initFocus();
258

    
259
                if (s.equals("_spline") || s.equals("_copy")
260
                                || s.equals("_equidistance") || s.equals("_matriz")
261
                                || s.equals("_symmetry") || s.equals("_rotate")
262
                                || s.equals("_stretch") || s.equals("_scale")
263
                                || s.equals("_extend") || s.equals("_trim")
264
                                || s.equals("_unit")
265
                                || s.equals("_chaflan") || s.equals("_join")) {
266
                        setCADTool(s, true);
267
                }
268
                CADToolAdapter cta=getCADToolAdapter();
269
                cta.configureMenu();
270
        }
271

    
272
        public static void addCADTool(String name, CADTool c) {
273
                CADToolAdapter.addCADTool(name, c);
274
        }
275

    
276
        public static void setCADTool(String text, boolean showCommand) {
277
                CADToolAdapter cta=getCADToolAdapter();
278
                CADTool ct=cta.getCADTool(text);
279

    
280
                if (ct == null)
281
                        throw new RuntimeException("No such cad tool");
282
                cta.initializeFlatness();
283
                cta.setCadTool(ct);
284
                ct.init();
285
                if (showCommand) {
286
                        if (PluginServices.getMDIManager().getActiveWindow() instanceof DefaultViewPanel) {
287
                                DefaultViewPanel vista = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow();
288
                                vista.getConsolePanel().addText("\n" + ct.getName(),
289
                                        JConsole.COMMAND);
290
                                cta.askQuestion();
291
                        }
292
                }
293
                // PluginServices.getMainFrame().setSelectedTool("SELECT");
294
                // PluginServices.getMainFrame().enableControls();
295
        }
296

    
297
        public static CADTool getCADTool() {
298
                CADToolAdapter cta=getCADToolAdapter();
299
                return cta.getCadTool();
300
        }
301

    
302
        /**
303
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
304
         */
305
        public boolean isEnabled() {
306
                // initFocus();
307
                return true;
308
        }
309

    
310
        /**
311
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
312
         */
313
        public boolean isVisible() {
314
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
315
                        view = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow();
316
                        mapControl = view.getMapControl();
317
                        FLayer[] layers = mapControl.getMapContext().getLayers()
318
                                        .getActives();
319
//                        if (!(layers[0] instanceof FLyrAnnotation)) {
320
//                                return true;
321
//                        }
322
                        return true;
323
                }
324
                return false;
325
        }
326

    
327
        public MapControl getMapControl() {
328
                return getEditionManager().getMapControl();
329
        }
330

    
331
        class KeyAction extends AbstractAction {
332

    
333
                private String key;
334

    
335
                public KeyAction(String key) {
336
                        this.key = key;
337
                }
338

    
339
                /*
340
                 * (non-Javadoc)
341
                 *
342
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
343
                 */
344
                public void actionPerformed(ActionEvent e) {
345
                        view.focusConsole(key);
346
                }
347

    
348
        }
349

    
350
        class MyAction extends AbstractAction {
351
                private String actionCommand;
352

    
353
                public MyAction(String command) {
354
                        actionCommand = command;
355
                }
356

    
357
                /**
358
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
359
                 */
360
                public void actionPerformed(ActionEvent e) {
361
                        CADToolAdapter cta=getCADToolAdapter();
362
                        cta.keyPressed(actionCommand);
363
                }
364

    
365
        }
366

    
367
        /**
368
         * @author fjp
369
         *
370
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
371
         * la consola de la vista para que salga por all?.
372
         */
373
        private class myKeyEventPostProcessor implements KeyEventPostProcessor {
374

    
375
                public boolean postProcessKeyEvent(KeyEvent e) {
376
                        // System.out.println("KeyEvent e = " + e);
377
                        CADToolAdapter cta=getCADToolAdapter();
378
                        if ((cta == null) || (view == null))
379
                                return false;
380

    
381
                        if (cta.getMapControl() == null){
382
                                return false;
383
                        }
384

    
385
                        if (e.getID() != KeyEvent.KEY_RELEASED)
386
                                return false;
387
                        if (!(e.getComponent() instanceof JTextComponent)) {
388
                                if (e.getKeyCode() == KeyEvent.VK_DELETE)
389
                                        cta.keyPressed("eliminar");
390
                                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
391
                                        cta.keyPressed("escape");
392
                                else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
393
                                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
394
                                        // JTEXTAREA
395
                                        // QUE NO ES EL DE CONSOLA
396
                                        if (!(e.getSource() instanceof JTable))
397
                                                view.focusConsole("");
398
                                }
399
                                else if ((!e.isActionKey() && e.getKeyCode()!=KeyEvent.VK_TAB)) {
400
                                                //if (Character.isLetterOrDigit(e.getKeyChar())) {
401
                                                        Character keyChar = new Character(e.getKeyChar());
402
                                                        if (e.getComponent().getName() != null) {
403
                                                                System.out
404
                                                                                .println("Evento de teclado desde el componente "
405
                                                                                                + e.getComponent().getName());
406
                                                                if (!e.getComponent().getName().equals(
407
                                                                                "CADConsole")) {
408
                                                                        view.focusConsole(keyChar + "");
409
                                                                }
410
                                                        } else {
411
                                                                if (!(e.getComponent() instanceof JTextComponent)) {
412
                                                                        view.focusConsole(keyChar + "");
413
                                                                }
414
                                                        }
415
                                                //}
416
                                        }
417
                                }
418
                        return false;
419
                }
420

    
421
        }
422

    
423
        /*
424
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
425
         * key++){ Character keyChar = new Character(key);
426
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
427
         * keyChar); mapControl.getActionMap().put(keyChar, new
428
         * KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
429
         * Character keyChar = new Character(key);
430
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
431
         * keyChar); mapControl.getActionMap().put(keyChar, new
432
         * KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
433
         * Character keyChar = new Character(key);
434
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
435
         * keyChar); mapControl.getActionMap().put(keyChar, new
436
         * KeyAction(keyChar+"")); }
437
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
438
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
439
         * Character keyChar = new
440
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
441
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
442
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
443
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
444
         * Character(' ');
445
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
446
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
447
         * KeyAction(""));
448
         *
449
         *  }
450
         */
451
        private static JPopupMenu popup = new JPopupMenu();
452

    
453
        public static void clearMenu() {
454
                popup.removeAll();
455
        }
456

    
457
        public static void addMenuEntry(String text) {
458
                JMenuItem menu = new JMenuItem(text);
459
                menu.setActionCommand(text);
460
                menu.setEnabled(true);
461
                menu.setVisible(true);
462
                menu.addActionListener(new ActionListener() {
463
                        public void actionPerformed(ActionEvent e) {
464
                                CADToolAdapter cta=getCADToolAdapter();
465
                                cta.transition(e.getActionCommand());
466
                        }
467
                });
468

    
469
                popup.add(menu);
470
        }
471

    
472
        public static void showPopup(MouseEvent e) {
473
                popup.show(e.getComponent(), e.getX(), e.getY());
474
        }
475

    
476
        public static DefaultViewPanel getView() {
477
                return view;
478
        }
479

    
480
        public static void clearView() {
481
                view = null;
482
        }
483

    
484
        /**
485
         * @return Returns the editionManager.
486
         */
487
        public static EditionManager getEditionManager() {
488
                CADToolAdapter cta=getCADToolAdapter();
489
                return cta.getEditionManager();
490
        }
491

    
492
        public static CADTool[] getCADTools() {
493
                return CADToolAdapter.getCADTools();
494
        }
495

    
496
        public static void initFocus() {
497
                view = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow();
498
                MapControl mapControl = view.getMapControl();
499
                CADToolAdapter cta=getCADToolAdapter();
500
                if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")){
501
                        // StatusBarListener sbl=new StatusBarListener(view.getMapControl());
502
                        // mapControl.addMapTool("cadtooladapter",  new Behavior[]{adapter,new MouseMovementBehavior(sbl)});
503
                        mapControl.addBehavior("cadtooladapter", cta);
504
                }
505
                // view.getMapControl().setTool("cadtooladapter");
506
                JEditTextArea jeta=view.getConsolePanel().getTxt();
507
                jeta.requestFocusInWindow();
508
                jeta.setCaretPosition(jeta.getText().length());
509

    
510
                view.addConsoleListener("cad", new ResponseListener() {
511
                        public void acceptResponse(String response) {
512
                                CADToolAdapter cta=getCADToolAdapter();
513
                                cta.textEntered(response);
514
                                // TODO:
515
                                // FocusManager fm=FocusManager.getCurrentManager();
516
                                // fm.focusPreviousComponent(mapControl);
517
                                /*
518
                                 * if (popup.isShowing()){ popup.setVisible(false); }
519
                                 */
520

    
521
                        }
522
                });
523
                cta.getEditionManager().setMapControl(mapControl);
524
                view.getMapControl().setTool("cadtooladapter");
525
        }
526

    
527
        public IPreference[] getPreferencesPages() {
528
                IPreference[] preferences=new IPreference[1];
529
                preferences[0]=editingPage;
530
                return preferences;
531
        }
532
}