root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableNumericFieldOperations.java @ 9212
History | View | Annotate | Download (5.13 KB)
1 | 2241 | fernando | package com.iver.cit.gvsig; |
---|---|---|---|
2 | |||
3 | 2266 | fernando | import java.math.BigDecimal; |
4 | 2241 | fernando | import java.sql.Types; |
5 | import java.util.BitSet; |
||
6 | |||
7 | 3940 | caballero | import com.hardcode.driverManager.DriverLoadException; |
8 | 2241 | fernando | import com.hardcode.gdbms.engine.data.driver.DriverException; |
9 | 9000 | caballero | import com.hardcode.gdbms.engine.values.NullValue; |
10 | 2241 | fernando | 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 | 6877 | cesar | import com.iver.andami.ui.mdiManager.IWindow; |
15 | 2241 | fernando | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
16 | 7304 | caballero | import com.iver.cit.gvsig.project.documents.table.gui.Statistics; |
17 | import com.iver.cit.gvsig.project.documents.table.gui.Table; |
||
18 | 2241 | fernando | |
19 | /**
|
||
20 | * @author Fernando Gonz?lez Cort?s
|
||
21 | */
|
||
22 | 5005 | jorpiell | public class TableNumericFieldOperations extends Extension{ |
23 | 2241 | fernando | |
24 | 4682 | jorpiell | /**
|
25 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#initialize()
|
26 | 4682 | jorpiell | */
|
27 | 5005 | jorpiell | public void initialize() { |
28 | 4682 | jorpiell | } |
29 | 2241 | fernando | |
30 | 4682 | jorpiell | /**
|
31 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
32 | 4682 | jorpiell | */
|
33 | public void execute(String actionCommand) { |
||
34 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
35 | 2241 | fernando | |
36 | if (v != null) { |
||
37 | if (v.getClass() == Table.class) {
|
||
38 | 3940 | caballero | |
39 | 4682 | jorpiell | Table table = (Table) v; |
40 | 3940 | caballero | |
41 | 4682 | jorpiell | 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 | 3940 | caballero | |
53 | 4682 | jorpiell | protected void doExecute(Table table){ |
54 | int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0); |
||
55 | 9000 | caballero | int numRows=0; |
56 | 4682 | jorpiell | SelectableDataSource ds=null;
|
57 | try {
|
||
58 | 9000 | caballero | ds = table.getModel().getModelo().getRecordset(); |
59 | 4682 | jorpiell | } catch (DriverLoadException e1) {
|
60 | // TODO Auto-generated catch block
|
||
61 | e1.printStackTrace(); |
||
62 | } |
||
63 | 6170 | caballero | BitSet selectedRows = (BitSet)ds.getSelection().clone(); |
64 | 4682 | jorpiell | 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.MIN_VALUE); |
||
75 | for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) { |
||
76 | 9000 | caballero | if (ds.getFieldValue(i, fieldIndex) instanceof NullValue) |
77 | continue;
|
||
78 | numRows++; |
||
79 | 4682 | jorpiell | 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 | 9000 | caballero | Statistics st = new Statistics();
|
85 | BigDecimal media = new BigDecimal(0); |
||
86 | 4682 | jorpiell | BigDecimal varianza = new BigDecimal(0); |
87 | 9000 | caballero | 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 | 4682 | jorpiell | } |
105 | 3940 | caballero | |
106 | 9000 | caballero | st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue()); |
107 | 6880 | cesar | PluginServices.getMDIManager().addWindow(st); |
108 | 4682 | jorpiell | |
109 | } catch (DriverException e) {
|
||
110 | NotificationManager.addError("No se pudo acceder a los datos", e);
|
||
111 | 2241 | fernando | } |
112 | 4682 | jorpiell | } |
113 | 2241 | fernando | |
114 | 4682 | jorpiell | /**
|
115 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
116 | 4682 | jorpiell | */
|
117 | public boolean isEnabled() { |
||
118 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
119 | 4682 | jorpiell | |
120 | 2241 | fernando | if (v == null) { |
121 | return false; |
||
122 | } |
||
123 | |||
124 | 6399 | fjp | if (v instanceof Table) { |
125 | 4682 | jorpiell | Table table = (Table) v; |
126 | return doIsEnabled(table);
|
||
127 | } |
||
128 | return false; |
||
129 | } |
||
130 | 3940 | caballero | |
131 | 4682 | jorpiell | protected boolean doIsEnabled(Table table){ |
132 | try{
|
||
133 | BitSet indices = table.getSelectedFieldIndices();
|
||
134 | 3940 | caballero | |
135 | 6399 | fjp | // System.out.println("TableNumericFieldOperations.isEnabled: Tabla: " + table.getModel().getModelo().getRecordset().getName() );
|
136 | 4682 | jorpiell | |
137 | if (indices.cardinality() == 1){ |
||
138 | try {
|
||
139 | int type = table.getModel().getModelo().getRecordset().getFieldType(indices.nextSetBit(0)); |
||
140 | if ((type == Types.BIGINT) || |
||
141 | (type == Types.DECIMAL) ||
|
||
142 | (type == Types.DOUBLE) ||
|
||
143 | (type == Types.FLOAT) ||
|
||
144 | (type == Types.INTEGER) ||
|
||
145 | (type == Types.SMALLINT) ||
|
||
146 | (type == Types.TINYINT) ||
|
||
147 | (type == Types.REAL) ||
|
||
148 | (type == Types.NUMERIC)){
|
||
149 | return true; |
||
150 | } |
||
151 | } catch (DriverException e) {
|
||
152 | return false; |
||
153 | } |
||
154 | } |
||
155 | } catch (DriverLoadException e1) {
|
||
156 | // TODO Auto-generated catch block
|
||
157 | e1.printStackTrace(); |
||
158 | 2241 | fernando | } |
159 | return false; |
||
160 | 4682 | jorpiell | } |
161 | 2241 | fernando | |
162 | 4682 | jorpiell | |
163 | /**
|
||
164 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isVisible()
|
165 | 4682 | jorpiell | */
|
166 | public boolean isVisible() { |
||
167 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
168 | 2241 | fernando | |
169 | if (v == null) { |
||
170 | return false; |
||
171 | } |
||
172 | |||
173 | 5900 | jorpiell | if (v instanceof Table) { |
174 | 4682 | jorpiell | return true; |
175 | 2241 | fernando | } |
176 | return false; |
||
177 | 4682 | jorpiell | } |
178 | 2241 | fernando | |
179 | } |