Revision 40870 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app.document.table.app/org.gvsig.app.document.table.app.mainplugin/src/main/java/org/gvsig/app/extension/TableFieldOperations.java
TableFieldOperations.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.app.extension; |
25 | 25 |
|
26 |
import java.util.ArrayList; |
|
27 |
import java.util.HashSet; |
|
28 |
import java.util.Iterator; |
|
29 |
import java.util.List; |
|
30 |
import java.util.Set; |
|
31 |
|
|
32 |
import javax.swing.JOptionPane; |
|
33 |
|
|
34 |
import org.slf4j.Logger; |
|
35 |
import org.slf4j.LoggerFactory; |
|
36 |
|
|
26 | 37 |
import org.gvsig.andami.IconThemeHelper; |
27 | 38 |
import org.gvsig.andami.PluginServices; |
28 | 39 |
import org.gvsig.andami.plugins.Extension; |
29 | 40 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
41 |
import org.gvsig.app.ApplicationLocator; |
|
30 | 42 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
43 |
import org.gvsig.fmap.dal.DataStoreNotification; |
|
31 | 44 |
import org.gvsig.fmap.dal.exception.DataException; |
45 |
import org.gvsig.fmap.dal.feature.Feature; |
|
46 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
47 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
|
48 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
|
49 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
|
50 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
51 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.FeatureTable; |
|
32 | 52 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.ConfigurableFeatureTableModel; |
53 |
import org.gvsig.i18n.Messages; |
|
54 |
import org.gvsig.tools.dispose.DisposableIterator; |
|
33 | 55 |
import org.gvsig.tools.exception.BaseException; |
34 | 56 |
|
35 | 57 |
/** |
36 |
* Ascending or descending order operations.
|
|
58 |
* Operations regarding order and duplicated values.
|
|
37 | 59 |
* |
38 | 60 |
* @author Vicente Caballero Navarro |
39 | 61 |
*/ |
40 | 62 |
public class TableFieldOperations extends Extension { |
41 | 63 |
|
64 |
private static Logger logger = |
|
65 |
LoggerFactory.getLogger(TableFieldOperations.class); |
|
42 | 66 |
private FeatureTableDocumentPanel table = null; |
43 | 67 |
|
44 | 68 |
/** |
... | ... | |
47 | 71 |
public void initialize() { |
48 | 72 |
IconThemeHelper.registerIcon("action", "table-order-desc", this); |
49 | 73 |
IconThemeHelper.registerIcon("action", "table-order-asc", this); |
74 |
IconThemeHelper.registerIcon("action", "selection-duplicates", this); |
|
50 | 75 |
} |
51 | 76 |
|
52 | 77 |
/** |
... | ... | |
54 | 79 |
*/ |
55 | 80 |
public void execute(String actionCommand) { |
56 | 81 |
doExecute(actionCommand, table); |
57 |
table.getModel().setModified(true); |
|
58 | 82 |
} |
59 | 83 |
|
60 | 84 |
/** |
... | ... | |
74 | 98 |
if ("table-order-asc".equals(actionCommand)) { |
75 | 99 |
cftm.orderByColumn(table.getTablePanel().getTable() |
76 | 100 |
.getSelectedColumnsAttributeDescriptor()[0].getName(), true); |
77 |
} else |
|
101 |
|
|
102 |
// Mark as modified: |
|
103 |
table.getModel().setModified(true); |
|
104 |
|
|
105 |
} else { |
|
78 | 106 |
if ("table-order-desc".equals(actionCommand)) { |
79 | 107 |
cftm.orderByColumn(table.getTablePanel().getTable() |
80 | 108 |
.getSelectedColumnsAttributeDescriptor()[0].getName(), |
81 | 109 |
false); |
110 |
|
|
111 |
// Mark as modified: |
|
112 |
table.getModel().setModified(true); |
|
113 |
|
|
114 |
} else { |
|
115 |
if ("selection-duplicates".equals(actionCommand)) { |
|
116 |
|
|
117 |
// Find and select duplicates in selected column |
|
118 |
long[] valCases = findSelectDuplicates(table); |
|
119 |
/* |
|
120 |
* Show info dialog |
|
121 |
*/ |
|
122 |
String msg = ""; |
|
123 |
if (valCases[0] == 0) { |
|
124 |
// No repetitions |
|
125 |
msg = Messages.getText("_No_repetitions_found"); |
|
126 |
} else { |
|
127 |
String[] args = new String[2]; |
|
128 |
args[0] = Long.toString(valCases[0]); |
|
129 |
args[1] = Long.toString(valCases[1]); |
|
130 |
msg = Messages.getText( |
|
131 |
"_Found_N_diff_repeated_vals_in_total_N_cases", args); |
|
132 |
} |
|
133 |
JOptionPane.showMessageDialog( |
|
134 |
ApplicationLocator.getManager().getRootComponent(), |
|
135 |
msg, |
|
136 |
Messages.getText("_Find_and_select_duplicates"), |
|
137 |
JOptionPane.INFORMATION_MESSAGE); |
|
138 |
} |
|
82 | 139 |
} |
140 |
} |
|
83 | 141 |
} catch (BaseException e) { |
84 |
e.printStackTrace();
|
|
142 |
logger.error("While applying field operation (" + actionCommand + ")", e);
|
|
85 | 143 |
} |
86 | 144 |
} |
87 | 145 |
|
146 |
|
|
88 | 147 |
/** |
148 |
* Detects repeated values |
|
149 |
* |
|
150 |
* @param tpanel |
|
151 |
* @return Returns two values: numbers of values repeated and number of rows |
|
152 |
* with values which are repeated |
|
153 |
* |
|
154 |
* @throws DataException |
|
155 |
*/ |
|
156 |
private long[] findSelectDuplicates(FeatureTableDocumentPanel tpanel) |
|
157 |
throws DataException { |
|
158 |
|
|
159 |
long[] values_cases = {0, 0}; |
|
160 |
|
|
161 |
ConfigurableFeatureTableModel cftm = tpanel.getTablePanel().getTableModel(); |
|
162 |
FeatureTable ftable = tpanel.getTablePanel().getTable(); |
|
163 |
|
|
164 |
// There is only one selected column |
|
165 |
String fieldName = ftable.getSelectedColumnsAttributeDescriptor()[0].getName(); |
|
166 |
FeatureStore fstore = cftm.getFeatureStore(); |
|
167 |
|
|
168 |
Set<Feature> selFeats = new HashSet<Feature>(); |
|
169 |
|
|
170 |
FeatureQuery fquery = fstore.createFeatureQuery(); |
|
171 |
/* |
|
172 |
* Create order based on selected column |
|
173 |
* (ascending but descending would be ok too) |
|
174 |
*/ |
|
175 |
FeatureQueryOrder fqo = new FeatureQueryOrder(); |
|
176 |
fqo.add(fieldName, true); |
|
177 |
fquery.setOrder(fqo); |
|
178 |
FeatureSet fset = fstore.getFeatureSet(fquery); |
|
179 |
DisposableIterator iter = fset.fastIterator(); |
|
180 |
Feature currFeat = null; |
|
181 |
Feature prevFeat = null; |
|
182 |
|
|
183 |
Object curVal = null; |
|
184 |
Object preVal = null; |
|
185 |
|
|
186 |
/* |
|
187 |
* Get first feature and go next if there is next |
|
188 |
*/ |
|
189 |
if (iter.hasNext()) { |
|
190 |
currFeat = (Feature) iter.next(); |
|
191 |
currFeat = currFeat.getCopy(); |
|
192 |
curVal = currFeat.get(fieldName); |
|
193 |
} else { |
|
194 |
iter.dispose(); |
|
195 |
return values_cases; |
|
196 |
} |
|
197 |
|
|
198 |
boolean equals = false; |
|
199 |
/* |
|
200 |
* Iterate and get copy of features with repeated value |
|
201 |
*/ |
|
202 |
while (iter.hasNext()) { |
|
203 |
prevFeat = currFeat; |
|
204 |
currFeat = (Feature) iter.next(); |
|
205 |
currFeat = currFeat.getCopy(); |
|
206 |
|
|
207 |
preVal = curVal; |
|
208 |
curVal = currFeat.get(fieldName); |
|
209 |
|
|
210 |
if (sameValue(preVal, curVal)) { |
|
211 |
if (!equals) { |
|
212 |
// We are entering a new set of repetitions |
|
213 |
values_cases[0]++; |
|
214 |
} |
|
215 |
equals = true; |
|
216 |
selFeats.add(prevFeat); |
|
217 |
} else { |
|
218 |
if (equals) { |
|
219 |
// We have gone out of equal group |
|
220 |
selFeats.add(prevFeat); |
|
221 |
} |
|
222 |
equals = false; |
|
223 |
} |
|
224 |
} |
|
225 |
iter.dispose(); |
|
226 |
FeatureSelection fsele = (FeatureSelection) fstore.getSelection(); |
|
227 |
fsele.beginComplexNotification(); |
|
228 |
/* |
|
229 |
* Set selection inside complex notification to prevent |
|
230 |
* lots of notifications |
|
231 |
*/ |
|
232 |
// ============================ |
|
233 |
fsele.deselectAll(); |
|
234 |
|
|
235 |
values_cases[1] = selFeats.size(); |
|
236 |
if (values_cases[1] > 0) { |
|
237 |
Iterator<Feature> fiter = selFeats.iterator(); |
|
238 |
while (fiter.hasNext()) { |
|
239 |
fsele.select(fiter.next()); |
|
240 |
} |
|
241 |
} |
|
242 |
// ============================ |
|
243 |
fsele.endComplexNotification(); |
|
244 |
return values_cases; |
|
245 |
} |
|
246 |
|
|
247 |
|
|
248 |
private boolean sameValue(Object v1, Object v2) { |
|
249 |
|
|
250 |
if (v1 == null) { |
|
251 |
if (v2 == null) { |
|
252 |
// Both null, same value |
|
253 |
return true; |
|
254 |
} else { |
|
255 |
// Only one is null, not same value |
|
256 |
return false; |
|
257 |
} |
|
258 |
} else { |
|
259 |
if (v2 == null) { |
|
260 |
// Only one is null, not same value |
|
261 |
return false; |
|
262 |
} else { |
|
263 |
// Delegate in equals method |
|
264 |
return v1.equals(v2); |
|
265 |
} |
|
266 |
} |
|
267 |
} |
|
268 |
|
|
269 |
/** |
|
89 | 270 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled() |
90 | 271 |
*/ |
91 | 272 |
public boolean isEnabled() { |
92 | 273 |
try { |
93 | 274 |
return (table.getTablePanel().getTable().getSelectedColumnCount() == 1); |
94 | 275 |
} catch (DataException e) { |
95 |
e.printStackTrace();
|
|
276 |
logger.error("While getting number of selected rows.", e);
|
|
96 | 277 |
} |
97 | 278 |
return false; |
98 | 279 |
} |
Also available in: Unified diff