root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / TableNumericFieldOperations.java @ 30011
History | View | Annotate | Download (8.55 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.math.BigDecimal; |
4 |
|
5 |
import org.gvsig.andami.PluginServices; |
6 |
import org.gvsig.andami.messages.NotificationManager; |
7 |
import org.gvsig.andami.plugins.Extension; |
8 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
9 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
10 |
import org.gvsig.app.project.documents.table.gui.Statistics; |
11 |
import org.gvsig.fmap.dal.DataTypes; |
12 |
import org.gvsig.fmap.dal.exception.DataException; |
13 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
14 |
import org.gvsig.fmap.dal.feature.Feature; |
15 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
16 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
17 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
18 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
19 |
|
20 |
/**
|
21 |
* @author Fernando Gonz?lez Cort?s
|
22 |
*/
|
23 |
public class TableNumericFieldOperations extends Extension{ |
24 |
private FeatureTableDocumentPanel table;
|
25 |
|
26 |
|
27 |
/**
|
28 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
29 |
*/
|
30 |
public void initialize() { |
31 |
registerIcons(); |
32 |
} |
33 |
|
34 |
private void registerIcons(){ |
35 |
PluginServices.getIconTheme().registerDefault( |
36 |
"table-statistics",
|
37 |
this.getClass().getClassLoader().getResource("images/statistics.png") |
38 |
); |
39 |
} |
40 |
|
41 |
/**
|
42 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
43 |
*/
|
44 |
public void execute(String actionCommand) { |
45 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
46 |
|
47 |
if (v != null) { |
48 |
if (v.getClass() == FeatureTableDocumentPanel.class) {
|
49 |
|
50 |
FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) v; |
51 |
|
52 |
doExecute(table); |
53 |
} |
54 |
} |
55 |
} |
56 |
/**
|
57 |
* "execute" method acction
|
58 |
* @param actionCommand
|
59 |
* The acction command that executes this method
|
60 |
* @param table
|
61 |
* Table to operate
|
62 |
*/
|
63 |
|
64 |
protected void doExecute(FeatureTableDocumentPanel table){ |
65 |
FeatureSet fCollection = null;
|
66 |
FeatureSet fCollec = null;
|
67 |
try{
|
68 |
FeatureAttributeDescriptor fad=table.getTablePanel().getTable().getSelectedColumnsAttributeDescriptor()[0];
|
69 |
int numRows=0; |
70 |
FeatureStore fs=null;
|
71 |
fs = table.getModel().getStore(); |
72 |
|
73 |
fCollection = (FeatureSet) fs.getSelection(); |
74 |
|
75 |
BigDecimal suma = new BigDecimal(0); |
76 |
BigDecimal min = new BigDecimal(Double.MAX_VALUE); |
77 |
BigDecimal max = new BigDecimal(-Double.MAX_VALUE); |
78 |
DisposableIterator iterator = null;
|
79 |
if (!fCollection.isEmpty()){
|
80 |
try {
|
81 |
iterator = fCollection.iterator(); |
82 |
while (iterator.hasNext()) {
|
83 |
Feature feature = (Feature) iterator.next(); |
84 |
if (feature.get(fad.getName()) == null) { |
85 |
continue;
|
86 |
} |
87 |
Number number = (Number) feature.get(fad.getName()); |
88 |
double d = number.doubleValue();
|
89 |
numRows++; |
90 |
suma = suma.add(new BigDecimal(d)); |
91 |
if (d < min.doubleValue()) {
|
92 |
min = new BigDecimal(d); |
93 |
} |
94 |
if (d > max.doubleValue()) {
|
95 |
max = new BigDecimal(d); |
96 |
} |
97 |
} |
98 |
} finally {
|
99 |
if (iterator != null) { |
100 |
iterator.dispose(); |
101 |
} |
102 |
} |
103 |
}else{
|
104 |
|
105 |
try {
|
106 |
FeatureQuery fq = fs.createFeatureQuery(); |
107 |
fq.setAttributeNames(new String[]{fad.getName()}); |
108 |
fCollec = fs.getFeatureSet(fq); |
109 |
|
110 |
iterator = fCollec.iterator(); |
111 |
while (iterator.hasNext()) {
|
112 |
Feature feature = (Feature) iterator.next(); |
113 |
// if (feature.get(fad.getName()) instanceof NullValue) {
|
114 |
if (feature.get(fad.getName()) == null) { |
115 |
continue;
|
116 |
} |
117 |
Number number = (Number) feature.get(fad.getName()); |
118 |
double d = number.doubleValue();
|
119 |
numRows++; |
120 |
suma = suma.add(new BigDecimal(d)); |
121 |
if (d < min.doubleValue()) {
|
122 |
min = new BigDecimal(d); |
123 |
} |
124 |
if (d > max.doubleValue()) {
|
125 |
max = new BigDecimal(d); |
126 |
} |
127 |
} |
128 |
} catch (DataException e) {
|
129 |
// TODO Auto-generated catch block
|
130 |
NotificationManager.addError(e); |
131 |
return;
|
132 |
} finally {
|
133 |
if (iterator != null) { |
134 |
iterator.dispose(); |
135 |
} |
136 |
if (fCollec != null) { |
137 |
fCollec.dispose(); |
138 |
} |
139 |
} |
140 |
} |
141 |
Statistics st = new Statistics();
|
142 |
BigDecimal media = new BigDecimal(0); |
143 |
BigDecimal varianza = new BigDecimal(0); |
144 |
double desviacion = 0; |
145 |
if (numRows==0) { |
146 |
suma = new BigDecimal(0); |
147 |
min = new BigDecimal(0); |
148 |
max = new BigDecimal(0); |
149 |
}else {
|
150 |
media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN); |
151 |
varianza = new BigDecimal(0); |
152 |
if (!fCollection.isEmpty()){
|
153 |
iterator=fCollection.iterator(); |
154 |
}else{
|
155 |
iterator=fCollec.iterator(); |
156 |
} |
157 |
try {
|
158 |
while (iterator.hasNext()) {
|
159 |
Feature feature = (Feature) iterator.next(); |
160 |
// if (feature.get(fad.getName()) instanceof NullValue) {
|
161 |
if (feature.get(fad.getName()) == null) { |
162 |
continue;
|
163 |
} |
164 |
Number number = (Number) feature.get(fad.getName()); |
165 |
double d = number.doubleValue();
|
166 |
|
167 |
BigDecimal dif = new BigDecimal(d).subtract(media); |
168 |
varianza = dif.multiply(dif).add(varianza); |
169 |
} |
170 |
} finally {
|
171 |
if (iterator != null) { |
172 |
iterator.dispose(); |
173 |
} |
174 |
} |
175 |
varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN); |
176 |
desviacion = Math.sqrt(varianza.doubleValue());
|
177 |
} |
178 |
st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue()); |
179 |
PluginServices.getMDIManager().addWindow(st); |
180 |
}catch (DataException e) {
|
181 |
NotificationManager.addError(e); |
182 |
} finally{
|
183 |
if (fCollection != null){ |
184 |
fCollection.dispose(); |
185 |
} |
186 |
} |
187 |
// }else{
|
188 |
|
189 |
// }
|
190 |
|
191 |
// BitSet selectedRows = (BitSet)ds.getSelection().clone();
|
192 |
// try {
|
193 |
// int rc = (int) ds.getRowCount();
|
194 |
// //Si no hay selecci?n se usan todos los registros
|
195 |
// if (selectedRows.cardinality() == 0){
|
196 |
// selectedRows.set(0, rc);
|
197 |
// }else{
|
198 |
// rc = selectedRows.cardinality();
|
199 |
// }
|
200 |
// BigDecimal suma = new BigDecimal(0);
|
201 |
// BigDecimal min = new BigDecimal(Double.MAX_VALUE);
|
202 |
// BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
|
203 |
// for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
|
204 |
// if (fs.getFieldValue(i, fieldIndex) instanceof NullValue)
|
205 |
// continue;
|
206 |
// numRows++;
|
207 |
// double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
|
208 |
// suma = suma.add(new BigDecimal(d));
|
209 |
// if (d < min.doubleValue()) min = new BigDecimal(d);
|
210 |
// if (d > max.doubleValue()) max = new BigDecimal(d);
|
211 |
// }
|
212 |
// Statistics st = new Statistics();
|
213 |
// BigDecimal media = new BigDecimal(0);
|
214 |
// BigDecimal varianza = new BigDecimal(0);
|
215 |
// double desviacion = 0;
|
216 |
// if (numRows==0) {
|
217 |
// suma = new BigDecimal(0);
|
218 |
// min = new BigDecimal(0);
|
219 |
// max = new BigDecimal(0);
|
220 |
// }else {
|
221 |
// media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
|
222 |
// varianza = new BigDecimal(0);
|
223 |
// for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
|
224 |
// if (ds.getFieldValue(i, fieldIndex) instanceof NullValue)
|
225 |
// continue;
|
226 |
// double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
|
227 |
// BigDecimal dif = new BigDecimal(d).subtract(media);
|
228 |
// varianza = dif.multiply(dif).add(varianza);
|
229 |
// }
|
230 |
// varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
|
231 |
// desviacion = Math.sqrt(varianza.doubleValue());
|
232 |
// }
|
233 |
|
234 |
// st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
|
235 |
// PluginServices.getMDIManager().addWindow(st);
|
236 |
|
237 |
// } catch (ReadDriverException e) {
|
238 |
// NotificationManager.addError("No se pudo acceder a los datos", e);
|
239 |
// }
|
240 |
} |
241 |
|
242 |
/**
|
243 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
244 |
*/
|
245 |
public boolean isEnabled() { |
246 |
return doIsEnabled(table);
|
247 |
} |
248 |
|
249 |
protected boolean doIsEnabled(FeatureTableDocumentPanel table){ |
250 |
// FIXME
|
251 |
FeatureAttributeDescriptor[] fads=null; |
252 |
try {
|
253 |
fads = table.getTablePanel().getTable().getSelectedColumnsAttributeDescriptor(); |
254 |
} catch (DataException e) {
|
255 |
e.printStackTrace(); |
256 |
} |
257 |
|
258 |
if (fads.length == 1){ |
259 |
// int index=indices.nextSetBit(0);
|
260 |
// if (table.getModel().getStore().getDefaultFeatureType().size()<index+1) {
|
261 |
// return false;
|
262 |
// }
|
263 |
int type = fads[0].getDataType(); |
264 |
if ((type==DataTypes.LONG) ||
|
265 |
(type== DataTypes.DOUBLE) || |
266 |
(type==DataTypes.FLOAT) || |
267 |
(type==DataTypes.INT)){ |
268 |
return true; |
269 |
} |
270 |
} |
271 |
return false; |
272 |
} |
273 |
|
274 |
|
275 |
/**
|
276 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
277 |
*/
|
278 |
public boolean isVisible() { |
279 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
280 |
if (v!=null && v instanceof FeatureTableDocumentPanel) { |
281 |
table=(FeatureTableDocumentPanel)v; |
282 |
return true; |
283 |
} |
284 |
return false; |
285 |
} |
286 |
|
287 |
} |