Statistics
| Revision:

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

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

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

    
93
        private static View view;
94

    
95
        private MapControl mapControl;
96
        private static CADToolAdapter adapter=null;
97
        private EditingPage editingPage=new EditingPage();
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
        public static CADToolAdapter getCADToolAdapter() {
116
                com.iver.andami.ui.mdiManager.IWindow view=PluginServices.getMDIManager().getActiveWindow();
117
                if (view instanceof View) {
118
                        View v=(View)view;
119
                        if (!adapters.containsKey(v.getModel())) {
120
                                adapters.put(v.getModel(),new CADToolAdapter());
121
                        }
122
                        adapter=(CADToolAdapter)adapters.get(v.getModel());
123
                        return adapter;
124
                }
125
                return adapter;
126

    
127
        }
128

    
129
        /**
130
         * @see com.iver.andami.plugins.IExtension#initialize()
131
         */
132
        public void initialize() {
133

    
134
                // Registramos los Popup menus:
135
        MenuEntry.register();
136
        Snapping.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
                PluginServices.getIconTheme().registerDefault(
225
                                "cad-selection-icon",
226
                                CADExtension.class.getClassLoader().getResource(
227
                                                "images/cadselection.png"));
228
        }
229

    
230
        /**
231
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
232
         */
233
        public void execute(String s) {
234
                initFocus();
235

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

    
249
        public static void addCADTool(String name, CADTool c) {
250
                CADToolAdapter.addCADTool(name, c);
251
        }
252

    
253
        public static void setCADTool(String text, boolean showCommand) {
254
                CADToolAdapter cta=getCADToolAdapter();
255
                CADTool ct=cta.getCADTool(text);
256

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

    
275
        public static CADTool getCADTool() {
276
                CADToolAdapter cta=getCADToolAdapter();
277
                return cta.getCadTool();
278
        }
279

    
280
        /**
281
         * @see com.iver.andami.plugins.IExtension#isEnabled()
282
         */
283
        public boolean isEnabled() {
284
                // initFocus();
285
                return true;
286
        }
287

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

    
304
        public MapControl getMapControl() {
305
                return getEditionManager().getMapControl();
306
        }
307

    
308
        class KeyAction extends AbstractAction {
309

    
310
                private String key;
311

    
312
                public KeyAction(String key) {
313
                        this.key = key;
314
                }
315

    
316
                /*
317
                 * (non-Javadoc)
318
                 *
319
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
320
                 */
321
                public void actionPerformed(ActionEvent e) {
322
                        view.focusConsole(key);
323
                }
324

    
325
        }
326

    
327
        class MyAction extends AbstractAction {
328
                private String actionCommand;
329

    
330
                public MyAction(String command) {
331
                        actionCommand = command;
332
                }
333

    
334
                /**
335
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
336
                 */
337
                public void actionPerformed(ActionEvent e) {
338
                        CADToolAdapter cta=getCADToolAdapter();
339
                        cta.keyPressed(actionCommand);
340
                }
341

    
342
        }
343

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

    
352
                public boolean postProcessKeyEvent(KeyEvent e) {
353
                        // System.out.println("KeyEvent e = " + e);
354
                        CADToolAdapter cta=getCADToolAdapter();
355
                        if ((cta == null) || (view == null))
356
                                return false;
357

    
358
                        if (cta.getMapControl() == null){
359
                                return false;
360
                        }
361

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

    
398
        }
399

    
400
        /*
401
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
402
         * key++){ 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+"")); } for (char key = 'A'; key <= 'Z'; key++){
410
         * Character keyChar = new Character(key);
411
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
412
         * keyChar); mapControl.getActionMap().put(keyChar, new
413
         * KeyAction(keyChar+"")); }
414
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
415
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
416
         * Character keyChar = new
417
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
418
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
419
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
420
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
421
         * Character(' ');
422
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
423
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
424
         * KeyAction(""));
425
         *
426
         *  }
427
         */
428
        private static JPopupMenu popup = new JPopupMenu();
429

    
430
        public static void clearMenu() {
431
                popup.removeAll();
432
        }
433

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

    
446
                popup.add(menu);
447
        }
448

    
449
        public static void showPopup(MouseEvent e) {
450
                popup.show(e.getComponent(), e.getX(), e.getY());
451
        }
452

    
453
        public static View getView() {
454
                return view;
455
        }
456

    
457
        public static void clearView() {
458
                view = null;
459
        }
460

    
461
        /**
462
         * @return Returns the editionManager.
463
         */
464
        public static EditionManager getEditionManager() {
465
                CADToolAdapter cta=getCADToolAdapter();
466
                return cta.getEditionManager();
467
        }
468

    
469
        public static CADTool[] getCADTools() {
470
                return CADToolAdapter.getCADTools();
471
        }
472

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

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

    
498
                        }
499
                });
500
                cta.getEditionManager().setMapControl(mapControl);
501
                view.getMapControl().setTool("cadtooladapter");
502
        }
503

    
504
        public IPreference[] getPreferencesPages() {
505
                IPreference[] preferences=new IPreference[1];
506
                preferences[0]=editingPage;
507
                return preferences;
508
        }
509
}