Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.spline / src / main / java / org / gvsig / vectorediting / lib / prov / spline / SplineEditingProvider.java @ 333

History | View | Annotate | Download (9.76 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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 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
 * 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

    
25
package org.gvsig.vectorediting.lib.prov.spline;
26

    
27
import java.util.ArrayList;
28
import java.util.Iterator;
29
import java.util.LinkedHashMap;
30
import java.util.List;
31
import java.util.Map;
32

    
33
import org.gvsig.fmap.dal.feature.FeatureStore;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
37
import org.gvsig.fmap.geom.aggregate.MultiCurve;
38
import org.gvsig.fmap.geom.primitive.Point;
39
import org.gvsig.fmap.geom.primitive.Spline;
40
import org.gvsig.fmap.geom.type.GeometryType;
41
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
42
import org.gvsig.tools.ToolsLocator;
43
import org.gvsig.tools.dynobject.DynObject;
44
import org.gvsig.tools.exception.BaseException;
45
import org.gvsig.tools.i18n.I18nManager;
46
import org.gvsig.tools.service.spi.ProviderServices;
47
import org.gvsig.vectorediting.lib.api.DrawingStatus;
48
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
49
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
50
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
51
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
52
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
53
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
54
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
55
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus;
56
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
57
import org.gvsig.vectorediting.lib.spi.EditingProvider;
58
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
59
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator;
60
import org.gvsig.vectorediting.lib.spi.EditingProviderManager;
61
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
62

    
63
/**
64
 * @author llmarques
65
 *
66
 */
67
public class SplineEditingProvider extends AbstractEditingProvider implements
68
    EditingProvider {
69

    
70
    protected EditingServiceParameter points;
71

    
72
    protected List<Point> values;
73

    
74
    protected Map<String, String> options;
75

    
76
    protected boolean closeSpline;
77

    
78
    protected boolean finishSpline;
79

    
80
    protected FeatureStore featureStore;
81

    
82
    /**
83
     * Default constructor.
84
     *
85
     * @param parameters
86
     *            of service.
87
     * @param providerServices
88
     *            available services. See {@link EditingProviderServices}.
89
     */
90
    public SplineEditingProvider(ProviderServices services, DynObject parameters) {
91
        super(services);
92

    
93
        this.featureStore =
94
            (FeatureStore) parameters
95
                .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
96

    
97
        I18nManager i18nManager = ToolsLocator.getI18nManager();
98
        options = new LinkedHashMap<String, String>();
99
        options.put(i18nManager.getTranslation("key_close"), "close_spline");
100
        options.put(i18nManager.getTranslation("key_finish"), "finish");
101

    
102
        EditingProviderServices editingProviderServices =
103
            (EditingProviderServices) getProviderServices();
104

    
105
        String consoleMsg =
106
            editingProviderServices.makeConsoleMessage("indicate_new_point",
107
                options);
108

    
109
        this.points =
110
            new DefaultEditingServiceParameter("insert_point", consoleMsg,
111
                options, EditingServiceParameter.TYPE.LIST_POSITIONS,
112
                EditingServiceParameter.TYPE.OPTION);
113

    
114
        this.closeSpline = false;
115

    
116
        this.finishSpline = false;
117
    }
118

    
119
    public EditingServiceParameter next() {
120
        if (finishSpline) {
121
            return null;
122
        }
123
        return points;
124
    }
125

    
126
    public DrawingStatus getDrawingStatus(Point mousePosition)
127
        throws DrawServiceException {
128

    
129
        DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
130
        EditingProviderManager editingProviderManager =
131
            EditingProviderLocator.getProviderManager();
132
        ISymbol lineSymbolEditing = editingProviderManager.getSymbol("line-symbol-editing");
133
        ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
134

    
135

    
136
        if (values != null) {
137

    
138
            if (values.size() >= 1) {
139
                for (Iterator iterator = values.iterator(); iterator.hasNext();) {
140
                    Point point = (Point) iterator.next();
141
                    drawingStatus.addStatus(point, auxiliaryPointSymbolEditing, "");
142
                }
143

    
144
                EditingProviderServices editingProviderServices =
145
                    (EditingProviderServices) getProviderServices();
146

    
147
                try {
148
                    int subtype =
149
                        editingProviderServices.getSubType(featureStore);
150
                    Spline spline =
151
                        editingProviderServices.createSpline(values, subtype);
152

    
153
                    spline.addVertex(mousePosition);
154
                    drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
155
                    drawingStatus.addStatus(spline, lineSymbolEditing, "");
156

    
157
//                    drawingStatus.addGeometry(spline);
158
                } catch (BaseException e) {
159
                    throw new DrawServiceException(e);
160
                }
161
            }
162
        }
163
        return drawingStatus;
164
    }
165

    
166
    public void stop() throws StopServiceException {
167
        values.clear();
168
    }
169

    
170
    public List<EditingServiceParameter> getParameters() {
171
        List<EditingServiceParameter> parameters =
172
            new ArrayList<EditingServiceParameter>();
173
        parameters.add(points);
174
        return parameters;
175
    }
176

    
177
    public void setValue(Object value) throws InvalidEntryException {
178
        EditingServiceParameter parameter = next();
179
        validateAndInsertValue(parameter, value);
180
    }
181

    
182
    /**
183
     * Checks if value is valid to parameter received as parameter.
184
     *
185
     * @param param
186
     *            of value
187
     * @param value
188
     *            to e inserted
189
     * @throws InvalidEntryException
190
     *             If values is no valid throw an InvalidEntryException
191
     */
192
    private void validateAndInsertValue(EditingServiceParameter parameter,
193
        Object value) throws InvalidEntryException {
194

    
195
        if (parameter == points) {
196

    
197
            if (value instanceof Point) {
198
                values.add((Point) value);
199
                return;
200

    
201
            } else if (value instanceof String) {
202

    
203
                if (values.size() > 1) {
204

    
205
                    String option = (String) value;
206
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
207

    
208
                    if (option.equalsIgnoreCase(i18nManager
209
                        .getTranslation("key_close"))
210
                        || option.equalsIgnoreCase(i18nManager
211
                            .getTranslation("key_finish"))) {
212

    
213
                        if (option.equalsIgnoreCase(i18nManager
214
                            .getTranslation("key_close"))) {
215
                            this.closeSpline = true;
216
                        }
217

    
218
                        this.finishSpline = true;
219
                        return;
220
                    }
221
                }
222
            }
223
        }
224
        throw new InvalidEntryException(null);
225
    }
226

    
227
    public Geometry finish() throws FinishServiceException {
228

    
229
        if (values != null) {
230

    
231
            EditingProviderServices editingProviderServices =
232
                (EditingProviderServices) getProviderServices();
233
            try {
234

    
235
                if (closeSpline) {
236
                    values.add(values.get(0));
237
                }
238

    
239
                int subtype = editingProviderServices.getSubType(featureStore);
240
                Spline spline =
241
                    editingProviderServices.createSpline(values, subtype);
242

    
243
                GeometryType geomType =
244
                    editingProviderServices.getGeomType(featureStore);
245

    
246
                if (geomType.isTypeOf(MULTICURVE)) {
247

    
248
                    GeometryManager geometryManager =
249
                        GeometryLocator.getGeometryManager();
250
                    MultiCurve multiCurve =
251
                        geometryManager.createMultiCurve(geomType.getSubType());
252
                    multiCurve.addCurve(spline);
253
                    return multiCurve;
254

    
255
                } else {
256
                    return spline;
257
                }
258
            } catch (BaseException e) {
259
                throw new FinishServiceException(e);
260
            }
261
        }
262
        return null;
263
    }
264

    
265
    public void finishAndStore() throws FinishServiceException {
266
        Geometry geometry = this.finish();
267
        EditingProviderServices editingProviderServices =
268
            (EditingProviderServices) getProviderServices();
269
        editingProviderServices.insertGeometryIntoFeatureStore(geometry,
270
            featureStore);
271
    }
272

    
273
    public void start() throws StartServiceException {
274
        values = new ArrayList<Point>();
275

    
276
        closeSpline = false;
277
        finishSpline = false;
278
    }
279

    
280
    public String getName() {
281
        return SplineEditingProviderFactory.PROVIDER_NAME;
282
    }
283

    
284
}