Statistics
| Revision:

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

History | View | Annotate | Download (4.53 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.driver.exceptions.ReadDriverException;
8
import com.iver.andami.PluginServices;
9
import com.iver.andami.messages.NotificationManager;
10
import com.iver.andami.plugins.Extension;
11
import com.iver.andami.ui.mdiManager.IWindow;
12
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
13
import com.iver.cit.gvsig.fmap.selection.SelectionFieldIterator;
14
import com.iver.cit.gvsig.project.documents.table.Statistics.NonNumericFieldException;
15
import com.iver.cit.gvsig.project.documents.table.gui.Statistics;
16
import com.iver.cit.gvsig.project.documents.table.gui.Table;
17

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

    
23
        /**
24
         * @see com.iver.andami.plugins.IExtension#initialize()
25
         */
26
        public void initialize() {
27
                registerIcons();
28
        }
29

    
30
        private void registerIcons(){
31
            PluginServices.getIconTheme().registerDefault(
32
                                "table-statistics",
33
                                this.getClass().getClassLoader().getResource("images/statistics.png")
34
                        );
35
        }
36

    
37
        /**
38
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
39
         */
40
        public void execute(String actionCommand) {
41
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
42

    
43
                if (v != null) {
44
                        if (v.getClass() == Table.class) {
45
                                Table table = (Table) v;
46
                                doExecute(table);
47
                        }
48
                }
49
        }
50
        
51
        /**
52
         * "execute" method acction
53
         * @param actionCommand
54
         * The acction command that executes this method
55
         * @param table
56
         * Table to operate
57
         */
58

    
59
        protected void doExecute(Table table){
60
                int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0);
61
                try {
62
                        SelectableDataSource ds = table.getModel().getModelo().getRecordset();
63
                        BitSet selectedRows = (BitSet)ds.getSelection().clone();
64
                        // If selection is empty, calculate on the full datasource
65
                        if (selectedRows.cardinality() == 0){
66
                                selectedRows.set(0, (int) ds.getRowCount());
67
                        }
68
                        SelectionFieldIterator iterator = new SelectionFieldIterator(ds, selectedRows, fieldIndex);
69
                        com.iver.cit.gvsig.project.documents.table.Statistics statsCalculator = new com.iver.cit.gvsig.project.documents.table.Statistics(iterator);
70
                        BigDecimal sum = statsCalculator.sum();
71
                        BigDecimal min = statsCalculator.min();
72
                        BigDecimal max = statsCalculator.max();
73
                        BigDecimal mean = statsCalculator.mean();
74
                        BigDecimal variance = statsCalculator.variance();
75
                        BigDecimal stdDeviation = statsCalculator.stdDeviation();
76
                        
77
                        Statistics st = new Statistics();
78
                        st.setStatistics(mean.doubleValue(),
79
                                        max.doubleValue(),
80
                                        min.doubleValue(),
81
                                        variance.doubleValue(),
82
                                        stdDeviation.doubleValue(),
83
                                        (int) statsCalculator.count(),
84
                                        new BigDecimal(max.doubleValue()).subtract(min).doubleValue(),
85
                                        sum.doubleValue());
86
                        PluginServices.getMDIManager().addWindow(st);
87
                } catch (ReadDriverException e) {
88
                        NotificationManager.showMessageError(
89
                                        PluginServices.getText(this, "Statistics__Error_accessing_the_data"), e);
90
                } catch (NonNumericFieldException e) {
91
                        NotificationManager.showMessageError("Statistics__Selected_field_is_not_numeric", e);
92
                }
93
        }
94
        
95

    
96
        /**
97
         * @see com.iver.andami.plugins.IExtension#isEnabled()
98
         */
99
        public boolean isEnabled() {
100
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
101

    
102
                if (v == null) {
103
                        return false;
104
                }
105

    
106
                if (v instanceof Table) {
107
                        Table table = (Table) v;
108
                        return doIsEnabled(table);
109
                }
110
                return false;
111
        }
112

    
113
        protected boolean doIsEnabled(Table table){
114
                BitSet indices = table.getSelectedFieldIndices();
115
                // System.out.println("TableNumericFieldOperations.isEnabled: Tabla: " + table.getModel().getModelo().getRecordset().getName() );
116
                if (indices.cardinality() == 1){
117
                        try {
118
                                int index=indices.nextSetBit(0);
119
                                if (table.getModel().getModelo().getRecordset().getFieldCount()<index+1)
120
                                        return false;
121
                                int type = table.getModel().getModelo().getRecordset().getFieldType(index);
122
                                if ((type == Types.BIGINT) ||
123
                                                (type == Types.DECIMAL) ||
124
                                                (type == Types.DOUBLE) ||
125
                                                (type == Types.FLOAT) ||
126
                                                (type == Types.INTEGER) ||
127
                                                (type == Types.SMALLINT) ||
128
                                                (type == Types.TINYINT) ||
129
                                                (type == Types.REAL) ||
130
                                                (type == Types.NUMERIC)){
131
                                        return true;
132
                                }
133
                        } catch (ReadDriverException e) {
134
                                return false;
135
                        }
136
                }
137
                return false;
138
        }
139

    
140

    
141
        /**
142
         * @see com.iver.andami.plugins.IExtension#isVisible()
143
         */
144
        public boolean isVisible() {
145
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
146

    
147
                if (v == null) {
148
                        return false;
149
                }
150

    
151
                if (v instanceof Table) {
152
                        return true;
153
                }
154
                return false;
155
        }
156

    
157
}