root / tags / v1_0_2_Build_914 / applications / appgvSIG / src / com / iver / cit / gvsig / TableNumericFieldOperations.java @ 12200
History | View | Annotate | Download (5.25 KB)
1 |
package com.iver.cit.gvsig; |
---|---|
2 |
|
3 |
import java.math.BigDecimal; |
4 |
import java.sql.Types; |
5 |
import java.util.BitSet; |
6 |
|
7 |
import com.hardcode.driverManager.DriverLoadException; |
8 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
9 |
import com.hardcode.gdbms.engine.values.NullValue; |
10 |
import com.hardcode.gdbms.engine.values.NumericValue; |
11 |
import com.iver.andami.PluginServices; |
12 |
import com.iver.andami.messages.NotificationManager; |
13 |
import com.iver.andami.plugins.Extension; |
14 |
import com.iver.andami.ui.mdiManager.IWindow; |
15 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
16 |
import com.iver.cit.gvsig.project.documents.table.gui.Statistics; |
17 |
import com.iver.cit.gvsig.project.documents.table.gui.Table; |
18 |
|
19 |
/**
|
20 |
* @author Fernando Gonz?lez Cort?s
|
21 |
*/
|
22 |
public class TableNumericFieldOperations extends Extension{ |
23 |
|
24 |
/**
|
25 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
26 |
*/
|
27 |
public void initialize() { |
28 |
} |
29 |
|
30 |
/**
|
31 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
32 |
*/
|
33 |
public void execute(String actionCommand) { |
34 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
35 |
|
36 |
if (v != null) { |
37 |
if (v.getClass() == Table.class) {
|
38 |
|
39 |
Table table = (Table) v; |
40 |
|
41 |
doExecute(table); |
42 |
} |
43 |
} |
44 |
} |
45 |
/**
|
46 |
* "execute" method acction
|
47 |
* @param actionCommand
|
48 |
* The acction command that executes this method
|
49 |
* @param table
|
50 |
* Table to operate
|
51 |
*/
|
52 |
|
53 |
protected void doExecute(Table table){ |
54 |
int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0); |
55 |
int numRows=0; |
56 |
SelectableDataSource ds=null;
|
57 |
try {
|
58 |
ds = table.getModel().getModelo().getRecordset(); |
59 |
} catch (DriverLoadException e1) {
|
60 |
// TODO Auto-generated catch block
|
61 |
e1.printStackTrace(); |
62 |
} |
63 |
BitSet selectedRows = (BitSet)ds.getSelection().clone(); |
64 |
try {
|
65 |
int rc = (int) ds.getRowCount(); |
66 |
//Si no hay selecci?n se usan todos los registros
|
67 |
if (selectedRows.cardinality() == 0){ |
68 |
selectedRows.set(0, rc);
|
69 |
}else{
|
70 |
rc = selectedRows.cardinality(); |
71 |
} |
72 |
BigDecimal suma = new BigDecimal(0); |
73 |
BigDecimal min = new BigDecimal(Double.MAX_VALUE); |
74 |
BigDecimal max = new BigDecimal(Double.NEGATIVE_INFINITY); |
75 |
for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) { |
76 |
if (ds.getFieldValue(i, fieldIndex) instanceof NullValue) |
77 |
continue;
|
78 |
numRows++; |
79 |
double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
|
80 |
suma = suma.add(new BigDecimal(d)); |
81 |
if (d < min.doubleValue()) min = new BigDecimal(d); |
82 |
if (d > max.doubleValue()) max = new BigDecimal(d); |
83 |
} |
84 |
Statistics st = new Statistics();
|
85 |
BigDecimal media = new BigDecimal(0); |
86 |
BigDecimal varianza = new BigDecimal(0); |
87 |
double desviacion = 0; |
88 |
if (numRows==0) { |
89 |
suma = new BigDecimal(0); |
90 |
min = new BigDecimal(0); |
91 |
max = new BigDecimal(0); |
92 |
}else {
|
93 |
media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN); |
94 |
varianza = new BigDecimal(0); |
95 |
for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) { |
96 |
if (ds.getFieldValue(i, fieldIndex) instanceof NullValue) |
97 |
continue;
|
98 |
double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
|
99 |
BigDecimal dif = new BigDecimal(d).subtract(media); |
100 |
varianza = dif.multiply(dif).add(varianza); |
101 |
} |
102 |
varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN); |
103 |
desviacion = Math.sqrt(varianza.doubleValue());
|
104 |
} |
105 |
|
106 |
st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue()); |
107 |
PluginServices.getMDIManager().addWindow(st); |
108 |
|
109 |
} catch (DriverException e) {
|
110 |
NotificationManager.addError("No se pudo acceder a los datos", e);
|
111 |
} |
112 |
} |
113 |
|
114 |
/**
|
115 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
116 |
*/
|
117 |
public boolean isEnabled() { |
118 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
119 |
|
120 |
if (v == null) { |
121 |
return false; |
122 |
} |
123 |
|
124 |
if (v instanceof Table) { |
125 |
Table table = (Table) v; |
126 |
return doIsEnabled(table);
|
127 |
} |
128 |
return false; |
129 |
} |
130 |
|
131 |
protected boolean doIsEnabled(Table table){ |
132 |
try{
|
133 |
BitSet indices = table.getSelectedFieldIndices();
|
134 |
|
135 |
// System.out.println("TableNumericFieldOperations.isEnabled: Tabla: " + table.getModel().getModelo().getRecordset().getName() );
|
136 |
|
137 |
if (indices.cardinality() == 1){ |
138 |
try {
|
139 |
int index=indices.nextSetBit(0); |
140 |
if (table.getModel().getModelo().getRecordset().getFieldCount()<index+1) |
141 |
return false; |
142 |
int type = table.getModel().getModelo().getRecordset().getFieldType(index);
|
143 |
if ((type == Types.BIGINT) || |
144 |
(type == Types.DECIMAL) ||
|
145 |
(type == Types.DOUBLE) ||
|
146 |
(type == Types.FLOAT) ||
|
147 |
(type == Types.INTEGER) ||
|
148 |
(type == Types.SMALLINT) ||
|
149 |
(type == Types.TINYINT) ||
|
150 |
(type == Types.REAL) ||
|
151 |
(type == Types.NUMERIC)){
|
152 |
return true; |
153 |
} |
154 |
} catch (DriverException e) {
|
155 |
return false; |
156 |
} |
157 |
} |
158 |
} catch (DriverLoadException e1) {
|
159 |
// TODO Auto-generated catch block
|
160 |
e1.printStackTrace(); |
161 |
} |
162 |
return false; |
163 |
} |
164 |
|
165 |
|
166 |
/**
|
167 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
168 |
*/
|
169 |
public boolean isVisible() { |
170 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
171 |
|
172 |
if (v == null) { |
173 |
return false; |
174 |
} |
175 |
|
176 |
if (v instanceof Table) { |
177 |
return true; |
178 |
} |
179 |
return false; |
180 |
} |
181 |
|
182 |
} |