Statistics
| Revision:

root / trunk / libraries / libUI / src / org / gvsig / gui / beans / comboBoxItemsSeeker / ComboBoxItemsSeekerDynamicModel.java @ 6577

History | View | Annotate | Download (17.6 KB)

1
package org.gvsig.gui.beans.comboBoxItemsSeeker;
2

    
3
import java.util.Arrays;
4
import java.util.Collections;
5
import java.util.HashSet;
6
import java.util.List;
7
import java.util.Set;
8
import java.util.Vector;
9

    
10
import javax.swing.DefaultComboBoxModel;
11

    
12

    
13
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
14
 *
15
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation; either version 2
20
 * of the License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
30
 *
31
 * For more information, contact:
32
 *
33
 *  Generalitat Valenciana
34
 *   Conselleria d'Infraestructures i Transport
35
 *   Av. Blasco Ib??ez, 50
36
 *   46010 VALENCIA
37
 *   SPAIN
38
 *
39
 *      +34 963862235
40
 *   gvsig@gva.es
41
 *      www.gvsig.gva.es
42
 *
43
 *    or
44
 *
45
 *   IVER T.I. S.A
46
 *   Salamanca 50
47
 *   46005 Valencia
48
 *   Spain
49
 *
50
 *   +34 963163400
51
 *   dac@iver.es
52
 */
53

    
54
/** VERSI?N EN DESARROLLO
55
 *  COMENTAR Y REVISAR
56
 * Se consideran los datos que contiene DefaultComboBoxModel como los que se tomar?n para devolver datos a no ser que sea para actualizar la lista
57
 *    de la vista, que en cuyo caso se tendr? en cuenta la configuraci?n del comportamiento y atributos internos.
58
 * 
59
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
60
 */
61
public class ComboBoxItemsSeekerDynamicModel extends DefaultComboBoxModel implements java.io.Serializable {
62
        // INNER MODEL STRUCTURES
63
        private Set itemsShowed;
64
        private Object itemsShowedSortedOrDisordered[];
65
        private String writtenText;
66
//        private String writtenString = "";
67
        // END INNER MODEL STRUCTURES
68
        
69
        // 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;
73

    
74
        public final int ORIGINAL_POSITION_SEARCH = 0;
75
        public final int SORTED_SEARCH = 1;
76
        public final int DISORDERED_SEARCH = 2;
77
        // END CONTANTS FOR CONFIGURE THE BEHAVIOR
78
        
79
        // CONFIGURATION FLAGS (Voy a Cambiar algunas!!!!)
80
        private boolean maintainPositionItems;        
81
        private boolean alphanumericSortedSearches;
82
        private boolean allAlphanumericSorted;
83
        private boolean keySensitive;
84
        private boolean showAllItems;
85
//        (With the default value)
86
//        private boolean maintainPositionItems = true;        
87
//        private boolean alphanumericSortedSearches = true;
88
//        private boolean allAlphanumericSorted  = true;
89
//        private boolean keySensitive = false;
90
//        private boolean showAllItems = true; //false;;
91
        // END FLAGS        
92

    
93
        // OTHER FLAGS
94
        private boolean allItemsAreSorted;
95
        private boolean allItemsAreDisordered;
96
        private boolean thisIsAComboBoxItemsSeekerDynamicModelClass; // To distinguish the class which invokes some methods of this class
97
        // END OTHER FLAGS
98

    
99
        /**
100
         * Default Constructor without parameters.
101
         * Constructs an empty ComboBoxItemsSeekerDynamicModel object.          
102
         */
103
        public ComboBoxItemsSeekerDynamicModel() {
104
                super();
105
                this.initialize();
106
        }
107
        
108
        /**
109
         * Default Constructor with an array of objects as parameter.
110
         * Constructs a ComboBoxItemsSeekerDynamicModel object initialized with an array of objects.
111
         * @throws Exception 
112
         */
113
        public ComboBoxItemsSeekerDynamicModel(Object[] items) throws Exception {                
114
                super(items);
115
                this.initialize();
116
                
117
                if (!this.itemsShowed.addAll(Arrays.asList(items)))
118
                        throw new Exception();
119
        }
120

    
121
        /**
122
         * Default Constructor with a vector as parameter.
123
         * Constructs a ComboBoxItemsSeekerDynamicModel object initialized with a vector.
124
         * @throws Exception 
125
         */
126
        public ComboBoxItemsSeekerDynamicModel(Vector objects) throws Exception {
127
                super(objects);
128
                this.initialize();
129
                
130
                if (!this.itemsShowed.addAll(objects))
131
                        throw new Exception();
132
        }
133
        
134
        /**
135
         * This method set the start values of inner attributes and creates the necessary inner objects
136
         */
137
        private void initialize() {
138
                thisIsAComboBoxItemsSeekerDynamicModelClass = true;
139
                
140
//                this.currentString = "";
141
                this.writtenText = "";
142
                this.setDefaultBehaviorFlagsConfiguration();
143
                this.allItemsAreSorted = false;
144
                this.allItemsAreDisordered = false;
145
                this.itemsShowed = new HashSet();
146
        }
147
        
148
        /**
149
         * Method that sets the default values of the behavior flags
150
         */
151
        private void setDefaultBehaviorFlagsConfiguration() {
152
                // Set the default values of the flags
153
                this.maintainPositionItems = true;                
154
                this.alphanumericSortedSearches = true;
155
                this.allAlphanumericSorted = false;
156
                this.keySensitive = false;
157
                this.showAllItems = true; //false;        
158
        }
159
        
160
        /** A BORRAR
161
         * @param currentString The currentString to set.
162
         */
163
//        public void setCurrentString(String currentString) {
164
//                this.currentString = currentString;
165
//                Vector v = new Vector();                
166
//                
167
//                //Refrescar el modelo                
168
//                for (int i=0 ; i<allObjects.size() ; i++){
169
//                        String obj = allObjects.get(i).toString();
170
//                        if (currentString.equals("")){
171
//                                v.add(allObjects.get(i));
172
//                        }else if (obj.indexOf(currentString) == 0){
173
//                                v.add(allObjects.get(i));
174
//                        }
175
//                }
176
//                orderedObjects = v;
177
//        }
178
        
179
        
180
        ////// METHODS FOR THE BEHAVIOR FLAGS  //////
181
        
182
        /**
183
         * This method tests the configuration of the flags and returns true or false if its ok or not with the
184
         *   logical behavior of this component
185
         * 
186
         * @return boolean True if the configuration of the flags is oks, false if not
187
         */
188
        public boolean testFlagsConfigurationOK() {
189
                if ((maintainPositionItems == allAlphanumericSorted) && (maintainPositionItems == true))
190
                        return false;
191
                
192
                if ((maintainPositionItems == false) && (allAlphanumericSorted == true) && (alphanumericSortedSearches == false))
193
                        return false;
194
                
195
//                if ((showAllItems == true) && (maintainPositionItems == false))
196
//                        return false;
197
                
198
                return true;
199
        }
200

    
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;
209
        }
210
        
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;
219
        }
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;
229
        }
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

    
253
        }
254
        
255
        /**
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
         * Gets the flag of the configuration of the keySensitive flag of this component 
283
         * (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
284
         * 
285
         * @return boolean The value of the flag
286
         */
287
        public boolean isKeySensitive() {
288
                return keySensitive;
289
        }
290
        
291
        /**
292
         * Sets the flag of the configuration of the keySensitive flag of this component 
293
         * (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
294
         * 
295
         * @param boolean The value of the flag
296
         */
297
        public void setKeySensitive(boolean key_Sensitive) {
298
                this.keySensitive = key_Sensitive;
299
        }
300

    
301
        /**
302
         * Gets the flag of the configuration of the showAllItems flag of this component
303
         * (true -> this component shows all items always; false -> this component only shows items that their first characters match with the string written by the user) 
304
         * 
305
         * @return boolean The value of the flag
306
         */
307
        public boolean isShowAllItems() {
308
                return showAllItems;
309
        }
310

    
311
        /**
312
         * Sets the flag of the configuration of the showAllItems flag of this component
313
         * (true -> this component shows all items always; false -> this component only shows items that their first characters match with the string written by the user)
314
         * 
315
         * @return boolean The value of the flag
316
         */
317
        public void setShowAllItems(boolean show_All_Items) {
318
                this.showAllItems = show_All_Items;
319
                
320
//                if (this.showAllItems == false)
321
//                        this.setModel(getCopyOfModel());
322
        }
323

    
324
        ////// END METHODS FOR THE BEHAVIOR FLAGS //////
325
        
326
        ////// REIMPLEMENTATION OF SOME METHODS OF "DefaultComboBoxModel" TO ADAPT THE BEHAVIOR OF THIS MODEL //////        
327
        
328
        /*
329
         *  (non-Javadoc)
330
         * @see javax.swing.DefaultComboBoxModel#getSize()
331
         */
332
        public int getSize() {
333
                // TENGO DUDAS DE ESTE ALGORITMO
334
                if (thisIsAComboBoxItemsSeekerDynamicModelClass == true)
335
                {
336
//                        System.out.println("ENTRA AQUI ComboBoxItemsSeekerDynamicModel");
337
                        if (this.showAllItems)
338
                                return this.itemsShowed.size();
339
                        else
340
                        {
341
                                // HAcerlo
342
//                                System.out.println("ENTRA AQUI");
343
                                return 0;
344
                        }
345
                }
346
                else
347
                {
348
                        // If the class which is this object instance is 'javax.swing.DefaultComboBoxModel'
349
//                        System.out.println("ENTRA AQUI DEFAULTCOMBOBOXMODEL");
350
                        return super.getSize();
351
                }
352
        }
353

    
354
        /*
355
         *  (non-Javadoc)
356
         * @see javax.swing.ListModel#getElementAt(int)
357
         */
358
        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)
370
                        {
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)
381
                                        return super.getElementAt(index);
382
                                else
383
                                {
384
                                        // If the items aren't sorted -> sort them!
385
                                        if (!this.allItemsAreDisordered)
386
                                                this.disorderItemsCopy();
387
//                                        System.out.println("BBB");
388
                                        // Return the item at that position
389
                                        return this.itemsShowedSortedOrDisordered[index];
390
                                }
391
                        }
392
                }
393
                else
394
                {
395
                        // HAcerlo
396
                        return new Object();
397
                }                
398
        }
399

    
400
        /*
401
         *  (non-Javadoc)
402
         * @see javax.swing.MutableComboBoxModel#addElement(java.lang.Object)
403
         */
404
        public void addElement(Object anObject) {
405
                super.addElement(anObject);
406
                
407
                // Add the item also in the inner attribute
408
                this.itemsShowed.add(anObject);
409
                
410
                // If this model has to return the elements sorted
411
                if (this.alphanumericSortedSearches)
412
                        this.allItemsAreSorted = false;
413
                
414
                // If elements have to be disordered
415
                if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
416
                        this.allItemsAreDisordered = false;
417
         }
418

    
419
        /*
420
         *  (non-Javadoc)
421
         * @see javax.swing.DefaultComboBoxModel#getIndexOf(java.lang.Object)
422
         */
423
        public int getIndexOf(Object anObject) {
424
                
425
                //if (this.alphanumericSortedSearches)
426
                        //return this.itemsShowedVector.indexOf(anObject);
427
                
428
                return super.getIndexOf(anObject); //this.itemsShowed.indexOf(anObject);                        
429
        }
430
                
431
    /*
432
     *  (non-Javadoc)
433
     * @see javax.swing.ComboBoxModel#getSelectedItem()
434
     */
435
        public Object getSelectedItem() {
436
                return super.getSelectedItem();
437
        }        
438
 
439
        /*
440
         *  (non-Javadoc)
441
         * @see javax.swing.MutableComboBoxModel#insertElementAt(java.lang.Object, int)
442
         */
443
        public void insertElementAt(Object anObject, int index) {
444
                super.insertElementAt(anObject, index);
445
                
446
                // Add the item also in the inner attribute
447
                this.itemsShowed.add(anObject);
448

    
449
                // If this model has to return the elements sorted
450
                if (this.alphanumericSortedSearches)
451
                        this.allItemsAreSorted = false;
452
                
453
                // If elements have to be disordered
454
                if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
455
                        this.allItemsAreDisordered = false;
456
        }
457
    
458
        /*
459
         *  (non-Javadoc)
460
         * @see javax.swing.DefaultComboBoxModel#removeAllElements()
461
         */
462
        public void removeAllElements() {
463
                super.removeAllElements();
464
                
465
                // Also empty the inner attribute
466
                itemsShowed.clear();                
467

    
468
                // If this model has to return the elements sorted
469
                if (this.alphanumericSortedSearches)
470
                        this.allItemsAreSorted = false;
471
                
472
                // If elements have to be disordered
473
                if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
474
                        this.allItemsAreDisordered = false;
475
        }
476
    
477
        /*
478
         *  (non-Javadoc)
479
         * @see javax.swing.MutableComboBoxModel#removeElement(java.lang.Object)
480
         */
481
        public void removeElement(Object anObject) {
482
                super.removeElement(anObject);
483
                
484
                // Remove the object also in the inner attribute
485
                itemsShowed.remove(anObject);
486
                
487
                // If this model has to return the elements sorted
488
                if (this.alphanumericSortedSearches)
489
                        this.allItemsAreSorted = false;
490
                
491
                // If elements have to be disordered
492
                if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
493
                        this.allItemsAreDisordered = false;
494
        }
495
    
496
        /*
497
         *  (non-Javadoc)
498
         * @see javax.swing.MutableComboBoxModel#removeElementAt(int)
499
         */
500
        public void removeElementAt(int index) {
501
                Object obj = new Object();
502
                
503
                obj = super.getElementAt(index);
504
                super.removeElementAt(index);
505
                
506
                // Remove the object also in the inner attribute
507
                itemsShowed.remove(obj);
508
                
509
                // If this model has to return the elements sorted
510
                if (this.alphanumericSortedSearches)
511
                        this.allItemsAreSorted = false;
512
                
513
                // If elements have to be disordered
514
                if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
515
                        this.allItemsAreDisordered = false;
516
        }
517
    
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
        /**
561
         * 
562
         */
563
        public void setWrittenString(String text) {
564
//                System.out.println("Entrada: " + writtenString);
565
                writtenText = text;
566
                
567
                if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))
568
                        this.disorderItemsCopy();
569
        }
570
        
571
        /**
572
         * This method sort the items of this component and store them on a array
573
         */
574
        private void sortItemsCopy() {
575
                if (this.alphanumericSortedSearches)
576
                {
577
                        this.itemsShowedSortedOrDisordered = this.itemsShowed.toArray();
578
                        Arrays.sort(this.itemsShowedSortedOrDisordered);
579
                        this.allItemsAreSorted = true;
580
                }
581
        }
582
        
583
        /**
584
         * This method gets the items of this component disordered and store them on a array
585
         */
586
        private void disorderItemsCopy() {
587
                if ((!this.maintainPositionItems) && (!this.alphanumericSortedSearches))                        
588
                {
589
                        this.itemsShowedSortedOrDisordered = this.itemsShowed.toArray();
590
                        Collections.shuffle(Arrays.asList(this.itemsShowedSortedOrDisordered)); // Disorder the items
591
                        this.allItemsAreDisordered = true;
592
                }
593
        }        
594
}