Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / SplineCADTool.java @ 35173

History | View | Annotate | Download (6.5 KB)

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

    
43
import java.awt.event.InputEvent;
44
import java.awt.event.MouseEvent;
45
import java.awt.geom.Point2D;
46
import java.util.ArrayList;
47

    
48
import org.gvsig.andami.PluginServices;
49
import org.gvsig.andami.messages.NotificationManager;
50
import org.gvsig.editing.gui.cad.DefaultCADTool;
51
import org.gvsig.editing.gui.cad.exception.CommandException;
52
import org.gvsig.editing.gui.cad.tools.smc.SplineCADToolContext;
53
import org.gvsig.editing.gui.cad.tools.smc.SplineCADToolContext.SplineCADToolState;
54
import org.gvsig.fmap.dal.exception.ReadException;
55
import org.gvsig.fmap.geom.Geometry;
56
import org.gvsig.fmap.mapcontext.ViewPort;
57
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
58
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
59

    
60

    
61

    
62
/**
63
 * CADTool Spline
64
 *
65
 * @author Vicente Caballero Navarro
66
 */
67
public class SplineCADTool extends DefaultCADTool {
68
    
69
        protected SplineCADToolContext _fsm;
70
        protected ArrayList list = new ArrayList();
71
        protected boolean close=false;
72

    
73
        /**
74
         * Crea un nuevo SplineCADTool.
75
         */
76
        public SplineCADTool() {
77

    
78
        }
79

    
80
        /**
81
         * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
82
         * carga previa a la utilizaci?n de la herramienta.
83
         */
84
        public void init() {
85
                _fsm = new SplineCADToolContext(this);
86
        }
87

    
88
        public void endGeometry() {
89
                try {
90
                    int geometryType = ((FLyrVect)getVLE().getLayer()).getShapeType();
91
                        if (((geometryType == Geometry.TYPES.SURFACE) || (geometryType == Geometry.TYPES.MULTISURFACE)) && !close){
92
                                closeGeometry();
93
                        }
94
                } catch (ReadException e) {
95
                        NotificationManager.addError(e.getMessage(),e);
96
                }
97

    
98
                // No queremos guardar FGeometryCollections:
99
                Geometry newGeom = createSpline((Point2D[])list.toArray(new Point2D[0]));
100
                insertAndSelectGeometry(newGeom);
101
                _fsm = new SplineCADToolContext(this);
102
                list.clear();
103
                clearTemporalCache();
104
                close=false;
105
        }
106
        public void closeGeometry(){
107
                close=true;
108
                Point2D endPoint=new Point2D.Double(((Point2D)list.get(0)).getX(),((Point2D)list.get(0)).getY());
109
                list.add(endPoint);
110
        }
111
        /* (non-Javadoc)
112
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
113
         */
114
        public void transition(double x, double y, InputEvent event) {
115
                _fsm.addPoint(x, y, event);
116
        }
117

    
118
        /* (non-Javadoc)
119
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
120
         */
121
        public void transition(double d) {
122
                _fsm.addValue(d);
123
        }
124

    
125
        /* (non-Javadoc)
126
         * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
127
         */
128
        public void transition(String s) throws CommandException {
129
                if (!super.changeCommand(s)){
130
                        _fsm.addOption(s);
131
                }
132
        }
133

    
134
        /**
135
         * Equivale al transition del prototipo pero sin pasarle como par?metro el
136
         * editableFeatureSource que ya estar? creado.
137
         *
138
         * @param sel Bitset con las geometr?as que est?n seleccionadas.
139
         * @param x par?metro x del punto que se pase en esta transici?n.
140
         * @param y par?metro y del punto que se pase en esta transici?n.
141
         */
142
        public void addPoint(double x, double y,InputEvent event) {
143
                SplineCADToolState actualState = (SplineCADToolState) _fsm.getPreviousState();
144
                String status = actualState.getName();
145
                if (status.equals("Spline.NextPoint") || status.equals("Spline.FirstPoint")) {
146
                        list.add(new Point2D.Double(x,y));
147
                        Geometry spline=createSpline((Point2D[])list.toArray(new Point2D[0]));
148
                        addTemporalCache(spline);
149
                }
150
        }
151

    
152
        /**
153
         * M?todo para dibujar lo necesario para el estado en el que nos
154
         * encontremos.
155
         *
156
         * @param g Graphics sobre el que dibujar.
157
         * @param selectedGeometries BitSet con las geometr?as seleccionadas.
158
         * @param x par?metro x del punto que se pase para dibujar.
159
         * @param y par?metro x del punto que se pase para dibujar.
160
         */
161
        public void drawOperation(MapControlDrawer renderer, double x,
162
                        double y) {
163
                SplineCADToolState actualState = _fsm.getState();
164
                String status = actualState.getName();
165
                if (status.equals("Spline.NextPoint") || status.equals("Spline.FirstPoint")) {
166
                        //                ArrayList points=new ArrayList();
167
                        Point2D[] points =new Point2D[list.size()+1];
168
                        Point2D[] auxPoints=(Point2D[])list.toArray(new Point2D[0]);
169
                        for (int i = 0; i < auxPoints.length; i++) {
170
                                points[i]=(Point2D)auxPoints[i].clone();
171
                        }
172
                        points[points.length-1]=new Point2D.Double(x,y);
173
                        Geometry spline=createSpline(points);
174
                        ViewPort vp=getCadToolAdapter().getMapControl().getViewPort();
175

    
176
                        renderer.draw(spline, mapControlManager.getSelectionSymbol());
177
                }                
178
        }        
179

    
180
        /**
181
         * Add a diferent option.
182
         *
183
         * @param sel DOCUMENT ME!
184
         * @param s Diferent option.
185
         */
186
        public void addOption(String s) {
187
        }
188

    
189
        /* (non-Javadoc)
190
         * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
191
         */
192
        public void addValue(double d) {
193
        }
194

    
195
        public void cancel(){
196
                list.clear();
197
                clearTemporalCache();
198
                close=false;
199
        }
200

    
201
        public void end() {
202
                /* CADExtension.setCADTool("Spline");
203
        PluginServices.getMainFrame().setSelectedTool("Spline"); */
204
        }
205

    
206
        public String getName() {
207
                return PluginServices.getText(this,"Spline_");
208
        }
209

    
210
        public String toString() {
211
                return "_Spline";
212
        }
213
        public boolean isApplicable(int shapeType) {
214
                switch (shapeType) {
215
                case Geometry.TYPES.POINT:
216
                case Geometry.TYPES.MULTIPOINT:
217
                        return false;
218
                }
219
                return true;
220
        }
221
        public void endTransition(double x, double y, MouseEvent event) {
222
                _fsm.endPoint(x, y, event);
223
        }
224
}