root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / TableNumericFieldOperations.java @ 30290
History | View | Annotate | Download (8.55 KB)
1 | 29596 | jpiera | package org.gvsig.app.extension; |
---|---|---|---|
2 | 2241 | fernando | |
3 | 2266 | fernando | import java.math.BigDecimal; |
4 | 2241 | fernando | |
5 | 29596 | jpiera | import org.gvsig.andami.PluginServices; |
6 | import org.gvsig.andami.messages.NotificationManager; |
||
7 | import org.gvsig.andami.plugins.Extension; |
||
8 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
9 | import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
||
10 | import org.gvsig.app.project.documents.table.gui.Statistics; |
||
11 | 25068 | vcaballero | import org.gvsig.fmap.dal.DataTypes; |
12 | 26053 | vcaballero | import org.gvsig.fmap.dal.exception.DataException; |
13 | 27525 | jmvivo | import org.gvsig.fmap.dal.feature.DisposableIterator; |
14 | 24759 | jmvivo | import org.gvsig.fmap.dal.feature.Feature; |
15 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
16 | 26053 | vcaballero | import org.gvsig.fmap.dal.feature.FeatureQuery; |
17 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
18 | 24759 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureStore; |
19 | 20994 | jmvivo | |
20 | 2241 | fernando | /**
|
21 | * @author Fernando Gonz?lez Cort?s
|
||
22 | */
|
||
23 | 5005 | jorpiell | public class TableNumericFieldOperations extends Extension{ |
24 | 26053 | vcaballero | private FeatureTableDocumentPanel table;
|
25 | 2241 | fernando | |
26 | 26053 | vcaballero | |
27 | 4682 | jorpiell | /**
|
28 | 29596 | jpiera | * @see org.gvsig.andami.plugins.IExtension#initialize()
|
29 | 4682 | jorpiell | */
|
30 | 5005 | jorpiell | public void initialize() { |
31 | 14821 | jmvivo | registerIcons(); |
32 | 4682 | jorpiell | } |
33 | 2241 | fernando | |
34 | 14821 | jmvivo | private void registerIcons(){ |
35 | 15647 | jmvivo | PluginServices.getIconTheme().registerDefault( |
36 | 14821 | jmvivo | "table-statistics",
|
37 | this.getClass().getClassLoader().getResource("images/statistics.png") |
||
38 | ); |
||
39 | } |
||
40 | |||
41 | 4682 | jorpiell | /**
|
42 | 29596 | jpiera | * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
43 | 4682 | jorpiell | */
|
44 | public void execute(String actionCommand) { |
||
45 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
46 | 2241 | fernando | |
47 | if (v != null) { |
||
48 | 25068 | vcaballero | if (v.getClass() == FeatureTableDocumentPanel.class) {
|
49 | 3940 | caballero | |
50 | 25068 | vcaballero | FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) v; |
51 | 3940 | caballero | |
52 | 4682 | jorpiell | 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 | 3940 | caballero | |
64 | 25068 | vcaballero | protected void doExecute(FeatureTableDocumentPanel table){ |
65 | 27634 | jmvivo | FeatureSet fCollection = null;
|
66 | FeatureSet fCollec = null;
|
||
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 | 27634 | jmvivo | fCollection = (FeatureSet) fs.getSelection(); |
74 | |||
75 | 26053 | vcaballero | BigDecimal suma = new BigDecimal(0); |
76 | BigDecimal min = new BigDecimal(Double.MAX_VALUE); |
||
77 | BigDecimal max = new BigDecimal(-Double.MAX_VALUE); |
||
78 | 27525 | jmvivo | DisposableIterator iterator = null;
|
79 | 26053 | vcaballero | if (!fCollection.isEmpty()){
|
80 | 27525 | jmvivo | try {
|
81 | iterator = fCollection.iterator(); |
||
82 | while (iterator.hasNext()) {
|
||
83 | Feature feature = (Feature) iterator.next(); |
||
84 | if (feature.get(fad.getName()) == null) { |
||
85 | continue;
|
||
86 | } |
||
87 | Number number = (Number) feature.get(fad.getName()); |
||
88 | double d = number.doubleValue();
|
||
89 | numRows++; |
||
90 | suma = suma.add(new BigDecimal(d)); |
||
91 | if (d < min.doubleValue()) {
|
||
92 | min = new BigDecimal(d); |
||
93 | } |
||
94 | if (d > max.doubleValue()) {
|
||
95 | max = new BigDecimal(d); |
||
96 | } |
||
97 | 26053 | vcaballero | } |
98 | 27525 | jmvivo | } finally {
|
99 | if (iterator != null) { |
||
100 | iterator.dispose(); |
||
101 | 26053 | vcaballero | } |
102 | } |
||
103 | }else{
|
||
104 | |||
105 | try {
|
||
106 | 27634 | jmvivo | FeatureQuery fq = fs.createFeatureQuery(); |
107 | 26053 | vcaballero | fq.setAttributeNames(new String[]{fad.getName()}); |
108 | fCollec = fs.getFeatureSet(fq); |
||
109 | |||
110 | 27525 | jmvivo | iterator = fCollec.iterator(); |
111 | while (iterator.hasNext()) {
|
||
112 | Feature feature = (Feature) iterator.next(); |
||
113 | 30011 | cordinyana | // if (feature.get(fad.getName()) instanceof NullValue) {
|
114 | if (feature.get(fad.getName()) == null) { |
||
115 | 27525 | jmvivo | continue;
|
116 | } |
||
117 | Number number = (Number) feature.get(fad.getName()); |
||
118 | double d = number.doubleValue();
|
||
119 | numRows++; |
||
120 | suma = suma.add(new BigDecimal(d)); |
||
121 | if (d < min.doubleValue()) {
|
||
122 | min = new BigDecimal(d); |
||
123 | } |
||
124 | if (d > max.doubleValue()) {
|
||
125 | max = new BigDecimal(d); |
||
126 | } |
||
127 | 26053 | vcaballero | } |
128 | 27525 | jmvivo | } catch (DataException e) {
|
129 | // TODO Auto-generated catch block
|
||
130 | NotificationManager.addError(e); |
||
131 | return;
|
||
132 | } finally {
|
||
133 | if (iterator != null) { |
||
134 | iterator.dispose(); |
||
135 | 26053 | vcaballero | } |
136 | 27525 | jmvivo | if (fCollec != null) { |
137 | fCollec.dispose(); |
||
138 | 26053 | vcaballero | } |
139 | } |
||
140 | } |
||
141 | Statistics st = new Statistics();
|
||
142 | BigDecimal media = new BigDecimal(0); |
||
143 | BigDecimal varianza = new BigDecimal(0); |
||
144 | double desviacion = 0; |
||
145 | if (numRows==0) { |
||
146 | suma = new BigDecimal(0); |
||
147 | min = new BigDecimal(0); |
||
148 | max = new BigDecimal(0); |
||
149 | }else {
|
||
150 | media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN); |
||
151 | varianza = new BigDecimal(0); |
||
152 | if (!fCollection.isEmpty()){
|
||
153 | iterator=fCollection.iterator(); |
||
154 | }else{
|
||
155 | iterator=fCollec.iterator(); |
||
156 | } |
||
157 | 27634 | jmvivo | try {
|
158 | while (iterator.hasNext()) {
|
||
159 | Feature feature = (Feature) iterator.next(); |
||
160 | 30011 | cordinyana | // if (feature.get(fad.getName()) instanceof NullValue) {
|
161 | if (feature.get(fad.getName()) == null) { |
||
162 | 27634 | jmvivo | 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 | 29596 | jpiera | * @see org.gvsig.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 | 29596 | jpiera | * @see org.gvsig.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 | } |