Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / TableNumericFieldOperations.java @ 22932

History | View | Annotate | Download (8.14 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.math.BigDecimal;
4
import java.sql.Types;
5
import java.util.BitSet;
6
import java.util.Iterator;
7

    
8
import org.gvsig.fmap.data.ReadException;
9
import org.gvsig.fmap.data.feature.Feature;
10
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
11
import org.gvsig.fmap.data.feature.FeatureCollection;
12
import org.gvsig.fmap.data.feature.FeatureStore;
13
import org.gvsig.fmap.mapcontext.rendering.legend.NullValue;
14

    
15
import com.iver.andami.PluginServices;
16
import com.iver.andami.messages.NotificationManager;
17
import com.iver.andami.plugins.Extension;
18
import com.iver.andami.ui.mdiManager.IWindow;
19
import com.iver.cit.gvsig.project.documents.table.gui.Statistics;
20
import com.iver.cit.gvsig.project.documents.table.gui.Table;
21

    
22
/**
23
 * @author Fernando Gonz?lez Cort?s
24
 */
25
public class TableNumericFieldOperations extends Extension{
26

    
27
        /**
28
         * @see com.iver.andami.plugins.IExtension#initialize()
29
         */
30
        public void initialize() {
31
                registerIcons();
32
        }
33

    
34
        private void registerIcons(){
35
            PluginServices.getIconTheme().registerDefault(
36
                                "table-statistics",
37
                                this.getClass().getClassLoader().getResource("images/statistics.png")
38
                        );
39
        }
40

    
41
        /**
42
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
43
         */
44
        public void execute(String actionCommand) {
45
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
46

    
47
                if (v != null) {
48
                        if (v.getClass() == Table.class) {
49

    
50
                                Table table = (Table) v;
51

    
52
                                doExecute(table);
53
                        }
54
                }
55
        }
56
        /**
57
         * "execute" method acction
58
         * @param actionCommand
59
         * The acction command that executes this method
60
         * @param table
61
         * Table to operate
62
         */
63

    
64
        protected void doExecute(Table table){
65
                int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0);
66
                int numRows=0;
67
                FeatureStore fs=null;
68
//                try {
69
                        fs = table.getModel().getModel();
70
//                } catch (ReadException e) {
71
//                        e.printStackTrace();
72
//                }
73

    
74
                FeatureCollection fCollection = (FeatureCollection)fs.getSelection();
75
                FeatureCollection fCollec=null;
76
                BigDecimal suma = new BigDecimal(0);
77
                BigDecimal min = new BigDecimal(Double.MAX_VALUE);
78
                BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
79
                Iterator iterator=null;
80
                if (!fCollection.isEmpty()){
81
                        iterator=fCollection.iterator();
82
                        while (iterator.hasNext()) {
83
                                Feature feature = (Feature) iterator.next();
84
                                if (feature.get(fieldIndex) instanceof NullValue){
85
                                        continue;
86
                                }
87
                                Number number=(Number)feature.get(fieldIndex);
88
                                double d=number.doubleValue();
89
                                numRows++;
90
                                suma = suma.add(new BigDecimal(d));
91
                                if (d < min.doubleValue()) min = new BigDecimal(d);
92
                                if (d > max.doubleValue()) max = new BigDecimal(d);
93
                        }
94
                }else{
95

    
96
                        try {
97
                                fCollec = (FeatureCollection)fs.getDataCollection(new String[]{((FeatureAttributeDescriptor)fs.getDefaultFeatureType().get(fieldIndex)).getName()},null,null);
98
                        } catch (ReadException e) {
99
                                // TODO Auto-generated catch block
100
                                e.printStackTrace();
101
                        }
102

    
103
                        iterator=fCollec.iterator();
104
                        while (iterator.hasNext()) {
105
                                Feature feature = (Feature) iterator.next();
106
                                if (feature.get(fieldIndex) instanceof NullValue){
107
                                        continue;
108
                                }
109
                                Number number=(Number)feature.get(fieldIndex);
110
                                double d=number.doubleValue();
111
                                numRows++;
112
                                suma = suma.add(new BigDecimal(d));
113
                                if (d < min.doubleValue()) min = new BigDecimal(d);
114
                                if (d > max.doubleValue()) max = new BigDecimal(d);
115
                        }
116
                }
117
                        Statistics st = new Statistics();
118
                        BigDecimal media = new BigDecimal(0);
119
                        BigDecimal varianza = new BigDecimal(0);
120
                        double desviacion = 0;
121
                        if (numRows==0) {
122
                                suma = new BigDecimal(0);
123
                                min = new BigDecimal(0);
124
                                max = new BigDecimal(0);
125
                        }else {
126
                                media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
127
                                varianza = new BigDecimal(0);
128
                                if (!fCollection.isEmpty()){
129
                                        iterator=fCollection.iterator();
130
                                }else{
131
                                        iterator=fCollec.iterator();
132
                                }
133
                                while (iterator.hasNext()) {
134
                                        Feature feature = (Feature) iterator.next();
135
                                        if (feature.get(fieldIndex) instanceof NullValue){
136
                                                continue;
137
                                        }
138
                                        Number number=(Number)feature.get(fieldIndex);
139
                                        double d=number.doubleValue();
140

    
141
                                        BigDecimal dif = new BigDecimal(d).subtract(media);
142
                                        varianza = dif.multiply(dif).add(varianza);
143
                                }
144
                                varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
145
                                desviacion = Math.sqrt(varianza.doubleValue());
146
                        }
147
                        st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
148
                        PluginServices.getMDIManager().addWindow(st);
149
//                }else{
150
//
151
//                }
152

    
153
//                BitSet selectedRows = (BitSet)ds.getSelection().clone();
154
//                try {
155
//                        int rc = (int) ds.getRowCount();
156
//                        //Si no hay selecci?n se usan todos los registros
157
//                        if (selectedRows.cardinality() == 0){
158
//                                selectedRows.set(0, rc);
159
//                        }else{
160
//                                rc = selectedRows.cardinality();
161
//                        }
162
//                        BigDecimal suma = new BigDecimal(0);
163
//                        BigDecimal min = new BigDecimal(Double.MAX_VALUE);
164
//                        BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
165
//                        for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
166
//                                if (fs.getFieldValue(i, fieldIndex) instanceof NullValue)
167
//                                        continue;
168
//                                numRows++;
169
//                                double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
170
//                                suma = suma.add(new BigDecimal(d));
171
//                                if (d < min.doubleValue()) min = new BigDecimal(d);
172
//                                if (d > max.doubleValue()) max = new BigDecimal(d);
173
//                        }
174
//                        Statistics st = new Statistics();
175
//                        BigDecimal media = new BigDecimal(0);
176
//                        BigDecimal varianza = new BigDecimal(0);
177
//                        double desviacion = 0;
178
//                        if (numRows==0) {
179
//                                suma = new BigDecimal(0);
180
//                                min = new BigDecimal(0);
181
//                                max = new BigDecimal(0);
182
//                        }else {
183
//                                media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
184
//                                varianza = new BigDecimal(0);
185
//                                for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
186
//                                        if (ds.getFieldValue(i, fieldIndex) instanceof NullValue)
187
//                                                continue;
188
//                                        double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
189
//                                        BigDecimal dif = new BigDecimal(d).subtract(media);
190
//                                        varianza = dif.multiply(dif).add(varianza);
191
//                                }
192
//                                varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
193
//                                desviacion = Math.sqrt(varianza.doubleValue());
194
//                        }
195
//
196
//                        st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
197
//                        PluginServices.getMDIManager().addWindow(st);
198
//
199
//                } catch (ReadDriverException e) {
200
//                        NotificationManager.addError("No se pudo acceder a los datos", e);
201
//                }
202
        }
203

    
204
        /**
205
         * @see com.iver.andami.plugins.IExtension#isEnabled()
206
         */
207
        public boolean isEnabled() {
208
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
209

    
210
                if (v == null) {
211
                        return false;
212
                }
213

    
214
                if (v instanceof Table) {
215
                        Table table = (Table) v;
216
                        return doIsEnabled(table);
217
                }
218
                return false;
219
        }
220

    
221
        protected boolean doIsEnabled(Table table){
222
                BitSet indices = table.getSelectedFieldIndices();
223
                // System.out.println("TableNumericFieldOperations.isEnabled: Tabla: " + table.getModel().getModelo().getRecordset().getName() );
224
                if (indices.cardinality() == 1){
225
//                        try {
226
                                int index=indices.nextSetBit(0);
227
                                if (table.getModel().getModel().getDefaultFeatureType().size()<index+1)
228
                                        return false;
229
                                String type = ((FeatureAttributeDescriptor)table.getModel().getModel().getDefaultFeatureType().get(index)).getDataType();
230
                                if ((type.equals(FeatureAttributeDescriptor.TYPE_LONG)) ||
231
                                                (type.equals(FeatureAttributeDescriptor.TYPE_DOUBLE)) ||
232
                                                                (type.equals(FeatureAttributeDescriptor.TYPE_FLOAT)) ||
233
                                                                                (type.equals(FeatureAttributeDescriptor.TYPE_INT))){
234
                                        return true;
235
                                }
236
//                        } catch (ReadException e) {
237
//                                return false;
238
//                        }
239
                }
240
                return false;
241
        }
242

    
243

    
244
        /**
245
         * @see com.iver.andami.plugins.IExtension#isVisible()
246
         */
247
        public boolean isVisible() {
248
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
249

    
250
                if (v == null) {
251
                        return false;
252
                }
253

    
254
                if (v instanceof Table) {
255
                        return true;
256
                }
257
                return false;
258
        }
259

    
260
}