Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / CADExtension.java @ 40557

History | View | Annotate | Download (16.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing;
25

    
26
import java.awt.KeyEventPostProcessor;
27
import java.awt.KeyboardFocusManager;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.awt.event.KeyEvent;
31
import java.awt.event.MouseEvent;
32
import java.util.HashMap;
33

    
34
import javax.swing.AbstractAction;
35
import javax.swing.JMenuItem;
36
import javax.swing.JPopupMenu;
37
import javax.swing.JTable;
38
import javax.swing.KeyStroke;
39
import javax.swing.text.JTextComponent;
40

    
41
import org.gvsig.andami.IconThemeHelper;
42
import org.gvsig.andami.PluginServices;
43
import org.gvsig.andami.PluginsLocator;
44
import org.gvsig.andami.PluginsManager;
45
import org.gvsig.andami.plugins.Extension;
46
import org.gvsig.andami.preferences.IPreference;
47
import org.gvsig.andami.preferences.IPreferenceExtension;
48
import org.gvsig.andami.ui.mdiManager.IWindow;
49
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
50
import org.gvsig.editing.gui.accelerators.ForceCursorAccelerator;
51
import org.gvsig.editing.gui.accelerators.GridAccelerator;
52
import org.gvsig.editing.gui.accelerators.OrtoAccelerator;
53
import org.gvsig.editing.gui.accelerators.RefentAccelerator;
54
import org.gvsig.editing.gui.cad.CADTool;
55
import org.gvsig.editing.gui.cad.CADToolAdapter;
56
import org.gvsig.editing.gui.cad.tools.CopyCADTool;
57
import org.gvsig.editing.gui.cad.tools.RotateCADTool;
58
import org.gvsig.editing.gui.cad.tools.ScaleCADTool;
59
import org.gvsig.editing.gui.cad.tools.SymmetryCADTool;
60
import org.gvsig.editing.gui.preferences.EditingPage;
61
import org.gvsig.editing.project.documents.view.toc.MenuEntry;
62
import org.gvsig.fmap.mapcontext.layers.FLayer;
63
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
64
import org.gvsig.fmap.mapcontrol.MapControl;
65
import org.gvsig.fmap.mapcontrol.MapControlLocator;
66
import org.gvsig.fmap.mapcontrol.MapControlManager;
67
import org.gvsig.tools.dynobject.DynObject;
68
import org.gvsig.utils.console.JConsole;
69
import org.gvsig.utils.console.ResponseListener;
70
import org.gvsig.utils.console.jedit.JEditTextArea;
71
import org.slf4j.Logger;
72
import org.slf4j.LoggerFactory;
73

    
74

    
75
/**
76
 * Extensi?n dedicada a controlar las diferentes operaciones sobre el editado de
77
 * una capa.
78
 *
79
 * @author Vicente Caballero Navarro
80
 */
81
public class CADExtension extends Extension implements IPreferenceExtension{
82
        private static Logger logger = LoggerFactory.getLogger(CADExtension.class);
83

    
84
        private static DefaultViewPanel view;
85
        private MapControl mapControl;
86
        private EditingPage editingPage=new EditingPage();
87

    
88

    
89
        /**
90
     * Gets the CADToolAdapter of the view where the layer is
91
     * contained or null if the layer is not found
92
     * in any view
93
         *  
94
         * @param layer
95
         * @return
96
         * 
97
     * @deprecated Use same method in {@link EditionLocator}
98
         */
99
        public static CADToolAdapter getCADToolAdapter(FLayer layer) {
100
            
101
            return EditionLocator.getCADToolAdapter(layer);
102
        }
103

    
104
        /**
105
         * Gets the CADToolAdapter of the current active view
106
         * or the CADToolAdapter of the view that was active more
107
         * recently (if there is not an active view)
108
         * or null if there never was an active view
109
         * 
110
         * @return
111
         * 
112
     * @deprecated Use same method in {@link EditionLocator}
113
         */
114
        public static CADToolAdapter getCADToolAdapter() {
115
            
116
            return EditionLocator.getCADToolAdapter();
117
        }
118

    
119
        /**
120
         * @see org.gvsig.andami.plugins.IExtension#initialize()
121
         */
122
        public void initialize() {
123

    
124
                // Registramos los Popup menus:
125
        MenuEntry.register();
126

    
127
                CopyCADTool copy = new CopyCADTool();
128
                RotateCADTool rotate = new RotateCADTool();
129
                ScaleCADTool scale = new ScaleCADTool();
130
                SymmetryCADTool symmetry=new SymmetryCADTool();
131
                addCADTool("_copy", copy);
132
                addCADTool("_rotate", rotate);
133
                addCADTool("_scale", scale);
134
                addCADTool("_symmetry", symmetry);
135
                
136
                KeyboardFocusManager kfm = KeyboardFocusManager
137
                                .getCurrentKeyboardFocusManager();
138
                kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
139

    
140
                registerIcons();
141
        }
142

    
143
        @Override
144
    public void postInitialize() {
145
        //Register the edition symbology
146
        registerEditionSymbology();
147
    }
148

    
149
    /**
150
     * It registers the default symbology used in edition
151
     */
152
    private void registerEditionSymbology() {
153
        PluginsManager pluginsManager = PluginsLocator.getManager();
154
        DynObject dynObject = pluginsManager.getPlugin(getClass()).getPluginProperties();
155
       
156
        MapControlManager mapControlManager = MapControlLocator.getMapControlManager();
157
               
158
        if (dynObject.hasDynValue(EditingPage.DEFAULT_AXIS_REFERENCES_SYMBOL)){
159
            mapControlManager.setAxisReferenceSymbol(
160
                (ISymbol)dynObject.getDynValue(EditingPage.DEFAULT_AXIS_REFERENCES_SYMBOL));
161
        }        
162
     
163
        if (dynObject.hasDynValue(EditingPage.DEFAULT_RECTANGLE_SELECTION_SYMBOL)) {                      
164
            mapControlManager.setGeometrySelectionSymbol(
165
                (ISymbol)dynObject.getDynValue(EditingPage.DEFAULT_RECTANGLE_SELECTION_SYMBOL));
166
        }
167
        
168
        if (dynObject.hasDynValue(EditingPage.DEFAULT_HANDLER_SYMBOL)) {           
169
            mapControlManager.setHandlerSymbol(
170
                (ISymbol)dynObject.getDynValue(EditingPage.DEFAULT_HANDLER_SYMBOL));
171
        }        
172
    }
173

    
174
    private void registerIcons(){
175
            IconThemeHelper.registerIcon("action", "layer-modify-copy", this);
176
            IconThemeHelper.registerIcon("action", "layer-modify-symmetry", this);
177
            IconThemeHelper.registerIcon("action", "layer-modify-rotate", this);
178
            IconThemeHelper.registerIcon("action", "layer-modify-scale", this);
179
            IconThemeHelper.registerIcon("action", "layer-modify-edit-vertex", this);
180
            IconThemeHelper.registerIcon("action", "layer-modify-join", this);
181
            IconThemeHelper.registerIcon("action", "layer-modify-matrix", this);
182
            IconThemeHelper.registerIcon("action", "layer-modify-move", this);
183
            IconThemeHelper.registerIcon("action", "layer-modify-rotate", this);
184
            IconThemeHelper.registerIcon("action", "layer-modify-stretch", this);
185
            
186
            
187
            IconThemeHelper.registerIcon("preferences", "preferences-layer-modify-flatness", this);
188
            IconThemeHelper.registerIcon("preferences", "preferences-snapper", this);
189
        }
190

    
191
        /**
192
         * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
193
         */
194
        public void execute(String s) {
195
                initFocus();
196
            if( "layer-modify-copy".equalsIgnoreCase(s) ) {
197
                    setCADTool("_copy", true);
198
            } else if( "layer-modify-symmetry".equalsIgnoreCase(s) ) {
199
                    setCADTool("_symmetry", true);
200
            } else if( "layer-modify-rotate".equalsIgnoreCase(s) ) {
201
                    setCADTool("_rotate", true);
202
            } else if( "layer-modify-scale".equalsIgnoreCase(s) ) {
203
                    setCADTool("_scale", true);
204
                    
205
                    // -------------------------------
206
                    // The following action commands are not declared
207
                    // in the section of config.xml dedicated to CADExtension.
208
                    // They are used in other extensions of this plugin.
209
                    // -------------------------------
210
                    /*
211
            } else if( "layer-modify-edit-vertex".equalsIgnoreCase(s) ) {
212
                    setCADTool("_editvertex", true);
213
            } else if( "layer-modify-join".equalsIgnoreCase(s) ) {
214
                    setCADTool("_join", true);
215
            } else if( "layer-modify-matrix".equalsIgnoreCase(s) ) {
216
                    setCADTool("_matriz", true);
217
            } else if( "layer-modify-move".equalsIgnoreCase(s) ) {
218
                    setCADTool("_move", true);
219
            } else if( "layer-modify-stretch".equalsIgnoreCase(s) ) {
220
                    setCADTool("_stretch", true);
221
                    */
222
                    
223
            } else {
224
                    logger.info("Action command '"+s+"' not supportted in "+this.getClass().getName());
225
            }
226
                CADToolAdapter cta=getCADToolAdapter();
227
                cta.configureMenu();
228
        }
229

    
230
        public static void addCADTool(String name, CADTool c) {
231
                CADToolAdapter.addCADTool(name, c);
232
        }
233

    
234
        public static void setCADTool(String text, boolean showCommand) {
235
                CADToolAdapter cta=getCADToolAdapter();
236
                CADTool ct=cta.getCADTool(text);
237

    
238
                if (ct == null)
239
                        throw new RuntimeException("No such cad tool");
240
                cta.initializeFlatness();
241
                cta.setCadTool(ct);
242
                ct.init();
243
                if (showCommand) {
244
                        if (PluginServices.getMDIManager().getActiveWindow() instanceof DefaultViewPanel) {
245
                                DefaultViewPanel vista = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow();
246
                                vista.getConsolePanel().addText("\n" + ct.getName(),
247
                                        JConsole.COMMAND);
248
                                cta.askQuestion();
249
                        }
250
                }
251
                // PluginServices.getMainFrame().setSelectedTool("SELECT");
252
                // PluginServices.getMainFrame().enableControls();
253
        }
254

    
255
        public static CADTool getCADTool() {
256
                CADToolAdapter cta=getCADToolAdapter();
257
                return cta.getCadTool();
258
        }
259

    
260
        /**
261
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
262
         */
263
        public boolean isEnabled() {
264
                // initFocus();
265
                return true;
266
        }
267

    
268
        /**
269
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
270
         */
271
        public boolean isVisible() {
272
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
273
                        view = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow();
274
                        mapControl = view.getMapControl();
275
                        FLayer[] layers = mapControl.getMapContext().getLayers()
276
                                        .getActives();
277
//                        if (!(layers[0] instanceof FLyrAnnotation)) {
278
//                                return true;
279
//                        }
280
                        return true;
281
                }
282
                return false;
283
        }
284

    
285
        public MapControl getMapControl() {
286
                return getEditionManager().getMapControl();
287
        }
288

    
289
        class KeyAction extends AbstractAction {
290

    
291
                private String key;
292

    
293
                public KeyAction(String key) {
294
                        this.key = key;
295
                }
296

    
297
                /*
298
                 * (non-Javadoc)
299
                 *
300
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
301
                 */
302
                public void actionPerformed(ActionEvent e) {
303
                        view.focusConsole(key);
304
                }
305

    
306
        }
307

    
308
        class MyAction extends AbstractAction {
309
                private String actionCommand;
310

    
311
                public MyAction(String command) {
312
                        actionCommand = command;
313
                }
314

    
315
                /**
316
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
317
                 */
318
                public void actionPerformed(ActionEvent e) {
319
                        CADToolAdapter cta=getCADToolAdapter();
320
                        cta.keyPressed(actionCommand);
321
                }
322

    
323
        }
324

    
325
        /**
326
         * @author fjp
327
         *
328
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
329
         * la consola de la vista para que salga por all?.
330
         */
331
        private class myKeyEventPostProcessor implements KeyEventPostProcessor {
332

    
333
                public boolean postProcessKeyEvent(KeyEvent e) {
334
                        // System.out.println("KeyEvent e = " + e);
335
                        CADToolAdapter cta=getCADToolAdapter();
336
                        if ((cta == null) || (view == null))
337
                                return false;
338

    
339
                        if (cta.getMapControl() == null){
340
                                return false;
341
                        }
342

    
343
                        if (e.getID() != KeyEvent.KEY_RELEASED)
344
                                return false;
345
                        if (!(e.getComponent() instanceof JTextComponent)) {
346
                                if (e.getKeyCode() == KeyEvent.VK_DELETE)
347
                                        cta.keyPressed("eliminar");
348
                                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
349
                                        cta.keyPressed("escape");
350
                                else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
351
                                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
352
                                        // JTEXTAREA
353
                                        // QUE NO ES EL DE CONSOLA
354
                                        if (!(e.getSource() instanceof JTable))
355
                                                view.focusConsole("");
356
                                }
357
                                else if ((!e.isActionKey() && e.getKeyCode()!=KeyEvent.VK_TAB)) {
358
                                                //if (Character.isLetterOrDigit(e.getKeyChar())) {
359
                                                        Character keyChar = new Character(e.getKeyChar());
360
                                                        if (e.getComponent().getName() != null) {
361
                                                            /*
362
                                                                System.out
363
                                                                                .println("Evento de teclado desde el componente "
364
                                                                                                + e.getComponent().getName());
365
                                                                */
366
                                                                if (!e.getComponent().getName().equals(
367
                                                                                "CADConsole")) {
368
                                                                        view.focusConsole(keyChar + "");
369
                                                                }
370
                                                        } else {
371
                                                                if (!(e.getComponent() instanceof JTextComponent)) {
372
                                                                        view.focusConsole(keyChar + "");
373
                                                                }
374
                                                        }
375
                                                //}
376
                                        }
377
                                }
378
                        return false;
379
                }
380

    
381
        }
382

    
383
        /*
384
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
385
         * key++){ Character keyChar = new Character(key);
386
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
387
         * keyChar); mapControl.getActionMap().put(keyChar, new
388
         * KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
389
         * Character keyChar = new Character(key);
390
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
391
         * keyChar); mapControl.getActionMap().put(keyChar, new
392
         * KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
393
         * Character keyChar = new Character(key);
394
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
395
         * keyChar); mapControl.getActionMap().put(keyChar, new
396
         * KeyAction(keyChar+"")); }
397
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
398
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
399
         * Character keyChar = new
400
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
401
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
402
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
403
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
404
         * Character(' ');
405
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
406
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
407
         * KeyAction(""));
408
         *
409
         *  }
410
         */
411
        private static JPopupMenu popup = new JPopupMenu();
412

    
413
        public static void clearMenu() {
414
                popup.removeAll();
415
        }
416

    
417
        public static void addMenuEntry(String text) {
418
                JMenuItem menu = new JMenuItem(text);
419
                menu.setActionCommand(text);
420
                menu.setEnabled(true);
421
                menu.setVisible(true);
422
                menu.addActionListener(new ActionListener() {
423
                        public void actionPerformed(ActionEvent e) {
424
                                CADToolAdapter cta=getCADToolAdapter();
425
                                cta.transition(e.getActionCommand());
426
                        }
427
                });
428

    
429
                popup.add(menu);
430
        }
431

    
432
        public static void showPopup(MouseEvent e) {
433
                popup.show(e.getComponent(), e.getX(), e.getY());
434
        }
435

    
436
        public static DefaultViewPanel getView() {
437
                return view;
438
        }
439

    
440
        public static void clearView() {
441
                view = null;
442
        }
443

    
444
        /**
445
         * 
446
     * Gets the edition manager of the currently active view
447
     * or the edition manager of the view that was active more
448
     * recently (if there is not an active view)
449
     * or null if there never was an active view
450
     * 
451
         * @return Returns the editionManager.
452
         * 
453
         * @deprecated Use same method in {@link EditionLocator}
454
         */
455
        public static IEditionManager getEditionManager() {
456
            return EditionLocator.getEditionManager();
457
        }
458

    
459
        public static CADTool[] getCADTools() {
460
                return CADToolAdapter.getCADTools();
461
        }
462

    
463
        public static void initFocus() {
464
                view = (DefaultViewPanel) PluginServices.getMDIManager().getActiveWindow();
465
                MapControl mapControl = view.getMapControl();
466
                CADToolAdapter cta=getCADToolAdapter();
467
                if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")){
468
                        // StatusBarListener sbl=new StatusBarListener(view.getMapControl());
469
                        // mapControl.addMapTool("cadtooladapter",  new Behavior[]{adapter,new MouseMovementBehavior(sbl)});
470
                        mapControl.addBehavior("cadtooladapter", cta);
471
                }
472
                // view.getMapControl().setTool("cadtooladapter");
473
                JEditTextArea jeta=view.getConsolePanel().getTxt();
474
                jeta.requestFocusInWindow();
475
                jeta.setCaretPosition(jeta.getText().length());
476

    
477
                view.addConsoleListener("cad", new ResponseListener() {
478
                        public void acceptResponse(String response) {
479
                                CADToolAdapter cta=getCADToolAdapter();
480
                                cta.textEntered(response);
481
                                // TODO:
482
                                // FocusManager fm=FocusManager.getCurrentManager();
483
                                // fm.focusPreviousComponent(mapControl);
484
                                /*
485
                                 * if (popup.isShowing()){ popup.setVisible(false); }
486
                                 */
487

    
488
                        }
489
                });
490
                cta.getEditionManager().setMapControl(mapControl);
491
                view.getMapControl().setTool("cadtooladapter");
492
        }
493

    
494
        public IPreference[] getPreferencesPages() {
495
                IPreference[] preferences=new IPreference[1];
496
                preferences[0]=editingPage;
497
                return preferences;
498
        }
499
}