Statistics
| Revision:

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

History | View | Annotate | Download (13.8 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.SymmetryCadTool;
96
import com.iver.cit.gvsig.gui.cad.tools.StretchingCadTool;
97
import com.iver.utiles.console.JConsole;
98
public class CadMapControl extends JPanel {
99
        private HashMap namesCadTools = new HashMap();
100
        private CADToolAdapter cadToolAdapter = new CADToolAdapter();
101
        private LayerEditionListener layerEditionListener = new LayerEditionListener();
102
        private LayerColListener layerListener = new LayerColListener();
103
        private MapControl mapControl = null;
104
        private JConsole jConsole = null;
105
        private JSplitPane jSplitPane = null;
106
        
107
        //Menu contextual
108
        private JPopupMenu popup = new JPopupMenu();
109

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

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

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

    
213
                                public void mouseDragged(MouseEvent arg0) {
214
                                }
215

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

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

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

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

    
338
        /**
339
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
340
         */
341
        public void layerAdded(LayerCollectionEvent e) {
342
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
343
                        ((EditableVectorialLayer) e.getAffectedLayer()).addEditionListener(layerEditionListener);
344
                }
345
        }
346

    
347
        /**
348
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
349
         */
350
        public void layerMoved(LayerPositionEvent e) {
351
        }
352

    
353
        /**
354
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
355
         */
356
        public void layerRemoved(LayerCollectionEvent e) {
357
                if (e.getAffectedLayer() instanceof EditableVectorialLayer){
358
                        ((EditableVectorialLayer) e.getAffectedLayer()).removeEditionListener(layerEditionListener);
359
                }
360
        }
361

    
362
        /**
363
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
364
         */
365
        public void layerAdding(LayerCollectionEvent e) throws CancelationException {
366
        }
367

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

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

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

    
386
        /**
387
         * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
388
         */
389
        public void visibilityChanged(LayerCollectionEvent e) throws CancelationException {
390
        }
391
        
392
}
393
        
394
private class LayerEditionListener implements EditionListener{
395

    
396
        /**
397
         * @see com.iver.cit.gvsig.fmap.layers.LayerListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
398
         */
399
        public void startEdition(EditionEvent e) {
400
                FLayer lyr = e.getSource();
401
                cadToolAdapter.setEditableFeatureSource(e.getEditingAdapter(), ((Selectable)lyr).getSelection());                
402
        }
403

    
404
        /**
405
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.EditionListener#stopEdition(com.iver.cit.gvsig.fmap.layers.EditionEvent)
406
         */
407
        public void stopEdition(EditionEvent layer) {
408
                CadMapControl.this.getMapControl().setTool("zoomIn");
409
        }
410
        
411
}
412

    
413
}