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