Statistics
| Revision:

svn-gvsig-desktop / branches / pilotoDWG / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / CadMapControl.java @ 1573

History | View | Annotate | Download (12.9 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.edition.EditableFeatureSourceProxy;
72
import com.iver.cit.gvsig.fmap.layers.CancelationException;
73
import com.iver.cit.gvsig.fmap.layers.EditionEvent;
74
import com.iver.cit.gvsig.fmap.layers.FLayer;
75
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
76
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener;
77
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
78
import com.iver.cit.gvsig.fmap.layers.layerOperations.EditableVectorialLayer;
79
import com.iver.cit.gvsig.fmap.layers.layerOperations.EditionListener;
80
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
81
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
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.LineCadTool;
87
import com.iver.cit.gvsig.gui.cad.tools.PointCadTool;
88
import com.iver.cit.gvsig.gui.cad.tools.PolyLineCadTool;
89
import com.iver.cit.gvsig.gui.cad.tools.PolygonCadTool;
90
import com.iver.cit.gvsig.gui.cad.tools.RectangleCadTool;
91
import com.iver.cit.gvsig.gui.cad.tools.RotateCadTool;
92
import com.iver.cit.gvsig.gui.cad.tools.ScaleCadTool;
93
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
94
import com.iver.cit.gvsig.gui.cad.tools.StretchingCadTool;
95
import com.iver.utiles.console.JConsole;
96
public class CadMapControl extends JPanel {
97
        private HashMap namesCadTools = new HashMap();
98
        private CADToolAdapter cadToolAdapter = new CADToolAdapter();
99
        private LayerEditionListener layerEditionListener = new LayerEditionListener();
100
        private LayerColListener layerListener = new LayerColListener();
101
        private MapControl mapControl = null;
102
        private JConsole jConsole = null;
103
        private JSplitPane jSplitPane = null;
104
        
105
        //Menu contextual
106
        private JPopupMenu popup = new JPopupMenu();
107

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

    
171
        this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "elimina");
172
        this.getActionMap().put("elimina", new AbstractAction() {
173
                        public void actionPerformed(ActionEvent e) {
174
                                cadToolAdapter.delete();
175
                        }
176
                });
177
        }
178
        /**
179
         * This method initializes mapControl        
180
         *         
181
         * @return com.iver.cit.gvsig.fmap.MapControl        
182
         */    
183
        public MapControl getMapControl() {
184
                if (mapControl == null) {
185
                        mapControl = new MapControl();
186
                        mapControl.addMouseMotionListener(new MouseMotionListener(){
187

    
188
                                public void mouseDragged(MouseEvent arg0) {
189
                                }
190

    
191
                                public void mouseMoved(MouseEvent arg0) {
192
                                        //CadMapControl.this.setCursor(cadToolAdapter.getCursor());
193
                                        clearMouseImage();
194
                                }
195
                                
196
                        });
197
                        mapControl.addMouseListener(new java.awt.event.MouseAdapter() { 
198
                                public void mousePressed(java.awt.event.MouseEvent e) {
199
                                        show(e);
200
                                }
201
                                public void mouseReleased(java.awt.event.MouseEvent e) {
202
                                        show(e);
203
                                }
204
                                private void show(java.awt.event.MouseEvent e){
205
                                if (e.isPopupTrigger() && (mapControl.getCurrentMapTool() == cadToolAdapter)) {
206
                                    popup.show(e.getComponent(),
207
                                               e.getX(), e.getY());
208
                                }
209
                                }
210
                        });
211
                }
212
                return mapControl;
213
        }
214
        /**
215
         * This method initializes jConsole        
216
         *         
217
         * @return com.iver.utiles.console.JConsole        
218
         */    
219
        public JConsole getConsole() {
220
                if (jConsole == null) {
221
                        jConsole = new JConsole();
222
                        jConsole.setPreferredSize(new java.awt.Dimension(3,35));
223
                        jConsole.addFocusListener(new FocusListener() {
224
                                public void focusGained(FocusEvent e) {
225
                                }
226

    
227
                                public void focusLost(FocusEvent e) {
228
                                        jConsole.requestFocus();
229
                                        jConsole.requestFocusInWindow();
230
                                }
231
                        });
232
                        jConsole.requestFocus();
233
                        jConsole.requestFocusInWindow();
234
                }
235
                return jConsole;
236
        }
237
        /**
238
         * This method initializes jSplitPane        
239
         *         
240
         * @return javax.swing.JSplitPane        
241
         */    
242
        private JSplitPane getJSplitPane() {
243
                if (jSplitPane == null) {
244
                        jSplitPane = new JSplitPane();
245
                        jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
246
                        jSplitPane.setTopComponent(getMapControl());
247
                        jSplitPane.setBottomComponent(getConsole());
248
                        jSplitPane.setDividerLocation(130);
249
                        jSplitPane.setResizeWeight(0.85);
250
                }
251
                return jSplitPane;
252
        }
253
        public FMap getMapContext() {
254
                return mapControl.getMapContext();
255
        }
256
        
257
        public void addMapTool(String name, Behavior tool) {
258
                mapControl.addMapTool(name, tool);
259
        }
260
        
261
        public void addMapTool(String name, Behavior[] tools) {
262
                mapControl.addMapTool(name, tools);
263
        }
264
        public void setMapContext(FMap model) {
265
                mapControl.getMapContext().getLayers().removeLayerCollectionListener(layerListener);
266
                mapControl.setMapContext(model);
267
                mapControl.getMapContext().getLayers().addLayerCollectionListener(layerListener);
268
        }
269

    
270
        public void addCadTool(String name, CadTool c){
271
                namesCadTools.put(name, c);
272
        }
273
        
274
        public void setCadTool(String text){
275
                CadTool ct = (CadTool) namesCadTools.get(text);
276
                if (ct == null) throw new RuntimeException("No such cad tool");
277
                
278
                cadToolAdapter.setCadTool(ct);
279
                getMapControl().setTool("cadEdition");
280
                //clearMouseImage();
281
        }
282
        private void clearMouseImage(){
283
                int[] pixels = new int[16 * 16];
284
                Image image = Toolkit.getDefaultToolkit().createImage(
285
                        new MemoryImageSource(16, 16, pixels, 0, 16));
286
                Cursor transparentCursor =
287
                        Toolkit.getDefaultToolkit().createCustomCursor
288
                            (image, new Point(0,0), "invisiblecursor");
289
                this.setCursor(transparentCursor);
290
        }
291
        public void clearMenu(){
292
                popup.removeAll();
293
        }
294

    
295
        public void addMenuEntry(String text, String actionCommand){
296
                JMenuItem menu = new JMenuItem(text);
297
                menu.setActionCommand(actionCommand);
298
                menu.setEnabled(true);
299
                menu.setVisible(true);
300
                menu.addActionListener(new ActionListener() {
301
                        public void actionPerformed(ActionEvent e) {
302
                                cadToolAdapter.transition(e.getActionCommand());
303
                        }
304
                });
305
                
306
                popup.add(menu);
307
        }
308
        public CADToolAdapter getCadToolAdapter(){
309
                return cadToolAdapter;
310
        }
311
private class LayerColListener implements LayerCollectionListener{
312

    
313
        /**
314
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
315
         */
316
        public void layerAdded(LayerCollectionEvent e) {
317
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
318
                        ((EditableVectorialLayer) e.getAffectedLayer()).addEditionListener(layerEditionListener);
319
                }
320
        }
321

    
322
        /**
323
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
324
         */
325
        public void layerMoved(LayerPositionEvent e) {
326
        }
327

    
328
        /**
329
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
330
         */
331
        public void layerRemoved(LayerCollectionEvent e) {
332
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
333
                        ((EditableVectorialLayer) e.getAffectedLayer()).removeEditionListener(layerEditionListener);
334
                }
335
        }
336

    
337
        /**
338
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
339
         */
340
        public void layerAdding(LayerCollectionEvent e) throws CancelationException {
341
        }
342

    
343
        /**
344
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
345
         */
346
        public void layerMoving(LayerPositionEvent e) throws CancelationException {
347
        }
348

    
349
        /**
350
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
351
         */
352
        public void layerRemoving(LayerCollectionEvent e) throws CancelationException {
353
        }
354

    
355
        /**
356
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
357
         */
358
        public void activationChanged(LayerCollectionEvent e) throws CancelationException {
359
        }
360

    
361
        /**
362
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
363
         */
364
        public void visibilityChanged(LayerCollectionEvent e) throws CancelationException {
365
        }
366
        
367
}
368
        
369
private class LayerEditionListener implements EditionListener{
370

    
371
        /**
372
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
373
         */
374
        public void startEdition(EditionEvent e) {
375
                FLayer lyr = e.getSource();
376
                cadToolAdapter.setEditableFeatureSource(e.getEditingAdapter(), ((Selectable)lyr).getSelection());                
377
        }
378

    
379
        /**
380
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.EditionListener#stopEdition(com.iver.cit.gvsig.fmap.layers.EditionEvent)
381
         */
382
        public void stopEdition(EditionEvent layer) {
383
                CadMapControl.this.getMapControl().setTool("zoomIn");
384
        }
385
        
386
}
387

    
388
}