KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > general > GeneralOptionsModel


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.options.general;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.prefs.Preferences JavaDoc;
28 import org.netbeans.api.editor.mimelookup.MimeLookup;
29 import org.netbeans.api.editor.mimelookup.MimePath;
30 import org.netbeans.core.ProxySettings;
31 import org.netbeans.modules.editor.options.AllOptionsFolder;
32 import org.netbeans.modules.editor.options.BaseOptions;
33 import org.netbeans.modules.editor.settings.storage.api.EditorSettings;
34 import org.openide.util.NbPreferences;
35
36
37 class GeneralOptionsModel {
38     
39     
40     private static Preferences JavaDoc getProxyPreferences () {
41         return NbPreferences.root ().node ("org/netbeans/core");
42     }
43     
44     private static Preferences JavaDoc getAutoupdatePreferences () {
45         return NbPreferences.root ().node ("org/netbeans/modules/autoupdate");
46     }
47     
48     int getProxyType () {
49         return getProxyPreferences ().getInt (ProxySettings.PROXY_TYPE, ProxySettings.AUTO_DETECT_PROXY);
50     }
51     
52     void setProxyType (int proxyType) {
53         if (proxyType != getProxyType ()) {
54             getProxyPreferences ().putInt (ProxySettings.PROXY_TYPE, proxyType);
55         }
56     }
57     
58     String JavaDoc getHttpProxyHost () {
59         return ProxySettings.getHttpHost ();
60     }
61     
62     void setHttpProxyHost (String JavaDoc proxyHost) {
63         if (!proxyHost.equals(getHttpProxyHost ())) {
64             getProxyPreferences ().put (ProxySettings.PROXY_HTTP_HOST, proxyHost);
65         }
66     }
67     
68     String JavaDoc getHttpProxyPort () {
69         return ProxySettings.getHttpPort ();
70     }
71     
72     void setHttpProxyPort (String JavaDoc proxyPort) {
73         if (proxyPort != getHttpProxyPort()) {
74             getProxyPreferences().put(ProxySettings.PROXY_HTTP_PORT, validatePort (proxyPort) ? proxyPort : "");
75         }
76     }
77     
78     String JavaDoc getHttpsProxyHost () {
79         return ProxySettings.getHttpsHost ();
80     }
81     
82     void setHttpsProxyHost (String JavaDoc proxyHost) {
83         if (!proxyHost.equals(getHttpsProxyHost ())) {
84             getProxyPreferences ().put (ProxySettings.PROXY_HTTPS_HOST, proxyHost);
85         }
86     }
87     
88     String JavaDoc getHttpsProxyPort () {
89         return ProxySettings.getHttpsPort ();
90     }
91     
92     void setHttpsProxyPort (String JavaDoc proxyPort) {
93         if (proxyPort != getHttpsProxyPort()) {
94             getProxyPreferences().put(ProxySettings.PROXY_HTTPS_PORT, validatePort (proxyPort) ? proxyPort : "");
95         }
96     }
97     
98     String JavaDoc getSocksHost () {
99         return ProxySettings.getSocksHost ();
100     }
101     
102     void setSocksHost (String JavaDoc socksHost) {
103         if (socksHost != getSocksHost()) {
104             getProxyPreferences ().put (ProxySettings.PROXY_SOCKS_HOST, socksHost);
105         }
106     }
107     
108     String JavaDoc getSocksPort () {
109         return ProxySettings.getSocksPort ();
110     }
111     
112     void setSocksPort (String JavaDoc socksPort) {
113         if (socksPort != getSocksPort()) {
114             getProxyPreferences ().put (ProxySettings.PROXY_SOCKS_PORT, validatePort (socksPort) ? socksPort : "");
115         }
116     }
117     
118     String JavaDoc getNonProxyHosts () {
119         return code2view (ProxySettings.getNonProxyHosts ());
120     }
121     
122     void setNonProxyHosts (String JavaDoc nonProxy) {
123         if (!nonProxy.equals(getNonProxyHosts())) {
124             getProxyPreferences ().put (ProxySettings.NOT_PROXY_HOSTS, view2code (nonProxy));
125         }
126     }
127     
128     boolean useProxyAuthentication () {
129         return ProxySettings.useAuthentication ();
130     }
131     
132     void setUseProxyAuthentication (boolean use) {
133         if (use != useProxyAuthentication()) {
134             getProxyPreferences ().putBoolean (ProxySettings.USE_PROXY_AUTHENTICATION, use);
135         }
136     }
137     
138     boolean useProxyAllProtocols () {
139         return ProxySettings.useProxyAllProtocols ();
140     }
141     
142     void setUseProxyAllProtocols (boolean use) {
143         if (use != useProxyAllProtocols ()) {
144             getProxyPreferences ().putBoolean (ProxySettings.USE_PROXY_ALL_PROTOCOLS, use);
145         }
146     }
147     
148     String JavaDoc getProxyAuthenticationUsername () {
149         return ProxySettings.getAuthenticationUsername ();
150     }
151
152     //TODO: not used yet - store valu just in case if modified
153
void setAuthenticationUsername (String JavaDoc username) {
154         getProxyPreferences ().put (ProxySettings.PROXY_AUTHENTICATION_USERNAME, username);
155     }
156     
157     char [] getProxyAuthenticationPassword () {
158         return ProxySettings.getAuthenticationPassword ();
159     }
160     
161     //TODO: not used yet - store valu just in case if modified
162
void setAuthenticationPassword(char [] password) {
163         getProxyPreferences().put(ProxySettings.PROXY_AUTHENTICATION_PASSWORD, new String JavaDoc(password));
164     }
165     
166     boolean getAutoUpdateAskBeforeCheck () {
167         return getAutoupdatePreferences ().getBoolean ("askBefore", false); // NOI18N
168
}
169     
170     void setAutoUpdateAskBeforeCheck (boolean ask) {
171         if (ask != getAutoUpdateAskBeforeCheck()) {
172             getAutoupdatePreferences ().putBoolean ("askBefore", ask); // NOI18N
173
}
174     }
175     
176     int getAutoUpdatePeriod () {
177         return getAutoupdatePreferences ().getInt ("period", 2 /*EVERY_WEEK*/); // NOI18N
178
}
179     
180     void setAutoUpdatePeriod (int period) {
181         if (period != getAutoUpdatePeriod()) {
182             getAutoupdatePreferences ().putInt ("period", period); // NOI18N
183
}
184     }
185     
186     boolean isTextAntialiasing () {
187         return AllOptionsFolder.getDefault ().isTextAntialiasing ();
188     }
189
190     //TODO: not used yet - store valu just in case if modified
191
void setTextAntialiasing(boolean textAntialiasing) {
192         AllOptionsFolder.getDefault().setTextAntialiasing(textAntialiasing);
193     }
194     
195     Map JavaDoc getCodeFoldingProperties () {
196        return (Map JavaDoc) getEditorOption
197             ("getCodeFoldingProps", "text/x-java");
198     }
199     
200      //TODO: not used yet - store valu just in case if modified
201
void setCodeFoldingProperties (Map JavaDoc properties) {
202         setEditorOption
203             ("setCodeFoldingProps", Map JavaDoc.class, properties);
204     }
205         
206     boolean getAutoPopupCompletion () {
207         Boolean JavaDoc b = (Boolean JavaDoc) getEditorOption
208             ("getCompletionAutoPopup", "text/x-java");
209         if (b == null) return true;
210         return b.booleanValue ();
211     }
212     
213     //TODO: not used yet - store valu just in case if modified
214
void setAutoPopupCompletion (boolean auto) {
215         setEditorOption ("setCompletionAutoPopup", Boolean.TYPE, new Boolean JavaDoc (auto));
216     }
217     
218     boolean getAutoPopupJavaDoc () {
219         Boolean JavaDoc b = (Boolean JavaDoc) getEditorOption
220             ("getJavaDocAutoPopup", "text/x-java");
221         if (b == null) return true;
222         return b.booleanValue ();
223     }
224     
225     //TODO: not used yet - store valu just in case if modified
226
void setAutoPopupJavaDoc (boolean auto) {
227         setEditorOption ("setJavaDocAutoPopup", Boolean.TYPE, new Boolean JavaDoc (auto));
228     }
229
230     
231     // private helper methods ..................................................
232

233     private static Object JavaDoc getEditorOption (String JavaDoc methodName, String JavaDoc mimeType) {
234         //S ystem.out.println("\n getEditorOption " + methodName + " : " + mimeType);
235
BaseOptions options = getOptions (mimeType);
236         
237         if (options != null) {
238             try {
239                 Method JavaDoc m = options.getClass().getMethod(methodName, new Class JavaDoc[] {});
240                 return m.invoke (options, new Object JavaDoc [] {});
241             } catch (NoSuchMethodException JavaDoc ex) {
242                 ex.printStackTrace ();
243             } catch (IllegalAccessException JavaDoc ex) {
244                 ex.printStackTrace ();
245             } catch (InvocationTargetException JavaDoc ex) {
246                 ex.printStackTrace ();
247             }
248         }
249         
250         return null;
251     }
252     
253     private static void setEditorOption (String JavaDoc methodName, Class JavaDoc paramType, Object JavaDoc value) {
254         //S ystem.out.println("\n setEditorOption " + methodName + " : " + value);
255
Set JavaDoc mimeTypes = EditorSettings.getDefault().getMimeTypes();
256         for(Iterator JavaDoc i = mimeTypes.iterator(); i.hasNext(); ) {
257             String JavaDoc mimeType = (String JavaDoc) i.next();
258             BaseOptions baseOptions = (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
259             
260             if (baseOptions == null) {
261                 continue;
262             }
263             
264             try {
265                 //S ystem.out.println (baseOptions.getClass ());
266
Method JavaDoc m = baseOptions.getClass ().getMethod
267                     (methodName, new Class JavaDoc[] {paramType});
268                 //S ystem.out.println(" " + m);
269
m.invoke (baseOptions, new Object JavaDoc[] {value});
270                 //S ystem.out.println("invoked! " + baseOptions);
271
} catch (NoSuchMethodException JavaDoc ex) {
272             } catch (InvocationTargetException JavaDoc ex) {
273                 ex.printStackTrace ();
274             } catch (IllegalAccessException JavaDoc ex) {
275             }
276         }
277     }
278     
279     private static boolean validatePort (String JavaDoc port) {
280         if (port.trim ().length () == 0) return true;
281         
282         boolean ok = false;
283         try {
284             Integer.parseInt (port);
285             ok = true;
286         } catch (NumberFormatException JavaDoc nfe) {
287             assert false : nfe;
288         }
289         return ok;
290     }
291     
292     private static BaseOptions getOptions (String JavaDoc mimeType) {
293         return (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class);
294     }
295     
296     private static String JavaDoc code2view (String JavaDoc code) {
297         return code == null ? code : code.replace ("|", ", ");
298     }
299     
300     private static String JavaDoc view2code (String JavaDoc view) {
301         return view == null ? view : view.replace (", ", "|");
302     }
303 }
304
305
306
Popular Tags