Statistics
| Revision:

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

History | View | Annotate | Download (11.7 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.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.awt.event.FocusEvent;
51
import java.awt.event.FocusListener;
52
import java.util.HashMap;
53

    
54
import javax.swing.JMenuItem;
55
import javax.swing.JPanel;
56
import javax.swing.JPopupMenu;
57
import javax.swing.JSplitPane;
58

    
59
import com.iver.cit.gvsig.fmap.FMap;
60
import com.iver.cit.gvsig.fmap.MapControl;
61
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
62
import com.iver.cit.gvsig.fmap.edition.EditionException;
63
import com.iver.cit.gvsig.fmap.layers.CancelationException;
64
import com.iver.cit.gvsig.fmap.layers.EditionEvent;
65
import com.iver.cit.gvsig.fmap.layers.FLayer;
66
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
67
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener;
68
import com.iver.cit.gvsig.fmap.layers.LayerEvent;
69
import com.iver.cit.gvsig.fmap.layers.LayerListener;
70
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
71
import com.iver.cit.gvsig.fmap.layers.layerOperations.EditableVectorialLayer;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.EditionListener;
73
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
74
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
75
import com.iver.cit.gvsig.gui.cad.tools.ArcCadTool;
76
import com.iver.cit.gvsig.gui.cad.tools.CircleCadTool;
77
import com.iver.cit.gvsig.gui.cad.tools.CopyCadTool;
78
import com.iver.cit.gvsig.gui.cad.tools.EllipseCadTool;
79
import com.iver.cit.gvsig.gui.cad.tools.LineCadTool;
80
import com.iver.cit.gvsig.gui.cad.tools.PointCadTool;
81
import com.iver.cit.gvsig.gui.cad.tools.PolyLineCadTool;
82
import com.iver.cit.gvsig.gui.cad.tools.PolygonCadTool;
83
import com.iver.cit.gvsig.gui.cad.tools.RectangleCadTool;
84
import com.iver.cit.gvsig.gui.cad.tools.RotateCadTool;
85
import com.iver.cit.gvsig.gui.cad.tools.ScaleCadTool;
86
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
87
import com.iver.cit.gvsig.gui.cad.tools.StretchingCadTool;
88
import com.iver.utiles.console.JConsole;
89
public class CadMapControl extends JPanel {
90
        private HashMap namesCadTools = new HashMap();
91
        private CADToolAdapter cadToolAdapter = new CADToolAdapter();
92
        private LayerEditionListener layerEditionListener = new LayerEditionListener();
93
        private LayerColListener layerListener = new LayerColListener();
94
        private MapControl mapControl = null;
95
        private JConsole jConsole = null;
96
        private JSplitPane jSplitPane = null;
97
        
98
        //Menu contextual
99
        private JPopupMenu popup = new JPopupMenu();
100

    
101
        /**
102
         * This is the default constructor
103
         */
104
        public CadMapControl() {
105
                super();
106
                initialize();
107
        }
108
        /**
109
         * This method initializes this
110
         * 
111
         * @return void
112
         */
113
        private  void initialize() {
114
        // Herramienta de edici?n de CAD
115
        getMapControl().addMapTool("cadEdition", cadToolAdapter);
116
        
117
        cadToolAdapter.setConsole(getConsole());
118
        cadToolAdapter.setCadMapControl(this);
119
        
120
        //Herramienta de CAD de a?adir punto
121
        PointCadTool pointCADTool = new PointCadTool();
122
        addCadTool("point", pointCADTool);
123
        //Herramienta de CAD de a?adir l?nea
124
        LineCadTool lineCADTool = new LineCadTool();
125
        addCadTool("line", lineCADTool);
126
        //Herramienta de CAD de a?adir c?rculo
127
        addCadTool("circle", new CircleCadTool());
128
        //Herramienta de CAD de a?adir c?rculo
129
        addCadTool("ellipse", new EllipseCadTool());
130
        //Herramienta de CAD de a?adir c?rculo
131
        addCadTool("arc", new ArcCadTool());
132
        //Herramienta de CAD de a?adir polyl?nea
133
        PolyLineCadTool polylineCADTool = new PolyLineCadTool();
134
        addCadTool("polyline", polylineCADTool);
135
        //Herramienta de CAD de a?adir rect?ngulo.
136
        RectangleCadTool rectangleCADTool = new RectangleCadTool();
137
        addCadTool("rectangle", rectangleCADTool);
138
        //Herramienta de CAD de a?adir pol?gono.
139
        PolygonCadTool polygonCADTool = new PolygonCadTool();
140
        addCadTool("polygon", polygonCADTool);
141
        //Herramienta de CAD para seleccionar geometr?as.
142
        SelectionCadTool selectionCADTool = new SelectionCadTool();
143
        addCadTool("selection", selectionCADTool);
144
        //Herramienta de CAD para copiar una geometria.
145
        CopyCadTool copyCADTool = new CopyCadTool();
146
        addCadTool("copy", copyCADTool);
147
        //Herramienta de CAD para girar una geometria.
148
        RotateCadTool rotateCADTool = new RotateCadTool();
149
        addCadTool("rotate", rotateCADTool);
150
        //Herramienta de CAD para escalar una geometria.
151
        ScaleCadTool scaleCADTool = new ScaleCadTool();
152
        addCadTool("scale", scaleCADTool);
153
        //Herramienta de CAD para extirar una geometria.
154
        StretchingCadTool stretchingCADTool = new StretchingCadTool();
155
        addCadTool("stretching", stretchingCADTool);
156
       
157
        addMenuEntry("aceptar", "aceptar");
158
        addMenuEntry("cancelar", "cancelar");
159
        
160
        this.setLayout(new BorderLayout());
161
                this.setSize(300,200);
162
                this.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
163
        }
164
        /**
165
         * This method initializes mapControl        
166
         *         
167
         * @return com.iver.cit.gvsig.fmap.MapControl        
168
         */    
169
        public MapControl getMapControl() {
170
                if (mapControl == null) {
171
                        mapControl = new MapControl();
172
                        mapControl.addMouseListener(new java.awt.event.MouseAdapter() { 
173
                                public void mousePressed(java.awt.event.MouseEvent e) {
174
                                        show(e);
175
                                }
176
                                public void mouseReleased(java.awt.event.MouseEvent e) {
177
                                        show(e);
178
                                }
179
                                private void show(java.awt.event.MouseEvent e){
180
                                if (e.isPopupTrigger() && (mapControl.getCurrentMapTool() == cadToolAdapter)) {
181
                                    popup.show(e.getComponent(),
182
                                               e.getX(), e.getY());
183
                                }
184
                                }
185
                        });
186
                }
187
                return mapControl;
188
        }
189
        /**
190
         * This method initializes jConsole        
191
         *         
192
         * @return com.iver.utiles.console.JConsole        
193
         */    
194
        public JConsole getConsole() {
195
                if (jConsole == null) {
196
                        jConsole = new JConsole();
197
                        jConsole.setPreferredSize(new java.awt.Dimension(3,35));
198
                        jConsole.addFocusListener(new FocusListener() {
199
                                public void focusGained(FocusEvent e) {
200
                                }
201

    
202
                                public void focusLost(FocusEvent e) {
203
                                        jConsole.requestFocus();
204
                                        jConsole.requestFocusInWindow();
205
                                }
206
                        });
207
                        jConsole.requestFocus();
208
                        jConsole.requestFocusInWindow();
209
                }
210
                return jConsole;
211
        }
212
        /**
213
         * This method initializes jSplitPane        
214
         *         
215
         * @return javax.swing.JSplitPane        
216
         */    
217
        private JSplitPane getJSplitPane() {
218
                if (jSplitPane == null) {
219
                        jSplitPane = new JSplitPane();
220
                        jSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
221
                        jSplitPane.setTopComponent(getMapControl());
222
                        jSplitPane.setBottomComponent(getConsole());
223
                        jSplitPane.setDividerLocation(130);
224
                }
225
                return jSplitPane;
226
        }
227
        public FMap getMapContext() {
228
                return mapControl.getMapContext();
229
        }
230
        
231
        public void addMapTool(String name, Behavior tool) {
232
                mapControl.addMapTool(name, tool);
233
        }
234
        
235
        public void addMapTool(String name, Behavior[] tools) {
236
                mapControl.addMapTool(name, tools);
237
        }
238
        public void setMapContext(FMap model) {
239
                mapControl.getMapContext().getLayers().removeLayerCollectionListener(layerListener);
240
                mapControl.setMapContext(model);
241
                mapControl.getMapContext().getLayers().addLayerCollectionListener(layerListener);
242
        }
243

    
244
        public void addCadTool(String name, CadTool c){
245
                namesCadTools.put(name, c);
246
        }
247
        
248
        public void setCadTool(String text){
249
                CadTool ct = (CadTool) namesCadTools.get(text);
250
                if (ct == null) throw new RuntimeException("No such cad tool");
251
                
252
                cadToolAdapter.setCadTool(ct);
253
                getMapControl().setTool("cadEdition");
254
        }
255
        
256
        public void clearMenu(){
257
                popup.removeAll();
258
        }
259

    
260
        public void addMenuEntry(String text, String actionCommand){
261
                JMenuItem menu = new JMenuItem(text);
262
                menu.setActionCommand(actionCommand);
263
                menu.setEnabled(true);
264
                menu.setVisible(true);
265
                menu.addActionListener(new ActionListener() {
266
                        public void actionPerformed(ActionEvent e) {
267
                                cadToolAdapter.transition(e.getActionCommand());
268
                        }
269
                });
270
                
271
                popup.add(menu);
272
        }
273
        
274
private class LayerColListener implements LayerCollectionListener{
275

    
276
        /**
277
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
278
         */
279
        public void layerAdded(LayerCollectionEvent e) {
280
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
281
                        ((EditableVectorialLayer) e.getAffectedLayer()).addEditionListener(layerEditionListener);
282
                }
283
        }
284

    
285
        /**
286
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
287
         */
288
        public void layerMoved(LayerPositionEvent e) {
289
        }
290

    
291
        /**
292
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
293
         */
294
        public void layerRemoved(LayerCollectionEvent e) {
295
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
296
                        ((EditableVectorialLayer) e.getAffectedLayer()).removeEditionListener(layerEditionListener);
297
                }
298
        }
299

    
300
        /**
301
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
302
         */
303
        public void layerAdding(LayerCollectionEvent e) throws CancelationException {
304
        }
305

    
306
        /**
307
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
308
         */
309
        public void layerMoving(LayerPositionEvent e) throws CancelationException {
310
        }
311

    
312
        /**
313
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
314
         */
315
        public void layerRemoving(LayerCollectionEvent e) throws CancelationException {
316
        }
317

    
318
        /**
319
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
320
         */
321
        public void activationChanged(LayerCollectionEvent e) throws CancelationException {
322
        }
323

    
324
        /**
325
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
326
         */
327
        public void visibilityChanged(LayerCollectionEvent e) throws CancelationException {
328
        }
329
        
330
}
331
        
332
private class LayerEditionListener implements EditionListener{
333

    
334
        /**
335
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
336
         */
337
        public void startEdition(EditionEvent e) {
338
                FLayer lyr = e.getSource();
339
                cadToolAdapter.setEditableFeatureSource(e.getEditingAdapter(), ((Selectable)lyr).getSelection());                
340
        }
341

    
342
        /**
343
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.EditionListener#stopEdition(com.iver.cit.gvsig.fmap.layers.EditionEvent)
344
         */
345
        public void stopEdition(EditionEvent layer) {
346
                CadMapControl.this.getMapControl().setTool("zoomIn");
347
        }
348
        
349
}
350

    
351
}