Revision 26053 branches/v2_0_0_prep/applications/appgvSIG/src/com/iver/cit/gvsig/TableNumericFieldOperations.java

View differences:

TableNumericFieldOperations.java
1 1
package com.iver.cit.gvsig;
2 2

  
3 3
import java.math.BigDecimal;
4
import java.util.BitSet;
5 4
import java.util.Iterator;
6 5

  
7 6
import org.gvsig.fmap.dal.DataTypes;
7
import org.gvsig.fmap.dal.exception.DataException;
8 8
import org.gvsig.fmap.dal.exception.ReadException;
9 9
import org.gvsig.fmap.dal.feature.Feature;
10 10
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
11
import org.gvsig.fmap.dal.feature.FeatureQuery;
12
import org.gvsig.fmap.dal.feature.FeatureSet;
11 13
import org.gvsig.fmap.dal.feature.FeatureStore;
12 14
import org.gvsig.fmap.mapcontext.rendering.legend.NullValue;
13 15
import org.gvsig.project.document.table.gui.FeatureTableDocumentPanel;
14
import org.opengis.feature.FeatureCollection;
15 16

  
16 17
import com.iver.andami.PluginServices;
17 18
import com.iver.andami.plugins.Extension;
......
22 23
 * @author Fernando Gonz?lez Cort?s
23 24
 */
24 25
public class TableNumericFieldOperations extends Extension{
26
	private FeatureTableDocumentPanel table;
25 27

  
28

  
26 29
	/**
27 30
	 * @see com.iver.andami.plugins.IExtension#initialize()
28 31
	 */
......
61 64
	 */
62 65

  
63 66
	protected void doExecute(FeatureTableDocumentPanel table){
64
//		FIXME
65
//		int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0);
66
//		int numRows=0;
67
//		FeatureStore fs=null;
68
////		try {
69
//			fs = table.getModel().getStore();
70
////		} catch (ReadException e) {
71
////			e.printStackTrace();
72
////		}
73
//
74
//		FeatureCollection fCollection = (FeatureCollection)fs.getSelection();
75
//		FeatureCollection fCollec=null;
67
		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
//		}
76 174
//		BigDecimal suma = new BigDecimal(0);
77 175
//		BigDecimal min = new BigDecimal(Double.MAX_VALUE);
78 176
//		BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
79
//		Iterator iterator=null;
80
//		if (!fCollection.isEmpty()){
81
//			iterator=fCollection.iterator();
82
//			while (iterator.hasNext()) {
83
//				Feature feature = (Feature) iterator.next();
84
//				if (feature.get(fieldIndex) == null) {
85
//					continue;
86
//				}
87
//				Number number=(Number)feature.get(fieldIndex);
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
//			}
98
//		}else{
99
//
100
//			try {
101
//				fCollec = (FeatureCollection)fs.getDataSet(new String[]{((FeatureAttributeDescriptor)fs.getDefaultFeatureType().get(fieldIndex)).getName()});
102
//			} catch (ReadException e) {
103
//				// TODO Auto-generated catch block
104
//				e.printStackTrace();
105
//			}
106
//
107
//			iterator=fCollec.iterator();
108
//			while (iterator.hasNext()) {
109
//				Feature feature = (Feature) iterator.next();
110
//				if (feature.get(fieldIndex) instanceof NullValue){
111
//					continue;
112
//				}
113
//				Number number=(Number)feature.get(fieldIndex);
114
//				double d=number.doubleValue();
115
//				numRows++;
116
//				suma = suma.add(new BigDecimal(d));
117
//				if (d < min.doubleValue()) {
118
//					min = new BigDecimal(d);
119
//				}
120
//				if (d > max.doubleValue()) {
121
//					max = new BigDecimal(d);
122
//				}
123
//			}
177
//		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);
124 185
//		}
125
//			Statistics st = new Statistics();
126
//			BigDecimal media = new BigDecimal(0);
127
//			BigDecimal varianza = new BigDecimal(0);
128
//			double desviacion = 0;
129
//			if (numRows==0) {
130
//				suma = new BigDecimal(0);
131
//				min = new BigDecimal(0);
132
//				max = new BigDecimal(0);
133
//			}else {
134
//				media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
135
//				varianza = new BigDecimal(0);
136
//				if (!fCollection.isEmpty()){
137
//					iterator=fCollection.iterator();
138
//				}else{
139
//					iterator=fCollec.iterator();
140
//				}
141
//				while (iterator.hasNext()) {
142
//					Feature feature = (Feature) iterator.next();
143
//					if (feature.get(fieldIndex) instanceof NullValue){
144
//						continue;
145
//					}
146
//					Number number=(Number)feature.get(fieldIndex);
147
//					double d=number.doubleValue();
148
//
149
//					BigDecimal dif = new BigDecimal(d).subtract(media);
150
//					varianza = dif.multiply(dif).add(varianza);
151
//				}
152
//				varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
153
//				desviacion = Math.sqrt(varianza.doubleValue());
154
//			}
155
//			st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
156
//			PluginServices.getMDIManager().addWindow(st);
157
////		}else{
158
////
159
////		}
160
//
161
////		BitSet selectedRows = (BitSet)ds.getSelection().clone();
162
////		try {
163
////			int rc = (int) ds.getRowCount();
164
////			//Si no hay selecci?n se usan todos los registros
165
////			if (selectedRows.cardinality() == 0){
166
////				selectedRows.set(0, rc);
167
////			}else{
168
////				rc = selectedRows.cardinality();
169
////			}
170
////			BigDecimal suma = new BigDecimal(0);
171
////			BigDecimal min = new BigDecimal(Double.MAX_VALUE);
172
////			BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
173
////			for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
174
////				if (fs.getFieldValue(i, fieldIndex) instanceof NullValue)
175
////					continue;
176
////				numRows++;
177
////				double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
178
////				suma = suma.add(new BigDecimal(d));
179
////				if (d < min.doubleValue()) min = new BigDecimal(d);
180
////				if (d > max.doubleValue()) max = new BigDecimal(d);
181
////			}
182
////			Statistics st = new Statistics();
183
////			BigDecimal media = new BigDecimal(0);
184
////			BigDecimal varianza = new BigDecimal(0);
185
////			double desviacion = 0;
186
////			if (numRows==0) {
187
////				suma = new BigDecimal(0);
188
////				min = new BigDecimal(0);
189
////				max = new BigDecimal(0);
190
////			}else {
191
////				media = suma.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
192
////				varianza = new BigDecimal(0);
193
////				for(int i=selectedRows.nextSetBit(0); i>=0; i=selectedRows.nextSetBit(i+1)) {
194
////					if (ds.getFieldValue(i, fieldIndex) instanceof NullValue)
195
////						continue;
196
////					double d = ((NumericValue) ds.getFieldValue(i, fieldIndex)).doubleValue();
197
////					BigDecimal dif = new BigDecimal(d).subtract(media);
198
////					varianza = dif.multiply(dif).add(varianza);
199
////				}
200
////				varianza = varianza.divide(new BigDecimal(numRows), BigDecimal.ROUND_HALF_DOWN);
201
////				desviacion = Math.sqrt(varianza.doubleValue());
202
////			}
203
////
204
////			st.setStatistics(media.doubleValue(), max.doubleValue(), min.doubleValue(), varianza.doubleValue(), desviacion, numRows, new BigDecimal(max.doubleValue()).subtract(min).doubleValue(), suma.doubleValue());
205
////			PluginServices.getMDIManager().addWindow(st);
206
////
207
////		} catch (ReadDriverException e) {
208
////			NotificationManager.addError("No se pudo acceder a los datos", e);
209
////		}
186
//		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
//		}
210 214
	}
211 215

  
212 216
	/**
213 217
	 * @see com.iver.andami.plugins.IExtension#isEnabled()
214 218
	 */
215 219
	public boolean isEnabled() {
216
		IWindow v = PluginServices.getMDIManager().getActiveWindow();
217

  
218
		if (v == null) {
219
			return false;
220
		}
221

  
222
		if (v instanceof FeatureTableDocumentPanel) {
223
			FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) v;
224
			return doIsEnabled(table);
225
		}
226
		return false;
220
		return doIsEnabled(table);
227 221
	}
228 222

  
229 223
	protected boolean doIsEnabled(FeatureTableDocumentPanel table){
230 224
//		FIXME
231
//		BitSet indices = table.getSelectedFieldIndices();
232
//		// System.out.println("TableNumericFieldOperations.isEnabled: Tabla: " + table.getModel().getModelo().getRecordset().getName() );
233
//		if (indices.cardinality() == 1){
234
////			try {
225
		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){
235 233
//				int index=indices.nextSetBit(0);
236 234
//				if (table.getModel().getStore().getDefaultFeatureType().size()<index+1) {
237 235
//					return false;
238 236
//				}
239
//				int type = ((FeatureAttributeDescriptor)table.getModel().getStore().getDefaultFeatureType().get(index)).getDataType();
240
//				if ((type==DataTypes.LONG) ||
241
//						(type== DataTypes.DOUBLE) ||
242
//								(type==DataTypes.FLOAT) ||
243
//										(type==DataTypes.INT)){
244
//					return true;
245
//				}
246
////			} catch (ReadException e) {
247
////				return false;
248
////			}
249
//		}
237
				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
		}
250 245
		return false;
251 246
	}
252 247

  
......
256 251
	 */
257 252
	public boolean isVisible() {
258 253
		IWindow v = PluginServices.getMDIManager().getActiveWindow();
259

  
260
		if (v == null) {
261
			return false;
262
		}
263

  
264
		if (v instanceof FeatureTableDocumentPanel) {
254
		if (v!=null && v instanceof FeatureTableDocumentPanel) {
255
			table=(FeatureTableDocumentPanel)v;
265 256
			return true;
266 257
		}
267 258
		return false;

Also available in: Unified diff