Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / jComboBoxItemsSeeker / StringComparator.java @ 13136

History | View | Annotate | Download (5.53 KB)

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

    
43
import java.text.Collator;
44
import java.util.Comparator;
45

    
46
/**
47
 * Compares two chain of characters alphabetically
48
 *
49
 * This class is a copy of a class with the same name located in <i>libIverUtiles</i>
50
 * 
51
 * @author Fernando Gonz?lez Cort?s
52
 * @author Pablo Piqueras Bartolom?
53
 */
54
public class StringComparator implements Comparator {
55
        private boolean caseSensitive = true;
56
        private LocaleRules localeRules = null;
57

    
58
    /**
59
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
60
     */
61
    public int compare(Object o1, Object o2) {
62
        String s1 = o1.toString();
63
        String s2 = o2.toString();
64

    
65
        // If localeRules is null -> use the default rules
66
        if (localeRules == null) {
67
                if (caseSensitive) {
68
                        return s1.compareTo(s2);
69
                }
70
                else {
71
                        return s1.compareToIgnoreCase(s2);
72
                }
73
        }
74
        else {
75
                if (localeRules.isUseLocaleRules()) {
76
                        Collator collator = localeRules.getCollator();
77
                        
78
                        if (caseSensitive) {
79
                                return collator.compare(s1, s2);
80
                        }
81
                        else {
82
                                return collator.compare(s1.toLowerCase(), s2.toLowerCase());
83
                        }
84
                }
85
                else {
86
                    if (caseSensitive) {
87
                            return s1.compareTo(s2);
88
                    }
89
                    else {
90
                            return s1.compareToIgnoreCase(s2);
91
                    }
92
                }
93
        }
94
    }
95

    
96
    /**
97
     * Returns if the comparator is sensitive to small and big letters
98
     *
99
     * @return
100
     */
101
    public boolean isCaseSensitive() {
102
        return caseSensitive;
103
    }
104

    
105
    /**
106
     * Establece la sensibilidad del comparador a las mayusculas y minusculas
107
     *
108
     * @param b
109
     */
110
    public void setCaseSensitive(boolean b) {
111
        caseSensitive = b;
112
    }
113
    
114
    /**
115
     * Gets an object with the information for use the locale rules in comparation between strings. <br>
116
     * <ul>
117
     * <li>A boolean value -> if want or not use the locale rules</li>
118
     * <li>A reference to the locale rules</li>
119
     * </ul>
120
     * 
121
     * @return @see LocaleRules
122
     */
123
    public LocaleRules getLocaleRules() {
124
            return localeRules;
125
    }    
126
    /**
127
     * Sets an object with the information for use the locale rules in comparation between strings. <br>
128
     * <ul>
129
     * <li>A boolean value -> if want or not use the locale rules</li>
130
     * <li>A reference to the locale rules</li>
131
     * </ul>
132
     * 
133
     * @param @see LocaleRules
134
     */
135
    public void setLocaleRules(LocaleRules locRules) {
136
            localeRules = locRules;
137
    }
138
    
139
    /**
140
     * Represents the information needed by <i>StringComparator</i> for use or not locale-sensitive String comparison-rules in the <b><i>compare</i></b> method
141
     * 
142
     * @author Pablo Piqueras Bartolom?
143
     */
144
    public class LocaleRules {
145
             private boolean useLocaleRules;
146
             private Collator _collator;
147
             
148
             /**
149
              * Default constructor without parameters
150
              */
151
             public LocaleRules() {
152
                     useLocaleRules = false;
153
                     _collator = null;
154
             }
155
             
156
             /**
157
              * Default constructor with two parameters
158
              * 
159
              * @param b Use locale rules
160
              * @param collator A reference to an object configurated for locale-sensitive String comparison
161
              */
162
             public LocaleRules(boolean b, Collator collator) {
163
                     useLocaleRules = b;
164
                     _collator = collator;
165
             }
166
             
167
                 /**
168
                  * Gets the value of the inner attribute <i>_collator</i>
169
                  * 
170
                  * @return Returns A reference to an object configurated for locale-sensitive String comparison
171
                  */
172
                 public Collator getCollator() {
173
                         return _collator;
174
                 }
175

    
176
                 /**
177
                  * Sets a value to the inner attribute <i>_collator</i>
178
                  * 
179
                  * @param collator A reference to an object configurated for locale-sensitive String comparison
180
                  */
181
                 public void setCollator(Collator collator) {
182
                         this._collator = collator;
183
                 }
184

    
185
                /**
186
                 * Gets the value of the inner attribute <i>useLocaleRules</i>
187
                 * 
188
                 * @return Returns the useLocaleRules.
189
                 */
190
                public boolean isUseLocaleRules() {
191
                        return useLocaleRules;
192
                }
193

    
194
                /**
195
                 * Sets a value to the inner attribute <i>useLocaleRules</i>
196
                 * 
197
                 * @param useLocaleRules The useLocaleRules to set.
198
                 */
199
                public void setUseLocaleRules(boolean useLocaleRules) {
200
                        this.useLocaleRules = useLocaleRules;
201
                }
202
    }
203
}