Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2060 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / SplineCADTool.java @ 39348

History | View | Annotate | Download (6.46 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22
package org.gvsig.editing.gui.cad.tools;
23

    
24
import java.awt.event.InputEvent;
25
import java.awt.event.MouseEvent;
26
import java.awt.geom.Point2D;
27
import java.util.ArrayList;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.messages.NotificationManager;
31
import org.gvsig.editing.gui.cad.exception.CommandException;
32
import org.gvsig.editing.gui.cad.tools.smc.SplineCADToolContext;
33
import org.gvsig.editing.gui.cad.tools.smc.SplineCADToolContext.SplineCADToolState;
34
import org.gvsig.fmap.dal.exception.ReadException;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.mapcontext.ViewPort;
37
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
38
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
39

    
40
/**
41
 * CADTool Spline
42
 * 
43
 * @author Vicente Caballero Navarro
44
 */
45
public class SplineCADTool extends AbstractCurveCADTool {
46

    
47
    protected SplineCADToolContext _fsm;
48
    protected ArrayList list = new ArrayList();
49
    protected boolean close = false;
50

    
51
    /**
52
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
53
     * carga previa a la utilizaci?n de la herramienta.
54
     */
55
    public void init() {
56
        _fsm = new SplineCADToolContext(this);
57
    }
58

    
59
    public void endGeometry() {
60
        try {
61
            int geometryType = ((FLyrVect) getVLE().getLayer()).getShapeType();
62
            if (((geometryType == Geometry.TYPES.SURFACE) || (geometryType == Geometry.TYPES.MULTISURFACE))
63
                && !close) {
64
                closeGeometry();
65
            }
66
        } catch (ReadException e) {
67
            NotificationManager.addError(e.getMessage(), e);
68
        }
69

    
70
        // No queremos guardar FGeometryCollections:
71
        Geometry newGeom =
72
            createSpline((Point2D[]) list.toArray(new Point2D[0]));
73
        insertAndSelectGeometry(newGeom);
74
        _fsm = new SplineCADToolContext(this);
75
        list.clear();
76
        clearTemporalCache();
77
        close = false;
78
    }
79

    
80
    public void closeGeometry() {
81
        close = true;
82
        Point2D endPoint =
83
            new Point2D.Double(((Point2D) list.get(0)).getX(),
84
                ((Point2D) list.get(0)).getY());
85
        list.add(endPoint);
86
    }
87

    
88
    public void transition(double x, double y, InputEvent event) {
89
        _fsm.addPoint(x, y, event);
90
    }
91

    
92
    public void transition(double d) {
93
        _fsm.addValue(d);
94
    }
95

    
96
    public void transition(String s) throws CommandException {
97
        if (!super.changeCommand(s)) {
98
            _fsm.addOption(s);
99
        }
100
    }
101

    
102
    /**
103
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
104
     * editableFeatureSource que ya estar? creado.
105
     * 
106
     * @param sel
107
     *            Bitset con las geometr?as que est?n seleccionadas.
108
     * @param x
109
     *            par?metro x del punto que se pase en esta transici?n.
110
     * @param y
111
     *            par?metro y del punto que se pase en esta transici?n.
112
     */
113
    public void addPoint(double x, double y, InputEvent event) {
114
        SplineCADToolState actualState =
115
            (SplineCADToolState) _fsm.getPreviousState();
116
        String status = actualState.getName();
117
        if (status.equals("Spline.NextPoint")
118
            || status.equals("Spline.FirstPoint")) {
119
            list.add(new Point2D.Double(x, y));
120
            Geometry spline =
121
                createSpline((Point2D[]) list.toArray(new Point2D[0]));
122
            
123
            if (spline != null) {
124
                addTemporalCache(spline);
125
            }
126
        }
127
    }
128

    
129
    /**
130
     * M?todo para dibujar lo necesario para el estado en el que nos
131
     * encontremos.
132
     * 
133
     * @param g
134
     *            Graphics sobre el que dibujar.
135
     * @param selectedGeometries
136
     *            BitSet con las geometr?as seleccionadas.
137
     * @param x
138
     *            par?metro x del punto que se pase para dibujar.
139
     * @param y
140
     *            par?metro x del punto que se pase para dibujar.
141
     */
142
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
143
        SplineCADToolState actualState = _fsm.getState();
144
        String status = actualState.getName();
145
        if (status.equals("Spline.NextPoint")
146
            || status.equals("Spline.FirstPoint")) {
147
            // ArrayList points=new ArrayList();
148
            Point2D[] points = new Point2D[list.size() + 1];
149
            Point2D[] auxPoints = (Point2D[]) list.toArray(new Point2D[0]);
150
            for (int i = 0; i < auxPoints.length; i++) {
151
                points[i] = (Point2D) auxPoints[i].clone();
152
            }
153
            points[points.length - 1] = new Point2D.Double(x, y);
154
            Geometry spline = createSpline(points);
155
            ViewPort vp = getCadToolAdapter().getMapControl().getViewPort();
156

    
157
            renderer.draw(spline, mapControlManager.getSelectionSymbol());
158
        }
159
    }
160

    
161
    /**
162
     * Add a diferent option.
163
     * 
164
     * @param sel
165
     *            DOCUMENT ME!
166
     * @param s
167
     *            Diferent option.
168
     */
169
    public void addOption(String s) {
170
    }
171

    
172
    /*
173
     * (non-Javadoc)
174
     * 
175
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
176
     */
177
    public void addValue(double d) {
178
    }
179

    
180
    public void cancel() {
181
        list.clear();
182
        clearTemporalCache();
183
        close = false;
184
    }
185

    
186
    public void end() {
187
        // Nothing to do
188
    }
189

    
190
    public String getName() {
191
        return PluginServices.getText(this, "Spline_");
192
    }
193

    
194
    public String toString() {
195
        return "_Spline";
196
    }
197

    
198
    public void endTransition(double x, double y, MouseEvent event) {
199
        _fsm.endPoint(x, y, event);
200
    }
201

    
202
    @Override
203
    protected int getSupportedPrimitiveGeometryType() {
204
        return SPLINE;
205
    }
206
}