Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / CADExtension.java @ 30335

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

    
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

    
99
        public static CADToolAdapter getCADToolAdapter(FLayer layer) {
100
                IWindow[] windows=PluginServices.getMDIManager().getAllWindows();
101
                for (int i = 0; i < windows.length; i++) {
102
                        IWindow window=windows[i];
103
                        if (window instanceof View){
104
                                View v=(View)window;
105
                                if (v.getModel().getMapContext().equals(layer.getMapContext())){
106
                                        if (!adapters.containsKey(v.getModel())) {
107
                                                adapters.put(v.getModel(),new CADToolAdapter());
108
                                        }
109
                                        return (CADToolAdapter)adapters.get(v.getModel());
110
                                }
111
                        }
112
                }
113
                return null;
114
        }
115

    
116
        public static CADToolAdapter getCADToolAdapter() {
117
                org.gvsig.andami.ui.mdiManager.IWindow view=PluginServices.getMDIManager().getActiveWindow();
118
                if (view instanceof View) {
119
                        View v=(View)view;
120
                        if (!adapters.containsKey(v.getModel())) {
121
                                adapters.put(v.getModel(),new CADToolAdapter());
122
                        }
123
                        adapter=(CADToolAdapter)adapters.get(v.getModel());
124
                        return adapter;
125
                }
126
                return adapter;
127

    
128
        }
129

    
130
        /**
131
         * @see org.gvsig.andami.plugins.IExtension#initialize()
132
         */
133
        public void initialize() {
134

    
135
                // Registramos los Popup menus:
136
        MenuEntry.register();
137
        // Fijamos que los s?mbolos de dibujo tengan outline
138
                // TODO: Esto se debe configurar en el cuadro de di?logo de preferencias
139
// jaume, this should be unnecessary
140
//                CADTool.drawingSymbol.setOutlined(true);
141
//                CADTool.drawingSymbol.setOutlineColor(CADTool.drawingSymbol.getColor().darker());
142
//                CADTool.modifySymbol.setOutlined(true);
143
//                CADTool.modifySymbol.setOutlineColor(CADTool.modifySymbol.getColor().darker());
144
//                CADTool.selectSymbol.setOutlined(true);
145
//                CADTool.selectSymbol.setOutlineColor(CADTool.selectSymbol.getColor().darker());
146

    
147

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

    
158
                KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
159
                RefentAccelerator accRef = new RefentAccelerator();
160
                PluginServices.registerKeyStroke(key, accRef);
161

    
162
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0);
163
                OrtoAccelerator accOrto = new OrtoAccelerator();
164
                PluginServices.registerKeyStroke(key, accOrto);
165

    
166
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0);
167
                GridAccelerator accGrid = new GridAccelerator();
168
                PluginServices.registerKeyStroke(key, accGrid);
169

    
170
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
171
                ForceCursorAccelerator accForce = new ForceCursorAccelerator();
172
                PluginServices.registerKeyStroke(key, accForce);
173

    
174

    
175
                KeyboardFocusManager kfm = KeyboardFocusManager
176
                                .getCurrentKeyboardFocusManager();
177
                kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
178

    
179
                registerIcons();
180
        }
181

    
182
        private void registerIcons(){
183
                PluginServices.getIconTheme().registerDefault(
184
                                "edition-geometry-copy",
185
                                this.getClass().getClassLoader().getResource("images/Copy.png")
186
                        );
187

    
188
                PluginServices.getIconTheme().registerDefault(
189
                                "edition-geometry-symmetry",
190
                                this.getClass().getClassLoader().getResource("images/Symmetry.png")
191
                        );
192

    
193
                PluginServices.getIconTheme().registerDefault(
194
                                "edition-geometry-rotate",
195
                                this.getClass().getClassLoader().getResource("images/Rotation.png")
196
                        );
197

    
198
                PluginServices.getIconTheme().registerDefault(
199
                                "edition-geometry-scale",
200
                                this.getClass().getClassLoader()
201
                                                .getResource("images/Scale.png"));
202

    
203
                PluginServices.getIconTheme().registerDefault(
204
                                "edition-properties",
205
                                CADExtension.class.getClassLoader().getResource(
206
                                                "images/EditingProperties.png"));
207

    
208
                PluginServices.getIconTheme().registerDefault(
209
                                "field-expresion",
210
                                CADExtension.class.getClassLoader().getResource(
211
                                                "images/FieldExpresion.png"));
212
                PluginServices.getIconTheme().registerDefault(
213
                                "flatness-icon",
214
                                CADExtension.class.getClassLoader().getResource(
215
                                                "images/Flatness.png"));
216
                PluginServices.getIconTheme().registerDefault(
217
                                "grid-icon",
218
                                CADExtension.class.getClassLoader().getResource(
219
                                                "images/Grid.png"));
220
                PluginServices.getIconTheme().registerDefault(
221
                                "snapper-icon",
222
                                CADExtension.class.getClassLoader().getResource(
223
                                                "images/Snapper.png"));
224
        }
225

    
226
        /**
227
         * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
228
         */
229
        public void execute(String s) {
230
                initFocus();
231

    
232
                if (s.equals("_spline") || s.equals("_copy")
233
                                || s.equals("_equidistance") || s.equals("_matriz")
234
                                || s.equals("_symmetry") || s.equals("_rotate")
235
                                || s.equals("_stretch") || s.equals("_scale")
236
                                || s.equals("_extend") || s.equals("_trim")
237
                                || s.equals("_unit")
238
                                || s.equals("_chaflan") || s.equals("_join")) {
239
                        setCADTool(s, true);
240
                }
241
                CADToolAdapter cta=getCADToolAdapter();
242
                cta.configureMenu();
243
        }
244

    
245
        public static void addCADTool(String name, CADTool c) {
246
                CADToolAdapter.addCADTool(name, c);
247
        }
248

    
249
        public static void setCADTool(String text, boolean showCommand) {
250
                CADToolAdapter cta=getCADToolAdapter();
251
                CADTool ct=cta.getCADTool(text);
252

    
253
                if (ct == null)
254
                        throw new RuntimeException("No such cad tool");
255
                cta.initializeFlatness();
256
                cta.setCadTool(ct);
257
                ct.init();
258
                if (showCommand) {
259
                        if (PluginServices.getMDIManager().getActiveWindow() instanceof View) {
260
                                View vista = (View) PluginServices.getMDIManager().getActiveWindow();
261
                                vista.getConsolePanel().addText("\n" + ct.getName(),
262
                                        JConsole.COMMAND);
263
                                cta.askQuestion();
264
                        }
265
                }
266
                // PluginServices.getMainFrame().setSelectedTool("SELECT");
267
                // PluginServices.getMainFrame().enableControls();
268
        }
269

    
270
        public static CADTool getCADTool() {
271
                CADToolAdapter cta=getCADToolAdapter();
272
                return cta.getCadTool();
273
        }
274

    
275
        /**
276
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
277
         */
278
        public boolean isEnabled() {
279
                // initFocus();
280
                return true;
281
        }
282

    
283
        /**
284
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
285
         */
286
        public boolean isVisible() {
287
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
288
                        view = (View) PluginServices.getMDIManager().getActiveWindow();
289
                        mapControl = view.getMapControl();
290
                        FLayer[] layers = mapControl.getMapContext().getLayers()
291
                                        .getActives();
292
//                        if (!(layers[0] instanceof FLyrAnnotation)) {
293
//                                return true;
294
//                        }
295
                        return true;
296
                }
297
                return false;
298
        }
299

    
300
        public MapControl getMapControl() {
301
                return getEditionManager().getMapControl();
302
        }
303

    
304
        class KeyAction extends AbstractAction {
305

    
306
                private String key;
307

    
308
                public KeyAction(String key) {
309
                        this.key = key;
310
                }
311

    
312
                /*
313
                 * (non-Javadoc)
314
                 *
315
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
316
                 */
317
                public void actionPerformed(ActionEvent e) {
318
                        view.focusConsole(key);
319
                }
320

    
321
        }
322

    
323
        class MyAction extends AbstractAction {
324
                private String actionCommand;
325

    
326
                public MyAction(String command) {
327
                        actionCommand = command;
328
                }
329

    
330
                /**
331
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
332
                 */
333
                public void actionPerformed(ActionEvent e) {
334
                        CADToolAdapter cta=getCADToolAdapter();
335
                        cta.keyPressed(actionCommand);
336
                }
337

    
338
        }
339

    
340
        /**
341
         * @author fjp
342
         *
343
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
344
         * la consola de la vista para que salga por all?.
345
         */
346
        private class myKeyEventPostProcessor implements KeyEventPostProcessor {
347

    
348
                public boolean postProcessKeyEvent(KeyEvent e) {
349
                        // System.out.println("KeyEvent e = " + e);
350
                        CADToolAdapter cta=getCADToolAdapter();
351
                        if ((cta == null) || (view == null))
352
                                return false;
353

    
354
                        if (cta.getMapControl() == null){
355
                                return false;
356
                        }
357

    
358
                        if (e.getID() != KeyEvent.KEY_RELEASED)
359
                                return false;
360
                        if (!(e.getComponent() instanceof JTextComponent)) {
361
                                if (e.getKeyCode() == KeyEvent.VK_DELETE)
362
                                        cta.keyPressed("eliminar");
363
                                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
364
                                        cta.keyPressed("escape");
365
                                else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
366
                                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
367
                                        // JTEXTAREA
368
                                        // QUE NO ES EL DE CONSOLA
369
                                        if (!(e.getSource() instanceof JTable))
370
                                                view.focusConsole("");
371
                                }
372
                                else if ((!e.isActionKey() && e.getKeyCode()!=KeyEvent.VK_TAB)) {
373
                                                //if (Character.isLetterOrDigit(e.getKeyChar())) {
374
                                                        Character keyChar = new Character(e.getKeyChar());
375
                                                        if (e.getComponent().getName() != null) {
376
                                                                System.out
377
                                                                                .println("Evento de teclado desde el componente "
378
                                                                                                + e.getComponent().getName());
379
                                                                if (!e.getComponent().getName().equals(
380
                                                                                "CADConsole")) {
381
                                                                        view.focusConsole(keyChar + "");
382
                                                                }
383
                                                        } else {
384
                                                                if (!(e.getComponent() instanceof JTextComponent)) {
385
                                                                        view.focusConsole(keyChar + "");
386
                                                                }
387
                                                        }
388
                                                //}
389
                                        }
390
                                }
391
                        return false;
392
                }
393

    
394
        }
395

    
396
        /*
397
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
398
         * key++){ Character keyChar = new Character(key);
399
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
400
         * keyChar); mapControl.getActionMap().put(keyChar, new
401
         * KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
402
         * Character keyChar = new Character(key);
403
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
404
         * keyChar); mapControl.getActionMap().put(keyChar, new
405
         * KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
406
         * Character keyChar = new Character(key);
407
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
408
         * keyChar); mapControl.getActionMap().put(keyChar, new
409
         * KeyAction(keyChar+"")); }
410
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
411
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
412
         * Character keyChar = new
413
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
414
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
415
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
416
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
417
         * Character(' ');
418
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
419
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
420
         * KeyAction(""));
421
         *
422
         *  }
423
         */
424
        private static JPopupMenu popup = new JPopupMenu();
425

    
426
        public static void clearMenu() {
427
                popup.removeAll();
428
        }
429

    
430
        public static void addMenuEntry(String text) {
431
                JMenuItem menu = new JMenuItem(text);
432
                menu.setActionCommand(text);
433
                menu.setEnabled(true);
434
                menu.setVisible(true);
435
                menu.addActionListener(new ActionListener() {
436
                        public void actionPerformed(ActionEvent e) {
437
                                CADToolAdapter cta=getCADToolAdapter();
438
                                cta.transition(e.getActionCommand());
439
                        }
440
                });
441

    
442
                popup.add(menu);
443
        }
444

    
445
        public static void showPopup(MouseEvent e) {
446
                popup.show(e.getComponent(), e.getX(), e.getY());
447
        }
448

    
449
        public static View getView() {
450
                return view;
451
        }
452

    
453
        public static void clearView() {
454
                view = null;
455
        }
456

    
457
        /**
458
         * @return Returns the editionManager.
459
         */
460
        public static EditionManager getEditionManager() {
461
                CADToolAdapter cta=getCADToolAdapter();
462
                return cta.getEditionManager();
463
        }
464

    
465
        public static CADTool[] getCADTools() {
466
                return CADToolAdapter.getCADTools();
467
        }
468

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

    
483
                view.addConsoleListener("cad", new ResponseListener() {
484
                        public void acceptResponse(String response) {
485
                                CADToolAdapter cta=getCADToolAdapter();
486
                                cta.textEntered(response);
487
                                // TODO:
488
                                // FocusManager fm=FocusManager.getCurrentManager();
489
                                // fm.focusPreviousComponent(mapControl);
490
                                /*
491
                                 * if (popup.isShowing()){ popup.setVisible(false); }
492
                                 */
493

    
494
                        }
495
                });
496
                cta.getEditionManager().setMapControl(mapControl);
497
                view.getMapControl().setTool("cadtooladapter");
498
        }
499

    
500
        public IPreference[] getPreferencesPages() {
501
                IPreference[] preferences=new IPreference[1];
502
                preferences[0]=editingPage;
503
                return preferences;
504
        }
505
}