Statistics
| Revision:

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

History | View | Annotate | Download (13.2 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.cit.gvsig.fmap.MapControl;
61
import com.iver.cit.gvsig.fmap.layers.FLayer;
62
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
63
import com.iver.cit.gvsig.gui.View;
64
import com.iver.cit.gvsig.gui.accelerators.ForceCursorAccelerator;
65
import com.iver.cit.gvsig.gui.accelerators.GridAccelerator;
66
import com.iver.cit.gvsig.gui.accelerators.OrtoAccelerator;
67
import com.iver.cit.gvsig.gui.accelerators.RefentAccelerator;
68
import com.iver.cit.gvsig.gui.cad.CADTool;
69
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
70
import com.iver.cit.gvsig.gui.cad.tools.CopyCADTool;
71
import com.iver.cit.gvsig.gui.cad.tools.RotateCADTool;
72
import com.iver.cit.gvsig.gui.cad.tools.ScaleCADTool;
73
import com.iver.cit.gvsig.gui.cad.tools.SymmetryCADTool;
74
import com.iver.cit.gvsig.gui.popupmenu.PopupEditionProperties;
75
import com.iver.cit.gvsig.gui.toc.FPopupMenu;
76
import com.iver.utiles.console.JConsole;
77
import com.iver.utiles.console.ResponseListener;
78
import com.iver.utiles.console.jedit.JEditTextArea;
79

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

    
89
        private static View view;
90

    
91
        private MapControl mapControl;
92
        private static CADToolAdapter adapter=null;
93

    
94
        public static CADToolAdapter getCADToolAdapter() {
95
                com.iver.andami.ui.mdiManager.View view=(com.iver.andami.ui.mdiManager.View)PluginServices.getMDIManager().getActiveView();
96
                if (view instanceof View) {
97
                        View v=(View)view;
98
                        if (!adapters.containsKey(v)) {
99
                                adapters.put(v,new CADToolAdapter());
100
                        }
101
                        adapter=(CADToolAdapter)adapters.get(v);
102
                        return adapter;
103
                }
104
                return adapter;
105

    
106
        }
107

    
108
        /**
109
         * @see com.iver.andami.plugins.IExtension#initialize()
110
         */
111
        public void initialize() {
112

    
113
                // Registramos los Popup menus:
114

    
115
            // Adds an entry to the TOC's floating menu
116
        FPopupMenu.addEntry(new PopupEditionProperties());
117

    
118

    
119

    
120
                // Fijamos que los s?mbolos de dibujo tengan outline
121
                // TODO: Esto se debe configurar en el cuadro de di?logo de preferencias
122
                CADTool.drawingSymbol.setOutlined(true);
123
                CADTool.drawingSymbol.setOutlineColor(CADTool.drawingSymbol.getColor().darker());
124
                CADTool.modifySymbol.setOutlined(true);
125
                CADTool.modifySymbol.setOutlineColor(CADTool.modifySymbol.getColor().darker());
126
                CADTool.selectSymbol.setOutlined(true);
127
                CADTool.selectSymbol.setOutlineColor(CADTool.selectSymbol.getColor().darker());
128

    
129

    
130
                CopyCADTool copy = new CopyCADTool();
131
                RotateCADTool rotate = new RotateCADTool();
132
                ScaleCADTool scale = new ScaleCADTool();
133
                SymmetryCADTool symmetry=new SymmetryCADTool();
134
                addCADTool("_copy", copy);
135
                addCADTool("_rotate", rotate);
136
                addCADTool("_scale", scale);
137
                addCADTool("_symmetry", symmetry);
138
        // Registramos las teclas de acceso r?pido que vamos a usar.
139

    
140
                KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
141
                RefentAccelerator accRef = new RefentAccelerator();
142
                PluginServices.registerKeyStroke(key, accRef);
143

    
144
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0);
145
                OrtoAccelerator accOrto = new OrtoAccelerator();
146
                PluginServices.registerKeyStroke(key, accOrto);
147

    
148
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0);
149
                GridAccelerator accGrid = new GridAccelerator();
150
                PluginServices.registerKeyStroke(key, accGrid);
151

    
152
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
153
                ForceCursorAccelerator accForce = new ForceCursorAccelerator();
154
                PluginServices.registerKeyStroke(key, accForce);
155

    
156

    
157
                KeyboardFocusManager kfm = KeyboardFocusManager
158
                                .getCurrentKeyboardFocusManager();
159
                kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
160
        }
161

    
162
        /**
163
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
164
         */
165
        public void execute(String s) {
166
                initFocus();
167

    
168
                if (s.equals("_spline") || s.equals("_copy")
169
                                || s.equals("_equidistance") || s.equals("_matriz")
170
                                || s.equals("_symmetry") || s.equals("_rotate")
171
                                || s.equals("_stretch") || s.equals("_scale")
172
                                || s.equals("_extend") || s.equals("_trim")
173
                                || s.equals("_unit")
174
                                || s.equals("_chaflan") || s.equals("_join")) {
175
                        setCADTool(s, true);
176
                }
177
                CADToolAdapter cta=getCADToolAdapter();
178
                cta.configureMenu();
179
        }
180

    
181
        public static void addCADTool(String name, CADTool c) {
182
                CADToolAdapter.addCADTool(name, c);
183
        }
184

    
185
        public static void setCADTool(String text, boolean showCommand) {
186
                CADToolAdapter cta=getCADToolAdapter();
187
                CADTool ct=cta.getCADTool(text);
188

    
189
                if (ct == null)
190
                        throw new RuntimeException("No such cad tool");
191
                cta.setCadTool(ct);
192
                ct.init();
193
                if (showCommand) {
194
                        if (PluginServices.getMDIManager().getActiveView() instanceof View) {
195
                                View vista = (View) PluginServices.getMDIManager().getActiveView();
196
                                vista.getConsolePanel().addText("\n" + ct.getName(),
197
                                        JConsole.COMMAND);
198
                                cta.askQuestion();
199
                        }
200
                }
201
                // PluginServices.getMainFrame().setSelectedTool("SELECT");
202
                // PluginServices.getMainFrame().enableControls();
203
        }
204

    
205
        public static CADTool getCADTool() {
206
                CADToolAdapter cta=getCADToolAdapter();
207
                return cta.getCadTool();
208
        }
209

    
210
        /**
211
         * @see com.iver.andami.plugins.IExtension#isEnabled()
212
         */
213
        public boolean isEnabled() {
214
                // initFocus();
215
                return true;
216
        }
217

    
218
        /**
219
         * @see com.iver.andami.plugins.IExtension#isVisible()
220
         */
221
        public boolean isVisible() {
222
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
223
                        view = (View) PluginServices.getMDIManager().getActiveView();
224
                        mapControl = (MapControl) view.getMapControl();
225
                        FLayer[] layers = mapControl.getMapContext().getLayers()
226
                                        .getActives();
227
                        if (!(layers[0] instanceof FLyrAnnotation)) {
228
                                return true;
229
                        }
230
                }
231
                return false;
232
        }
233

    
234
        public MapControl getMapControl() {
235
                return getEditionManager().getMapControl();
236
        }
237

    
238
        class KeyAction extends AbstractAction {
239

    
240
                private String key;
241

    
242
                public KeyAction(String key) {
243
                        this.key = key;
244
                }
245

    
246
                /*
247
                 * (non-Javadoc)
248
                 *
249
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
250
                 */
251
                public void actionPerformed(ActionEvent e) {
252
                        view.focusConsole(key);
253
                }
254

    
255
        }
256

    
257
        class MyAction extends AbstractAction {
258
                private String actionCommand;
259

    
260
                public MyAction(String command) {
261
                        actionCommand = command;
262
                }
263

    
264
                /**
265
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
266
                 */
267
                public void actionPerformed(ActionEvent e) {
268
                        CADToolAdapter cta=getCADToolAdapter();
269
                        cta.keyPressed(actionCommand);
270
                }
271

    
272
        }
273

    
274
        /**
275
         * @author fjp
276
         *
277
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
278
         * la consola de la vista para que salga por all?.
279
         */
280
        private class myKeyEventPostProcessor implements KeyEventPostProcessor {
281

    
282
                public boolean postProcessKeyEvent(KeyEvent e) {
283
                        // System.out.println("KeyEvent e = " + e);
284
                        CADToolAdapter cta=getCADToolAdapter();
285
                        if ((cta == null) || (view == null))
286
                                return false;
287

    
288
                        if (e.getID() != KeyEvent.KEY_RELEASED)
289
                                return false;
290
                        if (!(e.getComponent() instanceof JTextComponent)) {
291
                                if (e.getKeyCode() == KeyEvent.VK_DELETE)
292
                                        cta.keyPressed("eliminar");
293
                                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
294
                                        cta.keyPressed("escape");
295
                                else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
296
                                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
297
                                        // JTEXTAREA
298
                                        // QUE NO ES EL DE CONSOLA
299
                                        if (!(e.getSource() instanceof JTable))
300
                                                view.focusConsole("");
301
                                }
302
                                else if ((!e.isActionKey())) {
303
                                                //if (Character.isLetterOrDigit(e.getKeyChar())) {
304
                                                        Character keyChar = new Character(e.getKeyChar());
305
                                                        if (e.getComponent().getName() != null) {
306
                                                                System.out
307
                                                                                .println("Evento de teclado desde el componente "
308
                                                                                                + e.getComponent().getName());
309
                                                                if (!e.getComponent().getName().equals(
310
                                                                                "CADConsole")) {
311
                                                                        view.focusConsole(keyChar + "");
312
                                                                }
313
                                                        } else {
314
                                                                if (!(e.getComponent() instanceof JTextComponent)) {
315
                                                                        view.focusConsole(keyChar + "");
316
                                                                }
317
                                                        }
318
                                                //}
319
                                        }
320
                                }
321
                        return false;
322
                }
323

    
324
        }
325

    
326
        /*
327
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
328
         * key++){ Character keyChar = new Character(key);
329
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
330
         * keyChar); mapControl.getActionMap().put(keyChar, new
331
         * KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
332
         * Character keyChar = new Character(key);
333
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
334
         * keyChar); mapControl.getActionMap().put(keyChar, new
335
         * KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
336
         * Character keyChar = new Character(key);
337
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
338
         * keyChar); mapControl.getActionMap().put(keyChar, new
339
         * KeyAction(keyChar+"")); }
340
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
341
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
342
         * Character keyChar = new
343
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
344
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
345
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
346
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
347
         * Character(' ');
348
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
349
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
350
         * KeyAction(""));
351
         *
352
         *  }
353
         */
354
        private static JPopupMenu popup = new JPopupMenu();
355

    
356
        public static void clearMenu() {
357
                popup.removeAll();
358
        }
359

    
360
        public static void addMenuEntry(String text) {
361
                JMenuItem menu = new JMenuItem(text);
362
                menu.setActionCommand(text);
363
                menu.setEnabled(true);
364
                menu.setVisible(true);
365
                menu.addActionListener(new ActionListener() {
366
                        public void actionPerformed(ActionEvent e) {
367
                                CADToolAdapter cta=getCADToolAdapter();
368
                                cta.transition(e.getActionCommand());
369
                        }
370
                });
371

    
372
                popup.add(menu);
373
        }
374

    
375
        public static void showPopup(MouseEvent e) {
376
                popup.show(e.getComponent(), e.getX(), e.getY());
377
        }
378

    
379
        public static View getView() {
380
                return view;
381
        }
382

    
383
        /**
384
         * @return Returns the editionManager.
385
         */
386
        public static EditionManager getEditionManager() {
387
                CADToolAdapter cta=getCADToolAdapter();
388
                return cta.getEditionManager();
389
        }
390

    
391
        public static CADTool[] getCADTools() {
392
                return CADToolAdapter.getCADTools();
393
        }
394

    
395
        public static void initFocus() {
396
                view = (View) PluginServices.getMDIManager().getActiveView();
397
                MapControl mapControl = (MapControl) view.getMapControl();
398
                CADToolAdapter cta=getCADToolAdapter();
399
                if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")){
400
                        // StatusBarListener sbl=new StatusBarListener(view.getMapControl());
401
                        // mapControl.addMapTool("cadtooladapter",  new Behavior[]{adapter,new MouseMovementBehavior(sbl)});
402
                        mapControl.addMapTool("cadtooladapter", cta);
403
                }
404
                // view.getMapControl().setTool("cadtooladapter");
405
                JEditTextArea jeta=view.getConsolePanel().getTxt();
406
                jeta.requestFocusInWindow();
407
                jeta.setCaretPosition(jeta.getText().length());
408

    
409
                view.addConsoleListener("cad", new ResponseListener() {
410
                        public void acceptResponse(String response) {
411
                                CADToolAdapter cta=getCADToolAdapter();
412
                                cta.textEntered(response);
413
                                // TODO:
414
                                // FocusManager fm=FocusManager.getCurrentManager();
415
                                // fm.focusPreviousComponent(mapControl);
416
                                /*
417
                                 * if (popup.isShowing()){ popup.setVisible(false); }
418
                                 */
419

    
420
                        }
421
                });
422
                cta.getEditionManager().setMapControl(mapControl);
423
                view.getMapControl().setTool("cadtooladapter");
424
        }
425
}