Revision 8074 trunk/libraries/libUI/src/org/gvsig/gui/beans/filterPanel/AbstractFilterQueryJPanel.java

View differences:

AbstractFilterQueryJPanel.java
3 3
import java.awt.Dimension;
4 4
import java.awt.GridBagConstraints;
5 5
import java.awt.GridBagLayout;
6
import java.awt.event.MouseAdapter;
7
import java.awt.event.MouseEvent;
8
import java.awt.event.MouseListener;
6 9
import java.beans.PropertyChangeEvent;
7 10
import java.beans.PropertyChangeListener;
8 11
import java.io.Serializable;
9 12
import java.util.Vector;
10 13

  
11 14
import javax.swing.BorderFactory;
15
import javax.swing.DefaultListModel;
12 16
import javax.swing.JPanel;
17
import javax.swing.tree.DefaultMutableTreeNode;
18
import javax.swing.tree.DefaultTreeCellRenderer;
19
import javax.swing.tree.DefaultTreeModel;
13 20

  
14 21
import org.gvsig.gui.beans.filterPanel.filterButtons.FilterButtonsJPanel;
15 22

  
......
92 99
	protected javax.swing.JTree fieldsJTree = null;
93 100
	protected javax.swing.JList valuesJList = null;
94 101
	protected javax.swing.JScrollPane fieldsJScrollPane = null;
95
	protected javax.swing.JScrollPane valuesJScrollPane = null;
96
	
97
	protected Vector fields;
102
	protected javax.swing.JScrollPane valuesJScrollPane = null;		
103

  
98 104
	protected String title;
99 105
	
106
	protected DefaultMutableTreeNode jtreeRoot;
107
	protected DefaultTreeModel defaultTreeModel;
108
	protected LayerFeaturesFields layerFeaturesFields;
109
	
110
	
100 111
	/**
101 112
	 * This is the default constructor
102 113
	 */
......
128 139
		gridBagConstraints.anchor = GridBagConstraints.SOUTH;
129 140
		this.add(getBottomJPanel(), gridBagConstraints);
130 141
		
131
		fields = new Vector();
142
		layerFeaturesFields = new LayerFeaturesFields();
132 143
	}
133 144
	
134 145
	/**
......
239 250
	 *
240 251
	 * @return javax.swing.JTree
241 252
	 */
242
	protected abstract javax.swing.JTree getFieldsJTree();
253
	/*
254
	 *  (non-Javadoc)
255
	 * @see org.gvsig.gui.beans.filterPanel.AbstractFilterQueryJPanel#getLstCampos()
256
	 */
257
	protected javax.swing.JTree getFieldsJTree() {
258
		if (fieldsJTree == null) {
259
			fieldsJTree = new javax.swing.JTree(new Vector(0,1));
260
			
261
			// Remove icons:
262
			DefaultTreeCellRenderer defaultTreeCellRenderer = new DefaultTreeCellRenderer();
263
			defaultTreeCellRenderer.setOpenIcon(null);
264
			defaultTreeCellRenderer.setClosedIcon(null);
265
			defaultTreeCellRenderer.setLeafIcon(null);
266
			
267
			// Root not visible
268
			fieldsJTree.setRootVisible(false);
269
			fieldsJTree.setCellRenderer(defaultTreeCellRenderer);
270
			
271
			jtreeRoot = new DefaultMutableTreeNode(fieldsJTree.getModel().getRoot());
272
			
273
			fieldsJTree.addMouseListener(new MouseAdapter() {
274
				/*
275
				 *  (non-Javadoc)
276
				 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
277
				 */
278
				public void mouseClicked(MouseEvent e) {
279
					int row = fieldsJTree.getRowForLocation(e.getX(), e.getY());
280
					
281
					if (row > -1) {
282
						Vector values = layerFeaturesFields.getValues(row);
283
						
284
						((DefaultListModel)valuesJList.getModel()).clear();
285
						
286
						for (int i = 0; i < values.size(); i++)
287
							((DefaultListModel)valuesJList.getModel()).addElement(values.get(i).toString());
288
					}
289
				}
290
			});
291
		}
292

  
293
		return fieldsJTree;
294
	}
243 295
	
244 296
	/**
245 297
	 * This method initializes valuesJList
246 298
	 *
247 299
	 * @return javax.swing.JList
248 300
	 */
249
	protected abstract javax.swing.JList getValuesJList();
301
	protected javax.swing.JList getValuesJList() {
302
		if (valuesJList == null) {
303
			valuesJList = new javax.swing.JList(new DefaultListModel());
304
			
305
			valuesJList.addMouseListener(new MouseAdapter() {
306
				/*
307
				 *  (non-Javadoc)
308
				 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
309
				 */
310
				public void mouseClicked(MouseEvent e) {
311
					if (e.getClickCount() == 2) {
312
						int row = fieldsJTree.getRowForLocation(e.getX(), e.getY());
250 313
	
314
						if (row > -1) {
315
							putSymbol(((DefaultListModel)valuesJList.getModel()).get(row).toString());
316
						}
317
					}
318
				}
319
			});
320
		}
321

  
322
		return valuesJList;
323
	}
324
	
251 325
    /**
252 326
     * Inserta una string en otra en la posici?n indicada
253 327
     *
......
343 417
			valuesJScrollPane.setPreferredSize(new Dimension(valuesJScrollPane.getPreferredSize().width, valuesJScrollPane.getPreferredSize().height + difference));
344 418
		}
345 419
	}
420

  
421
	
422
	/**
423
	 * This class cointains the data of features of a layer
424
	 * 
425
	 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
426
	 */
427
	protected class LayerFeaturesFields {
428
		Vector layerFeaturesFields;
429
		Vector layerFeaturesValues;
430
		
431
		/**
432
		 * @see Vector#Vector()
433
		 */
434
		public LayerFeaturesFields() {
435
			initialize();
436
		}
437

  
438
		/**
439
		 * Initializes the attributes of this class
440
		 */
441
		private void initialize() {
442
			layerFeaturesFields = new Vector(0, 1);
443
			layerFeaturesValues = new Vector(0, 1);
444
		}
445

  
446
		/**
447
		 * Adds a new field
448
		 * 
449
		 * @param _field An object
450
		 */
451
		public void addField(Object _field) {
452
			layerFeaturesFields.add(_field);
453
			layerFeaturesValues.add(new Vector(0, 1));
454
		}
455
		
456
		/**
457
		 * Adds a new field
458
		 * 
459
		 * @param _field An object
460
		 * @param _values A Vector
461
		 */
462
		public void addFieldAndValues(Object _field, Vector _values) {
463
			layerFeaturesFields.add(_field);
464
			layerFeaturesValues.add(_values);
465
		}
466
		
467
		/**
468
		 * Adds a new value for a field
469
		 * 
470
		 * @param _field An object
471
		 * @param _values An object
472
		 */
473
		public void addValue(Object _field, Object _value) {
474
			Vector values = this.getValues(_field);
475
			
476
			if (values != null) {
477
				values.add(_value);	
478
			}
479
		}
480

  
481
		/**
482
		 * Adds a values for a field
483
		 * 
484
		 * @param _field An object
485
		 * @param _values A vector
486
		 */
487
		public void addValues(Object _field, Vector _values) {
488
//			layerFeaturesValues.add(getFieldIndex(_field), _values);
489
			int index = getFieldIndex(_field);
490
			
491
			layerFeaturesValues.remove(index);
492
			layerFeaturesValues.add(index, _values);
493
		}
494
		
495
		/**
496
		 * Returns a field
497
		 * 
498
		 * @param fieldPosition A position
499
		 * @return An object
500
		 */
501
		public Object getField(int fieldPosition) {
502
			if (fieldPosition == -1)
503
				return null;
504
			else
505
				return layerFeaturesFields.get(fieldPosition);
506
		}
507
		
508
		/**
509
		 * Returns the values associated to a field
510
		 * 
511
		 * @param fieldPosition A position
512
		 * @return A Vector
513
		 */
514
		public Vector getValues(int fieldPosition) {
515
			if (fieldPosition == -1)
516
				return null;
517
			else
518
				return (Vector)layerFeaturesValues.get(fieldPosition);
519
		}
520

  
521
		/**
522
		 * Returns the values associated to a field
523
		 * 
524
		 * @param field An object
525
		 * @return A Vector
526
		 */
527
		public Vector getValues(Object field) {
528
			return (Vector)layerFeaturesValues.get(this.getFieldIndex(field));
529
		}
530

  
531
		
532
		/**
533
		 * Returns the index of a 'field'
534
		 * 
535
		 * @param _field An object
536
		 * @return A position
537
		 */
538
		public int getFieldIndex(Object _field) {
539
			return layerFeaturesFields.indexOf(_field);
540
		}
541
		
542
		/**
543
		 * Removes a field
544
		 * 
545
		 * @param fieldPosition A position
546
		 */
547
		public void removeField(int fieldPosition) {
548
			layerFeaturesFields.remove(fieldPosition);
549
			layerFeaturesValues.remove(fieldPosition);
550
		}
551
		
552
		/**
553
		 * Removes a field
554
		 * 
555
		 * @param field An object
556
		 */
557
		public void removeField(Object field) {
558
			layerFeaturesValues.remove(this.getFieldIndex(field));
559
			layerFeaturesFields.remove(field);
560
		}
561
		
562
		/**
563
		 * @see Vector#clear()
564
		 */
565
		public void clear() {
566
			layerFeaturesFields.clear();
567
			layerFeaturesValues.clear();
568
		}
569
	}	
346 570
}

Also available in: Unified diff