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