Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / TableNumericFieldOperations.java @ 28153

History | View | Annotate | Download (8.49 KB)

1 2241 fernando
package com.iver.cit.gvsig;
2
3 2266 fernando
import java.math.BigDecimal;
4 2241 fernando
5 25068 vcaballero
import org.gvsig.fmap.dal.DataTypes;
6 26053 vcaballero
import org.gvsig.fmap.dal.exception.DataException;
7 27525 jmvivo
import org.gvsig.fmap.dal.feature.DisposableIterator;
8 24759 jmvivo
import org.gvsig.fmap.dal.feature.Feature;
9
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
10 26053 vcaballero
import org.gvsig.fmap.dal.feature.FeatureQuery;
11
import org.gvsig.fmap.dal.feature.FeatureSet;
12 24759 jmvivo
import org.gvsig.fmap.dal.feature.FeatureStore;
13 21354 vcaballero
import org.gvsig.fmap.mapcontext.rendering.legend.NullValue;
14 25068 vcaballero
import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel;
15 26241 vcaballero
import org.gvsig.project.document.table.gui.Statistics;
16 20994 jmvivo
17 2241 fernando
import com.iver.andami.PluginServices;
18 27525 jmvivo
import com.iver.andami.messages.NotificationManager;
19 2241 fernando
import com.iver.andami.plugins.Extension;
20 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
21 2241 fernando
22
/**
23
 * @author Fernando Gonz?lez Cort?s
24
 */
25 5005 jorpiell
public class TableNumericFieldOperations extends Extension{
26 26053 vcaballero
        private FeatureTableDocumentPanel table;
27 2241 fernando
28 26053 vcaballero
29 4682 jorpiell
        /**
30 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#initialize()
31 4682 jorpiell
         */
32 5005 jorpiell
        public void initialize() {
33 14821 jmvivo
                registerIcons();
34 4682 jorpiell
        }
35 2241 fernando
36 14821 jmvivo
        private void registerIcons(){
37 15647 jmvivo
            PluginServices.getIconTheme().registerDefault(
38 14821 jmvivo
                                "table-statistics",
39
                                this.getClass().getClassLoader().getResource("images/statistics.png")
40
                        );
41
        }
42
43 4682 jorpiell
        /**
44 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
45 4682 jorpiell
         */
46
        public void execute(String actionCommand) {
47 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
48 2241 fernando
49
                if (v != null) {
50 25068 vcaballero
                        if (v.getClass() == FeatureTableDocumentPanel.class) {
51 3940 caballero
52 25068 vcaballero
                                FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) v;
53 3940 caballero
54 4682 jorpiell
                                doExecute(table);
55
                        }
56
                }
57
        }
58
        /**
59
         * "execute" method acction
60
         * @param actionCommand
61
         * The acction command that executes this method
62
         * @param table
63
         * Table to operate
64
         */
65 3940 caballero
66 25068 vcaballero
        protected void doExecute(FeatureTableDocumentPanel table){
67 27634 jmvivo
                FeatureSet fCollection = null;
68
                FeatureSet fCollec = null;
69 26053 vcaballero
                try{
70
                        FeatureAttributeDescriptor fad=table.getTablePanel().getTable().getSelectedColumnsAttributeDescriptor()[0];
71
                        int numRows=0;
72
                        FeatureStore fs=null;
73
                        fs = table.getModel().getStore();
74
75 27634 jmvivo
                        fCollection = (FeatureSet) fs.getSelection();
76
77 26053 vcaballero
                        BigDecimal suma = new BigDecimal(0);
78
                        BigDecimal min = new BigDecimal(Double.MAX_VALUE);
79
                        BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
80 27525 jmvivo
                        DisposableIterator iterator = null;
81 26053 vcaballero
                        if (!fCollection.isEmpty()){
82 27525 jmvivo
                                try {
83
                                        iterator = fCollection.iterator();
84
                                        while (iterator.hasNext()) {
85
                                                Feature feature = (Feature) iterator.next();
86
                                                if (feature.get(fad.getName()) == null) {
87
                                                        continue;
88
                                                }
89
                                                Number number = (Number) feature.get(fad.getName());
90
                                                double d = number.doubleValue();
91
                                                numRows++;
92
                                                suma = suma.add(new BigDecimal(d));
93
                                                if (d < min.doubleValue()) {
94
                                                        min = new BigDecimal(d);
95
                                                }
96
                                                if (d > max.doubleValue()) {
97
                                                        max = new BigDecimal(d);
98
                                                }
99 26053 vcaballero
                                        }
100 27525 jmvivo
                                } finally {
101
                                        if (iterator != null) {
102
                                                iterator.dispose();
103 26053 vcaballero
                                        }
104
                                }
105
                        }else{
106
107
                                try {
108 27634 jmvivo
                                        FeatureQuery fq = fs.createFeatureQuery();
109 26053 vcaballero
                                        fq.setAttributeNames(new String[]{fad.getName()});
110
                                        fCollec = fs.getFeatureSet(fq);
111
112 27525 jmvivo
                                        iterator = fCollec.iterator();
113
                                        while (iterator.hasNext()) {
114
                                                Feature feature = (Feature) iterator.next();
115
                                                if (feature.get(fad.getName()) instanceof NullValue) {
116
                                                        continue;
117
                                                }
118
                                                Number number = (Number) feature.get(fad.getName());
119
                                                double d = number.doubleValue();
120
                                                numRows++;
121
                                                suma = suma.add(new BigDecimal(d));
122
                                                if (d < min.doubleValue()) {
123
                                                        min = new BigDecimal(d);
124
                                                }
125
                                                if (d > max.doubleValue()) {
126
                                                        max = new BigDecimal(d);
127
                                                }
128 26053 vcaballero
                                        }
129 27525 jmvivo
                                } catch (DataException e) {
130
                                        // TODO Auto-generated catch block
131
                                        NotificationManager.addError(e);
132
                                        return;
133
                                } finally {
134
                                        if (iterator != null) {
135
                                                iterator.dispose();
136 26053 vcaballero
                                        }
137 27525 jmvivo
                                        if (fCollec != null) {
138
                                                fCollec.dispose();
139 26053 vcaballero
                                        }
140
                                }
141
                        }
142
                        Statistics st = new Statistics();
143
                        BigDecimal media = new BigDecimal(0);
144
                        BigDecimal varianza = new BigDecimal(0);
145
                        double desviacion = 0;
146
                        if (numRows==0) {
147
                                suma = new BigDecimal(0);
148
                                min = new BigDecimal(0);
149
                                max = new BigDecimal(0);
150
                        }else {
151
                                media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
152
                                varianza = new BigDecimal(0);
153
                                if (!fCollection.isEmpty()){
154
                                        iterator=fCollection.iterator();
155
                                }else{
156
                                        iterator=fCollec.iterator();
157
                                }
158 27634 jmvivo
                                try {
159
                                        while (iterator.hasNext()) {
160
                                                Feature feature = (Feature) iterator.next();
161
                                                if (feature.get(fad.getName()) instanceof NullValue) {
162
                                                        continue;
163
                                                }
164
                                                Number number = (Number) feature.get(fad.getName());
165
                                                double d = number.doubleValue();
166
167
                                                BigDecimal dif = new BigDecimal(d).subtract(media);
168
                                                varianza = dif.multiply(dif).add(varianza);
169 26053 vcaballero
                                        }
170 27634 jmvivo
                                } finally {
171
                                        if (iterator != null) {
172
                                                iterator.dispose();
173
                                        }
174 26053 vcaballero
                                }
175
                                varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
176
                                desviacion = Math.sqrt(varianza.doubleValue());
177
                        }
178
                        st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
179
                        PluginServices.getMDIManager().addWindow(st);
180
                }catch (DataException e) {
181 27525 jmvivo
                        NotificationManager.addError(e);
182 27634 jmvivo
                } finally{
183
                        if (fCollection != null){
184
                                fCollection.dispose();
185
                        }
186 26053 vcaballero
                }
187
//                }else{
188
189
//                }
190
191
//                BitSet selectedRows = (BitSet)ds.getSelection().clone();
192
//                try {
193
//                int rc = (int) ds.getRowCount();
194
//                //Si no hay selecci?n se usan todos los registros
195
//                if (selectedRows.cardinality() == 0){
196
//                selectedRows.set(0, rc);
197
//                }else{
198
//                rc = selectedRows.cardinality();
199
//                }
200 25089 jmvivo
//                BigDecimal suma = new BigDecimal(0);
201
//                BigDecimal min = new BigDecimal(Double.MAX_VALUE);
202
//                BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
203 26053 vcaballero
//                for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
204
//                if (fs.getFieldValue(i, fieldIndex) instanceof NullValue)
205
//                continue;
206
//                numRows++;
207
//                double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
208
//                suma = suma.add(new BigDecimal(d));
209
//                if (d < min.doubleValue()) min = new BigDecimal(d);
210
//                if (d > max.doubleValue()) max = new BigDecimal(d);
211 25089 jmvivo
//                }
212 26053 vcaballero
//                Statistics st = new Statistics();
213
//                BigDecimal media = new BigDecimal(0);
214
//                BigDecimal varianza = new BigDecimal(0);
215
//                double desviacion = 0;
216
//                if (numRows==0) {
217
//                suma = new BigDecimal(0);
218
//                min = new BigDecimal(0);
219
//                max = new BigDecimal(0);
220
//                }else {
221
//                media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
222
//                varianza = new BigDecimal(0);
223
//                for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
224
//                if (ds.getFieldValue(i, fieldIndex) instanceof NullValue)
225
//                continue;
226
//                double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
227
//                BigDecimal dif = new BigDecimal(d).subtract(media);
228
//                varianza = dif.multiply(dif).add(varianza);
229
//                }
230
//                varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
231
//                desviacion = Math.sqrt(varianza.doubleValue());
232
//                }
233
234
//                st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
235
//                PluginServices.getMDIManager().addWindow(st);
236
237
//                } catch (ReadDriverException e) {
238
//                NotificationManager.addError("No se pudo acceder a los datos", e);
239
//                }
240 4682 jorpiell
        }
241 2241 fernando
242 4682 jorpiell
        /**
243 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
244 4682 jorpiell
         */
245
        public boolean isEnabled() {
246 26053 vcaballero
                return doIsEnabled(table);
247 4682 jorpiell
        }
248 3940 caballero
249 25068 vcaballero
        protected boolean doIsEnabled(FeatureTableDocumentPanel table){
250 25089 jmvivo
//                FIXME
251 26053 vcaballero
                FeatureAttributeDescriptor[] fads=null;
252
                try {
253
                        fads = table.getTablePanel().getTable().getSelectedColumnsAttributeDescriptor();
254
                } catch (DataException e) {
255
                        e.printStackTrace();
256
                }
257
258
                if (fads.length == 1){
259 25089 jmvivo
//                                int index=indices.nextSetBit(0);
260
//                                if (table.getModel().getStore().getDefaultFeatureType().size()<index+1) {
261
//                                        return false;
262
//                                }
263 26053 vcaballero
                                int type = fads[0].getDataType();
264
                                if ((type==DataTypes.LONG) ||
265
                                                (type== DataTypes.DOUBLE) ||
266
                                                                (type==DataTypes.FLOAT) ||
267
                                                                                (type==DataTypes.INT)){
268
                                        return true;
269
                                }
270
                }
271 2241 fernando
                return false;
272 4682 jorpiell
        }
273 2241 fernando
274 4682 jorpiell
275
        /**
276 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isVisible()
277 4682 jorpiell
         */
278
        public boolean isVisible() {
279 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
280 26053 vcaballero
                if (v!=null && v instanceof FeatureTableDocumentPanel) {
281
                        table=(FeatureTableDocumentPanel)v;
282 4682 jorpiell
                        return true;
283 2241 fernando
                }
284
                return false;
285 4682 jorpiell
        }
286 2241 fernando
287
}