gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.remove / src / main / java / org / gvsig / vectorediting / lib / prov / remove / RemoveEditingProvider.java @ 493
History | View | Annotate | Download (6.08 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.remove; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
|
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.Feature; |
34 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.geom.Geometry; |
37 |
import org.gvsig.fmap.geom.primitive.Point; |
38 |
import org.gvsig.tools.dynobject.DynObject; |
39 |
import org.gvsig.tools.exception.BaseException; |
40 |
import org.gvsig.tools.service.spi.ProviderServices; |
41 |
import org.gvsig.tools.visitor.VisitCanceledException; |
42 |
import org.gvsig.tools.visitor.Visitor; |
43 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
44 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
45 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
46 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
47 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
48 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
49 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
51 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
52 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
53 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
54 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
55 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
56 |
|
57 |
/**
|
58 |
* @author fdiaz
|
59 |
*
|
60 |
*/
|
61 |
public class RemoveEditingProvider extends AbstractEditingProvider implements |
62 |
EditingProvider { |
63 |
|
64 |
private EditingServiceParameter selection;
|
65 |
|
66 |
private FeatureStore featureStore;
|
67 |
|
68 |
private Map<EditingServiceParameter, Object> values; |
69 |
|
70 |
/**
|
71 |
* Default constructor.
|
72 |
*
|
73 |
* @param providerServices
|
74 |
* available services for this provider
|
75 |
* @param parameters
|
76 |
* of this provider
|
77 |
*/
|
78 |
public RemoveEditingProvider(ProviderServices providerServices,
|
79 |
DynObject parameters) { |
80 |
super(providerServices);
|
81 |
|
82 |
this.featureStore =
|
83 |
(FeatureStore) parameters |
84 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
85 |
|
86 |
this.selection =
|
87 |
new DefaultEditingServiceParameter("selection", "selection", |
88 |
TYPE.SELECTION); |
89 |
|
90 |
} |
91 |
|
92 |
public EditingServiceParameter next() {
|
93 |
if (values.get(selection) == null) { |
94 |
return selection;
|
95 |
} |
96 |
return null; |
97 |
} |
98 |
|
99 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
100 |
throws DrawServiceException {
|
101 |
return null; |
102 |
} |
103 |
|
104 |
public void stop() throws StopServiceException { |
105 |
if (values != null) { |
106 |
values.clear(); |
107 |
} |
108 |
} |
109 |
|
110 |
public List<EditingServiceParameter> getParameters() { |
111 |
List<EditingServiceParameter> parameters =
|
112 |
new ArrayList<EditingServiceParameter>(); |
113 |
parameters.add(selection); |
114 |
return parameters;
|
115 |
} |
116 |
|
117 |
public void setValue(Object value) throws InvalidEntryException { |
118 |
EditingServiceParameter parameter = next(); |
119 |
validateAndInsertValue(parameter, value); |
120 |
} |
121 |
|
122 |
private void validateAndInsertValue(EditingServiceParameter parameter, |
123 |
Object value) {
|
124 |
|
125 |
if (parameter == selection) {
|
126 |
if (value instanceof FeatureSelection) { |
127 |
if (((FeatureSelection) value).getSelectedCount() > 0) { |
128 |
values.put(selection, value); |
129 |
} |
130 |
} |
131 |
} |
132 |
} |
133 |
|
134 |
public Geometry finish() throws FinishServiceException { |
135 |
return null; |
136 |
} |
137 |
|
138 |
public void finishAndStore() throws FinishServiceException { |
139 |
|
140 |
if (values != null) { |
141 |
|
142 |
FeatureSelection featureSelection = |
143 |
(FeatureSelection) values.get(selection); |
144 |
|
145 |
try {
|
146 |
featureSelection.accept(new Visitor() {
|
147 |
|
148 |
public void visit(Object obj) |
149 |
throws VisitCanceledException, BaseException {
|
150 |
Feature feature = (Feature) obj; |
151 |
|
152 |
((EditingProviderServices) getProviderServices()) |
153 |
.deleteFeatureFromFeatureStore(feature, |
154 |
featureStore); |
155 |
} |
156 |
}); |
157 |
|
158 |
featureStore.getFeatureSelection().deselectAll(); |
159 |
} catch (BaseException e) {
|
160 |
throw new FinishServiceException(e); |
161 |
} |
162 |
} |
163 |
} |
164 |
|
165 |
public void start() throws StartServiceException, InvalidEntryException { |
166 |
values = new HashMap<EditingServiceParameter, Object>(); |
167 |
FeatureSelection selected = null;
|
168 |
if (featureStore != null) { |
169 |
try {
|
170 |
selected = featureStore.getFeatureSelection(); |
171 |
} catch (DataException e) {
|
172 |
throw new StartServiceException(e); |
173 |
} |
174 |
try {
|
175 |
setValue(selected); |
176 |
} catch (InvalidEntryException e) {
|
177 |
throw new InvalidEntryException(e); |
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
public String getName() { |
183 |
return RemoveEditingProviderFactory.PROVIDER_NAME;
|
184 |
} |
185 |
|
186 |
} |