root / branches / v2_0_0_prep / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / TableNumericFieldOperations.java @ 38564
History | View | Annotate | Download (7.45 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.app.extension; |
23 |
|
24 |
import java.math.BigDecimal; |
25 |
|
26 |
import org.gvsig.andami.IconThemeHelper; |
27 |
import org.gvsig.andami.PluginServices; |
28 |
import org.gvsig.andami.messages.NotificationManager; |
29 |
import org.gvsig.andami.plugins.Extension; |
30 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
31 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
32 |
import org.gvsig.app.project.documents.table.gui.Statistics; |
33 |
import org.gvsig.fmap.dal.DataTypes; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.feature.Feature; |
36 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
37 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
38 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
39 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
41 |
import org.gvsig.tools.dispose.DisposableIterator; |
42 |
|
43 |
/**
|
44 |
* @author 2004-2005 Fernando Gonz?lez Cort?s
|
45 |
* @author 2005- Vicente Caballero
|
46 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
47 |
*/
|
48 |
public class TableNumericFieldOperations extends Extension { |
49 |
|
50 |
private FeatureTableDocumentPanel table;
|
51 |
|
52 |
/**
|
53 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
54 |
*/
|
55 |
public void initialize() { |
56 |
IconThemeHelper.registerIcon("action", "table-statistics", this); |
57 |
} |
58 |
|
59 |
/**
|
60 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
61 |
*/
|
62 |
public void execute(String actionCommand) { |
63 |
if( "table-statistics".equalsIgnoreCase(actionCommand)) { |
64 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
65 |
|
66 |
if (v != null) { |
67 |
if (v.getClass() == FeatureTableDocumentPanel.class) {
|
68 |
|
69 |
FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) v; |
70 |
|
71 |
doExecute(table); |
72 |
} |
73 |
} |
74 |
} |
75 |
|
76 |
} |
77 |
|
78 |
/**
|
79 |
* "execute" method acction
|
80 |
*
|
81 |
* @param actionCommand
|
82 |
* The acction command that executes this method
|
83 |
* @param table
|
84 |
* Table to operate
|
85 |
*/
|
86 |
protected void doExecute(FeatureTableDocumentPanel table) { |
87 |
FeatureSet featureSet = null;
|
88 |
DisposableIterator iterator = null;
|
89 |
try {
|
90 |
FeatureAttributeDescriptor fad = |
91 |
table.getTablePanel().getTable() |
92 |
.getSelectedColumnsAttributeDescriptor()[0];
|
93 |
long numRows = 0; |
94 |
FeatureStore fs = table.getModel().getStore(); |
95 |
|
96 |
// Get the iterator from the selection or all the data
|
97 |
FeatureSelection selection = fs.getFeatureSelection(); |
98 |
if (selection.isEmpty()) {
|
99 |
FeatureQuery fq = fs.createFeatureQuery(); |
100 |
fq.setAttributeNames(new String[] { fad.getName() }); |
101 |
featureSet = fs.getFeatureSet(); |
102 |
numRows = featureSet.getSize(); |
103 |
iterator = featureSet.fastIterator(); |
104 |
} else {
|
105 |
numRows = selection.getSize(); |
106 |
iterator = selection.fastIterator(); |
107 |
} |
108 |
|
109 |
// Read all values and calculate the min, max and the sum.
|
110 |
// Also, calculate what we can for the variance
|
111 |
BigDecimal sum = BigDecimal.ZERO; |
112 |
double min = Double.MAX_VALUE; |
113 |
double max = -Double.MAX_VALUE; |
114 |
BigDecimal variance = BigDecimal.ZERO; |
115 |
while (iterator.hasNext()) {
|
116 |
Feature feature = (Feature) iterator.next(); |
117 |
Number valueNumber = (Number) feature.get(fad.getName()); |
118 |
if (valueNumber == null) { |
119 |
continue;
|
120 |
} |
121 |
double value = valueNumber.doubleValue();
|
122 |
BigDecimal valueBig = BigDecimal.valueOf(value); |
123 |
sum = sum.add(valueBig); |
124 |
if (value < min) {
|
125 |
min = value; |
126 |
} |
127 |
if (value > max) {
|
128 |
max = value; |
129 |
} |
130 |
variance = variance.add(valueBig.pow(2));
|
131 |
} |
132 |
|
133 |
// Calculate the average, the variance final value and the standard
|
134 |
// desviation
|
135 |
BigDecimal average;
|
136 |
double desviation = 0.0d; |
137 |
if (numRows < 1l) { |
138 |
average = BigDecimal.ZERO;
|
139 |
} else {
|
140 |
BigDecimal numRowsBig = BigDecimal.valueOf(numRows); |
141 |
average = sum.divide(numRowsBig, BigDecimal.ROUND_HALF_DOWN);
|
142 |
variance = |
143 |
variance.divide(numRowsBig, BigDecimal.ROUND_HALF_DOWN)
|
144 |
.subtract(average.pow(2));
|
145 |
desviation = Math.sqrt(variance.doubleValue());
|
146 |
} |
147 |
|
148 |
// Create and shot the statistics window
|
149 |
Statistics st = new Statistics();
|
150 |
st.setStatistics(average.doubleValue(), max, min, |
151 |
variance.doubleValue(), desviation, numRows, max - min, |
152 |
sum.doubleValue()); |
153 |
PluginServices.getMDIManager().addWindow(st); |
154 |
} catch (DataException e) {
|
155 |
NotificationManager.addError(e); |
156 |
} finally {
|
157 |
if (iterator != null) { |
158 |
iterator.dispose(); |
159 |
} |
160 |
if (featureSet != null) { |
161 |
featureSet.dispose(); |
162 |
} |
163 |
} |
164 |
} |
165 |
|
166 |
/**
|
167 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
168 |
*/
|
169 |
public boolean isEnabled() { |
170 |
return doIsEnabled(table);
|
171 |
} |
172 |
|
173 |
protected boolean doIsEnabled(FeatureTableDocumentPanel table) { |
174 |
// FIXME
|
175 |
FeatureAttributeDescriptor[] fads = null; |
176 |
try {
|
177 |
fads = |
178 |
table.getTablePanel().getTable() |
179 |
.getSelectedColumnsAttributeDescriptor(); |
180 |
} catch (DataException e) {
|
181 |
e.printStackTrace(); |
182 |
} |
183 |
|
184 |
if (fads.length == 1) { |
185 |
// int index=indices.nextSetBit(0);
|
186 |
// if
|
187 |
// (table.getModel().getStore().getDefaultFeatureType().size()<index+1)
|
188 |
// {
|
189 |
// return false;
|
190 |
// }
|
191 |
int type = fads[0].getType(); |
192 |
if ((type == DataTypes.LONG) || (type == DataTypes.DOUBLE)
|
193 |
|| (type == DataTypes.FLOAT) || (type == DataTypes.INT)) { |
194 |
return true; |
195 |
} |
196 |
} |
197 |
return false; |
198 |
} |
199 |
|
200 |
/**
|
201 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
202 |
*/
|
203 |
public boolean isVisible() { |
204 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
205 |
if (v != null && v instanceof FeatureTableDocumentPanel) { |
206 |
table = (FeatureTableDocumentPanel) v; |
207 |
return true; |
208 |
} |
209 |
return false; |
210 |
} |
211 |
|
212 |
} |