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