Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableNumericFieldOperations.java @ 7771

History | View | Annotate | Download (4.69 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
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 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
14 2241 fernando
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
15 7304 caballero
import com.iver.cit.gvsig.project.documents.table.gui.Statistics;
16
import com.iver.cit.gvsig.project.documents.table.gui.Table;
17 2241 fernando
18
/**
19
 * @author Fernando Gonz?lez Cort?s
20
 */
21 5005 jorpiell
public class TableNumericFieldOperations extends Extension{
22 2241 fernando
23 4682 jorpiell
        /**
24 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#initialize()
25 4682 jorpiell
         */
26 5005 jorpiell
        public void initialize() {
27 4682 jorpiell
        }
28 2241 fernando
29 4682 jorpiell
        /**
30 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
31 4682 jorpiell
         */
32
        public void execute(String actionCommand) {
33 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
34 2241 fernando
35
                if (v != null) {
36
                        if (v.getClass() == Table.class) {
37 3940 caballero
38 4682 jorpiell
                                Table table = (Table) v;
39 3940 caballero
40 4682 jorpiell
                                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 3940 caballero
52 4682 jorpiell
        protected void doExecute(Table table){
53
                int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0);
54 2241 fernando
55 4682 jorpiell
                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 6170 caballero
                BitSet selectedRows = (BitSet)ds.getSelection().clone();
63 4682 jorpiell
                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 3940 caballero
90 4682 jorpiell
                        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 6880 cesar
                        PluginServices.getMDIManager().addWindow(st);
93 4682 jorpiell
94
                } catch (DriverException e) {
95
                        NotificationManager.addError("No se pudo acceder a los datos", e);
96 2241 fernando
                }
97 4682 jorpiell
        }
98 2241 fernando
99 4682 jorpiell
        /**
100 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
101 4682 jorpiell
         */
102
        public boolean isEnabled() {
103 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
104 4682 jorpiell
105 2241 fernando
                if (v == null) {
106
                        return false;
107
                }
108
109 6399 fjp
                if (v instanceof Table) {
110 4682 jorpiell
                        Table table = (Table) v;
111
                        return doIsEnabled(table);
112
                }
113
                return false;
114
        }
115 3940 caballero
116 4682 jorpiell
        protected boolean doIsEnabled(Table table){
117
                try{
118
                        BitSet indices = table.getSelectedFieldIndices();
119 3940 caballero
120 6399 fjp
                        // System.out.println("TableNumericFieldOperations.isEnabled: Tabla: " + table.getModel().getModelo().getRecordset().getName() );
121 4682 jorpiell
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 2241 fernando
                }
144
                return false;
145 4682 jorpiell
        }
146 2241 fernando
147 4682 jorpiell
148
        /**
149 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isVisible()
150 4682 jorpiell
         */
151
        public boolean isVisible() {
152 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
153 2241 fernando
154
                if (v == null) {
155
                        return false;
156
                }
157
158 5900 jorpiell
                if (v instanceof Table) {
159 4682 jorpiell
                        return true;
160 2241 fernando
                }
161
                return false;
162 4682 jorpiell
        }
163 2241 fernando
164
}