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.point / src / main / java / org / gvsig / vectorediting / lib / prov / point / PointEditingProvider.java @ 2204

History | View | Annotate | Download (5.94 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.point;
26

    
27
import java.util.ArrayList;
28
import java.util.List;
29

    
30
import org.gvsig.fmap.dal.feature.FeatureStore;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.GeometryLocator;
33
import org.gvsig.fmap.geom.aggregate.MultiPoint;
34
import org.gvsig.fmap.geom.primitive.Point;
35
import org.gvsig.fmap.geom.type.GeometryType;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynObject;
38
import org.gvsig.tools.i18n.I18nManager;
39
import org.gvsig.tools.service.spi.ProviderServices;
40
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
41
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
42
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
43
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
44
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
45
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
46
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
47
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
48
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus;
49
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
50
import org.gvsig.vectorediting.lib.spi.EditingProvider;
51
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
52
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
53

    
54
public class PointEditingProvider extends AbstractEditingProvider
55
implements EditingProvider {
56

    
57
    private EditingProviderServices editingProviderServices =
58
        (EditingProviderServices) getProviderServices();
59

    
60
    private EditingServiceParameter point;
61

    
62
    private Point valuePoint;
63

    
64
    private FeatureStore featureStore;
65

    
66
    public PointEditingProvider(ProviderServices services,
67
        DynObject parameters) {
68
        super(services);
69
        this.featureStore =
70
            (FeatureStore) parameters
71
            .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
72

    
73
        this.point =
74
            new DefaultEditingServiceParameter("insert_point",
75
                "indicate_new_point", TYPE.POSITION);
76
    }
77

    
78
    public DefaultDrawingStatus getDrawingStatus(Point mousePosition)
79
        throws DrawServiceException {
80
        return null;
81
    }
82

    
83
    public void stop() throws StopServiceException {
84
        valuePoint = null;
85
    }
86

    
87
    private boolean isValidValue(EditingServiceParameter param, Object value) {
88
        return (value instanceof Point) ? true : false;
89
    }
90

    
91
    public EditingServiceParameter next() {
92
        if (valuePoint == null) {
93
            return this.point;
94
        }
95
        return null;
96
    }
97

    
98
    public List<EditingServiceParameter> getParameters() {
99
        List<EditingServiceParameter> list =
100
            new ArrayList<EditingServiceParameter>();
101
        list.add(point);
102
        return list;
103
    }
104

    
105
    @Override
106
    public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException {
107
        validateAndInsertValue(parameter, value);
108
    }
109

    
110
    public void setValue(Object value) throws InvalidEntryException {
111
        EditingServiceParameter param = next();
112
        validateAndInsertValue(param, value);
113
    }
114
    
115
    private void validateAndInsertValue(EditingServiceParameter parameter,
116
        Object value) throws InvalidEntryException {
117

    
118
        if (isValidValue(parameter, value)) {
119
            this.valuePoint = (Point) value;
120
        } else {
121
            throw new InvalidEntryException(null);
122
        }
123
    }
124

    
125

    
126
    public void finishAndStore() throws FinishServiceException {
127
        Geometry geometry = null;
128
        try {
129
            geometry = finish();
130
            editingProviderServices.insertGeometryIntoFeatureStore(geometry,
131
                featureStore);
132
        } catch (Exception e) {
133
            throw new FinishServiceException("Can't finalize " + this.getName()
134
                + "with X=" + valuePoint.getX() + " Y= " + valuePoint.getY(), e);
135
        }
136
    }
137

    
138
    public void start() throws StartServiceException {
139
        this.valuePoint = null;
140
    }
141

    
142
    public String getName() {
143
        return PointEditingProviderFactory.PROVIDER_NAME;
144
    }
145

    
146
    public Geometry finish() throws FinishServiceException {
147
        Point geometry = null;
148
        try {
149
            int subtype = editingProviderServices.getSubType(featureStore);
150
            GeometryType geomType =
151
                editingProviderServices.getGeomType(featureStore);
152
            geometry =
153
                editingProviderServices.createPoint(valuePoint.getX(),
154
                    valuePoint.getY(), subtype);
155

    
156
            if (geomType.isTypeOf(MULTIPOINT)) {
157
                MultiPoint multiPoint;
158
                multiPoint = GeometryLocator.getGeometryManager().createMultiPoint(subtype);
159
                multiPoint.addPoint(geometry);
160
                return multiPoint;
161
            }
162

    
163
        } catch (Exception e) {
164
            throw new FinishServiceException("Can't finalize " + this.getName()
165
                + "with X=" + valuePoint.getX() + " Y= " + valuePoint.getY(), e);
166
        }
167
        return geometry;
168
    }
169
}