Statistics
| Revision:

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

History | View | Annotate | Download (12.8 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package 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.KeyStroke;
55
import javax.swing.text.JTextComponent;
56

    
57
import com.iver.andami.PluginServices;
58
import com.iver.andami.plugins.Extension;
59
import com.iver.cit.gvsig.fmap.MapControl;
60
import com.iver.cit.gvsig.fmap.layers.FLayer;
61
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
62
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
63
import com.iver.cit.gvsig.fmap.tools.Behavior.MouseMovementBehavior;
64
import com.iver.cit.gvsig.gui.View;
65
import com.iver.cit.gvsig.gui.accelerators.ForceCursorAccelerator;
66
import com.iver.cit.gvsig.gui.accelerators.GridAccelerator;
67
import com.iver.cit.gvsig.gui.accelerators.OrtoAccelerator;
68
import com.iver.cit.gvsig.gui.accelerators.RefentAccelerator;
69
import com.iver.cit.gvsig.gui.cad.CADTool;
70
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
71
import com.iver.cit.gvsig.gui.cad.tools.CopyCADTool;
72
import com.iver.cit.gvsig.gui.cad.tools.RotateCADTool;
73
import com.iver.cit.gvsig.gui.cad.tools.ScaleCADTool;
74
import com.iver.cit.gvsig.gui.cad.tools.SymmetryCADTool;
75
import com.iver.cit.gvsig.gui.popupmenu.PopupEditionProperties;
76
import com.iver.cit.gvsig.gui.toc.FPopupMenu;
77
import com.iver.cit.gvsig.gui.toolListeners.StatusBarListener;
78
import com.iver.utiles.console.JConsole;
79
import com.iver.utiles.console.ResponseListener;
80
import com.iver.utiles.console.jedit.JEditTextArea;
81

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

    
91
        private static EditionManager editionManager = new EditionManager();
92

    
93
        private static HashMap namesCadTools = new HashMap();
94

    
95
        // /private MapControl mapControl;
96
        private static View view;
97

    
98
        private MapControl mapControl;
99

    
100
        public static CADToolAdapter getCADToolAdapter() {
101
                return adapter;
102
        }
103

    
104
        /**
105
         * @see com.iver.andami.plugins.IExtension#initialize()
106
         */
107
        public void initialize() {
108

    
109
                // Registramos los Popup menus:
110

    
111
            // Adds an entry to the TOC's floating menu
112
        FPopupMenu.addEntry(new PopupEditionProperties());
113

    
114

    
115

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

    
125

    
126
                CopyCADTool copy = new CopyCADTool();
127
                RotateCADTool rotate = new RotateCADTool();
128
                ScaleCADTool scale = new ScaleCADTool();
129
                SymmetryCADTool symmetry=new SymmetryCADTool();
130
                addCADTool("_copy", copy);
131
                addCADTool("_rotate", rotate);
132
                addCADTool("_scale", scale);
133
                addCADTool("_symmetry", symmetry);
134
        // Registramos las teclas de acceso r?pido que vamos a usar.
135
                
136
                KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
137
                RefentAccelerator accRef = new RefentAccelerator();                
138
                PluginServices.registerKeyStroke(key, accRef);
139

    
140
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0);
141
                OrtoAccelerator accOrto = new OrtoAccelerator();                
142
                PluginServices.registerKeyStroke(key, accOrto);
143

    
144
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0);
145
                GridAccelerator accGrid = new GridAccelerator();                
146
                PluginServices.registerKeyStroke(key, accGrid);
147

    
148
                key = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
149
                ForceCursorAccelerator accForce = new ForceCursorAccelerator();                
150
                PluginServices.registerKeyStroke(key, accForce);
151
                
152
                
153
                KeyboardFocusManager kfm = KeyboardFocusManager
154
                                .getCurrentKeyboardFocusManager();
155
                kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
156
        }
157

    
158
        /**
159
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
160
         */
161
        public void execute(String s) {
162
                initFocus();
163

    
164
                if (s.equals("_spline") || s.equals("_copy")
165
                                || s.equals("_equidistance") || s.equals("_matriz")
166
                                || s.equals("_symmetry") || s.equals("_rotate")
167
                                || s.equals("_stretch") || s.equals("_scale")
168
                                || s.equals("_extend") || s.equals("_trim")
169
                                || s.equals("_unit")
170
                                || s.equals("_chaflan") || s.equals("_join")) {
171
                        setCADTool(s, true);
172
                }
173
                adapter.configureMenu();
174
        }
175

    
176
        public static void addCADTool(String name, CADTool c) {
177
                namesCadTools.put(name, c);
178
        }
179

    
180
        public static void setCADTool(String text, boolean showCommand) {
181
                CADTool ct = (CADTool) namesCadTools.get(text);
182
                if (ct == null)
183
                        throw new RuntimeException("No such cad tool");
184
                adapter.setCadTool(ct);
185
                ct.init();
186
                if (showCommand) {
187
                        View vista = (View) PluginServices.getMDIManager().getActiveView();
188
                        vista.getConsolePanel().addText("\n" + ct.getName(),
189
                                        JConsole.COMMAND);
190
                        adapter.askQuestion();
191
                }
192
                // PluginServices.getMainFrame().setSelectedTool("SELECT");
193
                // PluginServices.getMainFrame().enableControls();
194
        }
195

    
196
        public static CADTool getCADTool() {
197
                return adapter.getCadTool();
198
        }
199

    
200
        /**
201
         * @see com.iver.andami.plugins.IExtension#isEnabled()
202
         */
203
        public boolean isEnabled() {
204
                // initFocus();
205
                return true;
206
        }
207

    
208
        /**
209
         * @see com.iver.andami.plugins.IExtension#isVisible()
210
         */
211
        public boolean isVisible() {
212
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
213
                        view = (View) PluginServices.getMDIManager().getActiveView();
214
                        mapControl = (MapControl) view.getMapControl();
215
                        FLayer[] layers = mapControl.getMapContext().getLayers()
216
                                        .getActives();
217
                        if (!(layers[0] instanceof FLyrAnnotation)) {
218
                                return true;
219
                        }
220
                }
221
                return false;
222
        }
223

    
224
        public MapControl getMapControl() {
225
                return editionManager.getMapControl();
226
        }
227

    
228
        class KeyAction extends AbstractAction {
229

    
230
                private String key;
231

    
232
                public KeyAction(String key) {
233
                        this.key = key;
234
                }
235

    
236
                /*
237
                 * (non-Javadoc)
238
                 *
239
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
240
                 */
241
                public void actionPerformed(ActionEvent e) {
242
                        view.focusConsole(key);
243
                }
244

    
245
        }
246

    
247
        class MyAction extends AbstractAction {
248
                private String actionCommand;
249

    
250
                public MyAction(String command) {
251
                        actionCommand = command;
252
                }
253

    
254
                /**
255
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
256
                 */
257
                public void actionPerformed(ActionEvent e) {
258
                        adapter.keyPressed(actionCommand);
259
                }
260

    
261
        }
262

    
263
        /**
264
         * @author fjp
265
         *
266
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
267
         * la consola de la vista para que salga por all?.
268
         */
269
        private class myKeyEventPostProcessor implements KeyEventPostProcessor {
270

    
271
                public boolean postProcessKeyEvent(KeyEvent e) {
272
                        // System.out.println("KeyEvent e = " + e);
273
                        if ((adapter == null) || (view == null))
274
                                return false;
275

    
276
                        if (e.getID() != KeyEvent.KEY_RELEASED)
277
                                return false;
278
                        if (!(e.getComponent() instanceof JTextComponent)) {
279
                                if (e.getKeyCode() == KeyEvent.VK_DELETE)
280
                                        adapter.keyPressed("eliminar");
281
                                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
282
                                        adapter.keyPressed("escape");
283
                                else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
284
                                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
285
                                        // JTEXTAREA
286
                                        // QUE NO ES EL DE CONSOLA
287
                                        view.focusConsole("");
288
                                }
289
                                else if ((!e.isActionKey())) {
290
                                                //if (Character.isLetterOrDigit(e.getKeyChar())) {
291
                                                        Character keyChar = new Character(e.getKeyChar());
292
                                                        if (e.getComponent().getName() != null) {
293
                                                                System.out
294
                                                                                .println("Evento de teclado desde el componente "
295
                                                                                                + e.getComponent().getName());
296
                                                                if (!e.getComponent().getName().equals(
297
                                                                                "CADConsole")) {
298
                                                                        view.focusConsole(keyChar + "");
299
                                                                }
300
                                                        } else {
301
                                                                if (!(e.getComponent() instanceof JTextComponent)) {
302
                                                                        view.focusConsole(keyChar + "");
303
                                                                }
304
                                                        }
305
                                                //}
306
                                        }
307
                                }
308
                        return false;
309
                }
310

    
311
        }
312

    
313
        /*
314
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
315
         * key++){ Character keyChar = new Character(key);
316
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
317
         * keyChar); mapControl.getActionMap().put(keyChar, new
318
         * KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
319
         * Character keyChar = new Character(key);
320
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
321
         * keyChar); mapControl.getActionMap().put(keyChar, new
322
         * KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
323
         * Character keyChar = new Character(key);
324
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
325
         * keyChar); mapControl.getActionMap().put(keyChar, new
326
         * KeyAction(keyChar+"")); }
327
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
328
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
329
         * Character keyChar = new
330
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
331
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
332
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
333
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
334
         * Character(' ');
335
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
336
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
337
         * KeyAction(""));
338
         *
339
         *  }
340
         */
341
        private static JPopupMenu popup = new JPopupMenu();
342

    
343
        public static void clearMenu() {
344
                popup.removeAll();
345
        }
346

    
347
        public static void addMenuEntry(String text) {
348
                JMenuItem menu = new JMenuItem(text);
349
                menu.setActionCommand(text);
350
                menu.setEnabled(true);
351
                menu.setVisible(true);
352
                menu.addActionListener(new ActionListener() {
353
                        public void actionPerformed(ActionEvent e) {
354
                                adapter.transition(e.getActionCommand());
355
                        }
356
                });
357

    
358
                popup.add(menu);
359
        }
360

    
361
        public static void showPopup(MouseEvent e) {
362
                popup.show(e.getComponent(), e.getX(), e.getY());
363
        }
364

    
365
        public static View getView() {
366
                return view;
367
        }
368

    
369
        /**
370
         * @return Returns the editionManager.
371
         */
372
        public static EditionManager getEditionManager() {
373
                return editionManager;
374
        }
375

    
376
        public static CADTool[] getCADTools() {
377
                return (CADTool[]) namesCadTools.values().toArray(new CADTool[0]);
378
        }
379

    
380
        public static void initFocus() {
381
                view = (View) PluginServices.getMDIManager().getActiveView();
382
                MapControl mapControl = (MapControl) view.getMapControl();
383
                if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")){
384
                        // StatusBarListener sbl=new StatusBarListener(view.getMapControl());
385
                        // mapControl.addMapTool("cadtooladapter",  new Behavior[]{adapter,new MouseMovementBehavior(sbl)});
386
                        mapControl.addMapTool("cadtooladapter", adapter);
387
                }
388
                // view.getMapControl().setTool("cadtooladapter");
389
                JEditTextArea jeta=view.getConsolePanel().getTxt();
390
                jeta.requestFocusInWindow();
391
                jeta.setCaretPosition(jeta.getText().length());
392

    
393
                view.addConsoleListener("cad", new ResponseListener() {
394
                        public void acceptResponse(String response) {
395
                                adapter.textEntered(response);
396
                                // TODO:
397
                                // FocusManager fm=FocusManager.getCurrentManager();
398
                                // fm.focusPreviousComponent(mapControl);
399
                                /*
400
                                 * if (popup.isShowing()){ popup.setVisible(false); }
401
                                 */
402

    
403
                        }
404
                });
405
                editionManager.setMapControl(mapControl);
406
                view.getMapControl().setTool("cadtooladapter");
407
        }
408
}