1 19 20 package org.netbeans.modules.options.general; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.prefs.Preferences ; 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 getProxyPreferences () { 41 return NbPreferences.root ().node ("org/netbeans/core"); 42 } 43 44 private static Preferences 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 getHttpProxyHost () { 59 return ProxySettings.getHttpHost (); 60 } 61 62 void setHttpProxyHost (String proxyHost) { 63 if (!proxyHost.equals(getHttpProxyHost ())) { 64 getProxyPreferences ().put (ProxySettings.PROXY_HTTP_HOST, proxyHost); 65 } 66 } 67 68 String getHttpProxyPort () { 69 return ProxySettings.getHttpPort (); 70 } 71 72 void setHttpProxyPort (String proxyPort) { 73 if (proxyPort != getHttpProxyPort()) { 74 getProxyPreferences().put(ProxySettings.PROXY_HTTP_PORT, validatePort (proxyPort) ? proxyPort : ""); 75 } 76 } 77 78 String getHttpsProxyHost () { 79 return ProxySettings.getHttpsHost (); 80 } 81 82 void setHttpsProxyHost (String proxyHost) { 83 if (!proxyHost.equals(getHttpsProxyHost ())) { 84 getProxyPreferences ().put (ProxySettings.PROXY_HTTPS_HOST, proxyHost); 85 } 86 } 87 88 String getHttpsProxyPort () { 89 return ProxySettings.getHttpsPort (); 90 } 91 92 void setHttpsProxyPort (String proxyPort) { 93 if (proxyPort != getHttpsProxyPort()) { 94 getProxyPreferences().put(ProxySettings.PROXY_HTTPS_PORT, validatePort (proxyPort) ? proxyPort : ""); 95 } 96 } 97 98 String getSocksHost () { 99 return ProxySettings.getSocksHost (); 100 } 101 102 void setSocksHost (String socksHost) { 103 if (socksHost != getSocksHost()) { 104 getProxyPreferences ().put (ProxySettings.PROXY_SOCKS_HOST, socksHost); 105 } 106 } 107 108 String getSocksPort () { 109 return ProxySettings.getSocksPort (); 110 } 111 112 void setSocksPort (String socksPort) { 113 if (socksPort != getSocksPort()) { 114 getProxyPreferences ().put (ProxySettings.PROXY_SOCKS_PORT, validatePort (socksPort) ? socksPort : ""); 115 } 116 } 117 118 String getNonProxyHosts () { 119 return code2view (ProxySettings.getNonProxyHosts ()); 120 } 121 122 void setNonProxyHosts (String 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 getProxyAuthenticationUsername () { 149 return ProxySettings.getAuthenticationUsername (); 150 } 151 152 void setAuthenticationUsername (String username) { 154 getProxyPreferences ().put (ProxySettings.PROXY_AUTHENTICATION_USERNAME, username); 155 } 156 157 char [] getProxyAuthenticationPassword () { 158 return ProxySettings.getAuthenticationPassword (); 159 } 160 161 void setAuthenticationPassword(char [] password) { 163 getProxyPreferences().put(ProxySettings.PROXY_AUTHENTICATION_PASSWORD, new String (password)); 164 } 165 166 boolean getAutoUpdateAskBeforeCheck () { 167 return getAutoupdatePreferences ().getBoolean ("askBefore", false); } 169 170 void setAutoUpdateAskBeforeCheck (boolean ask) { 171 if (ask != getAutoUpdateAskBeforeCheck()) { 172 getAutoupdatePreferences ().putBoolean ("askBefore", ask); } 174 } 175 176 int getAutoUpdatePeriod () { 177 return getAutoupdatePreferences ().getInt ("period", 2 ); } 179 180 void setAutoUpdatePeriod (int period) { 181 if (period != getAutoUpdatePeriod()) { 182 getAutoupdatePreferences ().putInt ("period", period); } 184 } 185 186 boolean isTextAntialiasing () { 187 return AllOptionsFolder.getDefault ().isTextAntialiasing (); 188 } 189 190 void setTextAntialiasing(boolean textAntialiasing) { 192 AllOptionsFolder.getDefault().setTextAntialiasing(textAntialiasing); 193 } 194 195 Map getCodeFoldingProperties () { 196 return (Map ) getEditorOption 197 ("getCodeFoldingProps", "text/x-java"); 198 } 199 200 void setCodeFoldingProperties (Map properties) { 202 setEditorOption 203 ("setCodeFoldingProps", Map .class, properties); 204 } 205 206 boolean getAutoPopupCompletion () { 207 Boolean b = (Boolean ) getEditorOption 208 ("getCompletionAutoPopup", "text/x-java"); 209 if (b == null) return true; 210 return b.booleanValue (); 211 } 212 213 void setAutoPopupCompletion (boolean auto) { 215 setEditorOption ("setCompletionAutoPopup", Boolean.TYPE, new Boolean (auto)); 216 } 217 218 boolean getAutoPopupJavaDoc () { 219 Boolean b = (Boolean ) getEditorOption 220 ("getJavaDocAutoPopup", "text/x-java"); 221 if (b == null) return true; 222 return b.booleanValue (); 223 } 224 225 void setAutoPopupJavaDoc (boolean auto) { 227 setEditorOption ("setJavaDocAutoPopup", Boolean.TYPE, new Boolean (auto)); 228 } 229 230 231 233 private static Object getEditorOption (String methodName, String mimeType) { 234 BaseOptions options = getOptions (mimeType); 236 237 if (options != null) { 238 try { 239 Method m = options.getClass().getMethod(methodName, new Class [] {}); 240 return m.invoke (options, new Object [] {}); 241 } catch (NoSuchMethodException ex) { 242 ex.printStackTrace (); 243 } catch (IllegalAccessException ex) { 244 ex.printStackTrace (); 245 } catch (InvocationTargetException ex) { 246 ex.printStackTrace (); 247 } 248 } 249 250 return null; 251 } 252 253 private static void setEditorOption (String methodName, Class paramType, Object value) { 254 Set mimeTypes = EditorSettings.getDefault().getMimeTypes(); 256 for(Iterator i = mimeTypes.iterator(); i.hasNext(); ) { 257 String mimeType = (String ) 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 Method m = baseOptions.getClass ().getMethod 267 (methodName, new Class [] {paramType}); 268 m.invoke (baseOptions, new Object [] {value}); 270 } catch (NoSuchMethodException ex) { 272 } catch (InvocationTargetException ex) { 273 ex.printStackTrace (); 274 } catch (IllegalAccessException ex) { 275 } 276 } 277 } 278 279 private static boolean validatePort (String 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 nfe) { 287 assert false : nfe; 288 } 289 return ok; 290 } 291 292 private static BaseOptions getOptions (String mimeType) { 293 return (BaseOptions) MimeLookup.getLookup(MimePath.parse(mimeType)).lookup(BaseOptions.class); 294 } 295 296 private static String code2view (String code) { 297 return code == null ? code : code.replace ("|", ", "); 298 } 299 300 private static String view2code (String view) { 301 return view == null ? view : view.replace (", ", "|"); 302 } 303 } 304 305 306 | Popular Tags |