Statistics
| Revision:

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

History | View | Annotate | Download (14 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.util.HashMap;
61

    
62
import javax.swing.AbstractAction;
63
import javax.swing.JMenuItem;
64
import javax.swing.JPanel;
65
import javax.swing.JPopupMenu;
66
import javax.swing.JSplitPane;
67
import javax.swing.KeyStroke;
68

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

    
111
        /**
112
         * This is the default constructor
113
         */
114
        public CadMapControl() {
115
                super();
116
                initialize();
117
        }
118
        /**
119
         * This method initializes this
120
         * 
121
         * @return void
122
         */
123
        private  void initialize() {
124
        // Herramienta de edici?n de CAD
125
        getMapControl().addMapTool("cadEdition", cadToolAdapter);
126
        
127
        cadToolAdapter.setConsole(getConsole());
128
        cadToolAdapter.setCadMapControl(this);
129
        
130
        //Herramienta de CAD de a?adir punto
131
        PointCadTool pointCADTool = new PointCadTool();
132
        addCadTool("point", pointCADTool);
133
        //Herramienta de CAD de a?adir l?nea
134
        LineCadTool lineCADTool = new LineCadTool();
135
        addCadTool("line", lineCADTool);
136
        //Herramienta de CAD de a?adir c?rculo
137
        addCadTool("circle", new CircleCadTool());
138
        //Herramienta de CAD de a?adir c?rculo
139
        addCadTool("ellipse", new EllipseCadTool());
140
        //Herramienta de CAD de a?adir c?rculo
141
        addCadTool("arc", new ArcCadTool());
142
        //Herramienta de CAD de a?adir polyl?nea
143
        PolyLineCadTool polylineCADTool = new PolyLineCadTool();
144
        addCadTool("polyline", polylineCADTool);
145
        //Herramienta de CAD de a?adir rect?ngulo.
146
        RectangleCadTool rectangleCADTool = new RectangleCadTool();
147
        addCadTool("rectangle", rectangleCADTool);
148
        //Herramienta de CAD de a?adir pol?gono.
149
        PolygonCadTool polygonCADTool = new PolygonCadTool();
150
        addCadTool("polygon", polygonCADTool);
151
        //Herramienta de CAD para seleccionar geometr?as.
152
        SelectionCadTool selectionCADTool = new SelectionCadTool();
153
        addCadTool("selection", selectionCADTool);
154
        //Herramienta de CAD para copiar una geometria.
155
        CopyCadTool copyCADTool = new CopyCadTool();
156
        addCadTool("copy", copyCADTool);
157
        //Herramienta de CAD para girar una geometria.
158
        RotateCadTool rotateCADTool = new RotateCadTool();
159
        addCadTool("rotate", rotateCADTool);
160
        //Herramienta de CAD para escalar una geometria.
161
        ScaleCadTool scaleCADTool = new ScaleCadTool();
162
        addCadTool("scale", scaleCADTool);
163
        //Herramienta de CAD para extirar una geometria.
164
        StretchingCadTool stretchingCADTool = new StretchingCadTool();
165
        addCadTool("stretching", stretchingCADTool);
166
        
167
        //Herramienta de CAD para crear una simetria de una geometria.
168
        SymmetryCadTool symmetryCADTool = new SymmetryCadTool();
169
        addCadTool("symmetry", symmetryCADTool);
170
        
171
//      Herramienta de CAD para explotar una geometria.
172
        ExploitCadTool exploitCADTool = new ExploitCadTool();
173
        addCadTool("exploit", exploitCADTool);
174
        
175
//      Herramienta de CAD para crear un spline geometria.
176
        SplineCadTool splainCADTool = new SplineCadTool();
177
        addCadTool("spline", splainCADTool);
178
        
179
        addMenuEntry("aceptar", "aceptar");
180
        addMenuEntry("cancelar", "cancelar");
181
        
182
        this.setLayout(new BorderLayout());
183
                this.setSize(300,200);
184
                this.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
185

    
186
        this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "elimina");
187
        this.getActionMap().put("elimina", new MyAction("eliminar"));
188
        this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
189
        this.getActionMap().put("escape", new MyAction("escape"));
190
        }
191
        
192
        class MyAction extends AbstractAction{
193
                private String actionCommand;
194
                
195
                public MyAction(String command){
196
                        actionCommand = command;
197
                }
198

    
199
                /**
200
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
201
                 */
202
                public void actionPerformed(ActionEvent e) {
203
                        cadToolAdapter.keyPressed(actionCommand);
204
                }
205
                
206
        }
207
        
208
        /**
209
         * This method initializes mapControl        
210
         *         
211
         * @return com.iver.cit.gvsig.fmap.MapControl        
212
         */    
213
        public MapControl getMapControl() {
214
                if (mapControl == null) {
215
                        mapControl = new MapControl();
216
                        mapControl.addMouseMotionListener(new MouseMotionListener(){
217

    
218
                                public void mouseDragged(MouseEvent arg0) {
219
                                }
220

    
221
                                public void mouseMoved(MouseEvent arg0) {
222
                                        //CadMapControl.this.setCursor(cadToolAdapter.getCursor());
223
                                        clearMouseImage();
224
                                }
225
                                
226
                        });
227
                        mapControl.addMouseListener(new java.awt.event.MouseAdapter() { 
228
                                public void mousePressed(java.awt.event.MouseEvent e) {
229
                                        show(e);
230
                                }
231
                                public void mouseReleased(java.awt.event.MouseEvent e) {
232
                                        show(e);
233
                                }
234
                                private void show(java.awt.event.MouseEvent e){
235
                                if (e.isPopupTrigger() && (mapControl.getCurrentMapTool() == cadToolAdapter)) {
236
                                    popup.show(e.getComponent(),
237
                                               e.getX(), e.getY());
238
                                }
239
                                }
240
                        });
241
                }
242
                return mapControl;
243
        }
244
        /**
245
         * This method initializes jConsole        
246
         *         
247
         * @return com.iver.utiles.console.JConsole        
248
         */    
249
        public JConsole getConsole() {
250
                if (jConsole == null) {
251
                        jConsole = new JConsole();
252
                        jConsole.setPreferredSize(new java.awt.Dimension(3,35));
253
                        jConsole.addFocusListener(new FocusListener() {
254
                                public void focusGained(FocusEvent e) {
255
                                }
256

    
257
                                public void focusLost(FocusEvent e) {
258
                                        jConsole.requestFocus();
259
                                        jConsole.requestFocusInWindow();
260
                                }
261
                        });
262
                        jConsole.requestFocus();
263
                        jConsole.requestFocusInWindow();
264
                }
265
                return jConsole;
266
        }
267
        /**
268
         * This method initializes jSplitPane        
269
         *         
270
         * @return javax.swing.JSplitPane        
271
         */    
272
        private JSplitPane getJSplitPane() {
273
                if (jSplitPane == null) {
274
                        jSplitPane = new JSplitPane();
275
                        jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
276
                        jSplitPane.setTopComponent(getMapControl());
277
                        jSplitPane.setBottomComponent(getConsole());
278
                        jSplitPane.setDividerLocation(130);
279
                        jSplitPane.setResizeWeight(0.85);
280
                }
281
                return jSplitPane;
282
        }
283
        public FMap getMapContext() {
284
                return mapControl.getMapContext();
285
        }
286
        
287
        public void addMapTool(String name, Behavior tool) {
288
                mapControl.addMapTool(name, tool);
289
        }
290
        
291
        public void addMapTool(String name, Behavior[] tools) {
292
                mapControl.addMapTool(name, tools);
293
        }
294
        public void setMapContext(FMap model) {
295
                mapControl.getMapContext().getLayers().removeLayerCollectionListener(layerListener);
296
                mapControl.setMapContext(model);
297
                mapControl.getMapContext().getLayers().addLayerCollectionListener(layerListener);
298
        }
299

    
300
        public void addCadTool(String name, CadTool c){
301
                namesCadTools.put(name, c);
302
        }
303
        
304
        public void setCadTool(String text){
305
                CadTool ct = (CadTool) namesCadTools.get(text);
306
                if (ct == null) throw new RuntimeException("No such cad tool");
307
                
308
                cadToolAdapter.setCadTool(ct);
309
                getMapControl().setTool("cadEdition");
310
                //clearMouseImage();
311
        }
312
        private void clearMouseImage(){
313
                int[] pixels = new int[16 * 16];
314
                Image image = Toolkit.getDefaultToolkit().createImage(
315
                        new MemoryImageSource(16, 16, pixels, 0, 16));
316
                Cursor transparentCursor =
317
                        Toolkit.getDefaultToolkit().createCustomCursor
318
                            (image, new Point(0,0), "invisiblecursor");
319
                this.setCursor(transparentCursor);
320
        }
321
        public void clearMenu(){
322
                popup.removeAll();
323
        }
324

    
325
        public void addMenuEntry(String text, String actionCommand){
326
                JMenuItem menu = new JMenuItem(text);
327
                menu.setActionCommand(actionCommand);
328
                menu.setEnabled(true);
329
                menu.setVisible(true);
330
                menu.addActionListener(new ActionListener() {
331
                        public void actionPerformed(ActionEvent e) {
332
                                cadToolAdapter.transition(e.getActionCommand());
333
                        }
334
                });
335
                
336
                popup.add(menu);
337
        }
338
        public CADToolAdapter getCadToolAdapter(){
339
                return cadToolAdapter;
340
        }
341
private class LayerColListener implements LayerCollectionListener{
342

    
343
        /**
344
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
345
         */
346
        public void layerAdded(LayerCollectionEvent e) {
347
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
348
                        ((EditableVectorialLayer) e.getAffectedLayer()).addEditionListener(layerEditionListener);
349
                }
350
        }
351

    
352
        /**
353
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
354
         */
355
        public void layerMoved(LayerPositionEvent e) {
356
        }
357

    
358
        /**
359
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
360
         */
361
        public void layerRemoved(LayerCollectionEvent e) {
362
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
363
                        ((EditableVectorialLayer) e.getAffectedLayer()).removeEditionListener(layerEditionListener);
364
                }
365
        }
366

    
367
        /**
368
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
369
         */
370
        public void layerAdding(LayerCollectionEvent e) throws CancelationException {
371
        }
372

    
373
        /**
374
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
375
         */
376
        public void layerMoving(LayerPositionEvent e) throws CancelationException {
377
        }
378

    
379
        /**
380
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
381
         */
382
        public void layerRemoving(LayerCollectionEvent e) throws CancelationException {
383
        }
384

    
385
        /**
386
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
387
         */
388
        public void activationChanged(LayerCollectionEvent e) throws CancelationException {
389
        }
390

    
391
        /**
392
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
393
         */
394
        public void visibilityChanged(LayerCollectionEvent e) throws CancelationException {
395
        }
396
        
397
}
398
        
399
private class LayerEditionListener implements EditionListener{
400

    
401
        /**
402
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
403
         */
404
        public void startEdition(EditionEvent e) {
405
                FLayer lyr = e.getSource();
406
                cadToolAdapter.setEditableFeatureSource(e.getEditingAdapter(), ((Selectable)lyr).getSelection());                
407
        }
408

    
409
        /**
410
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.EditionListener#stopEdition(com.iver.cit.gvsig.fmap.layers.EditionEvent)
411
         */
412
        public void stopEdition(EditionEvent layer) {
413
                CadMapControl.this.getMapControl().setTool("zoomIn");
414
        }
415
        
416
}
417

    
418
}