KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > settings > JavaSettings


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 package org.netbeans.modules.java.settings;
21
22 import java.io.*;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Collection JavaDoc;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31 import org.netbeans.modules.javacore.JMManager;
32
33 import org.openide.options.ContextSystemOption;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.Lookup;
36 import org.openide.util.NbBundle;
37 import org.openide.util.LookupEvent;
38 import org.openide.util.LookupListener;
39
40 /** Settings for java data loader and java source parser
41 *
42 * @author Ales Novak, Petr Hamernik
43 */

44 public class JavaSettings extends ContextSystemOption {
45     private static final int currentVersion = 1;
46
47     /** serial uid */
48     static final long serialVersionUID = -8522143676848697297L;
49
50     public static final String JavaDoc PROP_SHOW_OVERRIDING = "showOverriding"; //NOI18N
51

52     public static final String JavaDoc PROP_SHOW_COMPILE_STATUS = "showCompileStatus"; // NOI18N
53

54     public static final String JavaDoc PROP_REPLACEABLE_STRINGS_TABLE = "replaceableStringsTable"; // NOI18N
55

56     public static final String JavaDoc PROP_AUTO_PARSING_DELAY = "autoParsingDelay"; // NOI18N
57
public static final String JavaDoc PROP_PARSING_ERRORS = "parsingErrors"; // NOI18N
58

59     public static final int DEFAULT_AUTO_PARSING_DELAY = 2000;
60     public static final int DEFAULT_PARSING_ERRORS = 100;
61     private static final boolean DEFAULT_SHOW_OVERRIDING = true;
62
63     /**
64      * Current version of the settings object. If < currentVersion,
65      * a settings upgrade is made.
66      */

67     private int version;
68         
69     private static JavaSettings javaSettings;
70     
71     /** If true then external execution is used */
72     public String JavaDoc displayName () {
73         return getString("CTL_Java_option");
74     }
75
76     public HelpCtx getHelpCtx () {
77         return new HelpCtx (JavaSettings.class);
78     }
79
80     public JavaSettings() {
81         addOption(getJavaSynchronizationSettings());
82     }
83
84     /** Sets the replaceable strings table - used during instantiating
85     * from template.
86     */

87     public void setReplaceableStringsTable(String JavaDoc table) {
88         String JavaDoc t = getReplaceableStringsTable();
89         if (t.equals(table))
90             return;
91         putProperty(PROP_REPLACEABLE_STRINGS_TABLE, table, true);
92     }
93
94     /** Gets the replacable strings table - used during instantiating
95     * from template.
96     */

97     public String JavaDoc getReplaceableStringsTable() {
98         String JavaDoc table = (String JavaDoc)getProperty(PROP_REPLACEABLE_STRINGS_TABLE);
99         if (table == null) {
100             return "USER="+System.getProperty("user.name")+"\n"; // NOI18N
101
} else {
102             return table;
103         }
104     }
105
106     /** Gets the replaceable table as the Properties class.
107     * @return the properties
108     */

109     public Properties JavaDoc getReplaceableStringsProps() {
110         Properties JavaDoc props = new Properties JavaDoc();
111         String JavaDoc propString = getReplaceableStringsTable();
112         int i,len = propString.length();
113         StringBuffer JavaDoc unicodeString = new StringBuffer JavaDoc(len);
114         
115         for (i=0;i<len;i++) {
116             char aChar = propString.charAt(i);
117             
118             if (aChar > 0x00ff) {
119                 String JavaDoc hex = Integer.toHexString(aChar);
120                 
121                 unicodeString.append("\\u"); // NOI18N
122
if (aChar < 0x1000)
123                     unicodeString.append("0"); // NOI18N
124
unicodeString.append(hex);
125             } else {
126                 unicodeString.append(aChar);
127             }
128         }
129         try {
130             props.load(new ByteArrayInputStream(unicodeString.toString().getBytes()));
131         }
132         catch (IOException e) {
133         }
134         return props;
135     }
136
137     /** Gets the delay time for the start of the parsing.
138     * @return The time in milis
139     */

140     public int getAutoParsingDelay() {
141         Integer JavaDoc delay = (Integer JavaDoc)getProperty(PROP_AUTO_PARSING_DELAY);
142         if (delay == null)
143             return DEFAULT_AUTO_PARSING_DELAY;
144         return delay.intValue();
145     }
146
147     /** Sets the number of errors returned by the parsing and displayed as
148      * annotations in source text
149     * @param number of errors, 0 means disable
150     */

151     public void setParsingErrors(int errors) {
152         if (errors < 0)
153             throw new IllegalArgumentException JavaDoc();
154         putProperty(PROP_PARSING_ERRORS, new Integer JavaDoc(errors),true); // fire property change
155
}
156
157     /** Gets the number of errors returned by the parsing and displayed as
158      * annotations in source text
159      * @return number of errors
160     */

161     public int getParsingErrors() {
162         Integer JavaDoc errors = (Integer JavaDoc)getProperty(PROP_PARSING_ERRORS);
163         if (errors == null)
164             return DEFAULT_PARSING_ERRORS;
165         return errors.intValue();
166     }
167
168     public boolean getShowOverriding () {
169         Boolean JavaDoc value = (Boolean JavaDoc)getProperty (PROP_SHOW_OVERRIDING);
170         if (value == null)
171             return DEFAULT_SHOW_OVERRIDING;
172         return value.booleanValue();
173     }
174
175     public void setShowOverriding (boolean value) {
176         Boolean JavaDoc b = (Boolean JavaDoc)getProperty(PROP_SHOW_OVERRIDING);
177         if (b != null && b.booleanValue() == value)
178             return;
179         putProperty (PROP_SHOW_OVERRIDING, value? Boolean.TRUE : Boolean.FALSE,true);
180     }
181
182     /** Sets the delay time for the start of the parsing.
183     * @param delay The time in milis
184     */

185     public void setAutoParsingDelay(int delay) {
186         if (delay < 0)
187             throw new IllegalArgumentException JavaDoc();
188         putProperty(PROP_AUTO_PARSING_DELAY, new Integer JavaDoc(delay));
189     }
190
191     
192     public boolean isCompileStatusEnabled() {
193         Boolean JavaDoc b = (Boolean JavaDoc)getProperty(PROP_SHOW_COMPILE_STATUS);
194         if (b == null)
195             return true;
196         return b.booleanValue();
197     }
198     
199     public void enableCompileStatus(boolean b) {
200         if (isCompileStatusEnabled() == b)
201             return;
202         putProperty(PROP_SHOW_COMPILE_STATUS, b ? Boolean.TRUE : Boolean.FALSE, true);
203     }
204     
205     /** @return localized string */
206     static String JavaDoc getString(String JavaDoc s) {
207         return NbBundle.getMessage(JavaSettings.class, s);
208     }
209
210     private static JavaSynchronizationSettings getJavaSynchronizationSettings() {
211         return (JavaSynchronizationSettings) JavaSynchronizationSettings.findObject(JavaSynchronizationSettings.class, true);
212     }
213
214     public void readExternal(ObjectInput in)
215     throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
216         super.readExternal(in);
217         if (in.available() > 0) {
218             version = in.readInt();
219         }
220         if (version < currentVersion) {
221             // assume version == 0, for now
222

223             version = currentVersion;
224         }
225         JMManager.setDefaultEncoding(defaultEncoding);
226     }
227     
228     public void writeExternal(ObjectOutput out) throws IOException {
229         super.writeExternal(out);
230         out.writeInt(version);
231     }
232     
233     public static final JavaSettings getDefault() {
234         if (javaSettings==null)
235             javaSettings=(JavaSettings) JavaSettings.findObject(JavaSettings.class, true);
236         return javaSettings;
237     }
238     
239     /* ------------ Support for text I/O ---------------------- */
240     
241     /**
242      * Name of the "default encoding" property
243      */

244     public static final String JavaDoc PROP_DEFAULT_ENCODING = "defaultEncoding"; // NOI18N
245

246     /**
247      * Platform - default encoding. No specific encoding will be passed to readers
248      * or writers. It is specified as empty string so it won't produce the (null) text
249      * for the user.
250      */

251     public static final String JavaDoc ENCODING_PLATFORM_DEFAULT = ""; // NOI18N
252

253     /**
254      * Holds the name of the default encoding which should be used when reading
255      * or writing .java files. Can be one of the tagged values ENCODING_* manifested above.
256      */

257     private String JavaDoc defaultEncoding = ENCODING_PLATFORM_DEFAULT;
258     
259     public String JavaDoc getDefaultEncoding() {
260         return defaultEncoding;
261     }
262     
263     public void setDefaultEncoding(String JavaDoc enc) {
264         String JavaDoc e = defaultEncoding;
265         defaultEncoding = enc;
266         JMManager.setDefaultEncoding(enc);
267         firePropertyChange(PROP_DEFAULT_ENCODING, e, enc);
268     }
269     
270 }
271
Popular Tags