Statistics
| Revision:

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

History | View | Annotate | Download (14.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 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.FocusManager;
53
import javax.swing.JComponent;
54
import javax.swing.JMenuItem;
55
import javax.swing.JPopupMenu;
56
import javax.swing.JTextArea;
57
import javax.swing.KeyStroke;
58

    
59
import com.iver.andami.PluginServices;
60
import com.iver.andami.plugins.Extension;
61
import com.iver.cit.gvsig.fmap.MapControl;
62
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
63
import com.iver.cit.gvsig.fmap.layers.FLayer;
64
import com.iver.cit.gvsig.fmap.layers.FLayers;
65
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
66
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
67
import com.iver.cit.gvsig.gui.View;
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.ArcCADTool;
71
import com.iver.cit.gvsig.gui.cad.tools.CircleCADTool;
72
import com.iver.cit.gvsig.gui.cad.tools.CopyCADTool;
73
import com.iver.cit.gvsig.gui.cad.tools.EditVertexCADTool;
74
import com.iver.cit.gvsig.gui.cad.tools.EllipseCADTool;
75
import com.iver.cit.gvsig.gui.cad.tools.LineCADTool;
76
import com.iver.cit.gvsig.gui.cad.tools.MoveCADTool;
77
import com.iver.cit.gvsig.gui.cad.tools.PointCADTool;
78
import com.iver.cit.gvsig.gui.cad.tools.PolygonCADTool;
79
import com.iver.cit.gvsig.gui.cad.tools.PolylineCADTool;
80
import com.iver.cit.gvsig.gui.cad.tools.RectangleCADTool;
81
import com.iver.cit.gvsig.gui.cad.tools.RotateCADTool;
82
import com.iver.cit.gvsig.gui.cad.tools.ScaleCADTool;
83
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
84
import com.iver.utiles.console.ResponseListener;
85
/**
86
 * Extensi?n dedicada a controlar las diferentes operaciones sobre el editado
87
 * de una capa.
88
 *
89
 * @author Vicente Caballero Navarro
90
 */
91
public class CADExtension implements Extension {
92
   private static CADToolAdapter adapter=new CADToolAdapter();
93
   private static EditionManager editionManager = new EditionManager();
94
   private static HashMap namesCadTools = new HashMap();
95
   private MapControl mapControl;
96
   private static View view;
97
   public static CADToolAdapter getCADToolAdapter(){
98
            return adapter;
99
   }
100
   /**
101
     * @see com.iver.andami.plugins.Extension#inicializar()
102
     */
103
    public void inicializar() {
104
        SelectionCADTool selection=new SelectionCADTool();
105
            LineCADTool line = new LineCADTool();
106
        PointCADTool point = new PointCADTool();
107
        CircleCADTool circle=new CircleCADTool();
108
        RectangleCADTool rectangle=new RectangleCADTool();
109
        PolylineCADTool polyline=new PolylineCADTool();
110
        EllipseCADTool ellipse=new EllipseCADTool();
111
        ArcCADTool arc=new ArcCADTool();
112
        PolygonCADTool polygon=new PolygonCADTool();
113
        CopyCADTool copy=new CopyCADTool();
114
        MoveCADTool move=new MoveCADTool();
115
        RotateCADTool rotate=new RotateCADTool();
116
        ScaleCADTool scale=new ScaleCADTool();
117
        EditVertexCADTool editvertex=new EditVertexCADTool();
118
        addCADTool("selection", selection);
119
        addCADTool("line", line);
120
        addCADTool("point", point);
121
        addCADTool("circle",circle);
122
        addCADTool("rectangle", rectangle);
123
        addCADTool("polyline", polyline);
124
        addCADTool("ellipse", ellipse);
125
        addCADTool("arc", arc);
126
        addCADTool("polygon", polygon);
127
        addCADTool("copy",copy);
128
        addCADTool("move",move);
129
        addCADTool("rotate",rotate);
130
        addCADTool("scale",scale);
131
        addCADTool("editvertex",editvertex);
132

    
133
        KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
134
        kfm.addKeyEventPostProcessor(new myKeyEventPostProcessor());
135
    }
136

    
137
    /**
138
     * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
139
     */
140
    public void execute(String s) {
141
        view = (View) PluginServices.getMDIManager().getActiveView();
142
        mapControl = (MapControl) view.getMapControl();
143
        if (!mapControl.getNamesMapTools().containsKey("cadtooladapter"))
144
                mapControl.addMapTool("cadtooladapter",adapter);
145
                view.getMapControl().setTool("cadtooladapter");
146
                view.addConsoleListener("cad", new ResponseListener() {
147
                             public void acceptResponse(String response) {
148
                                     adapter.textEntered(response);
149
                                     // TODO:
150
                                     // FocusManager fm=FocusManager.getCurrentManager();
151
                                     // fm.focusPreviousComponent(mapControl);
152
                                     /*if (popup.isShowing()){
153
                                         popup.setVisible(false);
154
                                     }*/
155

    
156
                             }
157
                     });
158
                // registerKeyStrokes();
159
                /* view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "elimina");
160
            view.getActionMap().put("elimina", new MyAction("eliminar"));
161
            view.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
162
            view.getActionMap().put("escape", new MyAction("escape")); */
163

    
164
        /* FLayers layers=mapControl.getMapContext().getLayers();
165
                for (int i=0;i<layers.getLayersCount();i++){
166
                        if (layers.getLayer(i).isEditing() && layers.getLayer(i) instanceof FLyrVect){
167
                                adapter.setVectorialAdapter((VectorialEditableAdapter)((FLyrVect)layers.getLayer(i)).getSource());
168
                                adapter.setMapControl(mapControl);
169

170
                        }
171
                } */
172

    
173
        view.getMapControl().setTool("cadtooladapter");
174

    
175
        if (s.equals("SPLINE")) {
176
                setCADTool("spline");
177
        } else if (s.equals("COPY")) {
178
                setCADTool("copy");
179
        } else if (s.equals("MOVE")) {
180
                setCADTool("move");
181
        } else if (s.equals("EQUIDISTANCE")) {
182
                setCADTool("equidistance");
183
        } else if (s.equals("MATRIZ")) {
184
                setCADTool("matriz");
185
        } else if (s.equals("SYMMETRY")) {
186
                setCADTool("symmetry");
187
        } else if (s.equals("ROTATION")) {
188
                setCADTool("rotate");
189
        } else if (s.equals("STRETCHING")) {
190
                setCADTool("stretching");
191
        } else if (s.equals("SCALE")) {
192
                setCADTool("scale");
193
        } else if (s.equals("EXTEND")) {
194
                setCADTool("extend");
195
        } else if (s.equals("TRIM")) {
196
                setCADTool("trim");
197
        } else if (s.equals("UNIT")) {
198
                setCADTool("unit");
199
        } else if (s.equals("EXPLOIT")) {
200
                setCADTool("exploit");
201
        } else if (s.equals("CHAFLAN")) {
202
                setCADTool("chaflan");
203
        } else if (s.equals("JOIN")) {
204
                setCADTool("join");
205
        } else if (s.equals("SELCAD")) {
206
                setCADTool("selection");
207
        } else if (s.equals("POINT")) {
208
                setCADTool("point");
209
        } else if (s.equals("LINE")) {
210
                setCADTool("line");
211
        } else if (s.equals("POLYLINE")) {
212
                setCADTool("polyline");
213
        } else if (s.equals("CIRCLE")) {
214
           setCADTool("circle");
215
        } else if (s.equals("ARC")) {
216
                setCADTool("arc");
217
        } else if (s.equals("ELLIPSE")) {
218
                setCADTool("ellipse");
219
        } else if (s.equals("RECTANGLE")) {
220
                setCADTool("rectangle");
221
        } else if (s.equals("POLYGON")) {
222
                setCADTool("polygon");
223
        } else if (s.equals("EDITVERTEX")) {
224
                setCADTool("editvertex");
225
        }
226
        adapter.configureMenu();
227
        //ViewControls.CANCELED=false;
228
    }
229
    public void addCADTool(String name, CADTool c){
230
                namesCadTools.put(name, c);
231
        }
232
    public static void setCADTool(String text){
233
                CADTool ct = (CADTool) namesCadTools.get(text);
234
                if (ct == null) throw new RuntimeException("No such cad tool");
235
                adapter.setCadTool(ct);
236
                ct.init();
237
                //PluginServices.getMainFrame().setSelectedTool("SELECT");
238
                //PluginServices.getMainFrame().enableControls();
239
        }
240
    /**
241
     * @see com.iver.andami.plugins.Extension#isEnabled()
242
     */
243
    public boolean isEnabled() {
244
        /* com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
245
                                                             .getActiveView();
246

247
        if (f == null) {
248
            return false;
249
        }
250

251
        if (f.getClass() == View.class) {
252
            FLayer[] l = ((View) f).getModel().getMapContext().getLayers()
253
                          .getActives();
254

255
            for (int i = 0; i < l.length; i++) {
256
                if (l[i] instanceof FLyrVect && ((FLyrVect) l[i]).isEditing()) {
257
                    return true;
258
                }
259
            }
260
        }
261

262
        return false; */
263
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE)
264
                {
265
                        // Queremos que siempre que haya edici?n esto est? funcionando.
266
                editionManager.setMapControl(mapControl);
267
                        return true;
268
                }
269
                else
270
                        return false;
271

    
272
    }
273

    
274
    /**
275
     * @see com.iver.andami.plugins.Extension#isVisible()
276
     */
277
    public boolean isVisible() {
278
        com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager()
279
                                                             .getActiveView();
280

    
281
        if (f == null) {
282
            return false;
283
        }
284

    
285
        if (f.getClass() == View.class) {
286
            return true;
287
        } else {
288
            return false;
289
        }
290
    }
291
        public MapControl getMapControl() {
292
                return this.mapControl;
293
        }
294
        class KeyAction extends AbstractAction{
295

    
296
                private String key;
297

    
298
                public KeyAction(String key){
299
                        this.key = key;
300
                }
301

    
302
                /* (non-Javadoc)
303
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
304
                 */
305
                public void actionPerformed(ActionEvent e) {
306
                        view.focusConsole(key);
307
                }
308

    
309
        }
310

    
311
        class MyAction extends AbstractAction{
312
                private String actionCommand;
313

    
314
                public MyAction(String command){
315
                        actionCommand = command;
316
                }
317

    
318
                /**
319
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
320
                 */
321
                public void actionPerformed(ActionEvent e) {
322
                        adapter.keyPressed(actionCommand);
323
                }
324

    
325
        }
326

    
327
        /**
328
         * @author fjp
329
         *
330
         * La idea es usar esto para recibir lo que el usuario escribe y enviarlo
331
         * a la consola de la vista para que salga por all?.
332
         */
333
        private class myKeyEventPostProcessor implements KeyEventPostProcessor
334
        {
335

    
336
                public boolean postProcessKeyEvent(KeyEvent e) {
337
                        // System.out.println("KeyEvent e = " + e);
338
                        if ((adapter==null) ||  (view == null)) return false;
339
                        if (e.getID() != KeyEvent.KEY_RELEASED) return false;
340
                if (e.getKeyCode() == KeyEvent.VK_DELETE)
341
                        adapter.keyPressed("eliminar");
342
                else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
343
                        adapter.keyPressed("escape");
344
                else if (e.getKeyCode() == KeyEvent.VK_ENTER)
345
                        // TODO: REVISAR ESTO CUANDO VIENE UN INTRO DESDE UN JTEXTAREA
346
                        // QUE NO ES EL DE CONSOLA
347
                        view.focusConsole("");
348
                else
349
                {
350
                        if (e.getComponent().getName() != null)
351
                        {                                
352
                                System.out.println("Evento de teclado desde el componente " + e.getComponent().getName());
353
                                if (!e.getComponent().getName().equals("CADConsole"))
354
                                {
355
                                        if ((e.getID() == KeyEvent.KEY_TYPED) && (!e.isActionKey()))
356
                                        {
357
                                                    if (Character.isLetterOrDigit(e.getKeyChar()))
358
                                                    {
359
                                                            Character keyChar = new Character(e.getKeyChar());
360
                                                            view.focusConsole(keyChar+"");
361
                                                }
362
                                        }
363
                                }
364
                        }
365
                }
366
                        return false;
367
                }
368

    
369
        }
370

    
371
        private void registerKeyStrokes(){
372
                for (char key = '0'; key <= '9'; key++){
373
                        Character keyChar = new Character(key);
374
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
375
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
376
                }
377
                for (char key = 'a'; key <= 'z'; key++){
378
                        Character keyChar = new Character(key);
379
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
380
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
381
                }
382
                for (char key = 'A'; key <= 'Z'; key++){
383
                        Character keyChar = new Character(key);
384
                        mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
385
                        mapControl.getActionMap().put(keyChar, new KeyAction(keyChar+""));
386
                }
387
                //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
388
        //this.getActionMap().put("enter", new MyAction("enter"));
389
                Character keyChar = new Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
390
                mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),keyChar);
391
                mapControl.getActionMap().put(keyChar, new KeyAction(""));
392

    
393
                // El espacio como si fuera INTRO
394
                Character keyCharSpace = new Character(' ');
395
                mapControl.getInputMap(MapControl.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(' '), keyCharSpace);
396
                mapControl.getActionMap().put(keyCharSpace, new KeyAction(""));
397

    
398

    
399
        }
400

    
401
        private static JPopupMenu popup = new JPopupMenu();
402
        public static  void clearMenu(){
403
                popup.removeAll();
404
        }
405

    
406
        public static void addMenuEntry(String text){
407
                JMenuItem menu = new JMenuItem(text);
408
                menu.setActionCommand(text);
409
                menu.setEnabled(true);
410
                menu.setVisible(true);
411
                menu.addActionListener(new ActionListener() {
412
                        public void actionPerformed(ActionEvent e) {
413
                                adapter.transition(e.getActionCommand());
414
                        }
415
                });
416

    
417
                popup.add(menu);
418
        }
419
        public static void showPopup(MouseEvent e) {
420
                    popup.show(e.getComponent(),
421
                       e.getX(), e.getY());
422
    }
423
        public static View getView() {
424
                return view;
425
        }
426

    
427
        /**
428
         * @return Returns the editionManager.
429
         */
430
        public static EditionManager getEditionManager() {
431
                return editionManager;
432
        }
433
}