KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > LocalePropertyEditor


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
21 package org.netbeans.modules.i18n;
22
23
24 import java.awt.Component JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.beans.PropertyEditorSupport JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import org.netbeans.modules.properties.LocalePanel;
31
32
33 /**
34  * Property editor for editing <code>Locale</code> instance.
35  *
36  * @author Peter Zavadsky
37  */

38 public class LocalePropertyEditor extends PropertyEditorSupport JavaDoc {
39
40     /** Value to edit. */
41     private Locale JavaDoc locale;
42
43
44     /** Creates new LocalePropertyEditor */
45     public LocalePropertyEditor() {
46     }
47
48     
49     /** Sets value. Overrides superclass method. Overrides superclass method. */
50     public void setValue(Object JavaDoc value) {
51         if(!(value instanceof Locale JavaDoc))
52             throw new IllegalArgumentException JavaDoc("I18N module: Bad class type of value:"+value.getClass().getName()); // NOI18N
53

54         if(locale != null && locale.equals(value))
55             return;
56         
57         locale = (Locale JavaDoc)value;
58         firePropertyChange();
59     }
60     
61     /** Gets value. Overrides superclass method. Overrides superclass method. */
62     public Object JavaDoc getValue() {
63         return locale;
64     }
65     
66     /** Gets string representation of value. Overrides superclass method. */
67     public String JavaDoc getAsText() {
68         if(locale == null)
69             return super.getAsText(); // NOI18N
70

71         return locale.toString();
72     }
73     
74     /** Sets as text. Overrides superclass method. */
75     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
76         setValue(createLocaleFromText(text));
77     }
78
79     /** Gets java initialization string. Overrides superclass method. */
80     public String JavaDoc getJavaInitializationString() {
81         if(locale == null)
82             return super.getJavaInitializationString();
83         
84         StringBuffer JavaDoc localeInit = new StringBuffer JavaDoc("new Locale("); // NOI18N
85

86         String JavaDoc language = locale.getLanguage();
87         String JavaDoc country = locale.getCountry();
88         String JavaDoc variant = locale.getVariant();
89
90         if(language == null)
91             localeInit.append("\"\""); // NOI18N
92
else
93             localeInit.append("\""+language+"\""); // NOI18N
94

95         if(country == null)
96             localeInit.append(",\"\""); // NOI18N
97
else
98             localeInit.append(",\""+country+"\""); // NOI18N
99

100         if(variant == null)
101             localeInit.append(")"); // NOI18N
102
else
103             localeInit.append(",\""+variant+"\")"); // NOI18N
104

105         return localeInit.toString();
106     }
107     
108     /** Overrides superclass method.
109      * @return true */

110     public boolean supportsCustomEditor() {
111         return true;
112     }
113     
114     /** Gets custom editor. Overrides superclass method. */
115     public Component JavaDoc getCustomEditor() {
116         final LocalePanel localePanel = new LocalePanel(locale);
117         
118         localePanel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
119             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
120                 if(LocalePanel.PROP_CUSTOMIZED_LOCALE.equals(evt.getPropertyName()))
121                     setValue(localePanel.getLocale());
122             }
123         });
124         
125         return localePanel;
126     }
127
128     /** Creates <code>Locale</code> from text. Utility method. */
129     private static Locale JavaDoc createLocaleFromText(String JavaDoc text) {
130         if(text == null || "".equals(text)) // NOI18N
131
return new Locale JavaDoc("", ""); // NOI18N
132

133         // Get language code.
134
int underscore = text.indexOf('_');
135
136         String JavaDoc language;
137         
138         if(underscore == -1)
139             return new Locale JavaDoc(text, ""); // NOI18N
140
else if(underscore == 0)
141             language = ""; // NOI18N
142
else
143             language = text.substring(0, underscore);
144         
145         if(text.length() <= underscore + 1)
146             return new Locale JavaDoc(language, ""); // NOI18N
147

148         text = text.substring(underscore + 1);
149
150         // Get country code.
151
underscore = text.indexOf('_');
152
153         String JavaDoc country;
154         
155         if(underscore == -1)
156             return new Locale JavaDoc(language, text);
157         else if(underscore == 0)
158             country = ""; // NOI18N
159
else
160             country = text.substring(0, underscore);
161
162         // Get variant.
163
if(text.length() <= underscore + 1)
164             return new Locale JavaDoc(language, country); // NOI18N
165

166         String JavaDoc variant = text.substring(underscore + 1);
167         
168         return new Locale JavaDoc(language, country, variant);
169     }
170 }
171
Popular Tags