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 @ 2204

History | View | Annotate | Download (6.46 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
    @Override
120
    public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException {
121
        validateAndInsertValue(parameter, value);
122
    }
123

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

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

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

    
141
    public Geometry finish() throws FinishServiceException {
142
        return null;
143
    }
144

    
145
    public void finishAndStore() throws FinishServiceException {
146

    
147
        if (values != null) {
148

    
149
            final FeatureSelection featureSelection =
150
                (FeatureSelection) values.get(selection);
151
            ToolsLocator.getDisposableManager().bind(featureSelection);
152

    
153

    
154
            try {
155
                featureSelection.accept(new Visitor() {
156

    
157
                    public void visit(Object obj)
158
                        throws VisitCanceledException, BaseException {
159
                        final Feature feature = (Feature) obj;
160

    
161
                        ((EditingProviderServices) getProviderServices())
162
                            .deleteFeatureFromFeatureSet(feature,
163
                                featureStore, featureSelection);
164
                    }
165
                });
166

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

    
175
    public void start() throws StartServiceException, InvalidEntryException {
176
        values = new HashMap<EditingServiceParameter, Object>();
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
    public String getName() {
193
        return RemoveEditingProviderFactory.PROVIDER_NAME;
194
    }
195

    
196
}