svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / comboboxconfigurablelookup / StringComparator.java @ 40561
History | View | Annotate | Download (5.59 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gui.beans.comboboxconfigurablelookup; |
25 |
|
26 |
import java.text.Collator; |
27 |
import java.util.Comparator; |
28 |
|
29 |
/**
|
30 |
* <p>Compares two chain of characters alphabetically.</p>
|
31 |
*
|
32 |
* <p>This class is a copy of a class with the same name located in <i>libIverUtiles</i>.</p>
|
33 |
*
|
34 |
* @author Fernando Gonz?lez Cort?s
|
35 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es)
|
36 |
*/
|
37 |
public class StringComparator implements Comparator<Object> { |
38 |
private boolean caseSensitive = true; |
39 |
private LocaleRules localeRules = null; |
40 |
|
41 |
/**
|
42 |
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
43 |
*/
|
44 |
public int compare(Object o1, Object o2) { |
45 |
String s1 = o1.toString();
|
46 |
String s2 = o2.toString();
|
47 |
|
48 |
// If localeRules is null -> use the default rules
|
49 |
if (localeRules == null) { |
50 |
if (caseSensitive) {
|
51 |
return s1.compareTo(s2);
|
52 |
} |
53 |
else {
|
54 |
return s1.compareToIgnoreCase(s2);
|
55 |
} |
56 |
} |
57 |
else {
|
58 |
if (localeRules.isUseLocaleRules()) {
|
59 |
Collator collator = localeRules.getCollator();
|
60 |
|
61 |
if (caseSensitive) {
|
62 |
return collator.compare(s1, s2);
|
63 |
} |
64 |
else {
|
65 |
return collator.compare(s1.toLowerCase(), s2.toLowerCase());
|
66 |
} |
67 |
} |
68 |
else {
|
69 |
if (caseSensitive) {
|
70 |
return s1.compareTo(s2);
|
71 |
} |
72 |
else {
|
73 |
return s1.compareToIgnoreCase(s2);
|
74 |
} |
75 |
} |
76 |
} |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Determines if the comparator is sensitive to upper case and down case characters or not.
|
81 |
*
|
82 |
* @return <code>true</code> if the comparator is sensitive to upper case and down case characters;
|
83 |
* otherwise not
|
84 |
*/
|
85 |
public boolean isCaseSensitive() { |
86 |
return caseSensitive;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* Sets the comparator sensitive or insensitive to upper case and down case characters.
|
91 |
*
|
92 |
* @param b <code>true</code> if the comparator is sensitive to upper case and down case characters;
|
93 |
* <code>false</code> if not
|
94 |
*/
|
95 |
public void setCaseSensitive(boolean b) { |
96 |
caseSensitive = b; |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Gets an object with the information for use the locale rules in comparison between strings.
|
101 |
* <ul>
|
102 |
* <li>A boolean value -> if want or not use the locale rules</li>
|
103 |
* <li>A reference to the locale rules</li>
|
104 |
* </ul>
|
105 |
*
|
106 |
* @return @see LocaleRules
|
107 |
*/
|
108 |
public LocaleRules getLocaleRules() {
|
109 |
return localeRules;
|
110 |
} |
111 |
/**
|
112 |
* Sets an object with the information for use the locale rules in comparison between strings.
|
113 |
* <ul>
|
114 |
* <li>A boolean value -> if want or not use the locale rules.</li>
|
115 |
* <li>A reference to the locale rules.</li>
|
116 |
* </ul>
|
117 |
*
|
118 |
* @param @see LocaleRules
|
119 |
*/
|
120 |
public void setLocaleRules(LocaleRules locRules) { |
121 |
localeRules = locRules; |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Represents the information needed by <i>StringComparator</i> for use or not locale-sensitive String
|
126 |
* comparison-rules in the <b><i>compare</i></b> method.
|
127 |
*
|
128 |
* @author Pablo Piqueras Bartolom?
|
129 |
*/
|
130 |
public class LocaleRules { |
131 |
private boolean useLocaleRules; |
132 |
private Collator _collator; |
133 |
|
134 |
/**
|
135 |
* Default constructor without parameters.
|
136 |
*/
|
137 |
public LocaleRules() {
|
138 |
useLocaleRules = false;
|
139 |
_collator = null;
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* Default constructor with two parameters.
|
144 |
*
|
145 |
* @param b Use locale rules
|
146 |
* @param collator A reference to an object configurated for locale-sensitive String comparison
|
147 |
*/
|
148 |
public LocaleRules(boolean b, Collator collator) { |
149 |
useLocaleRules = b; |
150 |
_collator = collator; |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Gets the value of the inner attribute <i>_collator</i>.
|
155 |
*
|
156 |
* @return Returns A reference to an object configurated for locale-sensitive String comparison
|
157 |
*/
|
158 |
public Collator getCollator() { |
159 |
return _collator;
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* Sets a value to the inner attribute <i>_collator</i>.
|
164 |
*
|
165 |
* @param collator A reference to an object configurated for locale-sensitive String comparison
|
166 |
*/
|
167 |
public void setCollator(Collator collator) { |
168 |
this._collator = collator;
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* Gets the value of the inner attribute <i>useLocaleRules</i>.
|
173 |
*
|
174 |
* @return Returns the useLocaleRules.
|
175 |
*/
|
176 |
public boolean isUseLocaleRules() { |
177 |
return useLocaleRules;
|
178 |
} |
179 |
|
180 |
/**
|
181 |
* Sets a value to the inner attribute <i>useLocaleRules</i>.
|
182 |
*
|
183 |
* @param useLocaleRules The useLocaleRules to set.
|
184 |
*/
|
185 |
public void setUseLocaleRules(boolean useLocaleRules) { |
186 |
this.useLocaleRules = useLocaleRules;
|
187 |
} |
188 |
} |
189 |
} |