Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / gui / cad / tools / SplineCADTool.java @ 40557

History | View | Annotate | Download (6.51 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing.gui.cad.tools;
25

    
26
import java.awt.event.InputEvent;
27
import java.awt.event.MouseEvent;
28
import java.awt.geom.Point2D;
29
import java.util.ArrayList;
30

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

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

    
49
    protected SplineCADToolContext _fsm;
50
    protected ArrayList list = new ArrayList();
51
    protected boolean close = false;
52

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

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

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

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

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

    
94
    public void transition(double d) {
95
        _fsm.addValue(d);
96
    }
97

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

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

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

    
159
            renderer.draw(spline, mapControlManager.getSelectionSymbol());
160
        }
161
    }
162

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

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

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

    
188
    public void end() {
189
        // Nothing to do
190
    }
191

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

    
196
    public String toString() {
197
        return "_Spline";
198
    }
199

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

    
204
    @Override
205
    protected int getSupportedPrimitiveGeometryType() {
206
        return SPLINE;
207
    }
208
}