Statistics
| Revision:

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

History | View | Annotate | Download (4.78 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.gdbms.engine.data.driver.DriverException;
8
import com.hardcode.gdbms.engine.values.NumericValue;
9
import com.iver.andami.PluginServices;
10
import com.iver.andami.messages.NotificationManager;
11
import com.iver.andami.plugins.Extension;
12
import com.iver.andami.ui.mdiManager.View;
13
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
14
import com.iver.cit.gvsig.gui.Table;
15
import com.iver.cit.gvsig.gui.tables.Statistics;
16

    
17
/**
18
 * @author Fernando Gonz?lez Cort?s
19
 */
20
public class TableNumericFieldOperations implements Extension{
21

    
22
    /**
23
     * @see com.iver.andami.plugins.Extension#inicializar()
24
     */
25
    public void inicializar() {
26
    }
27

    
28
    /**
29
     * @see com.iver.andami.plugins.Extension#execute(java.lang.String)
30
     */
31
    public void execute(String actionCommand) {
32
                View v = PluginServices.getMDIManager().getActiveView();
33

    
34
                if (v != null) {
35
                        if (v.getClass() == Table.class) {
36
                            
37
                            Table t = (Table) v;
38
                            
39
                            int fieldIndex = t.getSelectedFieldIndices().nextSetBit(0);
40
                            
41
                            SelectableDataSource ds = t.getModel().getModelo();
42
                            BitSet selectedRows = ds.getSelection();
43
                try {
44
                    int rc = (int) ds.getRowCount();
45
                    //Si no hay selecci?n se usan todos los registros
46
                                if (selectedRows.cardinality() == 0){
47
                                    selectedRows.set(0, rc);
48
                                }else{
49
                                    rc = selectedRows.cardinality();
50
                                }
51
                    BigDecimal suma = new BigDecimal(0);
52
                    BigDecimal min = new BigDecimal(Double.MAX_VALUE);
53
                    BigDecimal max = new BigDecimal(Double.MIN_VALUE);
54
                    for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
55
                            double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
56
                            suma = suma.add(new BigDecimal(d));
57
                            if (d < min.doubleValue()) min = new BigDecimal(d);
58
                            if (d > max.doubleValue()) max = new BigDecimal(d);
59
                        }
60
                        BigDecimal media = suma.divide(new BigDecimal(rc), BigDecimal.ROUND_HALF_DOWN);
61
                        BigDecimal varianza = new BigDecimal(0);
62
                    for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
63
                            double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
64
                            BigDecimal dif = new BigDecimal(d).subtract(media);
65
                            varianza = dif.multiply(dif).add(varianza);
66
                        }
67
                        varianza = varianza.divide(new BigDecimal(rc), BigDecimal.ROUND_HALF_DOWN);
68
                        double desviacion = Math.sqrt(varianza.doubleValue());
69

    
70
                        Statistics st = new Statistics();
71
                                    st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, rc, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
72
                                    PluginServices.getMDIManager().addView(st);
73
                                    
74
                } catch (DriverException e) {
75
                    NotificationManager.addError("No se pudo acceder a los datos", e);
76
                }
77
                        }
78
                }
79
    }
80

    
81
    /**
82
     * @see com.iver.andami.plugins.Extension#isEnabled()
83
     */
84
    public boolean isEnabled() {
85
                View v = PluginServices.getMDIManager().getActiveView();
86

    
87
                if (v == null) {
88
                        return false;
89
                }
90

    
91
                if (v.getClass() == Table.class) {
92
                    Table t = (Table) v;
93
                    BitSet indices = t.getSelectedFieldIndices();
94
            System.out.println("TableNumericFieldOperations.isEnabled: Tabla: " + t.getModel().getModelo().getName() );
95
                    if (indices.cardinality() == 1){
96
                        try {
97
                    int type = t.getModel().getModelo().getFieldType(indices.nextSetBit(0));
98
                    if ((type == Types.BIGINT) ||
99
                            (type == Types.DECIMAL) ||
100
                            (type == Types.DOUBLE) ||
101
                            (type == Types.FLOAT) ||
102
                            (type == Types.INTEGER) ||
103
                            (type == Types.SMALLINT) ||
104
                            (type == Types.TINYINT) ||
105
                            (type == Types.REAL) ||
106
                            (type == Types.NUMERIC)){
107
                        return true;
108
                    }
109
                } catch (DriverException e) {
110
                    return false;
111
                }
112
                    }
113
                }
114
                return false;
115
    }
116

    
117
    /**
118
     * @see com.iver.andami.plugins.Extension#isVisible()
119
     */
120
    public boolean isVisible() {
121
                View v = PluginServices.getMDIManager().getActiveView();
122

    
123
                if (v == null) {
124
                        return false;
125
                }
126

    
127
                if (v.getClass() == Table.class) {
128
                    return true;
129
                }
130
                return false;
131
    }
132

    
133
}