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 | 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 | 9001 | 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 | 8765 | jjdelcerro | 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 | 9001 | caballero | int numRows=0; |
56 | 4682 | jorpiell | SelectableDataSource ds=null;
|
57 | try {
|
||
58 | 9001 | 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 | 11226 | caballero | BigDecimal max = new BigDecimal(Double.NEGATIVE_INFINITY); |
75 | 4682 | jorpiell | for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) { |
76 | 9001 | 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 | 9001 | caballero | Statistics st = new Statistics();
|
85 | BigDecimal media = new BigDecimal(0); |
||
86 | 4682 | jorpiell | BigDecimal varianza = new BigDecimal(0); |
87 | 9001 | 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 | 9001 | 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 | 10452 | caballero | 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 | 4682 | jorpiell | 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 | 2241 | fernando | } |
162 | return false; |
||
163 | 4682 | jorpiell | } |
164 | 2241 | fernando | |
165 | 4682 | jorpiell | |
166 | /**
|
||
167 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isVisible()
|
168 | 4682 | jorpiell | */
|
169 | public boolean isVisible() { |
||
170 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
171 | 2241 | fernando | |
172 | if (v == null) { |
||
173 | return false; |
||
174 | } |
||
175 | |||
176 | 5900 | jorpiell | if (v instanceof Table) { |
177 | 4682 | jorpiell | return true; |
178 | 2241 | fernando | } |
179 | return false; |
||
180 | 4682 | jorpiell | } |
181 | 2241 | fernando | |
182 | } |