KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26
27
28 import org.openide.loaders.DataFolder;
29 import org.openide.loaders.DataObject;
30 import org.openide.util.NbBundle;
31 import org.openide.util.SharedClassObject;
32 import org.netbeans.api.queries.VisibilityQuery;
33
34 /**
35  * Utilities class for I18N module.
36  *
37  * @author Peter Zavadsky
38  */

39 public final class I18nUtil {
40
41     /** Help ID for i18n module in general. */
42     public static final String JavaDoc HELP_ID_I18N = "internation.internation"; // NOI18N
43
/** Help ID for I18N dialog. */
44     public static final String JavaDoc HELP_ID_AUTOINSERT = "internation.autoinsert"; // NOI18N
45
/** Help ID for Insert I18N dialog. */
46     public static final String JavaDoc HELP_ID_MANINSERT = "internation.maninsert"; // NOI18N
47
/** Help ID for I18N form property editor. You can see it in Component inspector. */
48     public static final String JavaDoc HELP_ID_FORMED = "internation.formed"; // NOI18N
49
/** Help ID for I18N test wizard. */
50     public static final String JavaDoc HELP_ID_TESTING = "internation.testing"; // NOI18N
51
/** Help ID for I18N wizard. */
52     public static final String JavaDoc HELP_ID_WIZARD = "internation.wizard"; // NOI18N
53
/** Help ID for I18N options. */
54     public static final String JavaDoc HELP_ID_CUSTOM = "internation.custom"; // NOI18N
55
/** Help ID for parameters dialog. */
56     public static final String JavaDoc HELP_ID_ADDPARAMS = "internation.addparams"; // NOI18N
57
/** Help ID for replacing format. */
58     public static final String JavaDoc HELP_ID_REPLFORMAT = "internation.replformat"; // NOI18N
59
/** Help ID for Locale execution. */
60     public static final String JavaDoc HELP_ID_RUNLOCALE = "internation.runlocale"; // NOI18N
61

62     /** Help ID for property editor */
63     public static final String JavaDoc PE_REPLACE_CODE_HELP_ID = "i18n.pe.replacestring"; // NOI18N
64
/** Help ID for property editor */
65     public static final String JavaDoc PE_I18N_REGEXP_HELP_ID = "i18n.pe.i18nregexp"; // NOI18N
66
/** Help ID for property editor */
67     public static final String JavaDoc PE_BUNDLE_CODE_HELP_ID = "i18n.pe.bundlestring"; // NOI18N
68
/** Help ID for property editor */
69     public static final String JavaDoc PE_TEST_REGEXP_HELP_ID = "i18n.pe.testregexp"; // NOI18N
70
/** Help ID for javaI18nString. It is a universal one for all subclasses. */
71     public static final String JavaDoc PE_I18N_STRING_HELP_ID = "i18n.pe.i18nString"; // NOI18N
72

73     
74     /** Items for init format customizer. */
75     private static List JavaDoc initFormatItems;
76
77     /** Help description for init format customizer. */
78     private static List JavaDoc initHelpItems;
79
80     /** Items for replace format customizer. */
81     private static List JavaDoc replaceFormatItems;
82
83     /** Help description for replace format customizer. */
84     private static List JavaDoc replaceHelpItems;
85
86     /** Items for regular expression customizer. */
87     private static List JavaDoc regExpItems;
88
89     /** Help description for regular expression customizer. */
90     private static List JavaDoc regExpHelpItems;
91     
92     /** Items for i18n regular expression customizer. */
93     private static List JavaDoc i18nRegExpItems;
94
95     /** Gets <code>initFormatItems</code>. */
96     public static List JavaDoc getInitFormatItems() {
97         if(initFormatItems == null) {
98             initFormatItems = new ArrayList JavaDoc(2);
99             initFormatItems.add("java.util.ResourceBundle.getBundle(\"{bundleNameSlashes}\")"); // NOI18N
100
initFormatItems.add("org.openide.util.NbBundle.getBundle({sourceFileName}.class)"); // NOI18N
101
}
102               
103         return initFormatItems;
104     }
105
106     /** Gets <code>InitHelpFormats</code>. */
107     public static List JavaDoc getInitHelpItems() {
108         if(initHelpItems == null) {
109             ResourceBundle JavaDoc bundle = getBundle();
110             initHelpItems = new ArrayList JavaDoc(3);
111             initHelpItems.add("{bundleNameSlashes} - "+ bundle.getString("TXT_PackageNameSlashes")); // NOI18N
112
initHelpItems.add("{bundleNameDots} - " + bundle.getString("TXT_PackageNameDots")); // NOI18N
113
initHelpItems.add("{sourceFileName} - " + bundle.getString("TXT_SourceDataObjectName")); // NOI18N
114
}
115          
116         return initHelpItems;
117     }
118
119     /** Gets <code>replaceFormatItems</code>. */
120     public static List JavaDoc getReplaceFormatItems() {
121         if(replaceFormatItems == null) {
122             replaceFormatItems = new ArrayList JavaDoc(7);
123             replaceFormatItems.add("{identifier}.getString(\"{key}\")"); // NOI18N
124
replaceFormatItems.add("Utilities.getString(\"{key}\")"); // NOI18N
125
replaceFormatItems.add("java.util.ResourceBundle.getBundle(\"{bundleNameSlashes}\").getString(\"{key}\")"); // NOI18N
126
replaceFormatItems.add("org.openide.util.NbBundle.getBundle({sourceFileName}.class).getString(\"{key}\")"); // NOI18N
127
replaceFormatItems.add("java.text.MessageFormat.format(java.util.ResourceBundle.getBundle(\"{bundleNameSlashes}\").getString(\"{key}\"), {arguments})"); // NOI18N
128
replaceFormatItems.add("org.openide.util.NbBundle.getMessage({sourceFileName}.class, \"{key}\")"); // NOI18N
129
replaceFormatItems.add("org.openide.util.NbBundle.getMessage({sourceFileName}.class, \"{key}\", {arguments})"); // NOI18N
130
}
131             
132         return replaceFormatItems;
133     }
134
135     /** Gets default replace format - based on whether the project type is
136      * a NB module project or not. (Module projects use NbBundle preferentially.)
137      */

138     public static String JavaDoc getDefaultReplaceFormat(boolean nbProject) {
139         return (String JavaDoc) getReplaceFormatItems().get(nbProject ? 5 : 2);
140     }
141
142     /** Gets <code>replaceHeplItems</code>.*/
143     public static List JavaDoc getReplaceHelpItems() {
144         if(replaceHelpItems == null) {
145             ResourceBundle JavaDoc bundle = getBundle();
146             replaceHelpItems = new ArrayList JavaDoc(6);
147             replaceHelpItems.add("{identifier} - " + bundle.getString("TXT_FieldIdentifier")); // NOI18N
148
replaceHelpItems.add("{key} - " + bundle.getString("TXT_KeyHelp")); // NOI18N
149
replaceHelpItems.add("{bundleNameSlashes} - " + bundle.getString("TXT_PackageNameSlashes")); // NOI18N
150
replaceHelpItems.add("{bundleNameDots} - " + bundle.getString("TXT_PackageNameDots")); // NOI18N
151
replaceHelpItems.add("{sourceFileName} - " + bundle.getString("TXT_SourceDataObjectName")); // NOI18N
152
replaceHelpItems.add("{arguments} - " + bundle.getString("TXT_Arguments")); // NOI18N
153
}
154             
155         return replaceHelpItems;
156     }
157
158     /** Gets <code>regExpItems</code>. */
159     public static List JavaDoc getRegExpItems() {
160         if(regExpItems == null) {
161             regExpItems = new ArrayList JavaDoc(4);
162             regExpItems.add("(getString|getBundle)[:space:]*\\([:space:]*{hardString}|// *NOI18N"); // NOI18N
163
regExpItems.add("(getString|getBundle)[:space:]*\\([:space:]*{hardString}"); // NOI18N
164
regExpItems.add("// *NOI18N"); // NOI18N
165
regExpItems.add("(getString|getBundle)[:space:]*\\([:space:]*|getMessage[:space:]*\\(([:alnum:]|[:punct:]|[:space:])*,[:space:]*{hardString}|// *NOI18N"); // NOI18N
166
}
167             
168         return regExpItems;
169     }
170     
171     /** Gets <code>i18nRegExpItems</code>. */
172     public static List JavaDoc getI18nRegExpItems() {
173         if(i18nRegExpItems == null) {
174             i18nRegExpItems = new ArrayList JavaDoc(2);
175             i18nRegExpItems.add("getString[:space:]*\\([:space:]*{hardString}"); // NOI18N
176
i18nRegExpItems.add("(getString[:space:]*\\([:space:]*|getMessage[:space:]*\\(([:alnum:]|[:punct:]|[:space:])*,[:space:]*){hardString}"); // NOI18N
177
}
178             
179         return i18nRegExpItems;
180     }
181     
182     /** Gets <code>regExpHelpItems</code>. */
183     public static List JavaDoc getRegExpHelpItems() {
184         if(regExpHelpItems == null) {
185             ResourceBundle JavaDoc bundle = getBundle();
186             regExpHelpItems = new ArrayList JavaDoc(13);
187             regExpHelpItems.add("{hardString} - " + bundle.getString("TXT_HardString")); // NOI18N
188
regExpHelpItems.add("[:alnum:] - " + bundle.getString("TXT_Alnum")); // NOI18N
189
regExpHelpItems.add("[:alpha:] - " + bundle.getString("TXT_Alpha")); // NOI18N
190
regExpHelpItems.add("[:blank:] - " + bundle.getString("TXT_Blank")); // NOI18N
191
regExpHelpItems.add("[:cntrl:] - " + bundle.getString("TXT_Cntrl")); // NOI18N
192
regExpHelpItems.add("[:digit:] - " + bundle.getString("TXT_Digit")); // NOI18N
193
regExpHelpItems.add("[:graph:] - " + bundle.getString("TXT_Graph")); // NOI18N
194
regExpHelpItems.add("[:lower:] - " + bundle.getString("TXT_Lower")); // NOI18N
195
regExpHelpItems.add("[:print:] - " + bundle.getString("TXT_Print")); // NOI18N
196
regExpHelpItems.add("[:punct:] - " + bundle.getString("TXT_Punct")); // NOI18N
197
regExpHelpItems.add("[:space:] - " + bundle.getString("TXT_Space")); // NOI18N
198
regExpHelpItems.add("[:upper:] - " + bundle.getString("TXT_Upper")); // NOI18N
199
regExpHelpItems.add("[:xdigit:] - " + bundle.getString("TXT_Xdigit")); // NOI18N
200
//regExpHelpItems.add("[:javastart:] - " + bundle.getString("TXT_Javastart")); // NOI18N
201
//regExpHelpItems.add("[:javapart:] - " + bundle.getString("TXT_Javapart")); // NOI18N
202
}
203         
204         return regExpHelpItems;
205     }
206
207     /**
208      * Indicates if folder or its subfolders contains data object
209      * that is supported by any internationalization factory.
210      */

211     public static boolean containsAcceptedDataObject(DataFolder folder) {
212         DataObject[] children = folder.getChildren();
213         DataObject[] folders = new DataObject[children.length];
214         int i, foldersCount = 0;
215
216         for (i = 0; i < children.length; i++) {
217             if (children[i] instanceof DataFolder) {
218                 folders[foldersCount++] = children[i];
219             } else if (FactoryRegistry.hasFactory(children[i].getClass())) {
220                 return true;
221             }
222         }
223         for (i = 0; i < foldersCount; i++) {
224             if (containsAcceptedDataObject((DataFolder) children[i])) {
225                 return true;
226             }
227         }
228         return false;
229     }
230     
231     /**
232      * Recursivelly get all accepted data objects starting from given folder.
233      */

234     public static List JavaDoc getAcceptedDataObjects(DataObject.Container folder) {
235         List JavaDoc accepted = new ArrayList JavaDoc();
236         
237         final VisibilityQuery visQuery = VisibilityQuery.getDefault();
238
239         DataObject[] children = folder.getChildren();
240
241         for(int i = 0; i < children.length; i++) {
242             if (!visQuery.isVisible(children[i].getPrimaryFile())) {
243                 continue;
244             }
245             if(children[i] instanceof DataObject.Container) {
246                 accepted.addAll(getAcceptedDataObjects((DataObject.Container)children[i]));
247             } else {
248                 if(FactoryRegistry.hasFactory(children[i].getClass()))
249                     accepted.add(children[i]);
250             }
251         }
252
253         return accepted;
254     }
255     
256     /** Gets resource bundle for i18n module. */
257     public static ResourceBundle JavaDoc getBundle() {
258         return NbBundle.getBundle(I18nUtil.class);
259     }
260     
261     /** Gets i18n options. */
262     public static I18nOptions getOptions() {
263         return I18nOptions.getDefault();
264     }
265     
266
267 }
268
Popular Tags