svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / TableNumericFieldOperations.java @ 27390
History | View | Annotate | Download (7.93 KB)
1 | 2241 | fernando | package com.iver.cit.gvsig; |
---|---|---|---|
2 | |||
3 | 2266 | fernando | import java.math.BigDecimal; |
4 | 21354 | vcaballero | import java.util.Iterator; |
5 | 2241 | fernando | |
6 | 25068 | vcaballero | import org.gvsig.fmap.dal.DataTypes; |
7 | 26053 | vcaballero | import org.gvsig.fmap.dal.exception.DataException; |
8 | 24759 | jmvivo | import org.gvsig.fmap.dal.exception.ReadException; |
9 | import org.gvsig.fmap.dal.feature.Feature; |
||
10 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
11 | 26053 | vcaballero | import org.gvsig.fmap.dal.feature.FeatureQuery; |
12 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
13 | 24759 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureStore; |
14 | 21354 | vcaballero | import org.gvsig.fmap.mapcontext.rendering.legend.NullValue; |
15 | 25068 | vcaballero | import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel; |
16 | 26241 | vcaballero | import org.gvsig.project.document.table.gui.Statistics; |
17 | 20994 | jmvivo | |
18 | 2241 | fernando | import com.iver.andami.PluginServices; |
19 | 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 | 26053 | vcaballero | try{
|
68 | FeatureAttributeDescriptor fad=table.getTablePanel().getTable().getSelectedColumnsAttributeDescriptor()[0];
|
||
69 | int numRows=0; |
||
70 | FeatureStore fs=null;
|
||
71 | fs = table.getModel().getStore(); |
||
72 | |||
73 | FeatureSet fCollection = (FeatureSet)fs.getSelection(); |
||
74 | FeatureSet fCollec=null;
|
||
75 | BigDecimal suma = new BigDecimal(0); |
||
76 | BigDecimal min = new BigDecimal(Double.MAX_VALUE); |
||
77 | BigDecimal max = new BigDecimal(-Double.MAX_VALUE); |
||
78 | Iterator iterator=null; |
||
79 | if (!fCollection.isEmpty()){
|
||
80 | iterator=fCollection.iterator(); |
||
81 | while (iterator.hasNext()) {
|
||
82 | Feature feature = (Feature) iterator.next(); |
||
83 | if (feature.get(fad.getName()) == null) { |
||
84 | continue;
|
||
85 | } |
||
86 | Number number=(Number)feature.get(fad.getName()); |
||
87 | double d=number.doubleValue();
|
||
88 | numRows++; |
||
89 | suma = suma.add(new BigDecimal(d)); |
||
90 | if (d < min.doubleValue()) {
|
||
91 | min = new BigDecimal(d); |
||
92 | } |
||
93 | if (d > max.doubleValue()) {
|
||
94 | max = new BigDecimal(d); |
||
95 | } |
||
96 | } |
||
97 | }else{
|
||
98 | |||
99 | try {
|
||
100 | FeatureQuery fq=new FeatureQuery();
|
||
101 | fq.setAttributeNames(new String[]{fad.getName()}); |
||
102 | fCollec = fs.getFeatureSet(fq); |
||
103 | } catch (ReadException e) {
|
||
104 | // TODO Auto-generated catch block
|
||
105 | e.printStackTrace(); |
||
106 | } |
||
107 | |||
108 | iterator=fCollec.iterator(); |
||
109 | while (iterator.hasNext()) {
|
||
110 | Feature feature = (Feature) iterator.next(); |
||
111 | if (feature.get(fad.getName()) instanceof NullValue){ |
||
112 | continue;
|
||
113 | } |
||
114 | Number number=(Number)feature.get(fad.getName()); |
||
115 | double d=number.doubleValue();
|
||
116 | numRows++; |
||
117 | suma = suma.add(new BigDecimal(d)); |
||
118 | if (d < min.doubleValue()) {
|
||
119 | min = new BigDecimal(d); |
||
120 | } |
||
121 | if (d > max.doubleValue()) {
|
||
122 | max = new BigDecimal(d); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | Statistics st = new Statistics();
|
||
127 | BigDecimal media = new BigDecimal(0); |
||
128 | BigDecimal varianza = new BigDecimal(0); |
||
129 | double desviacion = 0; |
||
130 | if (numRows==0) { |
||
131 | suma = new BigDecimal(0); |
||
132 | min = new BigDecimal(0); |
||
133 | max = new BigDecimal(0); |
||
134 | }else {
|
||
135 | media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN); |
||
136 | varianza = new BigDecimal(0); |
||
137 | if (!fCollection.isEmpty()){
|
||
138 | iterator=fCollection.iterator(); |
||
139 | }else{
|
||
140 | iterator=fCollec.iterator(); |
||
141 | } |
||
142 | while (iterator.hasNext()) {
|
||
143 | Feature feature = (Feature) iterator.next(); |
||
144 | if (feature.get(fad.getName()) instanceof NullValue){ |
||
145 | continue;
|
||
146 | } |
||
147 | Number number=(Number)feature.get(fad.getName()); |
||
148 | double d=number.doubleValue();
|
||
149 | |||
150 | BigDecimal dif = new BigDecimal(d).subtract(media); |
||
151 | varianza = dif.multiply(dif).add(varianza); |
||
152 | } |
||
153 | varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN); |
||
154 | desviacion = Math.sqrt(varianza.doubleValue());
|
||
155 | } |
||
156 | st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue()); |
||
157 | PluginServices.getMDIManager().addWindow(st); |
||
158 | }catch (DataException e) {
|
||
159 | e.printStackTrace(); |
||
160 | } |
||
161 | // }else{
|
||
162 | |||
163 | // }
|
||
164 | |||
165 | // BitSet selectedRows = (BitSet)ds.getSelection().clone();
|
||
166 | // try {
|
||
167 | // int rc = (int) ds.getRowCount();
|
||
168 | // //Si no hay selecci?n se usan todos los registros
|
||
169 | // if (selectedRows.cardinality() == 0){
|
||
170 | // selectedRows.set(0, rc);
|
||
171 | // }else{
|
||
172 | // rc = selectedRows.cardinality();
|
||
173 | // }
|
||
174 | 25089 | jmvivo | // BigDecimal suma = new BigDecimal(0);
|
175 | // BigDecimal min = new BigDecimal(Double.MAX_VALUE);
|
||
176 | // BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
|
||
177 | 26053 | vcaballero | // for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
|
178 | // if (fs.getFieldValue(i, fieldIndex) instanceof NullValue)
|
||
179 | // continue;
|
||
180 | // numRows++;
|
||
181 | // double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
|
||
182 | // suma = suma.add(new BigDecimal(d));
|
||
183 | // if (d < min.doubleValue()) min = new BigDecimal(d);
|
||
184 | // if (d > max.doubleValue()) max = new BigDecimal(d);
|
||
185 | 25089 | jmvivo | // }
|
186 | 26053 | vcaballero | // Statistics st = new Statistics();
|
187 | // BigDecimal media = new BigDecimal(0);
|
||
188 | // BigDecimal varianza = new BigDecimal(0);
|
||
189 | // double desviacion = 0;
|
||
190 | // if (numRows==0) {
|
||
191 | // suma = new BigDecimal(0);
|
||
192 | // min = new BigDecimal(0);
|
||
193 | // max = new BigDecimal(0);
|
||
194 | // }else {
|
||
195 | // media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
|
||
196 | // varianza = new BigDecimal(0);
|
||
197 | // for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
|
||
198 | // if (ds.getFieldValue(i, fieldIndex) instanceof NullValue)
|
||
199 | // continue;
|
||
200 | // double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
|
||
201 | // BigDecimal dif = new BigDecimal(d).subtract(media);
|
||
202 | // varianza = dif.multiply(dif).add(varianza);
|
||
203 | // }
|
||
204 | // varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
|
||
205 | // desviacion = Math.sqrt(varianza.doubleValue());
|
||
206 | // }
|
||
207 | |||
208 | // st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
|
||
209 | // PluginServices.getMDIManager().addWindow(st);
|
||
210 | |||
211 | // } catch (ReadDriverException e) {
|
||
212 | // NotificationManager.addError("No se pudo acceder a los datos", e);
|
||
213 | // }
|
||
214 | 4682 | jorpiell | } |
215 | 2241 | fernando | |
216 | 4682 | jorpiell | /**
|
217 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
218 | 4682 | jorpiell | */
|
219 | public boolean isEnabled() { |
||
220 | 26053 | vcaballero | return doIsEnabled(table);
|
221 | 4682 | jorpiell | } |
222 | 3940 | caballero | |
223 | 25068 | vcaballero | protected boolean doIsEnabled(FeatureTableDocumentPanel table){ |
224 | 25089 | jmvivo | // FIXME
|
225 | 26053 | vcaballero | FeatureAttributeDescriptor[] fads=null; |
226 | try {
|
||
227 | fads = table.getTablePanel().getTable().getSelectedColumnsAttributeDescriptor(); |
||
228 | } catch (DataException e) {
|
||
229 | e.printStackTrace(); |
||
230 | } |
||
231 | |||
232 | if (fads.length == 1){ |
||
233 | 25089 | jmvivo | // int index=indices.nextSetBit(0);
|
234 | // if (table.getModel().getStore().getDefaultFeatureType().size()<index+1) {
|
||
235 | // return false;
|
||
236 | // }
|
||
237 | 26053 | vcaballero | int type = fads[0].getDataType(); |
238 | if ((type==DataTypes.LONG) ||
|
||
239 | (type== DataTypes.DOUBLE) || |
||
240 | (type==DataTypes.FLOAT) || |
||
241 | (type==DataTypes.INT)){ |
||
242 | return true; |
||
243 | } |
||
244 | } |
||
245 | 2241 | fernando | return false; |
246 | 4682 | jorpiell | } |
247 | 2241 | fernando | |
248 | 4682 | jorpiell | |
249 | /**
|
||
250 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isVisible()
|
251 | 4682 | jorpiell | */
|
252 | public boolean isVisible() { |
||
253 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
254 | 26053 | vcaballero | if (v!=null && v instanceof FeatureTableDocumentPanel) { |
255 | table=(FeatureTableDocumentPanel)v; |
||
256 | 4682 | jorpiell | return true; |
257 | 2241 | fernando | } |
258 | return false; |
||
259 | 4682 | jorpiell | } |
260 | 2241 | fernando | |
261 | } |