Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / MultiPointCADTool.java @ 37138

History | View | Annotate | Download (7.77 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.util.ArrayList;
26

    
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.editing.gui.cad.DefaultCADTool;
32
import org.gvsig.editing.gui.cad.exception.CommandException;
33
import org.gvsig.editing.gui.cad.tools.smc.MultiPointCADToolContext;
34
import org.gvsig.editing.gui.cad.tools.smc.MultiPointCADToolContext.MultiPointCADToolState;
35
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
36
import org.gvsig.fmap.geom.Geometry;
37
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
38
import org.gvsig.fmap.geom.Geometry.TYPES;
39
import org.gvsig.fmap.geom.aggregate.MultiPoint;
40
import org.gvsig.fmap.geom.primitive.Point;
41
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
42

    
43
/**
44
 * DOCUMENT ME!
45
 * 
46
 * @author Vicente Caballero Navarro
47
 */
48
public class MultiPointCADTool extends DefaultCADTool {
49

    
50
    private static final Logger LOG = LoggerFactory
51
        .getLogger(MultiPointCADTool.class);
52

    
53
    protected MultiPointCADToolContext _fsm;
54
    protected ArrayList points = new ArrayList();
55

    
56
    /**
57
     * Crea un nuevo PointCADTool.
58
     */
59
    public MultiPointCADTool() {
60

    
61
    }
62

    
63
    /**
64
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
65
     * carga previa a la utilizaci?n de la herramienta.
66
     */
67
    public void init() {
68
        _fsm = new MultiPointCADToolContext(this);
69
    }
70

    
71
    /**
72
     * DOCUMENT ME!
73
     * 
74
     * @param x
75
     *            DOCUMENT ME!
76
     * @param y
77
     *            DOCUMENT ME!
78
     * @param sel
79
     *            DOCUMENT ME!
80
     */
81
    public void transition(double x, double y, InputEvent event) {
82
        _fsm.addPoint(x, y, event);
83
    }
84

    
85
    /*
86
     * (non-Javadoc)
87
     * 
88
     * @see
89
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
90
     * .layers.FBitSet, double)
91
     */
92
    public void transition(double d) {
93
        _fsm.addValue(d);
94
    }
95

    
96
    /*
97
     * (non-Javadoc)
98
     * 
99
     * @see
100
     * com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
101
     * .layers.FBitSet, java.lang.String)
102
     */
103
    public void transition(String s) throws CommandException {
104
        if (!super.changeCommand(s)) {
105
            _fsm.addOption(s);
106
        }
107
    }
108

    
109
    /**
110
     * Equivale al transition del prototipo pero sin pasarle como par? metro el
111
     * editableFeatureSource que ya estar? creado.
112
     * 
113
     * @param sel
114
     *            Bitset con las geometr?as que est?n seleccionadas.
115
     * @param x
116
     *            par?metro x del punto que se pase en esta transici?n.
117
     * @param y
118
     *            par?metro y del punto que se pase en esta transici?n.
119
     */
120
    public void addPoint(double x, double y, InputEvent event) {
121
        MultiPointCADToolState actualState =
122
            (MultiPointCADToolState) _fsm.getPreviousState();
123
        String status = actualState.getName();
124

    
125
        if (status.equals("MultiPoint.InsertPoint")) {
126
            points.add(new double[] { x, y });
127
            // addGeometry(ShapeFactory.createPoint2D(x, y));
128
        }
129
    }
130

    
131
    /**
132
     * M?todo para dibujar la 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
        try {
146
            int num = points.size();
147
            Point[] pointsAux = new Point[num];
148
            for (int i = 0; i < num; i++) {
149
                double[] p = (double[]) points.get(i);
150
                pointsAux[i] =
151
                    (Point) geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
152
                pointsAux[i].setX(p[0]);
153
                pointsAux[i].setY(p[1]);
154
            }
155

    
156
            MultiPoint multiPoint =
157
                (MultiPoint) geomManager.create(TYPES.MULTIPOINT,
158
                    SUBTYPES.GEOM2D);
159
            for (int i = 0; i < pointsAux.length; i++) {
160
                multiPoint.addPoint(pointsAux[i]);
161
            }
162
            renderer.draw(multiPoint,
163
                mapControlManager.getGeometrySelectionSymbol());
164

    
165
            renderer.draw(createPoint(x, y),
166
                mapControlManager.getGeometrySelectionSymbol());
167
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
168
            LOG.error("Error drawing with renderer: " + renderer + ", x=" + x
169
                + ",y=" + y, new CreateGeometryException(TYPES.MULTIPOINT,
170
                SUBTYPES.GEOM2D, e));
171
        }
172

    
173
        // geomFactory.createMultipoint2D(xs,ys).draw((Graphics2D) g,
174
        // getCadToolAdapter().getMapControl().getViewPort(),
175
        // DefaultCADTool.geometrySelectSymbol);
176
        // geomFactory.createPoint2D(x,y).draw((Graphics2D) g,
177
        // getCadToolAdapter().getMapControl().getViewPort(),
178
        // DefaultCADTool.geometrySelectSymbol);
179
    }
180

    
181
    /**
182
     * Add a diferent option.
183
     * 
184
     * @param sel
185
     *            DOCUMENT ME!
186
     * @param s
187
     *            Diferent option.
188
     */
189
    public void addOption(String s) {
190
        // TODO Auto-generated method stub
191
    }
192

    
193
    /*
194
     * (non-Javadoc)
195
     * 
196
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
197
     */
198
    public void addValue(double d) {
199
        // TODO Auto-generated method stub
200
    }
201

    
202
    public String getName() {
203
        return PluginServices.getText(this, "multipoint_");
204
    }
205

    
206
    public String toString() {
207
        return "_multipoint";
208
    }
209

    
210
    public boolean isApplicable(int shapeType) {
211
        switch (shapeType) {
212
        case Geometry.TYPES.MULTIPOINT:
213
            return true;
214
        }
215
        return false;
216
    }
217

    
218
    public void endGeometry() {
219
        try {
220
            int num = points.size();
221
            Point[] pointsAux = new Point[num];
222
            for (int i = 0; i < num; i++) {
223
                double[] p = (double[]) points.get(i);
224
                pointsAux[i] =
225
                    (Point) geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
226
                pointsAux[i].setX(p[0]);
227
                pointsAux[i].setY(p[1]);
228
            }
229

    
230
            MultiPoint multiPoint;
231

    
232
            multiPoint =
233
                (MultiPoint) geomManager.create(TYPES.MULTIPOINT,
234
                    SUBTYPES.GEOM2D);
235
            for (int i = 0; i < pointsAux.length; i++) {
236
                multiPoint.addPoint(pointsAux[i]);
237
            }
238
            insertAndSelectGeometry(multiPoint);
239
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
240
            LOG.error("Error ending geometry", new CreateGeometryException(
241
                TYPES.MULTIPOINT, SUBTYPES.GEOM2D, e));
242
        }
243
        end();
244
    }
245

    
246
    public void end() {
247
        points.clear();
248
        super.end();
249
    }
250
}