Statistics
| Revision:

root / branches / gvSIG_CAD / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / CadMapControl.java @ 3513

History | View | Annotate | Download (15.3 KB)

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

    
47
import java.awt.BorderLayout;
48
import java.awt.Cursor;
49
import java.awt.Image;
50
import java.awt.Point;
51
import java.awt.Toolkit;
52
import java.awt.event.ActionEvent;
53
import java.awt.event.ActionListener;
54
import java.awt.event.FocusEvent;
55
import java.awt.event.FocusListener;
56
import java.awt.event.KeyEvent;
57
import java.awt.event.MouseEvent;
58
import java.awt.event.MouseMotionListener;
59
import java.awt.image.MemoryImageSource;
60
import java.beans.PropertyChangeListener;
61
import java.util.HashMap;
62

    
63
import javax.swing.AbstractAction;
64
import javax.swing.Action;
65
import javax.swing.FocusManager;
66
import javax.swing.JComponent;
67
import javax.swing.JMenuItem;
68
import javax.swing.JPanel;
69
import javax.swing.JPopupMenu;
70
import javax.swing.JSplitPane;
71
import javax.swing.KeyStroke;
72

    
73
import com.iver.andami.PluginServices;
74
import com.iver.cit.gvsig.fmap.FMap;
75
import com.iver.cit.gvsig.fmap.MapControl;
76
import com.iver.cit.gvsig.fmap.layers.CancelationException;
77
import com.iver.cit.gvsig.fmap.layers.EditionEvent;
78
import com.iver.cit.gvsig.fmap.layers.FLayer;
79
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
80
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener;
81
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
82
import com.iver.cit.gvsig.fmap.layers.layerOperations.EditableVectorialLayer;
83
import com.iver.cit.gvsig.fmap.layers.layerOperations.EditionListener;
84
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
85
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
86
import com.iver.cit.gvsig.gui.cad.automaton.Simetria;
87
import com.iver.cit.gvsig.gui.cad.tools.ArcCadTool;
88
import com.iver.cit.gvsig.gui.cad.tools.CircleCadTool;
89
import com.iver.cit.gvsig.gui.cad.tools.CopyCadTool;
90
import com.iver.cit.gvsig.gui.cad.tools.EllipseCadTool;
91
import com.iver.cit.gvsig.gui.cad.tools.ExploitCadTool;
92
import com.iver.cit.gvsig.gui.cad.tools.LineCadTool;
93
import com.iver.cit.gvsig.gui.cad.tools.PointCadTool;
94
import com.iver.cit.gvsig.gui.cad.tools.PolyLineCadTool;
95
import com.iver.cit.gvsig.gui.cad.tools.PolygonCadTool;
96
import com.iver.cit.gvsig.gui.cad.tools.RectangleCadTool;
97
import com.iver.cit.gvsig.gui.cad.tools.RotateCadTool;
98
import com.iver.cit.gvsig.gui.cad.tools.ScaleCadTool;
99
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
100
import com.iver.cit.gvsig.gui.cad.tools.SplineCadTool;
101
import com.iver.cit.gvsig.gui.cad.tools.SymmetryCadTool;
102
import com.iver.cit.gvsig.gui.cad.tools.StretchingCadTool;
103
import com.iver.utiles.console.JConsole;
104
import com.iver.utiles.console.ResponseListener;
105
public class CadMapControl extends JPanel {
106
        private HashMap namesCadTools = new HashMap();
107
        private CADToolAdapter cadToolAdapter = new CADToolAdapter();
108
        private LayerEditionListener layerEditionListener = new LayerEditionListener();
109
        private LayerColListener layerListener = new LayerColListener();
110
        private MapControl mapControl = null;
111
        private JPanel jSplitPane = null;
112
        
113
        //Menu contextual
114
        private JPopupMenu popup = new JPopupMenu();
115

    
116
        /**
117
         * This is the default constructor
118
         */
119
        public CadMapControl() {
120
                super();
121
                initialize();
122
        }
123
        /**
124
         * This method initializes this
125
         * 
126
         * @return void
127
         */
128
        private  void initialize() {
129
        // Herramienta de edici?n de CAD
130
        getMapControl().addMapTool("cadEdition", cadToolAdapter);
131
        
132
        cadToolAdapter.setCadMapControl(this);
133
        
134
        //Herramienta de CAD de a?adir punto
135
        PointCadTool pointCADTool = new PointCadTool();
136
        addCadTool("point", pointCADTool);
137
        //Herramienta de CAD de a?adir l?nea
138
        LineCadTool lineCADTool = new LineCadTool();
139
        addCadTool("line", lineCADTool);
140
        //Herramienta de CAD de a?adir c?rculo
141
        addCadTool("circle", new CircleCadTool());
142
        //Herramienta de CAD de a?adir c?rculo
143
        addCadTool("ellipse", new EllipseCadTool());
144
        //Herramienta de CAD de a?adir c?rculo
145
        addCadTool("arc", new ArcCadTool());
146
        //Herramienta de CAD de a?adir polyl?nea
147
        PolyLineCadTool polylineCADTool = new PolyLineCadTool();
148
        addCadTool("polyline", polylineCADTool);
149
        //Herramienta de CAD de a?adir rect?ngulo.
150
        RectangleCadTool rectangleCADTool = new RectangleCadTool();
151
        addCadTool("rectangle", rectangleCADTool);
152
        //Herramienta de CAD de a?adir pol?gono.
153
        PolygonCadTool polygonCADTool = new PolygonCadTool();
154
        addCadTool("polygon", polygonCADTool);
155
        //Herramienta de CAD para seleccionar geometr?as.
156
        SelectionCadTool selectionCADTool = new SelectionCadTool();
157
        addCadTool("selection", selectionCADTool);
158
        //Herramienta de CAD para copiar una geometria.
159
        CopyCadTool copyCADTool = new CopyCadTool();
160
        addCadTool("copy", copyCADTool);
161
        //Herramienta de CAD para girar una geometria.
162
        RotateCadTool rotateCADTool = new RotateCadTool();
163
        addCadTool("rotate", rotateCADTool);
164
        //Herramienta de CAD para escalar una geometria.
165
        ScaleCadTool scaleCADTool = new ScaleCadTool();
166
        addCadTool("scale", scaleCADTool);
167
        //Herramienta de CAD para extirar una geometria.
168
        StretchingCadTool stretchingCADTool = new StretchingCadTool();
169
        addCadTool("stretching", stretchingCADTool);
170
        
171
        //Herramienta de CAD para crear una simetria de una geometria.
172
        ///SymmetryCadTool symmetryCADTool = new SymmetryCadTool();
173
        ///addCadTool("symmetry", symmetryCADTool);
174
        
175
//      Herramienta de CAD para explotar una geometria.
176
        ExploitCadTool exploitCADTool = new ExploitCadTool();
177
        addCadTool("exploit", exploitCADTool);
178
        
179
//      Herramienta de CAD para crear un spline geometria.
180
        SplineCadTool splainCADTool = new SplineCadTool();
181
        addCadTool("spline", splainCADTool);
182
        
183
        addMenuEntry("aceptar", "aceptar");
184
        addMenuEntry("cancelar", "cancelar");
185
        
186
        this.setLayout(new BorderLayout());
187
                this.setSize(300,200);
188
                this.add(getMapControl(), java.awt.BorderLayout.CENTER);
189
                
190
        this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "elimina");
191
        this.getActionMap().put("elimina", new MyAction("eliminar"));
192
        this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
193
        this.getActionMap().put("escape", new MyAction("escape"));
194
        
195
        registerKeyStrokes();
196
        
197
        PluginServices.getMainFrame().addConsoleListener("cad", new ResponseListener() {
198
                        public void acceptResponse(String response) {
199
                                cadToolAdapter.textEntered(response);
200
                                FocusManager fm=FocusManager.getCurrentManager();
201
                                fm.focusPreviousComponent(mapControl);
202
                                if (popup.isShowing()){
203
                                    popup.setVisible(false);
204
                                }
205
                                
206
                        }
207
                });
208
        }
209
        
210
        class KeyAction extends AbstractAction{
211

    
212
                private String key;
213
                
214
                public KeyAction(String key){
215
                        this.key = key;
216
                }
217
                
218
                /* (non-Javadoc)
219
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
220
                 */
221
                public void actionPerformed(ActionEvent e) {
222
                        PluginServices.getMainFrame().focusConsole(key);
223
                }
224
                
225
        }
226
        
227
        class MyAction extends AbstractAction{
228
                private String actionCommand;
229
                
230
                public MyAction(String command){
231
                        actionCommand = command;
232
                }
233

    
234
                /**
235
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
236
                 */
237
                public void actionPerformed(ActionEvent e) {
238
                        cadToolAdapter.keyPressed(actionCommand);
239
                }
240
                
241
        }
242
        
243
        private void registerKeyStrokes(){
244
                for (char key = '0'; key <= '9'; key++){
245
                        Character keyChar = new Character(key);
246
                        this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
247
                        this.getActionMap().put(keyChar, new KeyAction(keyChar+""));
248
                }
249
                for (char key = 'a'; key <= 'z'; key++){
250
                        Character keyChar = new Character(key);
251
                        this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
252
                        this.getActionMap().put(keyChar, new KeyAction(keyChar+""));
253
                }
254
                for (char key = 'A'; key <= 'Z'; key++){
255
                        Character keyChar = new Character(key);
256
                        this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), keyChar);
257
                        this.getActionMap().put(keyChar, new KeyAction(keyChar+""));
258
                }
259
                //this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
260
        //this.getActionMap().put("enter", new MyAction("enter"));
261
                Character keyChar = new Character(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0).getKeyChar());
262
                this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),keyChar);
263
                this.getActionMap().put(keyChar, new KeyAction(""));
264
        }
265
        
266
        /**
267
         * This method initializes mapControl        
268
         *         
269
         * @return com.iver.cit.gvsig.fmap.MapControl        
270
         */    
271
        public MapControl getMapControl() {
272
                if (mapControl == null) {
273
                        mapControl = new MapControl();
274
                        mapControl.addMouseMotionListener(new MouseMotionListener(){
275

    
276
                                public void mouseDragged(MouseEvent arg0) {
277
                                }
278

    
279
                                public void mouseMoved(MouseEvent arg0) {
280
                                        //CadMapControl.this.setCursor(cadToolAdapter.getCursor());
281
                                        clearMouseImage();
282
                                }
283
                                
284
                        });
285
                        mapControl.addMouseListener(new java.awt.event.MouseAdapter() { 
286
                                public void mousePressed(java.awt.event.MouseEvent e) {
287
                                        show(e);
288
                                }
289
                                public void mouseReleased(java.awt.event.MouseEvent e) {
290
                                        show(e);
291
                                }
292
                                private void show(java.awt.event.MouseEvent e){
293
                                if (e.isPopupTrigger() && (mapControl.getCurrentMapTool() == cadToolAdapter)) {
294
                                    popup.show(e.getComponent(),
295
                                               e.getX(), e.getY());
296
                                }
297
                                }
298
                        });
299
                }
300
                return mapControl;
301
        }
302
        
303
        /**
304
         * This method initializes jSplitPane        
305
         *         
306
         * @return javax.swing.JSplitPane        
307
         */    
308
        private JPanel getJSplitPane() {
309
                if (jSplitPane == null) {
310
                        jSplitPane = new JPanel();
311
                        jSplitPane.add(getMapControl());
312
                }
313
                return jSplitPane;
314
        }
315
        public FMap getMapContext() {
316
                return mapControl.getMapContext();
317
        }
318
        
319
        public void addMapTool(String name, Behavior tool) {
320
                mapControl.addMapTool(name, tool);
321
        }
322
        
323
        public void addMapTool(String name, Behavior[] tools) {
324
                mapControl.addMapTool(name, tools);
325
        }
326
        public void setMapContext(FMap model) {
327
                mapControl.getMapContext().getLayers().removeLayerCollectionListener(layerListener);
328
                mapControl.setMapContext(model);
329
                mapControl.getMapContext().getLayers().addLayerCollectionListener(layerListener);
330
        }
331

    
332
        public void addCadTool(String name, CadTool c){
333
                namesCadTools.put(name, c);
334
        }
335
        
336
        public void setCadTool(String text){
337
                CadTool ct = (CadTool) namesCadTools.get(text);
338
                if (ct == null) throw new RuntimeException("No such cad tool");
339
                
340
                cadToolAdapter.setCadTool(ct);
341
                getMapControl().setTool("cadEdition");
342
                //clearMouseImage();
343
        }
344
        private void clearMouseImage(){
345
                int[] pixels = new int[16 * 16];
346
                Image image = Toolkit.getDefaultToolkit().createImage(
347
                        new MemoryImageSource(16, 16, pixels, 0, 16));
348
                Cursor transparentCursor =
349
                        Toolkit.getDefaultToolkit().createCustomCursor
350
                            (image, new Point(0,0), "invisiblecursor");
351
                this.setCursor(transparentCursor);
352
        }
353
        public void clearMenu(){
354
                popup.removeAll();
355
        }
356

    
357
        public void addMenuEntry(String text, String actionCommand){
358
                JMenuItem menu = new JMenuItem(text);
359
                menu.setActionCommand(actionCommand);
360
                menu.setEnabled(true);
361
                menu.setVisible(true);
362
                menu.addActionListener(new ActionListener() {
363
                        public void actionPerformed(ActionEvent e) {
364
                                cadToolAdapter.transition(e.getActionCommand());
365
                        }
366
                });
367
                
368
                popup.add(menu);
369
        }
370
        public CADToolAdapter getCadToolAdapter(){
371
                return cadToolAdapter;
372
        }
373
private class LayerColListener implements LayerCollectionListener{
374

    
375
        /**
376
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
377
         */
378
        public void layerAdded(LayerCollectionEvent e) {
379
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
380
                        ((EditableVectorialLayer) e.getAffectedLayer()).addEditionListener(layerEditionListener);
381
                }
382
        }
383

    
384
        /**
385
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
386
         */
387
        public void layerMoved(LayerPositionEvent e) {
388
        }
389

    
390
        /**
391
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
392
         */
393
        public void layerRemoved(LayerCollectionEvent e) {
394
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
395
                        ((EditableVectorialLayer) e.getAffectedLayer()).removeEditionListener(layerEditionListener);
396
                }
397
        }
398

    
399
        /**
400
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
401
         */
402
        public void layerAdding(LayerCollectionEvent e) throws CancelationException {
403
        }
404

    
405
        /**
406
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
407
         */
408
        public void layerMoving(LayerPositionEvent e) throws CancelationException {
409
        }
410

    
411
        /**
412
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
413
         */
414
        public void layerRemoving(LayerCollectionEvent e) throws CancelationException {
415
        }
416

    
417
        /**
418
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
419
         */
420
        public void activationChanged(LayerCollectionEvent e) throws CancelationException {
421
        }
422

    
423
        /**
424
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
425
         */
426
        public void visibilityChanged(LayerCollectionEvent e) throws CancelationException {
427
        }
428
        
429
}
430
        
431
private class LayerEditionListener implements EditionListener{
432

    
433
        /**
434
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
435
         */
436
        public void startEdition(EditionEvent e) {
437
                FLayer lyr = e.getSource();
438
                cadToolAdapter.setEditableFeatureSource(e.getEditingAdapter(), ((Selectable)lyr).getSelection());                
439
        }
440

    
441
        /**
442
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.EditionListener#stopEdition(com.iver.cit.gvsig.fmap.layers.EditionEvent)
443
         */
444
        public void stopEdition(EditionEvent layer) {
445
                CadMapControl.this.getMapControl().setTool("zoomIn");
446
        }
447
        
448
}
449

    
450
}