Statistics
| Revision:

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

History | View | Annotate | Download (4.53 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 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
8 2241 fernando
import com.iver.andami.PluginServices;
9
import com.iver.andami.messages.NotificationManager;
10
import com.iver.andami.plugins.Extension;
11 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
12 2241 fernando
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
13 26899 vcaballero
import com.iver.cit.gvsig.fmap.selection.SelectionFieldIterator;
14
import com.iver.cit.gvsig.project.documents.table.Statistics.NonNumericFieldException;
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 14821 jmvivo
                registerIcons();
28 4682 jorpiell
        }
29 2241 fernando
30 14821 jmvivo
        private void registerIcons(){
31 15647 jmvivo
            PluginServices.getIconTheme().registerDefault(
32 14821 jmvivo
                                "table-statistics",
33
                                this.getClass().getClassLoader().getResource("images/statistics.png")
34
                        );
35
        }
36
37 4682 jorpiell
        /**
38 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
39 4682 jorpiell
         */
40
        public void execute(String actionCommand) {
41 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
42 2241 fernando
43
                if (v != null) {
44
                        if (v.getClass() == Table.class) {
45 4682 jorpiell
                                Table table = (Table) v;
46
                                doExecute(table);
47
                        }
48
                }
49
        }
50 26899 vcaballero
51 4682 jorpiell
        /**
52
         * "execute" method acction
53
         * @param actionCommand
54
         * The acction command that executes this method
55
         * @param table
56
         * Table to operate
57
         */
58 3940 caballero
59 4682 jorpiell
        protected void doExecute(Table table){
60
                int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0);
61
                try {
62 26899 vcaballero
                        SelectableDataSource ds = table.getModel().getModelo().getRecordset();
63
                        BitSet selectedRows = (BitSet)ds.getSelection().clone();
64
                        // If selection is empty, calculate on the full datasource
65 4682 jorpiell
                        if (selectedRows.cardinality() == 0){
66 26899 vcaballero
                                selectedRows.set(0, (int) ds.getRowCount());
67 4682 jorpiell
                        }
68 26899 vcaballero
                        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 9000 caballero
                        Statistics st = new Statistics();
78 26899 vcaballero
                        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 6880 cesar
                        PluginServices.getMDIManager().addWindow(st);
87 10626 caballero
                } catch (ReadDriverException e) {
88 26899 vcaballero
                        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 2241 fernando
                }
93 4682 jorpiell
        }
94 26899 vcaballero
95 2241 fernando
96 4682 jorpiell
        /**
97 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
98 4682 jorpiell
         */
99
        public boolean isEnabled() {
100 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
101 4682 jorpiell
102 2241 fernando
                if (v == null) {
103
                        return false;
104
                }
105
106 6399 fjp
                if (v instanceof Table) {
107 4682 jorpiell
                        Table table = (Table) v;
108
                        return doIsEnabled(table);
109
                }
110
                return false;
111
        }
112 3940 caballero
113 4682 jorpiell
        protected boolean doIsEnabled(Table table){
114 10661 caballero
                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 4682 jorpiell
                                        return false;
121 10661 caballero
                                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 4682 jorpiell
                                }
133 10661 caballero
                        } catch (ReadDriverException e) {
134
                                return false;
135 4682 jorpiell
                        }
136 2241 fernando
                }
137
                return false;
138 4682 jorpiell
        }
139 2241 fernando
140 4682 jorpiell
141
        /**
142 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isVisible()
143 4682 jorpiell
         */
144
        public boolean isVisible() {
145 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
146 2241 fernando
147
                if (v == null) {
148
                        return false;
149
                }
150
151 5900 jorpiell
                if (v instanceof Table) {
152 4682 jorpiell
                        return true;
153 2241 fernando
                }
154
                return false;
155 4682 jorpiell
        }
156 2241 fernando
157
}