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 / client / ChangesImpl.java @ 2694
History | View | Annotate | Download (7.95 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.client; |
23 |
|
24 |
import java.util.Iterator; |
25 |
import java.util.function.Predicate; |
26 |
import javax.swing.event.ChangeListener; |
27 |
import org.gvsig.fmap.dal.feature.Feature; |
28 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
29 |
import org.gvsig.fmap.dal.feature.paging.FeaturePagingHelper; |
30 |
import org.gvsig.tools.exception.BaseException; |
31 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
32 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
33 |
import org.gvsig.tools.util.GetItemWithSize64; |
34 |
import org.gvsig.vcsgis.lib.client.VCSGisWorkspace.Change; |
35 |
import org.gvsig.vcsgis.lib.client.VCSGisWorkspace.Changes; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
/**
|
40 |
*
|
41 |
* @author gvSIG Team
|
42 |
* @param <<error>>
|
43 |
* @param <CHANGE>
|
44 |
*/
|
45 |
@SuppressWarnings("UseSpecificCatch") |
46 |
public abstract class ChangesImpl<CHANGE extends Change> implements Changes<CHANGE> { |
47 |
|
48 |
private static final Logger LOGGER = LoggerFactory.getLogger(ChangesImpl.class); |
49 |
|
50 |
final FeaturePagingHelper changes;
|
51 |
ChangeListenerHelper changeListernerHelper; |
52 |
private final String selectedFieldName; |
53 |
|
54 |
public ChangesImpl(GetItemWithSize64<Feature> changes, String selectedFieldName) { |
55 |
this.changes = (FeaturePagingHelper) changes;
|
56 |
this.changeListernerHelper = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
|
57 |
this.selectedFieldName = selectedFieldName;
|
58 |
|
59 |
} |
60 |
|
61 |
abstract protected CHANGE createChange(Feature f); |
62 |
|
63 |
abstract protected void updateChange(FeatureStore store, CHANGE change); |
64 |
|
65 |
@Override
|
66 |
public void addSelectionInterval(long start, long end) { |
67 |
try {
|
68 |
FeatureStore store = this.changes.getFeatureStore();
|
69 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
70 |
for (long i = start; i <= end; i++) { |
71 |
Feature feat = this.changes.getFeatureAt(i);
|
72 |
CHANGE row = this.createChange(feat);
|
73 |
row.setSelected(true);
|
74 |
updateChange(store, row); |
75 |
} |
76 |
store.finishEditingQuietly(); |
77 |
this.changes.reload();
|
78 |
this.fireChanges();
|
79 |
} catch (Exception ex) { |
80 |
LOGGER.warn("Can't add to selection.", ex);
|
81 |
} |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void removeSelectionInterval(long start, long end) { |
86 |
try {
|
87 |
FeatureStore store = this.changes.getFeatureStore();
|
88 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
89 |
for (long i = start; i <= end; i++) { |
90 |
Feature feat = this.changes.getFeatureAt(i);
|
91 |
CHANGE row = this.createChange(feat);
|
92 |
row.setSelected(false);
|
93 |
updateChange(store, row); |
94 |
} |
95 |
store.finishEditingQuietly(); |
96 |
this.changes.reload();
|
97 |
this.fireChanges();
|
98 |
} catch (Exception ex) { |
99 |
LOGGER.warn("Can't add to selection.", ex);
|
100 |
} |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public void setSelectionInterval(long start, long end) { |
105 |
try {
|
106 |
FeatureStore store = this.changes.getFeatureStore();
|
107 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
108 |
store.update(this.selectedFieldName, false); |
109 |
|
110 |
for (long i = start; i <= end; i++) { |
111 |
Feature feat = this.changes.getFeatureAt(i);
|
112 |
CHANGE row = this.createChange(feat);
|
113 |
row.setSelected(true);
|
114 |
updateChange(store, row); |
115 |
} |
116 |
|
117 |
store.finishEditingQuietly(); |
118 |
this.changes.reload();
|
119 |
this.fireChanges();
|
120 |
} catch (Exception ex) { |
121 |
LOGGER.warn("Can't set selection.", ex);
|
122 |
} |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void clearSelection() { |
127 |
try {
|
128 |
FeatureStore store = this.changes.getFeatureStore();
|
129 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
130 |
store.update(this.selectedFieldName, false); |
131 |
store.finishEditingQuietly(); |
132 |
this.changes.reload();
|
133 |
this.fireChanges();
|
134 |
} catch (Exception ex) { |
135 |
LOGGER.warn("Can't clear selection.", ex);
|
136 |
} |
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void setSelectionAll() { |
141 |
try {
|
142 |
FeatureStore store = this.changes.getFeatureStore();
|
143 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
144 |
store.update(this.selectedFieldName, true); |
145 |
store.finishEditingQuietly(); |
146 |
this.changes.reload();
|
147 |
this.fireChanges();
|
148 |
} catch (Exception ex) { |
149 |
LOGGER.warn("Can't set selection.", ex);
|
150 |
} |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void process(Iterator<Long> rows, Predicate<CHANGE> action) { |
155 |
try {
|
156 |
FeatureStore store = this.changes.getFeatureStore();
|
157 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
158 |
|
159 |
while(rows.hasNext()){
|
160 |
Long row = rows.next();
|
161 |
Feature feat = this.changes.getFeatureAt(row);
|
162 |
CHANGE change = this.createChange(feat);
|
163 |
if(action.test(change)){
|
164 |
updateChange(store, change); |
165 |
} |
166 |
} |
167 |
store.finishEditingQuietly(); |
168 |
this.changes.reload();
|
169 |
this.fireChanges();
|
170 |
} catch (Exception ex) { |
171 |
LOGGER.warn("Can't process.", ex);
|
172 |
} |
173 |
} |
174 |
|
175 |
|
176 |
@Override
|
177 |
public boolean isSelectedIndex(long index) { |
178 |
try {
|
179 |
Feature feat = this.changes.getFeatureAt(index);
|
180 |
return feat.getBoolean(this.selectedFieldName); |
181 |
} catch (BaseException ex) {
|
182 |
LOGGER.warn("Can't check if is selected.", ex);
|
183 |
throw new RuntimeException("Can't check if is selected.", ex); |
184 |
} |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public boolean isSelectionEmpty() { |
189 |
try {
|
190 |
FeatureStore store = this.changes.getFeatureStore();
|
191 |
Feature foundFeat = store.findFirst("\""+this.selectedFieldName+"\""); |
192 |
return foundFeat == null; |
193 |
} catch (BaseException ex) {
|
194 |
LOGGER.warn("Can't check if is empty selection.", ex);
|
195 |
throw new RuntimeException("Can't check if is selected.", ex); |
196 |
} |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public long size64() { |
201 |
return changes.size64();
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
public CHANGE get64(long position) { |
206 |
Feature f = changes.get64(position); |
207 |
if (f == null) { |
208 |
return null; |
209 |
} |
210 |
return (CHANGE) this.createChange(f); |
211 |
} |
212 |
|
213 |
private void fireChanges() { |
214 |
changeListernerHelper.fireEvent(); |
215 |
} |
216 |
|
217 |
@Override
|
218 |
public void addChangeListener(ChangeListener listener) { |
219 |
changeListernerHelper.addChangeListener(listener); |
220 |
} |
221 |
|
222 |
@Override
|
223 |
public ChangeListener[] getChangeListeners() { |
224 |
return changeListernerHelper.getChangeListeners();
|
225 |
} |
226 |
|
227 |
@Override
|
228 |
public void removeChangeListener(ChangeListener listener) { |
229 |
changeListernerHelper.removeChangeListener(listener); |
230 |
} |
231 |
|
232 |
@Override
|
233 |
public void removeAllChangeListener() { |
234 |
changeListernerHelper.removeAllChangeListener(); |
235 |
} |
236 |
|
237 |
@Override
|
238 |
public boolean hasChangeListeners() { |
239 |
return changeListernerHelper.hasChangeListeners();
|
240 |
} |
241 |
|
242 |
|
243 |
|
244 |
} |