gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / lib / workspace / ChangesImpl.java @ 3572
History | View | Annotate | Download (11.8 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 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 3
|
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, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
package org.gvsig.vcsgis.lib.workspace; |
23 |
|
24 |
import java.util.Iterator; |
25 |
import java.util.List; |
26 |
import java.util.function.Predicate; |
27 |
import javax.swing.event.ChangeListener; |
28 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
29 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
30 |
import org.gvsig.fmap.dal.feature.Feature; |
31 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
32 |
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper; |
33 |
import org.gvsig.tools.ToolsLocator; |
34 |
import org.gvsig.tools.exception.BaseException; |
35 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
36 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
37 |
import org.gvsig.tools.task.SimpleTaskStatus; |
38 |
import org.gvsig.tools.util.GetItemWithSize64; |
39 |
import org.gvsig.vcsgis.lib.VCSGisChange; |
40 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
41 |
import static org.gvsig.vcsgis.lib.workspace.tables.WorkspaceChangesTable.COD_ENTITY; |
42 |
import org.slf4j.Logger; |
43 |
import org.slf4j.LoggerFactory; |
44 |
|
45 |
/**
|
46 |
*
|
47 |
* @author gvSIG Team
|
48 |
* @param <CHANGE>
|
49 |
*/
|
50 |
@SuppressWarnings("UseSpecificCatch") |
51 |
public abstract class ChangesImpl<CHANGE extends VCSGisChange> implements VCSGisWorkspaceChanges<CHANGE> { |
52 |
|
53 |
private static final Logger LOGGER = LoggerFactory.getLogger(ChangesImpl.class); |
54 |
|
55 |
final FeaturePagingHelper changes;
|
56 |
ChangeListenerHelper changeListernerHelper; |
57 |
private final String selectedFieldName; |
58 |
|
59 |
public ChangesImpl(GetItemWithSize64<Feature> changes, String selectedFieldName) { |
60 |
this.changes = (FeaturePagingHelper) changes;
|
61 |
this.changeListernerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
|
62 |
this.selectedFieldName = selectedFieldName;
|
63 |
|
64 |
} |
65 |
|
66 |
abstract protected CHANGE createChange(Feature f); |
67 |
|
68 |
abstract protected void updateChange(FeatureStore store, CHANGE change); |
69 |
|
70 |
@Override
|
71 |
public void addSelectionInterval(long start, long end) { |
72 |
try {
|
73 |
FeatureStore store = this.changes.getFeatureStore();
|
74 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
75 |
for (long i = start; i <= end; i++) { |
76 |
Feature feat = this.changes.getFeatureAt(i);
|
77 |
CHANGE row = this.createChange(feat);
|
78 |
row.setSelected(true);
|
79 |
updateChange(store, row); |
80 |
} |
81 |
store.finishEditingQuietly(); |
82 |
this.changes.reload();
|
83 |
this.fireChanges();
|
84 |
} catch (Exception ex) { |
85 |
LOGGER.warn("Can't add to selection.", ex);
|
86 |
} |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void removeSelectionInterval(long start, long end) { |
91 |
try {
|
92 |
FeatureStore store = this.changes.getFeatureStore();
|
93 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
94 |
for (long i = start; i <= end; i++) { |
95 |
Feature feat = this.changes.getFeatureAt(i);
|
96 |
CHANGE row = this.createChange(feat);
|
97 |
row.setSelected(false);
|
98 |
updateChange(store, row); |
99 |
} |
100 |
store.finishEditingQuietly(); |
101 |
this.changes.reload();
|
102 |
this.fireChanges();
|
103 |
} catch (Exception ex) { |
104 |
LOGGER.warn("Can't add to selection.", ex);
|
105 |
} |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void setSelectionInterval(long start, long end) { |
110 |
SimpleTaskStatus status = ToolsLocator.getTaskStatusManager() |
111 |
.createDefaultSimpleTaskStatus("_Select_interval_changes");
|
112 |
status.setAutoremove(true);
|
113 |
status.add(); |
114 |
try {
|
115 |
status.setRangeOfValues(start, end); |
116 |
status.message("_Updating_selection");
|
117 |
FeatureStore store = this.changes.getFeatureStore();
|
118 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
119 |
store.update(this.selectedFieldName, false); |
120 |
|
121 |
for (long i = start; i <= end; i++) { |
122 |
Feature feat = this.changes.getFeatureAt(i);
|
123 |
CHANGE row = this.createChange(feat);
|
124 |
row.setSelected(true);
|
125 |
updateChange(store, row); |
126 |
status.setCurValue(i); |
127 |
} |
128 |
|
129 |
status.message("_Finising_process");
|
130 |
store.finishEditingQuietly(); |
131 |
status.message("_Reloading_changes");
|
132 |
this.changes.reload();
|
133 |
status.message("_Refresing");
|
134 |
this.fireChanges();
|
135 |
status.terminate(); |
136 |
} catch (Exception ex) { |
137 |
LOGGER.warn("Can't process.", ex);
|
138 |
status.message("Can't process changes ("+ex.getMessage()+")"); |
139 |
status.abort(); |
140 |
} |
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void clearSelection() { |
145 |
SimpleTaskStatus status = ToolsLocator.getTaskStatusManager() |
146 |
.createDefaultSimpleTaskStatus("_Clear_changes_selection");
|
147 |
status.setAutoremove(true);
|
148 |
status.add(); |
149 |
try {
|
150 |
status.setIndeterminate(); |
151 |
status.message("_Updating_selection");
|
152 |
FeatureStore store = this.changes.getFeatureStore();
|
153 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
154 |
store.update(this.selectedFieldName, false); |
155 |
status.message("_Finising_process");
|
156 |
store.finishEditingQuietly(); |
157 |
status.message("_Reloading_changes");
|
158 |
this.changes.reload();
|
159 |
status.message("_Refresing");
|
160 |
this.fireChanges();
|
161 |
status.terminate(); |
162 |
} catch (Exception ex) { |
163 |
LOGGER.warn("Can't process.", ex);
|
164 |
status.message("Can't process changes ("+ex.getMessage()+")"); |
165 |
status.abort(); |
166 |
} |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public void setSelectionAll() { |
171 |
SimpleTaskStatus status = ToolsLocator.getTaskStatusManager() |
172 |
.createDefaultSimpleTaskStatus("_Selecting_all_changes");
|
173 |
status.setAutoremove(true);
|
174 |
status.add(); |
175 |
try {
|
176 |
status.setIndeterminate(); |
177 |
status.message("_Updating_selection");
|
178 |
FeatureStore store = this.changes.getFeatureStore();
|
179 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
180 |
store.update(this.selectedFieldName, true); |
181 |
status.message("_Finising_process");
|
182 |
store.finishEditingQuietly(); |
183 |
status.message("_Reloading_changes");
|
184 |
this.changes.reload();
|
185 |
status.message("_Refresing");
|
186 |
this.fireChanges();
|
187 |
status.terminate(); |
188 |
} catch (Exception ex) { |
189 |
LOGGER.warn("Can't process.", ex);
|
190 |
status.message("Can't process changes ("+ex.getMessage()+")"); |
191 |
status.abort(); |
192 |
} |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public void process(Iterator<Long> rows, Predicate<CHANGE> action) { |
197 |
SimpleTaskStatus status = ToolsLocator.getTaskStatusManager() |
198 |
.createDefaultSimpleTaskStatus("_Processing_changes");
|
199 |
status.setAutoremove(true);
|
200 |
status.add(); |
201 |
try {
|
202 |
status.setRangeOfValues(0, this.size64()); |
203 |
status.message("_Processing");
|
204 |
FeatureStore store = this.changes.getFeatureStore();
|
205 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
206 |
|
207 |
while(rows.hasNext()){
|
208 |
Long row = rows.next();
|
209 |
Feature feat = this.changes.getFeatureAt(row);
|
210 |
CHANGE change = this.createChange(feat);
|
211 |
if(action.test(change)){
|
212 |
updateChange(store, change); |
213 |
} |
214 |
status.incrementCurrentValue(); |
215 |
} |
216 |
status.message("_Finising_process");
|
217 |
store.finishEditingQuietly(); |
218 |
status.message("_Reloading_changes");
|
219 |
this.changes.reload();
|
220 |
status.message("_Refresing");
|
221 |
this.fireChanges();
|
222 |
status.terminate(); |
223 |
} catch (Exception ex) { |
224 |
LOGGER.warn("Can't process.", ex);
|
225 |
status.message("Can't process changes ("+ex.getMessage()+")"); |
226 |
status.abort(); |
227 |
} |
228 |
} |
229 |
|
230 |
|
231 |
@Override
|
232 |
public boolean isSelectedIndex(long index) { |
233 |
try {
|
234 |
Feature feat = this.changes.getFeatureAt(index);
|
235 |
return feat.getBoolean(this.selectedFieldName); |
236 |
} catch (BaseException ex) {
|
237 |
LOGGER.warn("Can't check if is selected.", ex);
|
238 |
throw new RuntimeException("Can't check if is selected.", ex); |
239 |
} |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public boolean isSelectionEmpty() { |
244 |
try {
|
245 |
FeatureStore store = this.changes.getFeatureStore();
|
246 |
Feature foundFeat = store.findFirst("\""+this.selectedFieldName+"\""); |
247 |
return foundFeat == null; |
248 |
} catch (BaseException ex) {
|
249 |
LOGGER.warn("Can't check if is empty selection.", ex);
|
250 |
throw new RuntimeException("Can't check if is selected.", ex); |
251 |
} |
252 |
} |
253 |
|
254 |
@Override
|
255 |
public boolean isSelectionFromEntitiesEmpty(List<VCSGisEntity> entities) { |
256 |
try {
|
257 |
if(entities.isEmpty()){
|
258 |
return false; |
259 |
} |
260 |
|
261 |
ExpressionBuilder expBuilder = ExpressionUtils.createExpressionBuilder(); |
262 |
for (VCSGisEntity entity : entities) {
|
263 |
if(entity != null){ |
264 |
expBuilder.or( |
265 |
expBuilder.eq( |
266 |
expBuilder.column(COD_ENTITY), |
267 |
expBuilder.constant(entity.getEntityCode()) |
268 |
) |
269 |
); |
270 |
} |
271 |
} |
272 |
ExpressionBuilder.Value or = expBuilder.value(); |
273 |
expBuilder.set(null);
|
274 |
expBuilder.and(expBuilder.column(this.selectedFieldName));
|
275 |
expBuilder.and(expBuilder.group(or)); |
276 |
|
277 |
FeatureStore store = this.changes.getFeatureStore();
|
278 |
Feature foundFeat = store.findFirst(expBuilder.toString()); |
279 |
return foundFeat == null; |
280 |
} catch (BaseException ex) {
|
281 |
LOGGER.warn("Can't check if is empty selection.", ex);
|
282 |
throw new RuntimeException("Can't check if is selected.", ex); |
283 |
} |
284 |
} |
285 |
|
286 |
@Override
|
287 |
public long size64() { |
288 |
return changes.size64();
|
289 |
} |
290 |
|
291 |
@Override
|
292 |
public CHANGE get64(long position) { |
293 |
Feature f = changes.get64(position); |
294 |
if (f == null) { |
295 |
return null; |
296 |
} |
297 |
return (CHANGE) this.createChange(f); |
298 |
} |
299 |
|
300 |
private void fireChanges() { |
301 |
changeListernerHelper.fireEvent(); |
302 |
} |
303 |
|
304 |
@Override
|
305 |
public void addChangeListener(ChangeListener listener) { |
306 |
changeListernerHelper.addChangeListener(listener); |
307 |
} |
308 |
|
309 |
@Override
|
310 |
public ChangeListener[] getChangeListeners() { |
311 |
return changeListernerHelper.getChangeListeners();
|
312 |
} |
313 |
|
314 |
@Override
|
315 |
public void removeChangeListener(ChangeListener listener) { |
316 |
changeListernerHelper.removeChangeListener(listener); |
317 |
} |
318 |
|
319 |
@Override
|
320 |
public void removeAllChangeListener() { |
321 |
changeListernerHelper.removeAllChangeListener(); |
322 |
} |
323 |
|
324 |
@Override
|
325 |
public boolean hasChangeListeners() { |
326 |
return changeListernerHelper.hasChangeListeners();
|
327 |
} |
328 |
|
329 |
|
330 |
|
331 |
} |