KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > CharsetMapping


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * CharsetMapping.java
21  *
22  * Created on December 10, 2003, 3:22 PM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share;
26
27 import java.nio.charset.Charset JavaDoc;
28
29 import java.util.Collection JavaDoc;
30 import java.util.Comparator JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 import java.util.SortedMap JavaDoc;
35 import java.util.TreeMap JavaDoc;
36
37 import java.text.MessageFormat JavaDoc;
38
39 import java.beans.PropertyChangeSupport JavaDoc;
40 import java.beans.PropertyChangeListener JavaDoc;
41
42 /** Object for nice usage of Charsets in comboboxes, sorted lists, etc.
43  * Provides same equality properties as Charset (but with CharsetMapping)
44  * but a better "display name" via toString().
45  *
46  * There are also several static utility methods for finding and creating
47  * Charset and CharsetMappings.
48  *
49  * @author Peter Williams
50  */

51 public class CharsetMapping implements Comparable JavaDoc {
52     
53     private static final ResourceBundle JavaDoc webappBundle = ResourceBundle.getBundle(
54         "org.netbeans.modules.j2ee.sun.share.Bundle"); // NOI18N
55

56     private Charset JavaDoc charset;
57     private String JavaDoc chosenAlias;
58     private boolean showAliases;
59     private String JavaDoc displayText;
60     private boolean textOutOfDate;
61
62     /** Create a mapping for a charset
63      *
64      * @param c The charset for this mapping
65      */

66     public CharsetMapping(final Charset JavaDoc c) {
67         this(c, c.displayName(), true);
68     }
69     
70     /** Create a mapping for a charset with the obtion to turn on or off display
71      * of aliases next to the the charset canonical name.
72      *
73      * @param c The charset for this mapping
74      * @param sa
75      */

76     public CharsetMapping(final Charset JavaDoc c, boolean sa) {
77         this(c, c.displayName(), sa);
78     }
79     
80     /** Create a mapping for a charset alias
81      *
82      * @param c The charset for this mapping
83      * @param alias The particular alias represented by this mapping
84      */

85     public CharsetMapping(final Charset JavaDoc c, String JavaDoc alias) {
86         this(c, alias, false);
87     }
88     
89     private CharsetMapping(final Charset JavaDoc c, String JavaDoc alias, boolean sa) {
90         charset = c;
91         chosenAlias = alias;
92         showAliases = sa;
93         displayText = buildDisplayText();
94     }
95     
96     /** equals() maps to Charset.equals()
97      *
98      * @return true/false based on whether the embedded charset objects compare
99      * as equal.
100      */

101     public boolean equals(Object JavaDoc o) {
102         boolean result = false;
103         
104         if(o instanceof CharsetMapping) {
105             CharsetMapping cm = (CharsetMapping) o;
106             result = chosenAlias.equals(cm.getAlias());
107         }
108         
109         return result;
110     }
111     
112     /** hashCode() maps to Charset.hashCode()
113      *
114      * @return the hashcode
115      */

116     public int hashCode() {
117         return charset.hashCode();
118     }
119     
120     /** A more readable display string. If the mappings are for canonical charsets
121      * only and "showAliases" is true, then the string will include a bracketed
122      * display of all aliases for this charset after showing the canonical name.
123      *
124      * @return A descriptive string
125      */

126     public String JavaDoc toString() {
127         if(textOutOfDate) {
128             displayText = buildDisplayText();
129         }
130         
131         return displayText;
132     }
133
134     /** The charset
135      *
136      * @return the charset this is a mapping for
137      */

138     public Charset JavaDoc getCharset() {
139         return charset;
140     }
141     
142     /** The alias string. If this is a canonical map, will return canonical string.
143      *
144      * @return the charset alias this is a mapping for
145      */

146     public String JavaDoc getAlias() {
147         return chosenAlias;
148     }
149     
150     /** Force the display text to be recalculated. Recalculation won't happen
151      * until next time text is requested.
152      */

153     public void updateDisplayText() {
154         textOutOfDate = true;
155     }
156
157     private String JavaDoc buildDisplayText() {
158         String JavaDoc result = chosenAlias;
159         
160         if(showAliases) {
161             StringBuffer JavaDoc aliasList = new StringBuffer JavaDoc(200);
162
163             for(Iterator JavaDoc iter = charset.aliases().iterator(); iter.hasNext(); ) {
164                 aliasList.append((String JavaDoc) iter.next());
165                 if(iter.hasNext()) {
166                     aliasList.append(", "); // NOI18N
167
}
168             }
169             
170             Object JavaDoc [] args = new Object JavaDoc [] { chosenAlias, aliasList.toString() };
171             
172             result = MessageFormat.format(
173                 webappBundle.getString("LBL_CharsetComboBoxDisplayText"), args); // NOI18N
174
}
175         
176         if(result == null || result.length() == 0) {
177             result = webappBundle.getString("LBL_UnnamedCharset"); // NOI18N
178
}
179         
180         textOutOfDate = false;
181         
182         return result;
183     }
184     
185     /** For sorted collections. We compare the alias representations of the
186      * embedded charset.
187      *
188      * @param obj the Charset to compare to
189      * @return result of comparison (negative, 0, or positive depending on match)
190      */

191     public int compareTo(Object JavaDoc obj) {
192         int result = -1;
193         
194         if(obj instanceof CharsetMapping) {
195             // !PW FIXME This is different than equals. Why?
196
// -- To properly sort charsets by alias, we need this implementation
197
// like this. But how do we reconcile this with the way equals
198
// works?
199
//
200
CharsetMapping targetMapping = (CharsetMapping) obj;
201             result = chosenAlias.compareTo(targetMapping.getAlias());
202         }
203         
204         return result;
205     }
206     
207     private static boolean useAliases = false;
208     private static SortedMap JavaDoc sortedCanonicalCharsetMappings = null;
209     
210     private static SortedMap JavaDoc getSortedCanonicalCharsetMappings() {
211         if(sortedCanonicalCharsetMappings == null) {
212             SortedMap JavaDoc charsets = Charset.availableCharsets();
213             sortedCanonicalCharsetMappings = new TreeMap JavaDoc();
214
215             for(Iterator JavaDoc iter = charsets.entrySet().iterator(); iter.hasNext(); ) {
216                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
217                 CharsetMapping mapping = new CharsetMapping((Charset JavaDoc) entry.getValue());
218                 sortedCanonicalCharsetMappings.put(mapping.getAlias(), mapping);
219             }
220         }
221         
222         return sortedCanonicalCharsetMappings;
223     }
224     
225         private static SortedMap JavaDoc sortedAliasCharsetMappings = null;
226     
227     private static SortedMap JavaDoc getSortedAliasCharsetMappings() {
228         if(sortedAliasCharsetMappings == null) {
229             SortedMap JavaDoc charsets = Charset.availableCharsets();
230             sortedAliasCharsetMappings = new TreeMap JavaDoc();
231
232             for(Iterator JavaDoc iter = charsets.entrySet().iterator(); iter.hasNext(); ) {
233                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
234                 Charset JavaDoc charset = (Charset JavaDoc) entry.getValue();
235                 CharsetMapping mapping = new CharsetMapping(charset, false);
236                 sortedAliasCharsetMappings.put(mapping.getAlias(), mapping);
237
238                 for(Iterator JavaDoc aliasIter = charset.aliases().iterator(); aliasIter.hasNext(); ) {
239                     String JavaDoc alias = (String JavaDoc) aliasIter.next();
240                     CharsetMapping aliasMapping = new CharsetMapping(charset, alias);
241                     sortedAliasCharsetMappings.put(alias, aliasMapping);
242                 }
243             }
244         }
245         
246         return sortedAliasCharsetMappings;
247     }
248     
249     /** Return a sorted map containg mappings for all charsets supported by the
250      * current JVM. Depending on the value of the global alias flag, the map
251      * will contain Charsets indexed by canonical name only, or also indexed
252      * by alias.
253      *
254      * @return SortedMap containing CharsetMapping objects
255      */

256     public static SortedMap JavaDoc getSortedAvailableCharsetMappings() {
257         SortedMap JavaDoc result;
258         
259         if(useAliases) {
260                         if(sortedAliasCharsetMappings == null){
261                             sortedAliasCharsetMappings = getSortedAliasCharsetMappings();
262                         }
263             result = sortedAliasCharsetMappings;
264         } else {
265                         if(sortedCanonicalCharsetMappings == null){
266                             sortedCanonicalCharsetMappings = getSortedCanonicalCharsetMappings();
267                         }
268             result = sortedCanonicalCharsetMappings;
269         }
270         
271         return result;
272     }
273
274     /** Retrieve the CharsetMapping object matching this charset.
275      *
276      * @param c Charset to search for.
277      * @return CharsetMapping matching the passed in charset. Null if not found.
278      */

279     public static CharsetMapping getCharsetMapping(Charset JavaDoc c) {
280         return (CharsetMapping) getSortedAvailableCharsetMappings().get(c.name());
281     }
282     
283     /** Retrieve the CharsetMapping object matching this name.
284      *
285      * @param name Charset name to search for.
286      * @return CharsetMapping matching the passed in name. Null if not found.
287      */

288     public static CharsetMapping getCharsetMapping(String JavaDoc name) {
289         CharsetMapping result = null;
290         
291         if(name != null) {
292             try {
293                 Charset JavaDoc charset = Charset.forName(name);
294
295                 if(charset != null) {
296                     result = (CharsetMapping) getSortedAvailableCharsetMappings().get(charset.name());
297                 }
298             } catch(Exception JavaDoc ex) {
299                 // FIXME handle this one better: Unrecognized or otherwise illegal charset specified.
300
}
301         }
302         
303         return result;
304     }
305     
306 /*
307     public static class CharsetComparator implements Comparator {
308         public int compare(Object o1, Object o2) {
309             int result = -1;
310             
311             if(o1 instanceof Charset && o2 instanceof Charset) {
312                 Charset s1 = (Charset) o1;
313                 Charset s2 = (Charset) o2;
314                 
315                 result = s1.compareTo(s2);
316             }
317             
318             return result;
319         }
320     }
321  */

322     
323     /** -----------------------------------------------------------------------
324      * property storage and notification of the user option.
325      */

326     
327     public static final String JavaDoc CHARSET_DISPLAY_TYPE = "CharsetDisplayType";
328     public static final Integer JavaDoc CHARSET_CANONICAL = new Integer JavaDoc(0);
329     public static final Integer JavaDoc CHARSET_ALIAS_ASIDE = new Integer JavaDoc(1);
330     public static final Integer JavaDoc CHARSET_ALIAS_SELECTION = new Integer JavaDoc(2);
331
332     private static Integer JavaDoc displayOption = CHARSET_ALIAS_ASIDE;
333     private static java.beans.PropertyChangeSupport JavaDoc propSupport = new PropertyChangeSupport JavaDoc(CharsetMapping.class);;
334     
335     public static void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
336         propSupport.addPropertyChangeListener(listener);
337     }
338     
339     public static void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
340         propSupport.removePropertyChangeListener(listener);
341     }
342             
343     public static void setDisplayOption(Integer JavaDoc option) {
344         Integer JavaDoc newDisplayOption = getDisplayOptionEnum(option);
345         
346         if(newDisplayOption != null && newDisplayOption != displayOption) {
347             Integer JavaDoc oldDisplayOption = displayOption;
348             displayOption = newDisplayOption;
349             updateInternalState();
350             
351             propSupport.firePropertyChange(CHARSET_DISPLAY_TYPE, oldDisplayOption, displayOption);
352         }
353     }
354     
355     private static void updateInternalState() {
356         if(displayOption == CHARSET_CANONICAL) {
357             useAliases = false;
358                         if(sortedCanonicalCharsetMappings == null){
359                             sortedCanonicalCharsetMappings = getSortedCanonicalCharsetMappings();
360                         }
361             Collection JavaDoc mappings = sortedCanonicalCharsetMappings.values();
362             for(Iterator JavaDoc iter = mappings.iterator(); iter.hasNext(); ) {
363                 CharsetMapping mapping = (CharsetMapping) iter.next();
364                 mapping.showAliases = false;
365                 mapping.updateDisplayText();
366             }
367         } else if(displayOption == CHARSET_ALIAS_ASIDE) {
368             useAliases = false;
369                         if(sortedCanonicalCharsetMappings == null){
370                             sortedCanonicalCharsetMappings = getSortedCanonicalCharsetMappings();
371                         }
372             Collection JavaDoc mappings = sortedCanonicalCharsetMappings.values();
373             for(Iterator JavaDoc iter = mappings.iterator(); iter.hasNext(); ) {
374                 CharsetMapping mapping = (CharsetMapping) iter.next();
375                 mapping.showAliases = true;
376                 mapping.updateDisplayText();
377             }
378         } else if(displayOption == CHARSET_ALIAS_SELECTION) {
379             useAliases = true;
380         }
381     }
382     
383     public static Integer JavaDoc getDisplayOption() {
384         return displayOption;
385     }
386     
387     private static Integer JavaDoc getDisplayOptionEnum(Integer JavaDoc option) {
388         Integer JavaDoc result = null;
389         
390         if(option != null) {
391             if(CHARSET_CANONICAL.compareTo(option) == 0) {
392                 result = CHARSET_CANONICAL;
393             } else if(CHARSET_ALIAS_ASIDE.compareTo(option) == 0) {
394                 result = CHARSET_ALIAS_ASIDE;
395             } else if(CHARSET_ALIAS_SELECTION.compareTo(option) == 0) {
396                 result = CHARSET_ALIAS_SELECTION;
397             }
398         }
399         
400         return result;
401     }
402 }
403
Popular Tags