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