svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / TableNumericFieldOperations.java @ 40558
History | View | Annotate | Download (7.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.extension; |
25 |
|
26 |
import java.math.BigDecimal; |
27 |
|
28 |
import org.gvsig.andami.IconThemeHelper; |
29 |
import org.gvsig.andami.PluginServices; |
30 |
import org.gvsig.andami.messages.NotificationManager; |
31 |
import org.gvsig.andami.plugins.Extension; |
32 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
33 |
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
34 |
import org.gvsig.app.project.documents.table.gui.Statistics; |
35 |
import org.gvsig.fmap.dal.DataTypes; |
36 |
import org.gvsig.fmap.dal.exception.DataException; |
37 |
import org.gvsig.fmap.dal.feature.Feature; |
38 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
39 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
40 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
41 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
import org.gvsig.tools.dispose.DisposableIterator; |
44 |
|
45 |
/**
|
46 |
* @author 2004-2005 Fernando Gonz?lez Cort?s
|
47 |
* @author 2005- Vicente Caballero
|
48 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
49 |
*/
|
50 |
public class TableNumericFieldOperations extends Extension { |
51 |
|
52 |
private FeatureTableDocumentPanel table;
|
53 |
|
54 |
/**
|
55 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
56 |
*/
|
57 |
public void initialize() { |
58 |
IconThemeHelper.registerIcon("action", "table-statistics", this); |
59 |
} |
60 |
|
61 |
/**
|
62 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
63 |
*/
|
64 |
public void execute(String actionCommand) { |
65 |
if( "table-statistics".equalsIgnoreCase(actionCommand)) { |
66 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
67 |
|
68 |
if (v != null) { |
69 |
if (v.getClass() == FeatureTableDocumentPanel.class) {
|
70 |
|
71 |
FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) v; |
72 |
|
73 |
doExecute(table); |
74 |
} |
75 |
} |
76 |
} |
77 |
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* "execute" method acction
|
82 |
*
|
83 |
* @param actionCommand
|
84 |
* The acction command that executes this method
|
85 |
* @param table
|
86 |
* Table to operate
|
87 |
*/
|
88 |
protected void doExecute(FeatureTableDocumentPanel table) { |
89 |
FeatureSet featureSet = null;
|
90 |
DisposableIterator iterator = null;
|
91 |
try {
|
92 |
FeatureAttributeDescriptor fad = |
93 |
table.getTablePanel().getTable() |
94 |
.getSelectedColumnsAttributeDescriptor()[0];
|
95 |
long numRows = 0; |
96 |
FeatureStore fs = table.getModel().getStore(); |
97 |
|
98 |
// Get the iterator from the selection or all the data
|
99 |
FeatureSelection selection = fs.getFeatureSelection(); |
100 |
if (selection.isEmpty()) {
|
101 |
FeatureQuery fq = fs.createFeatureQuery(); |
102 |
fq.setAttributeNames(new String[] { fad.getName() }); |
103 |
featureSet = fs.getFeatureSet(); |
104 |
numRows = featureSet.getSize(); |
105 |
iterator = featureSet.fastIterator(); |
106 |
} else {
|
107 |
numRows = selection.getSize(); |
108 |
iterator = selection.fastIterator(); |
109 |
} |
110 |
|
111 |
// Read all values and calculate the min, max and the sum.
|
112 |
// Also, calculate what we can for the variance
|
113 |
BigDecimal sum = BigDecimal.ZERO; |
114 |
double min = Double.MAX_VALUE; |
115 |
double max = -Double.MAX_VALUE; |
116 |
BigDecimal variance = BigDecimal.ZERO; |
117 |
while (iterator.hasNext()) {
|
118 |
Feature feature = (Feature) iterator.next(); |
119 |
Number valueNumber = (Number) feature.get(fad.getName()); |
120 |
if (valueNumber == null) { |
121 |
continue;
|
122 |
} |
123 |
double value = valueNumber.doubleValue();
|
124 |
BigDecimal valueBig = BigDecimal.valueOf(value); |
125 |
sum = sum.add(valueBig); |
126 |
if (value < min) {
|
127 |
min = value; |
128 |
} |
129 |
if (value > max) {
|
130 |
max = value; |
131 |
} |
132 |
variance = variance.add(valueBig.pow(2));
|
133 |
} |
134 |
|
135 |
// Calculate the average, the variance final value and the standard
|
136 |
// desviation
|
137 |
BigDecimal average;
|
138 |
double desviation = 0.0d; |
139 |
if (numRows < 1l) { |
140 |
average = BigDecimal.ZERO;
|
141 |
} else {
|
142 |
BigDecimal numRowsBig = BigDecimal.valueOf(numRows); |
143 |
average = sum.divide(numRowsBig, BigDecimal.ROUND_HALF_DOWN);
|
144 |
variance = |
145 |
variance.divide(numRowsBig, BigDecimal.ROUND_HALF_DOWN)
|
146 |
.subtract(average.pow(2));
|
147 |
desviation = Math.sqrt(variance.doubleValue());
|
148 |
} |
149 |
|
150 |
// Create and shot the statistics window
|
151 |
Statistics st = new Statistics();
|
152 |
st.setStatistics(average.doubleValue(), max, min, |
153 |
variance.doubleValue(), desviation, numRows, max - min, |
154 |
sum.doubleValue()); |
155 |
PluginServices.getMDIManager().addWindow(st); |
156 |
} catch (DataException e) {
|
157 |
NotificationManager.addError(e); |
158 |
} finally {
|
159 |
if (iterator != null) { |
160 |
iterator.dispose(); |
161 |
} |
162 |
if (featureSet != null) { |
163 |
featureSet.dispose(); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
/**
|
169 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
170 |
*/
|
171 |
public boolean isEnabled() { |
172 |
return doIsEnabled(table);
|
173 |
} |
174 |
|
175 |
protected boolean doIsEnabled(FeatureTableDocumentPanel table) { |
176 |
// FIXME
|
177 |
FeatureAttributeDescriptor[] fads = null; |
178 |
try {
|
179 |
fads = |
180 |
table.getTablePanel().getTable() |
181 |
.getSelectedColumnsAttributeDescriptor(); |
182 |
} catch (DataException e) {
|
183 |
e.printStackTrace(); |
184 |
} |
185 |
|
186 |
if (fads.length == 1) { |
187 |
// int index=indices.nextSetBit(0);
|
188 |
// if
|
189 |
// (table.getModel().getStore().getDefaultFeatureType().size()<index+1)
|
190 |
// {
|
191 |
// return false;
|
192 |
// }
|
193 |
int type = fads[0].getType(); |
194 |
if ((type == DataTypes.LONG) || (type == DataTypes.DOUBLE)
|
195 |
|| (type == DataTypes.FLOAT) || (type == DataTypes.INT)) { |
196 |
return true; |
197 |
} |
198 |
} |
199 |
return false; |
200 |
} |
201 |
|
202 |
/**
|
203 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
204 |
*/
|
205 |
public boolean isVisible() { |
206 |
IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
207 |
if (v != null && v instanceof FeatureTableDocumentPanel) { |
208 |
table = (FeatureTableDocumentPanel) v; |
209 |
return true; |
210 |
} |
211 |
return false; |
212 |
} |
213 |
|
214 |
} |