Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / MultiPointCADTool.java @ 39296

History | View | Annotate | Download (6.65 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.SUBTYPES;
37
import org.gvsig.fmap.geom.Geometry.TYPES;
38
import org.gvsig.fmap.geom.aggregate.MultiPoint;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
41

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

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

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

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

    
63
    /**
64
     * DOCUMENT ME!
65
     * 
66
     * @param x
67
     *            DOCUMENT ME!
68
     * @param y
69
     *            DOCUMENT ME!
70
     * @param sel
71
     *            DOCUMENT ME!
72
     */
73
    public void transition(double x, double y, InputEvent event) {
74
        _fsm.addPoint(x, y, event);
75
    }
76

    
77
    public void transition(double d) {
78
        _fsm.addValue(d);
79
    }
80

    
81
    public void transition(String s) throws CommandException {
82
        if (!super.changeCommand(s)) {
83
            _fsm.addOption(s);
84
        }
85
    }
86

    
87
    /**
88
     * Equivale al transition del prototipo pero sin pasarle como par? metro el
89
     * editableFeatureSource que ya estar? creado.
90
     * 
91
     * @param sel
92
     *            Bitset con las geometr?as que est?n seleccionadas.
93
     * @param x
94
     *            par?metro x del punto que se pase en esta transici?n.
95
     * @param y
96
     *            par?metro y del punto que se pase en esta transici?n.
97
     */
98
    public void addPoint(double x, double y, InputEvent event) {
99
        MultiPointCADToolState actualState =
100
            (MultiPointCADToolState) _fsm.getPreviousState();
101
        String status = actualState.getName();
102

    
103
        if (status.equals("MultiPoint.InsertPoint")) {
104
            points.add(new double[] { x, y });
105
            // addGeometry(ShapeFactory.createPoint2D(x, y));
106
        }
107
    }
108

    
109
    /**
110
     * M?todo para dibujar la lo necesario para el estado en el que nos
111
     * encontremos.
112
     * 
113
     * @param g
114
     *            Graphics sobre el que dibujar.
115
     * @param selectedGeometries
116
     *            BitSet con las geometr?as seleccionadas.
117
     * @param x
118
     *            par?metro x del punto que se pase para dibujar.
119
     * @param y
120
     *            par?metro x del punto que se pase para dibujar.
121
     */
122
    public void drawOperation(MapControlDrawer renderer, double x, double y) {
123
        try {
124
            int num = points.size();
125
            Point[] pointsAux = new Point[num];
126
            for (int i = 0; i < num; i++) {
127
                double[] p = (double[]) points.get(i);
128
                pointsAux[i] =
129
                    (Point) geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
130
                pointsAux[i].setX(p[0]);
131
                pointsAux[i].setY(p[1]);
132
            }
133

    
134
            MultiPoint multiPoint =
135
                (MultiPoint) geomManager.create(TYPES.MULTIPOINT,
136
                    SUBTYPES.GEOM2D);
137
            for (int i = 0; i < pointsAux.length; i++) {
138
                multiPoint.addPoint(pointsAux[i]);
139
            }
140
            renderer.draw(multiPoint,
141
                mapControlManager.getGeometrySelectionSymbol());
142

    
143
            renderer.draw(createPoint(x, y),
144
                mapControlManager.getGeometrySelectionSymbol());
145
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
146
            LOG.error("Error drawing with renderer: " + renderer + ", x=" + x
147
                + ",y=" + y, new CreateGeometryException(TYPES.MULTIPOINT,
148
                SUBTYPES.GEOM2D, e));
149
        }
150
    }
151

    
152
    /**
153
     * Add a diferent option.
154
     * 
155
     * @param sel
156
     *            DOCUMENT ME!
157
     * @param s
158
     *            Diferent option.
159
     */
160
    public void addOption(String s) {
161
        // Nothing to do
162
    }
163

    
164
    public void addValue(double d) {
165
        // Nothing to do
166
    }
167

    
168
    public String getName() {
169
        return PluginServices.getText(this, "multipoint_");
170
    }
171

    
172
    public String toString() {
173
        return "_multipoint";
174
    }
175

    
176
    @Override
177
    protected int[] getSupportedGeometryTypes() {
178
        return new int[] { MULTIPOINT };
179
    }
180

    
181
    public void endGeometry() {
182
        try {
183
            int num = points.size();
184
            Point[] pointsAux = new Point[num];
185
            for (int i = 0; i < num; i++) {
186
                double[] p = (double[]) points.get(i);
187
                pointsAux[i] =
188
                    (Point) geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
189
                pointsAux[i].setX(p[0]);
190
                pointsAux[i].setY(p[1]);
191
            }
192

    
193
            MultiPoint multiPoint = createMultiPoint();
194

    
195
            for (int i = 0; i < pointsAux.length; i++) {
196
                multiPoint.addPoint(pointsAux[i]);
197
            }
198
            insertAndSelectGeometry(multiPoint);
199
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
200
            LOG.error("Error ending geometry", new CreateGeometryException(
201
                TYPES.MULTIPOINT, SUBTYPES.GEOM2D, e));
202
        }
203
        end();
204
    }
205

    
206
    public void end() {
207
        points.clear();
208
        super.end();
209
    }
210
}