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