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 / MultiPointCADTool.java @ 40557

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

    
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

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

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

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

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

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

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

    
79
    public void transition(double d) {
80
        _fsm.addValue(d);
81
    }
82

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

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

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

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

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

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

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

    
166
    public void addValue(double d) {
167
        // Nothing to do
168
    }
169

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

    
174
    public String toString() {
175
        return "_multipoint";
176
    }
177

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

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

    
195
            MultiPoint multiPoint = createMultiPoint();
196

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

    
208
    public void end() {
209
        points.clear();
210
        super.end();
211
    }
212
}