Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / CADExtension.java @ 5227

History | View | Annotate | Download (10.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.text.JTextComponent;
55

    
56
import com.iver.andami.PluginServices;
57
import com.iver.andami.plugins.Extension;
58
import com.iver.cit.gvsig.fmap.MapControl;
59
import com.iver.cit.gvsig.fmap.layers.FLayer;
60
import com.iver.cit.gvsig.fmap.layers.FLyrAnnotation;
61
import com.iver.cit.gvsig.fmap.tools.CompoundBehavior;
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.cad.CADTool;
66
import com.iver.cit.gvsig.gui.cad.CADToolAdapter;
67
import com.iver.cit.gvsig.gui.cad.tools.CopyCADTool;
68
import com.iver.cit.gvsig.gui.cad.tools.RotateCADTool;
69
import com.iver.cit.gvsig.gui.cad.tools.ScaleCADTool;
70
import com.iver.cit.gvsig.gui.toolListeners.StatusBarListener;
71
import com.iver.utiles.console.JConsole;
72
import com.iver.utiles.console.ResponseListener;
73

    
74
/**
75
 * Extensi?n dedicada a controlar las diferentes operaciones sobre el editado de
76
 * una capa.
77
 *
78
 * @author Vicente Caballero Navarro
79
 */
80
public class CADExtension extends Extension {
81
        private static CADToolAdapter adapter = new CADToolAdapter();
82

    
83
        private static EditionManager editionManager = new EditionManager();
84

    
85
        private static HashMap namesCadTools = new HashMap();
86

    
87
        // /private MapControl mapControl;
88
        private static View view;
89

    
90
        private MapControl mapControl;
91

    
92
        public static CADToolAdapter getCADToolAdapter() {
93
                return adapter;
94
        }
95

    
96
        /**
97
         * @see com.iver.andami.plugins.IExtension#initialize()
98
         */
99
        public void initialize() {
100

    
101
                CopyCADTool copy = new CopyCADTool();
102

    
103
                RotateCADTool rotate = new RotateCADTool();
104
                ScaleCADTool scale = new ScaleCADTool();
105

    
106
                addCADTool("_copy", copy);
107

    
108
                addCADTool("_rotate", rotate);
109
                addCADTool("_scale", scale);
110

    
111
                KeyboardFocusManager kfm = KeyboardFocusManager
112
                                .getCurrentKeyboardFocusManager();
113
                kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
114
        }
115

    
116
        /**
117
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
118
         */
119
        public void execute(String s) {
120
                initFocus();
121

    
122
                if (s.equals("_spline") || s.equals("_copy")
123
                                || s.equals("_equidistance") || s.equals("_matriz")
124
                                || s.equals("_symmetry") || s.equals("_rotate")
125
                                || s.equals("_stretch") || s.equals("_scale")
126
                                || s.equals("_extend") || s.equals("_trim")
127
                                || s.equals("_unit") || s.equals("_exploit")
128
                                || s.equals("_chaflan") || s.equals("_join")) {
129
                        setCADTool(s, true);
130
                }
131
                adapter.configureMenu();
132
        }
133

    
134
        public static void addCADTool(String name, CADTool c) {
135
                namesCadTools.put(name, c);
136
        }
137

    
138
        public static void setCADTool(String text, boolean showCommand) {
139
                CADTool ct = (CADTool) namesCadTools.get(text);
140
                if (ct == null)
141
                        throw new RuntimeException("No such cad tool");
142
                adapter.setCadTool(ct);
143
                ct.init();
144
                if (showCommand) {
145
                        View vista = (View) PluginServices.getMDIManager().getActiveView();
146
                        vista.getConsolePanel().addText("\n" + ct.getName(),
147
                                        JConsole.COMMAND);
148
                        adapter.askQuestion();
149
                }
150
                // PluginServices.getMainFrame().setSelectedTool("SELECT");
151
                // PluginServices.getMainFrame().enableControls();
152
        }
153

    
154
        public static CADTool getCADTool() {
155
                return adapter.getCadTool();
156
        }
157

    
158
        /**
159
         * @see com.iver.andami.plugins.IExtension#isEnabled()
160
         */
161
        public boolean isEnabled() {
162
                return true;
163
        }
164

    
165
        /**
166
         * @see com.iver.andami.plugins.IExtension#isVisible()
167
         */
168
        public boolean isVisible() {
169
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
170
                        view = (View) PluginServices.getMDIManager().getActiveView();
171
                        mapControl = (MapControl) view.getMapControl();
172
                        FLayer[] layers = mapControl.getMapContext().getLayers()
173
                                        .getActives();
174
                        if (!(layers[0] instanceof FLyrAnnotation)) {
175
                                return true;
176
                        }
177
                }
178
                return false;
179
        }
180

    
181
        public MapControl getMapControl() {
182
                return editionManager.getMapControl();
183
        }
184

    
185
        class KeyAction extends AbstractAction {
186

    
187
                private String key;
188

    
189
                public KeyAction(String key) {
190
                        this.key = key;
191
                }
192

    
193
                /*
194
                 * (non-Javadoc)
195
                 *
196
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
197
                 */
198
                public void actionPerformed(ActionEvent e) {
199
                        view.focusConsole(key);
200
                }
201

    
202
        }
203

    
204
        class MyAction extends AbstractAction {
205
                private String actionCommand;
206

    
207
                public MyAction(String command) {
208
                        actionCommand = command;
209
                }
210

    
211
                /**
212
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
213
                 */
214
                public void actionPerformed(ActionEvent e) {
215
                        adapter.keyPressed(actionCommand);
216
                }
217

    
218
        }
219

    
220
        /**
221
         * @author fjp
222
         *
223
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo a
224
         * la consola de la vista para que salga por all?.
225
         */
226
        private class myKeyEventPostProcessor implements KeyEventPostProcessor {
227

    
228
                public boolean postProcessKeyEvent(KeyEvent e) {
229
                        // System.out.println("KeyEvent e = " + e);
230
                        if ((adapter == null) || (view == null))
231
                                return false;
232

    
233
                        if (e.getID() != KeyEvent.KEY_RELEASED)
234
                                return false;
235
                        if (!(e.getComponent() instanceof JTextComponent)) {
236
                                if (e.getKeyCode() == KeyEvent.VK_DELETE)
237
                                        adapter.keyPressed("eliminar");
238
                                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
239
                                        adapter.keyPressed("escape");
240
                                else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
241
                                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN
242
                                        // JTEXTAREA
243
                                        // QUE NO ES EL DE CONSOLA
244
                                        view.focusConsole("");
245
                                }
246

    
247
                                else {
248
                                        if ((e.getID() == KeyEvent.KEY_RELEASED)
249
                                                        && (!e.isActionKey())) {
250
                                                //if (Character.isLetterOrDigit(e.getKeyChar())) {
251
                                                        Character keyChar = new Character(e.getKeyChar());
252
                                                        if (e.getComponent().getName() != null) {
253
                                                                System.out
254
                                                                                .println("Evento de teclado desde el componente "
255
                                                                                                + e.getComponent().getName());
256
                                                                if (!e.getComponent().getName().equals(
257
                                                                                "CADConsole")) {
258
                                                                        view.focusConsole(keyChar + "");
259
                                                                }
260
                                                        } else {
261
                                                                if (!(e.getComponent() instanceof JTextComponent)) {
262
                                                                        view.focusConsole(keyChar + "");
263
                                                                }
264
                                                        }
265
                                                //}
266
                                        }
267
                                }
268
                        }
269
                        return false;
270
                }
271

    
272
        }
273

    
274
        /*
275
         * private void registerKeyStrokes(){ for (char key = '0'; key <= '9';
276
         * key++){ Character keyChar = new Character(key);
277
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
278
         * keyChar); mapControl.getActionMap().put(keyChar, new
279
         * KeyAction(keyChar+"")); } for (char key = 'a'; key <= 'z'; key++){
280
         * Character keyChar = new Character(key);
281
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
282
         * keyChar); mapControl.getActionMap().put(keyChar, new
283
         * KeyAction(keyChar+"")); } for (char key = 'A'; key <= 'Z'; key++){
284
         * Character keyChar = new Character(key);
285
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),
286
         * keyChar); mapControl.getActionMap().put(keyChar, new
287
         * KeyAction(keyChar+"")); }
288
         * //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
289
         * 0), "enter"); //this.getActionMap().put("enter", new MyAction("enter"));
290
         * Character keyChar = new
291
         * Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
292
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
293
         * 0),keyChar); mapControl.getActionMap().put(keyChar, new KeyAction(""));
294
         *  // El espacio como si fuera INTRO Character keyCharSpace = new
295
         * Character(' ');
296
         * mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('
297
         * '), keyCharSpace); mapControl.getActionMap().put(keyCharSpace, new
298
         * KeyAction(""));
299
         *
300
         *  }
301
         */
302
        private static JPopupMenu popup = new JPopupMenu();
303

    
304
        public static void clearMenu() {
305
                popup.removeAll();
306
        }
307

    
308
        public static void addMenuEntry(String text) {
309
                JMenuItem menu = new JMenuItem(text);
310
                menu.setActionCommand(text);
311
                menu.setEnabled(true);
312
                menu.setVisible(true);
313
                menu.addActionListener(new ActionListener() {
314
                        public void actionPerformed(ActionEvent e) {
315
                                adapter.transition(e.getActionCommand());
316
                        }
317
                });
318

    
319
                popup.add(menu);
320
        }
321

    
322
        public static void showPopup(MouseEvent e) {
323
                popup.show(e.getComponent(), e.getX(), e.getY());
324
        }
325

    
326
        public static View getView() {
327
                return view;
328
        }
329

    
330
        /**
331
         * @return Returns the editionManager.
332
         */
333
        public static EditionManager getEditionManager() {
334
                return editionManager;
335
        }
336

    
337
        public static CADTool[] getCADTools() {
338
                return (CADTool[]) namesCadTools.values().toArray(new CADTool[0]);
339
        }
340

    
341
        public static void initFocus() {
342
                view = (View) PluginServices.getMDIManager().getActiveView();
343
                MapControl mapControl = (MapControl) view.getMapControl();
344
                if (!mapControl.getNamesMapTools().containsKey("cadtooladapter")){
345
                        StatusBarListener sbl=new StatusBarListener(view.getMapControl());
346
                        mapControl.addMapTool("cadtooladapter",  new Behavior[]{adapter,new MouseMovementBehavior(sbl)});
347
                }
348
                view.getMapControl().setTool("cadtooladapter");
349
                view.addConsoleListener("cad", new ResponseListener() {
350
                        public void acceptResponse(String response) {
351
                                adapter.textEntered(response);
352
                                // TODO:
353
                                // FocusManager fm=FocusManager.getCurrentManager();
354
                                // fm.focusPreviousComponent(mapControl);
355
                                /*
356
                                 * if (popup.isShowing()){ popup.setVisible(false); }
357
                                 */
358

    
359
                        }
360
                });
361
                editionManager.setMapControl(mapControl);
362
                view.getMapControl().setTool("cadtooladapter");
363
        }
364
}