KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30 import java.util.prefs.Preferences JavaDoc;
31
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileStateInvalidException;
34 import org.openide.filesystems.FileSystem;
35 import org.openide.filesystems.FileSystem;
36 import org.openide.filesystems.FileUtil;
37 import org.openide.loaders.DataObject;
38 import org.openide.util.HelpCtx;
39 import org.openide.loaders.DataObjectNotFoundException;
40 import org.openide.nodes.BeanNode;
41 import org.openide.util.NbPreferences;
42
43 /**
44  * Options for i18n module.
45  *
46  * @author Peter Zavadsky
47  */

48 public class I18nOptions {
49     private static final I18nOptions INSTANCE = new I18nOptions();
50
51     /** Property name for advanced wizard.
52      * Indicates wheter I18N Wizard has to show panel with genaration field values for java sources. */

53     public static final String JavaDoc PROP_ADVANCED_WIZARD = "advancedWizard"; // NOI18N
54

55     /** Property name for init java code.
56      * Format for code which initializes generated resource bundle field in java source. */

57     public static final String JavaDoc PROP_INIT_JAVA_CODE = "initJavaCode"; // NOI18N
58

59     /** Property name for replacing java code.
60      * Format for actual i18n-ized code which replaces found non-i18n-ized hardcoded string. */

61     public static final String JavaDoc PROP_REPLACE_JAVA_CODE = "replaceJavaCode"; // NOI18N
62

63     /** Property name for regular expression for finding non-i18n strings.
64      * Regular expression format which is used for deciding whether found hardcoded string is non-i18n-ized.
65      * If line with found hardcoded string doesn't satisfy the expression it's non-i18n-ized. */

66     public static final String JavaDoc PROP_REGULAR_EXPRESSION = "regularExpression"; // NOI18N
67

68     /** Property name for regular expression for finding i18n strings.
69      * Regular expression format which is used for deciding whether found hardcoded string is i18n-ized.
70      * If line with found hardcoded string satisfies the expression it's i18n-ized. */

71     public static final String JavaDoc PROP_I18N_REGULAR_EXPRESSION = "i18nRegularExpression"; // NOI18N
72

73     /** Property name for replace rseource value.
74      * Indicates wheter values in resources for existing keys has to be replacesed or kept the old ones. */

75     public static final String JavaDoc PROP_REPLACE_RESOURCE_VALUE = "replaceResourceValue"; // NOI18N
76

77     /** Property name for last used resource data object.
78      * Hidden property which serializes last resource data object used by i18n module. */

79     public static final String JavaDoc PROP_LAST_RESOURCE2 = "lastResource2"; // NOI18N
80

81     
82     /** Provided due exeternaliazation only.
83      * Don't create this object directly use superclass <code>findObject</code> method instead. */

84     private I18nOptions() {}
85
86     public static I18nOptions getDefault() {
87         return INSTANCE;
88     }
89
90     private static Preferences JavaDoc getPreferences() {
91         return NbPreferences.forModule(I18nOptions.class);
92     }
93     
94     /** Implements superclass abstract method. */
95     public String JavaDoc displayName() {
96         return I18nUtil.getBundle().getString("LBL_Internationalization");
97     }
98
99     /** Getter for init advanced wizard property. */
100     public boolean isAdvancedWizard() {
101         // Lazy init.
102
return getPreferences().getBoolean(PROP_ADVANCED_WIZARD,false);
103     }
104
105     /** Setter for init advanced wizard property. */
106     public void setAdvancedWizard(boolean generateField) {
107         // Stores in class-wide state and fires property changes if needed:
108
getPreferences().putBoolean(PROP_ADVANCED_WIZARD,generateField);
109     }
110     
111     /** Getter for init java code property. */
112     public String JavaDoc getInitJavaCode() {
113         return getPreferences().get(PROP_INIT_JAVA_CODE,(String JavaDoc)I18nUtil.getInitFormatItems().get(0));
114     }
115
116     /** Setter for init java code property. */
117     public void setInitJavaCode(String JavaDoc initJavaCode) {
118         getPreferences().put(PROP_INIT_JAVA_CODE,initJavaCode);
119     }
120     
121     /** Getter for replace java code property. */
122     public String JavaDoc getReplaceJavaCode() {
123         // Lazy init.
124
return getPreferences().get(PROP_REPLACE_JAVA_CODE,(String JavaDoc)I18nUtil.getDefaultReplaceFormat(false));
125     }
126
127     /** Setter for replace java code property. */
128     public void setReplaceJavaCode(String JavaDoc replaceJavaCode) {
129         getPreferences().put(PROP_REPLACE_JAVA_CODE,replaceJavaCode);
130     }
131
132     /** Getter for regular expression property.
133      * @see #PROP_REGULAR_EXPRESSION */

134     public String JavaDoc getRegularExpression() {
135         return getPreferences().get(PROP_REGULAR_EXPRESSION,(String JavaDoc)I18nUtil.getRegExpItems().get(0));
136     }
137
138     /** Setter for regular expression property.
139      * @see #PROP_REGULAR_EXPRESSION */

140     public void setRegularExpression(String JavaDoc regExp) {
141         getPreferences().put(PROP_REGULAR_EXPRESSION,regExp);
142     }
143     
144     /** Getter for i18n regular expression property.
145      * @see #PROP_I18N_REGULAR_EXPRESSION */

146     public String JavaDoc getI18nRegularExpression() {
147         return getPreferences().get(PROP_I18N_REGULAR_EXPRESSION,(String JavaDoc)I18nUtil.getI18nRegExpItems().get(0));
148     }
149
150     /** Setter for i18n regular expression property.
151      * @see #PROP_I18N_REGULAR_EXPRESSION */

152     public void setI18nRegularExpression(String JavaDoc regExp) {
153         getPreferences().put(PROP_I18N_REGULAR_EXPRESSION,regExp);
154     }
155
156     /** Getter for replace resource value property. */
157     public boolean isReplaceResourceValue() {
158         return getPreferences().getBoolean(PROP_REPLACE_RESOURCE_VALUE,false);
159     }
160
161     /** Setter for replacve resource value property. */
162     public void setReplaceResourceValue(boolean replaceResourceValue) {
163         getPreferences().putBoolean(PROP_REPLACE_RESOURCE_VALUE,replaceResourceValue);
164     }
165     
166     /** Getter for last resource property. */
167     public DataObject getLastResource2() {
168         String JavaDoc path = getPreferences().get(PROP_LAST_RESOURCE2,null);
169         FileObject f = (path != null) ? findFileObject(path) : null;
170         if ((f != null) && !f.isFolder() && f.isValid()) {
171             try {
172                 return DataObject.find(f);
173             } catch (DataObjectNotFoundException e) {
174                 /* The file was probably deleted or moved. */
175                 getPreferences().remove(PROP_LAST_RESOURCE2);
176             }
177         }
178         return null;
179     }
180     
181     /** Setter for last resource property. */
182     public void setLastResource2(DataObject lastResource) {
183         // Make sure it is sane.
184
if(lastResource == null)
185             return;
186         
187         getPreferences().put(PROP_LAST_RESOURCE2,lastResource.getPrimaryFile().getPath());
188     }
189     
190     /** Get context help for this system option.
191     * @return context help
192     */

193     public HelpCtx getHelpCtx () {
194         return new HelpCtx (I18nOptions.class);
195     }
196     
197     private static FileSystem[] getFileSystems() {
198         List JavaDoc retval = new ArrayList JavaDoc();
199         File JavaDoc[] all = File.listRoots();
200         for (int i = 0; i < all.length; i++) {
201             File JavaDoc file = all[i];
202             FileObject fo = FileUtil.toFileObject(file);
203             if (fo != null) {
204                 try {
205                     retval.add(fo.getFileSystem());
206                 } catch (FileStateInvalidException ex) {
207                         Logger.getLogger(I18nOptions.class.getName()).log(Level.INFO, null, ex);
208                 }
209             }
210         }
211         return (FileSystem[])retval.toArray(new FileSystem[retval.size()]);
212     }
213     
214     private static FileObject findFileObject(String JavaDoc path) {
215         FileObject retval = null;
216         FileSystem[] fss = getFileSystems();
217         for (int i = 0; retval == null && i < fss.length; i++) {
218             FileSystem fileSystem = fss[i];
219             retval = fileSystem.findResource(path);
220         }
221         return retval;
222     }
223
224     private static BeanNode createViewNode() throws java.beans.IntrospectionException JavaDoc {
225         return new BeanNode(I18nOptions.getDefault());
226     }
227 }
228
Popular Tags