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.remove / src / main / java / org / gvsig / vectorediting / lib / prov / remove / RemoveEditingProvider.java @ 2616

History | View | Annotate | Download (6.42 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
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.Feature;
33
import org.gvsig.fmap.dal.feature.FeatureSelection;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.tools.ToolsLocator;
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.vectorediting.lib.api.DrawingStatus;
42
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
43
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
44
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
45
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
46
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
47
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
48
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
49
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
50
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
51
import org.gvsig.vectorediting.lib.spi.EditingProvider;
52
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
53
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
54

    
55
/**
56
 * @author fdiaz
57
 *
58
 */
59
public class RemoveEditingProvider extends AbstractEditingProvider implements
60
    EditingProvider {
61

    
62
    private final EditingServiceParameter selection;
63

    
64
    private final FeatureStore featureStore;
65

    
66
    private Map<EditingServiceParameter, Object> values;
67

    
68
    /**
69
     * Default constructor.
70
     *
71
     * @param providerServices
72
     *            available services for this provider
73
     * @param parameters
74
     *            of this provider
75
     */
76
    public RemoveEditingProvider(ProviderServices providerServices,
77
        DynObject parameters) {
78
        super(providerServices);
79

    
80
        this.featureStore =
81
            (FeatureStore) parameters
82
                .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
83

    
84
        this.selection =
85
            new DefaultEditingServiceParameter("selection", "selection",
86
                TYPE.SELECTION);
87

    
88
    }
89

    
90
    @Override
91
    public EditingServiceParameter next() {
92
        if (values.get(selection) == null) {
93
            return selection;
94
        }
95
        return null;
96
    }
97

    
98
    @Override
99
    public DrawingStatus getDrawingStatus(Point mousePosition)
100
        throws DrawServiceException {
101
        return null;
102
    }
103

    
104
    @Override
105
    public void stop() throws StopServiceException {
106
        if (values != null) {
107
            values.clear();
108
        }
109
    }
110

    
111
    @Override
112
    public List<EditingServiceParameter> getParameters() {
113
        List<EditingServiceParameter> parameters =
114
            new ArrayList<>();
115
        parameters.add(selection);
116
        return parameters;
117
    }
118

    
119
    @Override
120
    public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException {
121
        validateAndInsertValue(parameter, value);
122
    }
123

    
124
    @Override
125
    public void setValue(Object value) throws InvalidEntryException {
126
        EditingServiceParameter parameter = next();
127
        validateAndInsertValue(parameter, value);
128
    }
129

    
130
    private void validateAndInsertValue(EditingServiceParameter parameter,
131
        Object value) {
132

    
133
        if (parameter == selection) {
134
            if (value instanceof FeatureSelection) {
135
                if (((FeatureSelection) value).getSelectedCount() > 0) {
136
                    values.put(selection, value);
137
                }
138
            }
139
        }
140
    }
141

    
142
    @Override
143
    public Geometry finish() throws FinishServiceException {
144
        return null;
145
    }
146

    
147
    @Override
148
    public void finishAndStore() throws FinishServiceException {
149

    
150
        if (values != null) {
151

    
152
            final FeatureSelection featureSelection =
153
                (FeatureSelection) values.get(selection);
154
            ToolsLocator.getDisposableManager().bind(featureSelection);
155

    
156

    
157
            try {
158
                featureSelection.accept((Object obj) -> {
159
                    final Feature feature = (Feature) obj;
160
                    
161
                    ((EditingProviderServices) getProviderServices())
162
                            .deleteFeatureFromFeatureSet(feature,
163
                                    featureStore, featureSelection);
164
                });
165

    
166
                featureSelection.deselectAll();
167
                featureSelection.dispose();
168
            } catch (BaseException e) {
169
                throw new FinishServiceException(e);
170
            }
171
        }
172
    }
173

    
174
    @Override
175
    public void start() throws StartServiceException, InvalidEntryException {
176
        values = new HashMap<>();
177
        FeatureSelection selected = null;
178
        if (featureStore != null) {
179
            try {
180
                selected = featureStore.getFeatureSelection();
181
            } catch (DataException e) {
182
                throw new StartServiceException(e);
183
            }
184
            try {
185
                setValue(selected);
186
            } catch (InvalidEntryException e) {
187
                throw new InvalidEntryException(e);
188
            }
189
        }
190
    }
191

    
192
    @Override
193
    public String getName() {
194
        return RemoveEditingProviderFactory.PROVIDER_NAME;
195
    }
196

    
197
    @Override
198
    public Object getValue(EditingServiceParameter parameter) {
199
        return values!=null?values.get(parameter):null;
200
    }
201
}