KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > IDESettings


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.core;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import java.util.prefs.Preferences JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28 import java.util.regex.PatternSyntaxException JavaDoc;
29 import org.openide.awt.HtmlBrowser;
30 import org.openide.cookies.InstanceCookie;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.Repository;
33 import org.openide.loaders.DataFolder;
34 import org.openide.loaders.DataNode;
35 import org.openide.loaders.DataObject;
36 import org.openide.nodes.BeanNode;
37 import org.openide.util.Exceptions;
38 import org.openide.util.HelpCtx;
39 import org.openide.util.Lookup;
40 import org.openide.util.NbBundle;
41 import org.openide.util.NbPreferences;
42
43 /** Global IDE settings.
44  *
45  * @author Ian Formanek
46  */

47 public class IDESettings {
48     private static final IDESettings INSTANCE = new IDESettings();
49     
50     /** showToolTipsInIDE property name */
51     public static final String JavaDoc PROP_SHOW_TOOLTIPS_IN_IDE = "showToolTipsInIDE"; // NOI18N
52
/** confirmDelete property name */
53     public static final String JavaDoc PROP_CONFIRM_DELETE = "confirmDelete"; // NOI18N
54
/** home page property name */
55     public static final String JavaDoc PROP_HOME_PAGE = "homePage"; // NOI18N
56
/** show file extensions property name */
57     public static final String JavaDoc PROP_SHOW_FILE_EXTENSIONS = "showFileExtensions"; // NOI18N
58
/** Web Browser prefered by user */
59     public static final String JavaDoc PROP_WWWBROWSER = "WWWBrowser"; // NOI18N
60

61     /** files that should be ignored
62      *
63      * DO NOT CHANGE THIS PROPERTY NAME without checking that
64      * this property name was changed also in GlobalVisibilityQueryImpl
65      * in module org.netbeans.modules.masterfs.
66      *
67      */

68     public static final String JavaDoc PROP_IGNORED_FILES = "IgnoredFiles"; // NOI18N
69

70    
71     // ------------------------------------------
72
// properties
73

74     public static IDESettings getInstance() {
75         return INSTANCE;
76     }
77     
78     static Preferences JavaDoc getPreferences() {
79         return NbPreferences.forModule(IDESettings.class);
80     }
81     
82     // ------------------------------------------
83
// property access methods
84

85     /** Getter for ShowToolTipsInIDE
86      * @return true if dialog will be shown*/

87     public boolean getShowToolTipsInIDE () {
88         return getPreferences().getBoolean(PROP_SHOW_TOOLTIPS_IN_IDE, true);
89     }
90     
91     /** Setter for ShowToolTipsInIDE
92      * @param value true if on the next start of corona the dialog will be shown
93      * false otherwise */

94     public void setShowToolTipsInIDE (boolean value) {
95         getPreferences().putBoolean(PROP_SHOW_TOOLTIPS_IN_IDE, value);
96     }
97     
98     /** Getter for ConfirmDelete
99      * @param true if the user should asked for confirmation of object delete, false otherwise */

100     public boolean getConfirmDelete () {
101         return getPreferences().getBoolean(PROP_CONFIRM_DELETE, true);
102     }
103     
104     /** Setter for ConfirmDelete
105      * @param value if true the user is asked for confirmation of object delete, not if false */

106     public void setConfirmDelete (boolean value) {
107         getPreferences().putBoolean(PROP_CONFIRM_DELETE, value);
108     }
109     
110     /** This method must be overriden. It returns display name of this options.
111      */

112     public String JavaDoc displayName () {
113         return NbBundle.getBundle (IDESettings.class).getString ("CTL_IDESettings");
114     }
115     
116     public HelpCtx getHelpCtx () {
117         return new HelpCtx (IDESettings.class);
118     }
119     
120     /** Getter for home page used in html viewer.
121      */

122     public String JavaDoc getHomePage () {
123         return HtmlBrowser.getHomePage ();
124     }
125     
126     /** Setter for home page used in html viewer.
127      */

128     public void setHomePage (String JavaDoc homePage) {
129         HtmlBrowser.setHomePage (homePage);
130     }
131     
132     /** Getter for showing file extensions.
133      * @return whether to show them
134      */

135     public boolean getShowFileExtensions () {
136         return DataNode.getShowFileExtensions ();
137     }
138     
139     /** Setter for showing file extensions.
140      * @param s whether to show them
141      */

142     public void setShowFileExtensions (boolean s) {
143         DataNode.setShowFileExtensions (s);
144     }
145     
146     /** Getter for preffered web browser.
147      *
148      * First time when this function is called Lookup is used
149      * to find prefered browser factory in a browser registry.
150      *
151      * @return prefered browser,
152      * may return null if it is not possible to get the browser
153      */

154     public static HtmlBrowser.Factory getWWWBrowser () {
155         try {
156             Object JavaDoc obj = getPreferences().get(PROP_WWWBROWSER, null);
157             
158             if (obj instanceof String JavaDoc && !"".equals (obj)) {
159                 // use new style
160
Lookup.Item<HtmlBrowser.Factory> item = Lookup.getDefault ().lookupItem (new Lookup.Template<HtmlBrowser.Factory> (HtmlBrowser.Factory.class, (String JavaDoc)obj, null));
161                 return item == null ? null : item.getInstance ();
162             }
163             
164             // the browser is not set yet - find the first one
165
if (obj == null || "".equals (obj)) {
166                 Lookup.Result<HtmlBrowser.Factory> res = Lookup.getDefault ().lookupResult (HtmlBrowser.Factory.class);
167                 java.util.Iterator JavaDoc<? extends HtmlBrowser.Factory> it = res.allInstances ().iterator ();
168                 while (it.hasNext ()) {
169                     HtmlBrowser.Factory brow = it.next ();
170                     
171                     // check if it is not set to be hidden
172
FileObject fo = Repository.getDefault ()
173                     .getDefaultFileSystem ().findResource ("Services/Browsers"); // NOI18N
174

175                     DataFolder folder = DataFolder.findFolder (fo);
176                     DataObject [] dobjs = folder.getChildren ();
177                     for (int i = 0; i < dobjs.length; i++) {
178                         Object JavaDoc o = null;
179                         
180                         try {
181                             if (Boolean.TRUE.equals (dobjs[i].getPrimaryFile ().getAttribute ("hidden")))
182                                 continue;
183                             InstanceCookie cookie = (InstanceCookie) dobjs[i].getCookie (InstanceCookie.class);
184                             
185                             if (cookie == null)
186                                 continue;
187                             o = cookie.instanceCreate ();
188                             if (o != null && o.equals (brow)) {
189                                 return brow;
190                             }
191                         }
192                         // exceptions are thrown if module is uninstalled
193
catch (java.io.IOException JavaDoc ex) {
194                             Logger.getLogger (IDESettings.class.getName ()).log (Level.WARNING, null, ex);
195                         } catch (ClassNotFoundException JavaDoc ex) {
196                             Logger.getLogger (IDESettings.class.getName ()).log (Level.WARNING, null, ex);
197                         }
198                     }
199                     
200                 }
201                 return null;
202             }
203         } catch (Exception JavaDoc ex) {
204             Exceptions.printStackTrace (ex);
205         }
206         return null;
207     }
208     
209     /** Setter for preffered browser.
210      *
211      * Actually Node.Handle of node that represent browser in lookup folder is stored.
212      *
213      * @param brow prefered browser capable of providing implementation
214      */

215     public static void setWWWBrowser (HtmlBrowser.Factory brow) {
216         try {
217             if (brow == null) {
218                 getPreferences().put(PROP_WWWBROWSER, "");//NOI18N
219
return;
220             }
221             
222             Lookup.Item<HtmlBrowser.Factory> item =
223                     Lookup.getDefault ().lookupItem (new Lookup.Template<HtmlBrowser.Factory> (HtmlBrowser.Factory.class, null, brow));
224             if (item != null) {
225                 getPreferences().put(PROP_WWWBROWSER, item.getId ());
226             } else {
227                 // strange
228
Logger.getLogger (IDESettings.class.getName ()).warning ("IDESettings: Cannot find browser in lookup");// NOI18N
229
getPreferences().put(PROP_WWWBROWSER, "");//NOI18N
230
}
231         } catch (Exception JavaDoc ex) {
232             Exceptions.printStackTrace (ex);
233         }
234     }
235     
236     // PRIVATE METHODS
237

238     public String JavaDoc getIgnoredFiles () {
239         return getPreferences().get(PROP_IGNORED_FILES, "^(CVS|SCCS|vssver\\.scc|#.*#|%.*%|\\.(cvsignore|svn|DS_Store)|_svn)$|~$|^\\..*$"); //NOI18N
240
}
241     
242     public void setIgnoredFiles(String JavaDoc ignoredFiles) throws IllegalArgumentException JavaDoc {
243         try {
244             Pattern.compile(ignoredFiles);
245             getPreferences().put(PROP_IGNORED_FILES, ignoredFiles); //NOI18N
246
} catch (PatternSyntaxException JavaDoc e) {
247             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc();
248             iae.initCause( e );
249             UIExceptions.annotateUser(iae, e.getMessage(),
250                     e.getLocalizedMessage(), null, null);
251             throw iae;
252         }
253     }
254
255     private static BeanNode createViewNode() throws java.beans.IntrospectionException JavaDoc {
256         return new BeanNode(IDESettings.getInstance());
257     }
258
259     private static org.netbeans.beaninfo.editors.HtmlBrowser.FactoryEditor createHtmlBrowserFactoryEditor() {
260         return new org.netbeans.beaninfo.editors.HtmlBrowser.FactoryEditor(){
261             public void setValue(Object JavaDoc value) {
262                 setWWWBrowser((HtmlBrowser.Factory)value);
263             }
264             
265             public Object JavaDoc getValue() {
266                 return getWWWBrowser();
267             }
268         };
269     }
270 }
271
Popular Tags