Statistics
| Revision:

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

History | View | Annotate | Download (6.8 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
         * Clear points in case the user exited editing
63
         * the previous time without ending multipoint
64
         */
65
        points.clear();
66
    }
67

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

    
82
    public void transition(double d) {
83
        _fsm.addValue(d);
84
    }
85

    
86
    public void transition(String s) throws CommandException {
87
        if (!super.changeCommand(s)) {
88
            _fsm.addOption(s);
89
        }
90
    }
91

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

    
108
        if (status.equals("MultiPoint.InsertPoint")) {
109
            points.add(new double[] { x, y });
110
            // addGeometry(ShapeFactory.createPoint2D(x, y));
111
        }
112
    }
113

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

    
139
            MultiPoint multiPoint =
140
                (MultiPoint) geomManager.create(TYPES.MULTIPOINT,
141
                    SUBTYPES.GEOM2D);
142
            for (int i = 0; i < pointsAux.length; i++) {
143
                multiPoint.addPoint(pointsAux[i]);
144
            }
145
            renderer.draw(multiPoint,
146
                mapControlManager.getGeometrySelectionSymbol());
147

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

    
157
    /**
158
     * Add a diferent option.
159
     * 
160
     * @param sel
161
     *            DOCUMENT ME!
162
     * @param s
163
     *            Diferent option.
164
     */
165
    public void addOption(String s) {
166
        // Nothing to do
167
    }
168

    
169
    public void addValue(double d) {
170
        // Nothing to do
171
    }
172

    
173
    public String getName() {
174
        return PluginServices.getText(this, "multipoint_");
175
    }
176

    
177
    public String toString() {
178
        return "_multipoint";
179
    }
180

    
181
    @Override
182
    protected int[] getSupportedGeometryTypes() {
183
        return new int[] { MULTIPOINT };
184
    }
185

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

    
198
            MultiPoint multiPoint = createMultiPoint();
199

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

    
211
    public void end() {
212
        points.clear();
213
        super.end();
214
    }
215
}