Revision 2076 branches/gvSIG_03_SLD/applications/appgvSIG/src/com/iver/cit/gvsig/gui/thememanager/legendmanager/panels/FPanelLegendBreaks.java

View differences:

FPanelLegendBreaks.java
56 56
import java.text.NumberFormat;
57 57
import java.util.ArrayList;
58 58
import java.util.Date;
59
import java.util.TreeMap;
60 59

  
61 60
import javax.swing.DefaultComboBoxModel;
62 61
import javax.swing.JButton;
......
68 67

  
69 68
import org.apache.log4j.Logger;
70 69

  
71
import com.hardcode.driverManager.DriverLoadException;
72 70
import com.hardcode.gdbms.engine.data.DataSource;
73 71
import com.hardcode.gdbms.engine.values.DoubleValue;
74 72
import com.hardcode.gdbms.engine.values.FloatValue;
......
77 75
import com.hardcode.gdbms.engine.values.NullValue;
78 76
import com.hardcode.gdbms.engine.values.Value;
79 77
import com.iver.andami.PluginServices;
80
import com.iver.andami.Utilities;
81 78
import com.iver.cit.gvsig.fmap.DriverException;
82
import com.iver.cit.gvsig.fmap.core.FShape;
83
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
84 79
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
85
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
86 80
import com.iver.cit.gvsig.fmap.layers.FLayer;
87 81
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
88 82
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
......
102 96
 */
103 97
public class FPanelLegendBreaks extends JPanel implements ILegendPanel {
104 98
	private static Logger logger = Logger.getLogger(FPanelLegendBreaks.class.getName());
105
	final static int EQUAL_INTERVALS = 0;
106
	final static int NATURAL_INTERVALS = 1;
107
	final static int QUANTILE_INTERVALS = 2;
108 99
	private MyListener listener = new MyListener();
109 100

  
110 101
	// private TOC m_TOC;
111 102
	private VectorialIntervalLegend m_Renderer;
112 103
	private ClassifiableVectorial m_lyr;
113
	private FSymbolTable m_symbolTable = new FSymbolTable();
104
	private FSymbolTable m_symbolTable = new FSymbolTable("intervals");
114 105
	private JComboBox m_cboFields;
106
	private JComboBox m_cboIntervalType;
115 107
	private JTextField m_txtNumIntervals;
116 108
	private ColorChooserPanel m_colorChooser1;
117 109
	private ColorChooserPanel m_colorChooser2;
118 110
	private JButton m_btnDeleteAll;
119 111
	private JButton m_btnDelete;
120 112
	private FInterval[] intervals;
113
	///private int tipoClasificacion = VectorialIntervalLegend.NATURAL_INTERVALS;
114
	private int count = 0;
121 115

  
122 116
	/**
123 117
	 *
......
128 122
	}
129 123

  
130 124
	/**
131
	 * DOCUMENT ME!
125
	 * EQUAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
126
	 * quieren crear. Los intervalos se crean con un tama?o igual entre ellos.
132 127
	 *
133
	 * @param numIntervals DOCUMENT ME!
134
	 * @param minValue DOCUMENT ME!
135
	 * @param maxValue DOCUMENT ME!
128
	 * @param numIntervals n?mero de intervalos
129
	 * @param minValue Valor m?nimo.
130
	 * @param maxValue Valor m?ximo.
136 131
	 *
137
	 * @return DOCUMENT ME!
132
	 * @return Array con los intervalos.
138 133
	 */
139
	static FInterval[] calculateEqualIntervals(int numIntervals,
134
	FInterval[] calculateEqualIntervals(int numIntervals,
140 135
		double minValue, double maxValue) {
141 136
		FInterval[] theIntervalArray = new FInterval[numIntervals];
142 137
		double step = (maxValue - minValue) / numIntervals;
......
150 145
	}
151 146

  
152 147
	/**
148
	 * NATURAL INTERVAL Devuelve un Array con el n?mero de intervalos que se
149
	 * quieren crear. Los intervalos se distribuyen de forma natural.
150
	 *
151
	 * @param numIntervals n?mero de intervalos
152
	 * @param minValue Valor m?nimo.
153
	 * @param maxValue Valor m?ximo.
154
	 *
155
	 * @return Array con los intervalos.
156
	 */
157
	FInterval[] calculateNaturalIntervals(int numIntervals, double minValue,
158
		double maxValue,String fieldName) {
159
		
160
		NaturalIntervalGenerator intervalGenerator = new NaturalIntervalGenerator(((AlphanumericData) m_lyr),fieldName,numIntervals);
161

  
162
		try {
163
			intervalGenerator.generarIntervalos();
164
		} catch (DriverException e) {
165
			e.printStackTrace();
166
		} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
167
			e.printStackTrace();
168
		}
169
		int numIntervalsGen=intervalGenerator.getNumIntervals()-1;
170
		if (numIntervalsGen==-1){
171
			//TODO cuando no puede calcular los intervalos.
172
			numIntervalsGen=1;
173
		}
174
		FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
175
		if (numIntervalsGen>1){
176
		
177
		theIntervalArray[0] = new FInterval(minValue, intervalGenerator.getValorRuptura(0));
178
		for (int i = 1; i < numIntervalsGen-1; i++) {
179
			theIntervalArray[i] = new FInterval(intervalGenerator.getValorRuptura(
180
						i-1)+0.01, intervalGenerator.getValorRuptura(i));
181
		}
182
		
183
		theIntervalArray[numIntervalsGen-1] = new FInterval(intervalGenerator.getValorRuptura(
184
				numIntervalsGen-2)+0.01, maxValue);
185
		}else{
186
			theIntervalArray[numIntervalsGen-1] = new FInterval(minValue, maxValue);
187
		}
188
		return theIntervalArray;
189
	}
190

  
191
	/**
192
	 * QUANTILE INTERVAL Devuelve un Array con el n?mero de intervalos que se
193
	 * quieren crear. Los intervalos se distribuyen de forma quantile.
194
	 *
195
	 * @param numIntervals n?mero de intervalos
196
	 * @param minValue Valor m?nimo.
197
	 * @param maxValue Valor m?ximo.
198
	 *
199
	 * @return Array con los intervalos.
200
	 */
201
	FInterval[] calculateQuantileIntervals(int numIntervals, double minValue,
202
		double maxValue,String fieldName) {
203
		QuantileIntervalGenerator intervalGenerator = new QuantileIntervalGenerator(((AlphanumericData) m_lyr),fieldName,numIntervals);
204

  
205
		try {
206
			intervalGenerator.generarIntervalos();
207
		} catch (DriverException e) {
208
			e.printStackTrace();
209
		} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
210
			e.printStackTrace();
211
		}
212
		int numIntervalsGen=intervalGenerator.getNumIntervalGen();
213
		FInterval[] theIntervalArray = new FInterval[numIntervalsGen];
214
		if (intervalGenerator.getNumIntervalGen()>1){
215
		theIntervalArray[0] = new FInterval(minValue, intervalGenerator.getNumRuptura(0));
216
		for (int i = 1; i < numIntervalsGen-1; i++) {
217
			theIntervalArray[i] = new FInterval(intervalGenerator.getNumRuptura(
218
						i-1)+0.01, intervalGenerator.getNumRuptura(i));
219
		}
220
		
221
		theIntervalArray[numIntervalsGen-1] = new FInterval(intervalGenerator.getNumRuptura(
222
				numIntervalsGen-2)+0.01, maxValue);
223
		}else{
224
			theIntervalArray[numIntervalsGen-1] = new FInterval(minValue, maxValue);
225
		}
226
		return theIntervalArray;
227
		
228
		
229
	/*	double numQ = count / numIntervals;
230

  
231
		for (int i = 0; i < count; i++) {
232
		}
233

  
234
		return theIntervalArray;
235
*/
236
	}
237

  
238
	/**
153 239
	 * DOCUMENT ME!
154 240
	 */
155 241
	protected void initComponents() {
......
219 305

  
220 306
		c.anchor = GridBagConstraints.WEST;
221 307

  
222
		JLabel lblFieldClassification = new JLabel(PluginServices.getText(this,
223
					"Campo_de_clasificacion"));
308
		JLabel lblFieldClassification = new JLabel(PluginServices.getText(
309
					this, "Campo_de_clasificacion"));
224 310
		c.gridx = 0;
225 311
		c.gridy = 0;
226 312
		c.insets = new Insets(5, 5, 2, 2);
......
247 333
		m_txtNumIntervals.setText("5");
248 334
		pAux1.add(m_txtNumIntervals);
249 335

  
336
		JLabel lblIntervalType = new JLabel(PluginServices.getText(this,
337
					"tipo_de_intervalo"));
338
		c.gridx = 0;
339
		c.gridy = 1;
340
		c.insets = new Insets(5, 5, 2, 2);
341
		c.gridwidth = 1;
342
		gridBag.setConstraints(lblIntervalType, c);
343
		pAux1.add(lblIntervalType);
344

  
345
		m_cboIntervalType = new JComboBox();
346
		m_cboIntervalType.setActionCommand("INTERVAL_TYPE");
347
		m_cboIntervalType.addActionListener(listener);
348
		m_cboIntervalType.addItem("EQUAL_INTERVALS");
349
		m_cboIntervalType.addItem("NATURAL_INTERVALS");
350
		m_cboIntervalType.addItem("QUANTILE_INTERVALS");
351
		m_cboIntervalType.setVisible(true);
352

  
353
		c.gridx = 1;
354
		c.gridy = 1;
355
		gridBag.setConstraints(m_cboIntervalType, c);
356
		pAux1.add(m_cboIntervalType);
357

  
250 358
		c.insets = new Insets(2, 2, 2, 2);
251 359

  
252
		JLabel lblColor1 = new JLabel(PluginServices.getText(this, "Color_inicio") +
253
				":");
360
		JLabel lblColor1 = new JLabel(PluginServices.getText(this,
361
					"Color_inicio") + ":");
254 362
		c.gridx = 0;
255
		c.gridy = 1;
363
		c.gridy = 2;
256 364
		gridBag.setConstraints(lblColor1, c);
257 365
		pAux1.add(lblColor1);
258 366

  
......
260 368
		m_colorChooser1.setColor(Color.red);
261 369
		m_colorChooser1.setAlpha(255);
262 370
		c.gridx = 1;
263
		c.gridy = 1;
371
		c.gridy = 2;
264 372
		gridBag.setConstraints(m_colorChooser1, c);
265 373
		pAux1.add(m_colorChooser1);
266 374

  
267 375
		JLabel lblColor2 = new JLabel(PluginServices.getText(this, "Color_final") +
268 376
				":");
269 377
		c.gridx = 3;
270
		c.gridy = 1;
378
		c.gridy = 2;
271 379
		gridBag.setConstraints(lblColor2, c);
272 380
		pAux1.add(lblColor2);
273 381

  
......
275 383
		m_colorChooser2.setColor(Color.blue);
276 384
		m_colorChooser2.setAlpha(255);
277 385
		c.gridx = 4;
278
		c.gridy = 1;
386
		c.gridy = 2;
279 387
		gridBag.setConstraints(m_colorChooser2, c);
280 388
		pAux1.add(m_colorChooser2);
281 389

  
......
295 403
		FInterval[] arrayIntervalos = null;
296 404
		DataSource elRs;
297 405
		m_symbolTable.removeAllItems();
298
		
406

  
299 407
		try {
300 408
			elRs = ((AlphanumericData) m_lyr).getRecordset();
301 409
			logger.debug("elRs.start()");
302 410
			elRs.start();
303 411

  
304 412
			int idField = -1;
305

  
306
			int numIntervalos = Integer.parseInt(m_txtNumIntervals.getText());
307
			int tipoClasificacion = EQUAL_INTERVALS;
308

  
413
			int numIntervalos=1;
414
			try{
415
			 numIntervalos= Integer.parseInt(m_txtNumIntervals.getText());
416
			}catch (NumberFormatException e) {
417
				System.out.println("Foramto de n?mero erroneo");
418
			}
309 419
			String fieldName = (String) m_cboFields.getSelectedItem();
310 420
			System.out.println("Nombre del campo de clasificaci?n: " +
311 421
				fieldName);
......
317 427
			// Creamos el primer y ?ltimo color.
318 428
			Color startColor = m_colorChooser1.getColor();
319 429
			m_Renderer.setStartColor(startColor);
430

  
320 431
			Color endColor = m_colorChooser2.getColor();
321 432
			m_Renderer.setEndColor(endColor);
322
			int r;
323
			int g;
324
			int b;
325
			int stepR;
326
			int stepG;
327
			int stepB;
328
			r = startColor.getRed();
329
			g = startColor.getGreen();
330
			b = startColor.getBlue();
331
			stepR = (endColor.getRed() - r) / numIntervalos;
332
			stepG = (endColor.getGreen() - g) / numIntervalos;
333
			stepB = (endColor.getBlue() - b) / numIntervalos;
334 433

  
434
			
435

  
335 436
			for (int i = 0; i < elRs.getFieldCount(); i++) {
336 437
				String nomAux = elRs.getFieldName(i).trim();
337 438

  
......
350 451

  
351 452
			///m_symbolTable.removeAllItems();
352 453
			///m_valuesCache = new Object[m_lyr.getSource().getShapeCount()];
353

  
354 454
			int numSymbols = 0;
355 455
			FSymbol theSymbol;
356 456
			Date valorDate;
......
361 461

  
362 462
			VectorialIntervalLegend auxLegend;
363 463
			auxLegend = LegendFactory.createVectorialIntervalLegend(m_lyr.getShapeType());
364
			
464

  
365 465
			Value clave;
366 466
			Object resul;
367 467
			int symbolType = 0;
368
			
468
			count = 0;
369 469

  
370 470
			for (int j = 0; j < elRs.getRowCount(); j++) {
371 471
				clave = elRs.getFieldValue(j, idField);
......
375 475
				////Comprobar que no esta repetido y no hace falta introducir en el hashtable el campo junto con el simbolo.
376 476
				if (auxLegend.getSymbolByInterval(interval) == null) {
377 477
					//si no esta creado el simbolo se crea
378
					double valor=0;
478
					double valor = 0;
379 479
					symbolType = m_lyr.getShapeType();
380 480

  
381 481
					if (clave.getClass().getName() == "com.hardcode.gdbms.engine.values.IntValue") {
......
392 492
						///if (valorDate.before(minValueDate)) minValueDate = valorDate;
393 493
						///if (valorDate.after(maxValueDate)) maxValueDate = valorDate;
394 494
					}
495

  
395 496
					if (valor < minValue) {
396 497
						minValue = (double) valor;
397 498
					}
499

  
398 500
					if (valor > maxValue) {
399 501
						maxValue = (double) valor;
400 502
					}
401 503
				}
504

  
505
				count++;
402 506
			}
403 507

  
404
			switch (tipoClasificacion) {
405
				case EQUAL_INTERVALS:
508
			switch (m_Renderer.getIntervalType()) {
509
				case VectorialIntervalLegend.EQUAL_INTERVALS:
406 510
					arrayIntervalos = calculateEqualIntervals(numIntervalos,
407 511
							minValue, maxValue);
408 512

  
409 513
					break;
410 514

  
411
				case NATURAL_INTERVALS:
515
				case VectorialIntervalLegend.NATURAL_INTERVALS:
516
					arrayIntervalos = calculateNaturalIntervals(numIntervalos,
517
							minValue, maxValue,fieldName);
412 518

  
413
					// POR HACER
414 519
					break;
520

  
521
				case VectorialIntervalLegend.QUANTILE_INTERVALS:
522
					arrayIntervalos = calculateQuantileIntervals(numIntervalos,
523
							minValue, maxValue,fieldName);
524

  
525
					break;
415 526
			}
416 527

  
417 528
			intervals = arrayIntervalos;
......
419 530
			FInterval elIntervalo;
420 531
			NumberFormat.getInstance().setMaximumFractionDigits(2);
421 532
			m_Renderer.clear();
533

  
534
			int r;
535
			int g;
536
			int b;
537
			int stepR;
538
			int stepG;
539
			int stepB;
540
			r = startColor.getRed();
541
			g = startColor.getGreen();
542
			b = startColor.getBlue();
543
			stepR = (endColor.getRed() - r) / arrayIntervalos.length;
544
			stepG = (endColor.getGreen() - g) / arrayIntervalos.length;
545
			stepB = (endColor.getBlue() - b) / arrayIntervalos.length;
422 546
			for (int k = 0; k < arrayIntervalos.length; k++) {
423 547
				elIntervalo = arrayIntervalos[k];
424 548

  
......
440 564

  
441 565
				/////////////////////////////////
442 566
				m_Renderer.addSymbol(elIntervalo, theSymbol);
443
				System.out.println("addSymbol = "+elIntervalo+" theSymbol = "+theSymbol.getDescription());
567
				System.out.println("addSymbol = " + elIntervalo +
568
					" theSymbol = " + theSymbol.getDescription());
444 569
				numSymbols++;
445 570

  
446 571
				if (numSymbols > 100) {
......
512 637
		}
513 638
	}
514 639

  
515

  
516 640
	/**
517 641
	 * DOCUMENT ME!
518 642
	 */
......
526 650
			rs.start();
527 651

  
528 652
			nomFields = new ArrayList();
653

  
529 654
			Value val;
530 655

  
531 656
			for (int i = 0; i < rs.getFieldCount(); i++) {
532 657
				//TODO deber?a de existir un getFieldType
533
				val = rs.getFieldValue(0,i);
534
				if (val.getClass() == NullValue.class) continue;
658
				val = rs.getFieldValue(0, i);
659

  
660
				if (val.getClass() == NullValue.class) {
661
					continue;
662
				}
663

  
535 664
				if ((val.getClass() == IntValue.class) ||
536 665
						(val.getClass() == DoubleValue.class) ||
537 666
						(val.getClass() == FloatValue.class) ||
......
595 724
				e.printStackTrace();
596 725
			}
597 726
		}
727
		m_cboIntervalType.setSelectedIndex(m_Renderer.getIntervalType());
598 728
	}
599 729

  
600 730
	/**
......
636 766
				m_symbolTable.removeAllItems();
637 767

  
638 768
				m_Renderer.setFieldName(fieldName);
769
			} else if (e.getActionCommand() == "INTERVAL_TYPE") {
770
				JComboBox cb = (JComboBox) e.getSource();
771
				
772
				if (m_Renderer!=null && cb.getSelectedIndex() != m_Renderer.getIntervalType()) {
773
					m_Renderer.setIntervalType(cb.getSelectedIndex());
774
					m_symbolTable.removeAllItems();
775
				}
776
				
639 777
			}
640 778

  
641 779
			//A?adir todos los elementos por valor

Also available in: Unified diff