Revision 6613 trunk/libraries/libUI/src/org/gvsig/gui/beans/comboBoxItemsSeeker/ComboBoxItemsSeekerDynamicModel.java

View differences:

ComboBoxItemsSeekerDynamicModel.java
62 62
	// INNER MODEL STRUCTURES
63 63
	private Set itemsShowed;
64 64
	private Object itemsShowedSortedOrDisordered[];
65
	private String writtenText;
66 65
//	private String writtenString = "";
67 66
	// END INNER MODEL STRUCTURES
68 67
	
69 68
	// CONTANTS FOR CONFIGURE THE BEHAVIOR
70
	public final int ORIGINAL_POSITION_ALL = 0;
71
	public final int SORTED_ALL = 1;
72
	public final int DISORDERED_ALL = 2;
69
	public static final int MAINTAIN_ORIGINAL_POSITION_START = 0;
70
	public static final int ORDERED_START = 1;
71
	public static final int DISORDERED_START = 2;
73 72

  
74
	public final int ORIGINAL_POSITION_SEARCH = 0;
75
	public final int SORTED_SEARCH = 1;
76
	public final int DISORDERED_SEARCH = 2;
73
	public static final int MAINTAIN_ORIGINAL_POSITION_ALL_ITEMS_SEARCH = 3;
74
	public static final int ORDERED_ALL_ITEMS_SEARCH = 4;
75
	public static final int DISORDERED_ALL_ITEMS_SEARCH = 5;
76
	public static final int MAINTAIN_ORIGINAL_POSITION_DYNAMIC_SEARCH = 6;
77
	public static final int ORDERED_DYNAMIC_SEARCH = 7;
78
	public static final int DISORDERED_DYNAMIC_SEARCH = 8;
79

  
77 80
	// END CONTANTS FOR CONFIGURE THE BEHAVIOR
78 81
	
79 82
	// CONFIGURATION FLAGS (Voy a Cambiar algunas!!!!)
80
	private boolean maintainPositionItems;	
81
	private boolean alphanumericSortedSearches;
82
	private boolean allAlphanumericSorted;
83
//	private boolean maintainPositionItems;	
84
//	private boolean alphanumericSortedSearches;
85
//	private boolean allAlphanumericSorted;
86
	private int startBehavior;
87
	private int searchBehavior;
83 88
	private boolean keySensitive;
84 89
	private boolean showAllItems;
85 90
//	(With the default value)
......
88 93
//	private boolean allAlphanumericSorted  = true;
89 94
//	private boolean keySensitive = false;
90 95
//	private boolean showAllItems = true; //false;;
91
	// END FLAGS	
96
	// END FLAGS	//		this.maintainPositionItems = true;		
97
//	this.alphanumericSortedSearches = true;
98
//	this.allAlphanumericSorted = false;
92 99

  
100

  
93 101
	// OTHER FLAGS
102
	private boolean startView;
94 103
	private boolean allItemsAreSorted;
95 104
	private boolean allItemsAreDisordered;
96
	private boolean thisIsAComboBoxItemsSeekerDynamicModelClass; // To distinguish the class which invokes some methods of this class
105
	private boolean thisIsAComboBoxItemsSeekerDynamicModelClass; // To distinguish the class which invokes some methods of this class	
97 106
	// END OTHER FLAGS
98 107

  
108
	// ATTRIBUTES FOR DYNAMIC CASES
109
	private int highIndex;
110
	private int lowIndex;
111
	private String writtenText;
112
	// END ATTRIBUTES FOR DYNAMIC CASES
113
	
99 114
	/**
100 115
	 * Default Constructor without parameters.
101 116
	 * Constructs an empty ComboBoxItemsSeekerDynamicModel object.          
......
140 155
//		this.currentString = "";
141 156
		this.writtenText = "";
142 157
		this.setDefaultBehaviorFlagsConfiguration();
158
		this.startView = true;
143 159
		this.allItemsAreSorted = false;
144 160
		this.allItemsAreDisordered = false;
145 161
		this.itemsShowed = new HashSet();
......
150 166
	 */
151 167
	private void setDefaultBehaviorFlagsConfiguration() {
152 168
		// Set the default values of the flags
153
		this.maintainPositionItems = true;		
154
		this.alphanumericSortedSearches = true;
155
		this.allAlphanumericSorted = false;
169
//		this.maintainPositionItems = true;		
170
//		this.alphanumericSortedSearches = true;
171
//		this.allAlphanumericSorted = false;
172
		this.startBehavior = this.MAINTAIN_ORIGINAL_POSITION_START;
173
		this.searchBehavior = this.ORDERED_ALL_ITEMS_SEARCH; //this.ORDERED_DYNAMIC_SEARCH;
156 174
		this.keySensitive = false;
157 175
		this.showAllItems = true; //false;	
158 176
	}
......
186 204
	 * @return boolean True if the configuration of the flags is oks, false if not
187 205
	 */
188 206
	public boolean testFlagsConfigurationOK() {
189
		if ((maintainPositionItems == allAlphanumericSorted) && (maintainPositionItems == true))
207
//		if ((maintainPositionItems == allAlphanumericSorted) && (maintainPositionItems == true))
208
//			return false;
209
//		
210
//		if ((maintainPositionItems == false) && (allAlphanumericSorted == true) && (alphanumericSortedSearches == false))
211
//			return false;
212
		
213
		if ((this.startBehavior < 0) || (this.startBehavior > 2))
190 214
			return false;
191 215
		
192
		if ((maintainPositionItems == false) && (allAlphanumericSorted == true) && (alphanumericSortedSearches == false))
216
		if ((this.searchBehavior < 3) || (this.searchBehavior > 8))
193 217
			return false;
194 218
		
195 219
//		if ((showAllItems == true) && (maintainPositionItems == false))
......
198 222
		return true;
199 223
	}
200 224

  
201
	/**
202
	 * Gets the flag of the configuration of the policy for maintain the position of the items of this component 
203
	 * (true -> maintains the position of the items, false -> don't maintains the position of the items)
204
	 * 
205
	 * @return boolean The value of the flag
206
	 */
207
	public boolean isMaintainPositionItems() {
208
		return maintainPositionItems;
225
	public int getStartBehavior() {
226
		return this.startBehavior;
209 227
	}
210 228
	
211
	/**
212
	 * Sets the flag of the configuration of the policy for maintain the position of the items of this component 
213
	 * (true -> maintains the position of the items, false -> don't maintains the position of the items)
214
	 * 
215
	 * @param boolean The value of the flag
216
	 */
217
	public void setMaintainPositionItems(boolean maintain_Position_Items) {
218
		this.maintainPositionItems = maintain_Position_Items;
229
	public void setStartBehavior(int start_Behavior) {
230
		this.startBehavior = start_Behavior;
219 231
	}
220
	
221
	/**
222
	 * Gets the flag of the configuration of the alphanumericSortedSearches flag of this component 
223
	 * (true -> all items showed when we write on the textfield will be showed in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
224
	 * 
225
	 * @return boolean The value of the flag
226
	 */
227
	public boolean isAlphanumericSortedSearches() {
228
		return alphanumericSortedSearches;
232

  
233
	public int getSearchBehavior() {
234
		return this.searchBehavior;
229 235
	}
230
	
231
	/**
232
	 * Sets the flag of the configuration of the alphanumericSortedSearches flag of this component 
233
	 * (true -> all items showed when we write on the textfield will be showed in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
234
	 * 
235
	 * @param boolean The value of the flag
236
	 */
237
	public void setAlphanumericSortedSearches(boolean alphanumeric_Sorted_Searches) {
238
		this.alphanumericSortedSearches = alphanumeric_Sorted_Searches;
239
		
240
//		if (!testFlagsConfigurationOK())
241
//			JOptionPane.showMessageDialog(this, PluginServices.getText(this, "error_message_JComboBoxSearcheableDynamic"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE);
242
//
243
//		if (alphanumericSortedSearches)
244
//		{
245
//			notRemovedItems = new HashSet();
246
//			
247
//			for (int i=0; i < this.getItemCount(); i++)
248
//				notRemovedItems.add(this.getItemAt(i));
249
//		}
250
//		else
251
//			notRemovedItems.clear();
252 236

  
237
	public void setSearchBehavior(int search_Behavior) {
238
		this.searchBehavior = search_Behavior;
253 239
	}
254
	
240

  
255 241
	/**
256
	 * Gets the flag of the configuration of the allAlphanumericSorted flag of this component 
257
	 * (true -> all items showed will be in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
258
	 * 
259
	 * @return boolean The value of the flag
260
	 */
261
	public boolean isAllAlphanumericSorted() {
262
		return allAlphanumericSorted;
263
	}
264
	
265
	/**
266
	 * Sets the flag of the configuration of the allAlphanumericSorted flag of this component 
267
	 * (true -> all items showed will be in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
268
	 * 
269
	 * @param boolean The value of the flag
270
	 */
271
	public void setAllAlphanumericSorted(boolean all_Alphanumeric_Sorted) {
272
		this.allAlphanumericSorted = all_Alphanumeric_Sorted;
273
		
274
		// If all_Alphanumeric_Sorted = true -> alphanumeric_Sorted_Searches = true
275
		setAlphanumericSortedSearches(this.alphanumericSortedSearches | this.allAlphanumericSorted);
276
		
277
//		if (allAlphanumericSorted)
278
//		
279
	}
280
	
281
	/**
282 242
	 * Gets the flag of the configuration of the keySensitive flag of this component 
283 243
	 * (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
284 244
	 * 
......
330 290
	 * @see javax.swing.DefaultComboBoxModel#getSize()
331 291
	 */
332 292
	public int getSize() {
333
		// TENGO DUDAS DE ESTE ALGORITMO
334
		if (thisIsAComboBoxItemsSeekerDynamicModelClass == true)
293
		if (thisIsAComboBoxItemsSeekerDynamicModelClass)
335 294
		{
336
//			System.out.println("ENTRA AQUI ComboBoxItemsSeekerDynamicModel");
337
			if (this.showAllItems)
338
				return this.itemsShowed.size();
339
			else
295
			if (this.startView)
296
				return this.itemsShowed.size();			
297
			
298
			switch(this.getSearchBehavior())
340 299
			{
341
				// HAcerlo
342
//				System.out.println("ENTRA AQUI");
343
				return 0;
300
				case MAINTAIN_ORIGINAL_POSITION_ALL_ITEMS_SEARCH: case ORDERED_ALL_ITEMS_SEARCH: case DISORDERED_ALL_ITEMS_SEARCH:
301
					return this.itemsShowed.size();
302
//					break;
303
				case MAINTAIN_ORIGINAL_POSITION_DYNAMIC_SEARCH:
304
					return 0; // HACER
305
//					break;
306
				case ORDERED_DYNAMIC_SEARCH:
307
					return 0; // HACER
308
//					break;
309
				case DISORDERED_DYNAMIC_SEARCH:
310
					return 0; // HACER
311
//					break;
312
				default:
313
					// If error configuration -> it gives back 0 for not to produce more errors 
314
					return 0; // HACER
344 315
			}
345 316
		}
346 317
		else
347
		{
348
			// If the class which is this object instance is 'javax.swing.DefaultComboBoxModel'
349
//			System.out.println("ENTRA AQUI DEFAULTCOMBOBOXMODEL");
350 318
			return super.getSize();
351
		}
352 319
	}
353 320

  
354 321
	/*
......
356 323
	 * @see javax.swing.ListModel#getElementAt(int)
357 324
	 */
358 325
	public Object getElementAt(int index) {
359
		if (this.showAllItems)
360
		{
361

  
362
			// Primero considerar el mostrar todos al comienzo
363
			
364
			
365
			
366
			// Caso de las b?squedas
367
			
368
			// If the items have to be showed in ascending order
369
			if (this.alphanumericSortedSearches)
326
		
327
		if (this.startView) // At the beginning shows all items
328
		{			
329
			switch(this.getStartBehavior())
370 330
			{
371
				// If the items aren't sorted -> sort them!
372
				if (!this.allItemsAreSorted)
373
					this.sortItemsCopy();
374
				
375
				// Return the item at that position
376
				return this.itemsShowedSortedOrDisordered[index];
377
			}
378
			else
379
			{
380
				if (this.maintainPositionItems)
331
				case MAINTAIN_ORIGINAL_POSITION_START :					
381 332
					return super.getElementAt(index);
382
				else
383
				{
333
//					break;
334
				case ORDERED_START :
384 335
					// If the items aren't sorted -> sort them!
336
					if (!this.allItemsAreSorted)
337
						this.sortItemsCopy();
338
					
339
					// Return the item at that position
340
					return this.itemsShowedSortedOrDisordered[index];
341
//					break;
342
				case DISORDERED_START :
343
					// If the items aren't sorted -> sort them!
385 344
					if (!this.allItemsAreDisordered)
386 345
						this.disorderItemsCopy();
387
//					System.out.println("BBB");
346

  
388 347
					// Return the item at that position
389 348
					return this.itemsShowedSortedOrDisordered[index];
390
				}
349
//					break;
350
				default:
351
					// If error configuration -> it gives back an empty object for not to produce more errors 
352
					return new Object();
391 353
			}
392 354
		}
393 355
		else
394
		{
395
			// HAcerlo
396
			return new Object();
356
		{			
357
			switch(this.getSearchBehavior())
358
			{
359
				case MAINTAIN_ORIGINAL_POSITION_ALL_ITEMS_SEARCH :
360
					return super.getElementAt(index);
361
//						break;
362
				case ORDERED_ALL_ITEMS_SEARCH :
363
					// If the items aren't sorted -> sort them!
364
					if (!this.allItemsAreSorted)
365
						this.sortItemsCopy();
366
					
367
					// Return the item at that position
368
					return this.itemsShowedSortedOrDisordered[index];
369
//						break;
370
				case DISORDERED_ALL_ITEMS_SEARCH :
371
					// If the items aren't sorted -> sort them!
372
					if (!this.allItemsAreDisordered)
373
						this.disorderItemsCopy();
374

  
375
					// Return the item at that position
376
					return this.itemsShowedSortedOrDisordered[index];
377
//						break;
378
				case MAINTAIN_ORIGINAL_POSITION_DYNAMIC_SEARCH :
379
					if (!this.allItemsAreSorted)
380
						this.sortItemsCopy();
381
					
382
					return new Object(); // HACER
383
//						break;
384
				case ORDERED_DYNAMIC_SEARCH :
385
					if (!this.allItemsAreSorted)
386
						this.sortItemsCopy();
387
					
388
					return new Object(); // HACER
389
//						break;
390
				case DISORDERED_DYNAMIC_SEARCH :
391
					if (!this.allItemsAreSorted)
392
						this.sortItemsCopy();
393
					
394
					return new Object(); // HACER
395
//						break;
396
				default:
397
					// If error configuration -> it gives back an empty object for not to produce more errors 
398
					return new Object();
399
			}
397 400
		}		
398 401
	}
399 402

  
......
408 411
		this.itemsShowed.add(anObject);
409 412
		
410 413
		// If this model has to return the elements sorted
411
		if (this.alphanumericSortedSearches)
414
		if ((this.getStartBehavior() == this.ORDERED_START) || (this.getSearchBehavior() == this.ORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.ORDERED_DYNAMIC_SEARCH))
415
		//if (this.alphanumericSortedSearches)
412 416
			this.allItemsAreSorted = false;
413
		
414
		// If elements have to be disordered
415
		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
416
			this.allItemsAreDisordered = false;
417
		else
418
		{
419
			// If elements have to be disordered
420
			if ((this.getStartBehavior() == this.DISORDERED_START) || (this.getSearchBehavior() == this.DISORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.DISORDERED_DYNAMIC_SEARCH))
421
			//if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
422
				this.allItemsAreDisordered = false;
423
		}
417 424
	 }
418 425

  
419 426
	/*
......
447 454
		this.itemsShowed.add(anObject);
448 455

  
449 456
		// If this model has to return the elements sorted
450
		if (this.alphanumericSortedSearches)
457
		if ((this.getStartBehavior() == this.ORDERED_START) || (this.getSearchBehavior() == this.ORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.ORDERED_DYNAMIC_SEARCH))
458
		//if (this.alphanumericSortedSearches)
451 459
			this.allItemsAreSorted = false;
452 460
		
453 461
		// If elements have to be disordered
454
		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
462
		if ((this.getStartBehavior() == this.DISORDERED_START) || (this.getSearchBehavior() == this.DISORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.DISORDERED_DYNAMIC_SEARCH))
463
		//if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
455 464
			this.allItemsAreDisordered = false;
456 465
	}
457 466
    
......
466 475
		itemsShowed.clear();		
467 476

  
468 477
		// If this model has to return the elements sorted
469
		if (this.alphanumericSortedSearches)
478
		if ((this.getStartBehavior() == this.ORDERED_START) || (this.getSearchBehavior() == this.ORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.ORDERED_DYNAMIC_SEARCH))
479
		//if (this.alphanumericSortedSearches)
470 480
			this.allItemsAreSorted = false;
471 481
		
472 482
		// If elements have to be disordered
473
		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
483
		if ((this.getStartBehavior() == this.DISORDERED_START) || (this.getSearchBehavior() == this.DISORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.DISORDERED_DYNAMIC_SEARCH))
484
		//if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
474 485
			this.allItemsAreDisordered = false;
475 486
	}
476 487
    
......
485 496
		itemsShowed.remove(anObject);
486 497
		
487 498
		// If this model has to return the elements sorted
488
		if (this.alphanumericSortedSearches)
499
		if ((this.getStartBehavior() == this.ORDERED_START) || (this.getSearchBehavior() == this.ORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.ORDERED_DYNAMIC_SEARCH))
500
		//if (this.alphanumericSortedSearches)
489 501
			this.allItemsAreSorted = false;
490 502
		
491 503
		// If elements have to be disordered
492
		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
504
		if ((this.getStartBehavior() == this.DISORDERED_START) || (this.getSearchBehavior() == this.DISORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.DISORDERED_DYNAMIC_SEARCH))
505
		//if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
493 506
			this.allItemsAreDisordered = false;
494 507
	}
495 508
    
......
507 520
		itemsShowed.remove(obj);
508 521
		
509 522
		// If this model has to return the elements sorted
510
		if (this.alphanumericSortedSearches)
523
		if ((this.getStartBehavior() == this.ORDERED_START) || (this.getSearchBehavior() == this.ORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.ORDERED_DYNAMIC_SEARCH))
524
		//if (this.alphanumericSortedSearches)
511 525
			this.allItemsAreSorted = false;
512 526
		
513 527
		// If elements have to be disordered
514
		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
528
		if ((this.getStartBehavior() == this.DISORDERED_START) || (this.getSearchBehavior() == this.DISORDERED_ALL_ITEMS_SEARCH) || (this.getSearchBehavior() == this.DISORDERED_DYNAMIC_SEARCH))
529
		//if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
515 530
			this.allItemsAreDisordered = false;
516 531
	}
517 532
    
518
//	/*
519
//	 *  (non-Javadoc)
520
//	 * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
521
//	 */
522
//	public void setSelectedItem(Object anObject) {
523
//		super.setSelectedItem(anObject);
524
//	}
525
//	
526
//	////// END REIMPLEMENTATION OF METHODS OF "DefaultComboBoxModel" TO ADAPT THE BEHAVIOR OF THIS MODEL //////	
527
//
528
//
529
//	/**
530
//	 * @param currentString 
531
//	 */
532
//	public void setCurrentString(String currentString) {
533
//		this.currentString = currentString;
534
//		
535
//		System.out.println("SetCurrentString-> CurrentString: '" + this.currentString + "'");
536
////		Vector v = new Vector();		
537
//		
538
//		//Refrescar el modelo
539
//		
540
////		for (int i=0 ; i< super.getSize() ; i++){
541
////			String obj = super.getElementAt(i).toString();
542
////			if (currentString.equals("")){
543
////				v.add(super.getElementAt(i));
544
////			}else if (obj.indexOf(currentString) == 0){
545
////				v.add(super.getElementAt(i));
546
////			}
547
////		}		
548
//	}
549
	
550
//	/**
551
//	 * 
552
//	 * @return
553
//	 */
554
//	public String getCurrentString() {
555
//		System.out.println("GetCurrentString-> CurrentString: '" + this.currentString + "'");
556
//		return this.currentString;
557
//	}
558
	
559
	
560 533
	/**
561 534
	 * 
562 535
	 */
......
564 537
//		System.out.println("Entrada: " + writtenString);
565 538
		writtenText = text;
566 539
		
567
		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
568
			this.disorderItemsCopy();
540
		
541
//		if (text.compareTo("") == 0)
542
//		{
543
//			this.startShow = true;
544
			
545
			if (this.getStartBehavior() == this.DISORDERED_START)
546
				this.disorderItemsCopy();
547
//		}
548
//		else
549
//			this.startShow = false;
550
		
551
//		if ((text.compareTo("") == 0) && (this.getStartBehavior() == this.DISORDERED_START))
552
//			this.disorderItemsCopy();
553
		
554
//		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
555
//			this.disorderItemsCopy();
569 556
	}
570 557
	
558
	public void setStartViewFlag(boolean flagValue) {
559
		this.startView = flagValue;
560
	}
561
	
571 562
	/**
572 563
	 * This method sort the items of this component and store them on a array
573 564
	 */
574 565
	private void sortItemsCopy() {
575
		if (this.alphanumericSortedSearches)
576
		{
566
		//if ((this.getStartBehavior() == this.ORDERED_START) || (this.getSearchBehavior() == this.ORDERED_SEARCH))
567
//		if (this.alphanumericSortedSearches)
568
//		{
577 569
			this.itemsShowedSortedOrDisordered = this.itemsShowed.toArray();
578 570
			Arrays.sort(this.itemsShowedSortedOrDisordered);
579 571
			this.allItemsAreSorted = true;
580
		}
572
//		}
581 573
	}
582 574
	
583 575
	/**
584 576
	 * This method gets the items of this component disordered and store them on a array
585 577
	 */
586 578
	private void disorderItemsCopy() {
587
		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))			
588
		{
579
//		if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))			
580
//		{
589 581
			this.itemsShowedSortedOrDisordered = this.itemsShowed.toArray();
590 582
			Collections.shuffle(Arrays.asList(this.itemsShowedSortedOrDisordered)); // Disorder the items
591 583
			this.allItemsAreDisordered = true;
592
		}
584
//		}
593 585
	}	
594 586
}

Also available in: Unified diff