Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.spi / src / main / java / org / gvsig / vectorediting / lib / spi / AbstractEditingProvider.java @ 4284

History | View | Annotate | Download (7.15 KB)

1 159 llmarques
/**
2
 * gvSIG. Desktop Geographic Information System.
3 46 fdiaz
 *
4 2611 jjdelcerro
 * Copyright � 2007-2014 gvSIG Association
5 159 llmarques
 *
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 19 llmarques
 */
24 159 llmarques
25 19 llmarques
package org.gvsig.vectorediting.lib.spi;
26
27 3067 jjdelcerro
import java.util.ArrayList;
28 2870 fdiaz
import java.util.List;
29 3067 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
30 2611 jjdelcerro
import org.gvsig.fmap.dal.feature.EditableFeature;
31 2616 fdiaz
import org.gvsig.fmap.dal.feature.Feature;
32 3067 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureSelection;
33 19 llmarques
import org.gvsig.fmap.geom.Geometry;
34 2616 fdiaz
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
35 3067 jjdelcerro
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dispose.DisposableIterator;
37 19 llmarques
import org.gvsig.tools.service.spi.AbstractProvider;
38
import org.gvsig.tools.service.spi.ProviderServices;
39 3067 jjdelcerro
import org.gvsig.tools.task.SimpleTaskStatus;
40 2125 fdiaz
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
41
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
42 2870 fdiaz
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
43
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
44 2444 fdiaz
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46 19 llmarques
47
public abstract class AbstractEditingProvider extends AbstractProvider
48 227 llmarques
implements EditingProvider, Geometry.TYPES, Geometry.SUBTYPES {
49 2444 fdiaz
50 2870 fdiaz
    protected static final Logger LOGGER = LoggerFactory
51 2444 fdiaz
        .getLogger(AbstractEditingProvider.class);
52 2616 fdiaz
53
    private boolean showPreviewSymbol;
54 19 llmarques
55 159 llmarques
    public AbstractEditingProvider(ProviderServices providerServices) {
56
        super(providerServices);
57 2616 fdiaz
        showPreviewSymbol = true;
58 159 llmarques
    }
59 19 llmarques
60 2125 fdiaz
    @Override
61
    public boolean isEnabled(EditingServiceParameter parameter) {
62
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
63
    }
64
65 2204 fdiaz
//    @Override
66
//    public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException {
67
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
68
//    }
69
70 2125 fdiaz
    @Override
71 2204 fdiaz
    public abstract void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException;
72 2125 fdiaz
73 2444 fdiaz
    @Override
74
    public void activate() {
75
76
    }
77
78
    @Override
79
    public Object getValue(EditingServiceParameter parameter) {
80
        // To implement in subclasses
81
        LOGGER.warn("'"+this.getClass().getName() + "' dooes not implement the method 'getValue'.");
82
        return null;
83
    }
84 2608 fdiaz
85 2748 fdiaz
    public Object getValue(EditingServiceParameter parameter, EditingServiceParameter.TYPE type) {
86
        return this.getValue(parameter);
87
    }
88
89 2608 fdiaz
    @Override
90
    protected EditingProviderServices getProviderServices() {
91
        return (EditingProviderServices)super.getProviderServices();
92
    }
93 2444 fdiaz
94 2616 fdiaz
    @Override
95 2611 jjdelcerro
    public void setDefaultFeatureValues(EditableFeature feature) {
96
        ((EditingProviderServices)(this.getProviderServices())).setDefaultFeatureValues(feature);
97
    }
98
99
    @Override
100
    public EditableFeature getDefaultFeatureValues() {
101
        return ((EditingProviderServices)(this.getProviderServices())).getDefaultFeatureValues();
102
    }
103 2616 fdiaz
104
    public ISymbol getPreviewSymbol() {
105
        if(!showPreviewSymbol){
106
            return null;
107
        }
108
        return this.getProviderServices().getPreviewSymbol();
109
    }
110 2611 jjdelcerro
111 2616 fdiaz
    public ISymbol getPreviewSymbol(Feature feature) {
112
        if(!showPreviewSymbol){
113
            return null;
114
        }
115
        return this.getProviderServices().getPreviewSymbol(feature);
116
    }
117
118
    @Override
119
    public void setShowPreviewSymbol(boolean showPreviewSymbol) {
120
        this.showPreviewSymbol = showPreviewSymbol;
121
    }
122
123
    @Override
124
    public boolean isShowPreviewSymbol() {
125
        return this.showPreviewSymbol;
126
    }
127 2723 fdiaz
128
//    protected Object validateAndInsertTypeOptionParameterValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException {
129
//        EditingServiceParameterOptions opt = parameter.getOptions2();
130
//        Object defaultValue = parameter.getDefaultValue();
131
//        if (value instanceof String) {
132
//            Object v = opt.getValue((String)value, defaultValue);
133
//            if(v != null){
134
//                return v;
135
//            } else if(defaultValue != null) {
136
//                return defaultValue;
137
//            } else {
138
//                throw new InvalidEntryException(null);
139
//            }
140
//        } else if(opt.isValidValue(value)){
141
//            return value;
142
//        } else {
143
//            throw new InvalidEntryException(null);
144
//        }
145
//    }
146 2616 fdiaz
147 2870 fdiaz
    @Override
148
    public void restart() throws StartServiceException, InvalidEntryException ,StopServiceException {
149
        this.stop();
150
        this.start();
151
    }
152 2723 fdiaz
153 2870 fdiaz
    @Override
154
    public void initDefaultValues() {
155
        List<EditingServiceParameter> parameters = this.getParameters();
156
        for (EditingServiceParameter parameter : parameters) {
157 2998 fdiaz
            if(parameter.isOptional() && getValue(parameter)==null){
158 2870 fdiaz
                try {
159
                    setValue(parameter, parameter.getDefaultValue());
160
                } catch (InvalidEntryException ex) {
161
                    LOGGER.debug("Can't init default value of "+parameter.getName(), ex);
162
                }
163
            }
164
        }
165
    }
166 2723 fdiaz
167 3067 jjdelcerro
    public List<Feature> getSelectedFeaturesCopy(FeatureSelection selection)  {
168
        SimpleTaskStatus status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus("Loading selection");
169
        try {
170 3618 fdiaz
            status.add();
171 3067 jjdelcerro
            status.setAutoremove(true);
172
            status.setRangeOfValues(0, selection.size());
173
            List<Feature> features = new ArrayList<>();
174
            DisposableIterator it = selection.fastIterator();
175
            while (it.hasNext()) {
176 3140 jjdelcerro
                if( status.isCancellationRequested() ) {
177
                    status.cancel();
178
                    return features;
179
                }
180 3067 jjdelcerro
                Feature feature = (Feature) it.next();
181
                if( feature.getDefaultGeometry()==null ) {
182
                    continue;
183
                }
184
                features.add(feature.getCopy());
185
                status.incrementCurrentValue();
186
            }
187
            status.terminate();
188
            return features;
189
        } catch (DataException ex) {
190
            status.abort();
191
            throw new RuntimeException("Can't calculate selected features", ex);
192
        }
193
    }
194 2870 fdiaz
195 4284 fdiaz
    @Override
196
    public boolean mustRestartAtFinish() {
197
        return true;
198
    }
199 2870 fdiaz
200 19 llmarques
}