Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableNumericFieldOperations.java @ 10661

History | View | Annotate | Download (5.08 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.driver.exceptions.ReadDriverException;
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 (ReadDriverException e) {
60
                        e.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
                                if (ds.getFieldValue(i, fieldIndex) instanceof NullValue)
76
                                        continue;
77
                                numRows++;
78
                                double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
79
                                suma = suma.add(new BigDecimal(d));
80
                                if (d < min.doubleValue()) min = new BigDecimal(d);
81
                                if (d > max.doubleValue()) max = new BigDecimal(d);
82
                        }
83
                        Statistics st = new Statistics();
84
                        BigDecimal media = new BigDecimal(0);
85
                        BigDecimal varianza = new BigDecimal(0);
86
                        double desviacion = 0;
87
                        if (numRows==0) {
88
                                suma = new BigDecimal(0);
89
                                min = new BigDecimal(0);
90
                                max = new BigDecimal(0);
91
                        }else {
92
                                media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
93
                                varianza = new BigDecimal(0);
94
                                for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
95
                                        if (ds.getFieldValue(i, fieldIndex) instanceof NullValue)
96
                                                continue;
97
                                        double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
98
                                        BigDecimal dif = new BigDecimal(d).subtract(media);
99
                                        varianza = dif.multiply(dif).add(varianza);
100
                                }
101
                                varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
102
                                desviacion = Math.sqrt(varianza.doubleValue());
103
                        }
104

    
105
                        st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
106
                        PluginServices.getMDIManager().addWindow(st);
107

    
108
                } catch (ReadDriverException e) {
109
                        NotificationManager.addError("No se pudo acceder a los datos", e);
110
                }
111
        }
112

    
113
        /**
114
         * @see com.iver.andami.plugins.IExtension#isEnabled()
115
         */
116
        public boolean isEnabled() {
117
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
118

    
119
                if (v == null) {
120
                        return false;
121
                }
122

    
123
                if (v instanceof Table) {
124
                        Table table = (Table) v;
125
                        return doIsEnabled(table);
126
                }
127
                return false;
128
        }
129

    
130
        protected boolean doIsEnabled(Table table){
131
                BitSet indices = table.getSelectedFieldIndices();
132
                // System.out.println("TableNumericFieldOperations.isEnabled: Tabla: " + table.getModel().getModelo().getRecordset().getName() );
133
                if (indices.cardinality() == 1){
134
                        try {
135
                                int index=indices.nextSetBit(0);
136
                                if (table.getModel().getModelo().getRecordset().getFieldCount()<index+1)
137
                                        return false;
138
                                int type = table.getModel().getModelo().getRecordset().getFieldType(index);
139
                                if ((type == Types.BIGINT) ||
140
                                                (type == Types.DECIMAL) ||
141
                                                (type == Types.DOUBLE) ||
142
                                                (type == Types.FLOAT) ||
143
                                                (type == Types.INTEGER) ||
144
                                                (type == Types.SMALLINT) ||
145
                                                (type == Types.TINYINT) ||
146
                                                (type == Types.REAL) ||
147
                                                (type == Types.NUMERIC)){
148
                                        return true;
149
                                }
150
                        } catch (ReadDriverException e) {
151
                                return false;
152
                        }
153
                }
154
                return false;
155
        }
156

    
157

    
158
        /**
159
         * @see com.iver.andami.plugins.IExtension#isVisible()
160
         */
161
        public boolean isVisible() {
162
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
163

    
164
                if (v == null) {
165
                        return false;
166
                }
167

    
168
                if (v instanceof Table) {
169
                        return true;
170
                }
171
                return false;
172
        }
173

    
174
}