Revision 6459 trunk/libraries/libUI/src/org/gvsig/gui/beans/SearcheableComboBox/JComboBoxSearcheableDynamic.java

View differences:

JComboBoxSearcheableDynamic.java
61 61
 */
62 62

  
63 63
/**
64
* This class allows users to insert a string and show all the sentences of the JComboBox
65
*   that match its first ones characters with the string.
64
* This class allows users to insert a chain of characters and show all the sentences of the JComboBox that match its first ones characters with it.
66 65
* This class is also a Java Bean.
67 66
* The behavior of this componet can be configurated by 6 flags:
68 67
*  + The appearence of the items showed at the beginning and when the user presses the ESCAPE key: (This is configured with 2 flags -> 3 different states are possible) 
......
76 75
*  + Key Sensitive or not when the user writes 
77 76
*      -> key_Sensitive -> When we write on the TextField it can discriminate upper cases from down cases (if true) or not (if false) (This is configured with 1 flag -> 2 different states are possible)
78 77
*  + Use one color (black) or 2 colors (black by default and red if the string written doesn't match with the beginning of all items)
79
*       -> only_One_Color -> The text on the textField only will be showed on black color (if true); false -> if the text on the textField doesn't match with any current item of this component -> the text
78
*      -> only_One_Color -> The text on the textField only will be showed on black color (if true); false -> if the text on the textField doesn't match with any current item of this component -> the text
80 79
*            will be showed on red color
81 80
* 
82 81
* Combinations of flags not allowed:
......
102 101
	private boolean onlyOneColor;
103 102
	private boolean showAllItems;
104 103
	private boolean allItemsAreSorted;
104
	private int lastSelectedItem;
105 105
	private JTextField txtField;
106 106
	
107 107
	/**
......
215 215
			
216 216
			// By default all items showed aren't sorted
217 217
			allItemsAreSorted = false;
218
			
219
			// Set the last selected item
220
			lastSelectedItem = 0;
218 221
		}
219 222
		catch(Exception e)
220 223
		{
......
367 370
	}
368 371
	
369 372
	/**
370
	 * This private method finds the first item (string) of the vector of the model of this component that it start matches with a chain of characters
373
	 * This private method finds the first item of the vector of the model of this component that it start matches with a chain of characters
371 374
	 * 
372 375
	 * @param String text
373 376
	 * @return int The position of the first coincidence or -1 if hasn't find any coincidence
......
395 398
			// Find the first match
396 399
			while ((first_position < this.getItemCount()) && (matchesTheStart == false))
397 400
			{					
398
				if ((((String)this.getItemAt(first_position)).toUpperCase()).startsWith(text.toUpperCase()))
401
				if (((this.getItemAt(first_position)).toString().toUpperCase()).startsWith(text.toUpperCase()))
399 402
					matchesTheStart = true;
400 403
				else
401 404
					first_position++; // Increments index
......
598 601
		if (ke.getKeyCode() == KeyEvent.VK_DOWN) {
599 602
		    // If the key (typed, pressed and) released has been the Down Key -> when there isn't any text (item of the list) selected, select the first 
600 603
			ComboBoxModel model = this.getModel();
601
			if ((this.getItemCount() > 0) && (this.getSelectedIndex() == (this.getItemCount() - 1))) {
604
			if ((this.getItemCount() > 0) && (lastSelectedItem == (this.getItemCount()-1))) {
602 605
				this.setSelectedIndex(0);
603 606
			}
604
			setSelectedItem(model.getSelectedItem());
607
			
608
			lastSelectedItem = this.getSelectedIndex();
605 609
			this.showPopup();
606 610
		} else if (ke.getKeyCode() == KeyEvent.VK_UP) {
607 611
		    // If the key (typed, pressed and) released has been the Up Key -> when there isn't any text (item of the list) selected, select the last 
608 612
			ComboBoxModel model = this.getModel();
609
			if (this.getSelectedIndex() == 0) {
610
				int numberItems = this.getItemCount();
611
				this.setSelectedIndex(numberItems-1);				
613
			
614
			if (lastSelectedItem == 0) {
615
				this.setSelectedIndex(this.getItemCount()-1);				
612 616
			}
613
			setSelectedItem(model.getSelectedItem());
617
			
618
			lastSelectedItem = this.getSelectedIndex();
614 619
			this.showPopup();
615 620
		} else if (ke.getKeyChar() == KeyEvent.VK_ESCAPE) {
616 621
			// If the key (typed, pressed and) released has been the Escape Key -> hide the popup list and empty the JTextField			
......
689 694
						// If there is key sensitive
690 695
						while (i < objs.length)
691 696
						{					
692
							s = (String)objs[i];
697
							s = objs[i].toString();
693 698
							if (s.startsWith(text))
694 699
							{
695 700
								removedItems.remove(objs[i]);
......
706 711
						// If there isn't key sensitive
707 712
						while (i < objs.length)
708 713
						{					
709
							s = ((String)objs[i]).toUpperCase();
714
							s = (objs[i].toString().toUpperCase());
710 715
							if (s.startsWith(text.toUpperCase()))
711 716
							{
712 717
								removedItems.remove(objs[i]);
......
734 739
						// If there is key sensitive
735 740
						while (i < objs.length)
736 741
						{
737
							s = (String)objs[i];
742
							s = objs[i].toString();
738 743
							if (s.startsWith(text))
739 744
							{
740 745
								removedItems.remove(objs[i]);
......
751 756
						// If there isn't key sensitive
752 757
						while (i < objs.length)
753 758
						{
754
							s = ((String)objs[i]).toUpperCase();						
759
							s = ((String)objs[i].toString().toUpperCase());						
755 760
													
756 761
							if (s.startsWith(text.toUpperCase()))
757 762
							{
......
782 787
				if (model.getSize() > 0)
783 788
				{
784 789
					this.setSelectedIndex(0);
785
					txtField.setText(model.getSelectedItem().toString());					
790
					txtField.setText(model.getSelectedItem().toString());
786 791
				}
787 792
				else
788 793
					txtField.setText(""); // If there isn't any item selected and there aren't items, show anything
......
805 810
			ComboBoxModel model = this.getModel();
806 811
			String text = txtField.getText();
807 812
					
808
			// Try to match the characters written with the start of any string (item) of the list
813
			// Try to match the characters written with the start of any string value of item of the list
809 814
			int i = 0;
810 815
			String s;
811 816
			boolean matchesTheStart = false;
......
844 849
					{
845 850
						while (i < model.getSize())
846 851
						{
847
							s = (String) model.getElementAt(i);
852
							s = model.getElementAt(i).toString();
848 853
							
849 854
							// If matches the start
850 855
							if (s.startsWith(text)) {
......
856 861
							else
857 862
							{
858 863
								// When the characters written doesn't match -> set this characters to red color   
859
								removedItems.add(model.getElementAt(i).toString());
864
								removedItems.add(model.getElementAt(i));
860 865
								notRemovedItems.remove(model.getElementAt(i));
861 866
								this.removeItemAt(i);
862 867
							}				
......
866 871
					{
867 872
						while (i < model.getSize())
868 873
						{
869
							s = ((String) model.getElementAt(i)).toUpperCase();
874
							s = (model.getElementAt(i).toString().toUpperCase());
870 875
													
871 876
							// If matches the start
872 877
							if (s.startsWith(text.toUpperCase())) {
......
878 883
							else
879 884
							{
880 885
								// When the characters written doesn't match -> set this characters to red color   
881
								removedItems.add(model.getElementAt(i).toString());
886
								removedItems.add(model.getElementAt(i));
882 887
								notRemovedItems.remove(model.getElementAt(i));
883 888
								this.removeItemAt(i);
884 889
							}				
......
895 900
					{
896 901
						while (i < model.getSize())
897 902
						{
898
							s = (String) model.getElementAt(i);
903
							s = model.getElementAt(i).toString();
899 904
							
900 905
							// If matches the start
901 906
							if (s.startsWith(text)) {
......
907 912
							else
908 913
							{
909 914
								// When the characters written doesn't match -> set this characters to red color   
910
								removedItems.add(model.getElementAt(i).toString());				
915
								removedItems.add(model.getElementAt(i));				
911 916
								this.removeItemAt(i);
912 917
							}				
913 918
						}
......
916 921
					{
917 922
						while (i < model.getSize())
918 923
						{
919
							s = ((String) model.getElementAt(i)).toUpperCase();
924
							s = (model.getElementAt(i).toString().toUpperCase());
920 925
													
921 926
							// If matches the start
922 927
							if (s.startsWith(text.toUpperCase())) {
......
928 933
							else
929 934
							{
930 935
								// When the characters written doesn't match -> set this characters to red color   
931
								removedItems.add(model.getElementAt(i).toString());				
936
								removedItems.add(model.getElementAt(i));				
932 937
								this.removeItemAt(i);
933 938
							}				
934 939
						}

Also available in: Unified diff