001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core;
043:
044: import java.net.MalformedURLException;
045: import java.net.URL;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import java.util.prefs.Preferences;
049: import java.util.regex.Pattern;
050: import java.util.regex.PatternSyntaxException;
051: import org.openide.awt.HtmlBrowser;
052: import org.openide.cookies.InstanceCookie;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.Repository;
055: import org.openide.loaders.DataFolder;
056: import org.openide.loaders.DataNode;
057: import org.openide.loaders.DataObject;
058: import org.openide.nodes.BeanNode;
059: import org.openide.util.Exceptions;
060: import org.openide.util.HelpCtx;
061: import org.openide.util.Lookup;
062: import org.openide.util.NbBundle;
063: import org.openide.util.NbPreferences;
064:
065: /** Global IDE settings.
066: *
067: * @author Ian Formanek
068: */
069: public class IDESettings {
070: private static final IDESettings INSTANCE = new IDESettings();
071:
072: /** showToolTipsInIDE property name */
073: public static final String PROP_SHOW_TOOLTIPS_IN_IDE = "showToolTipsInIDE"; // NOI18N
074: /** confirmDelete property name */
075: public static final String PROP_CONFIRM_DELETE = "confirmDelete"; // NOI18N
076: /** home page property name */
077: public static final String PROP_HOME_PAGE = "homePage"; // NOI18N
078: /** show file extensions property name */
079: public static final String PROP_SHOW_FILE_EXTENSIONS = "showFileExtensions"; // NOI18N
080: /** Web Browser prefered by user */
081: public static final String PROP_WWWBROWSER = "WWWBrowser"; // NOI18N
082:
083: /** files that should be ignored
084: *
085: * DO NOT CHANGE THIS PROPERTY NAME without checking that
086: * this property name was changed also in GlobalVisibilityQueryImpl
087: * in module org.netbeans.modules.masterfs.
088: *
089: */
090: public static final String PROP_IGNORED_FILES = "IgnoredFiles"; // NOI18N
091:
092: // ------------------------------------------
093: // properties
094:
095: public static IDESettings getInstance() {
096: return INSTANCE;
097: }
098:
099: static Preferences getPreferences() {
100: return NbPreferences.forModule(IDESettings.class);
101: }
102:
103: // ------------------------------------------
104: // property access methods
105:
106: /** Getter for ShowToolTipsInIDE
107: * @return true if dialog will be shown*/
108: public boolean getShowToolTipsInIDE() {
109: return getPreferences().getBoolean(PROP_SHOW_TOOLTIPS_IN_IDE,
110: true);
111: }
112:
113: /** Setter for ShowToolTipsInIDE
114: * @param value true if on the next start of corona the dialog will be shown
115: * false otherwise */
116: public void setShowToolTipsInIDE(boolean value) {
117: getPreferences().putBoolean(PROP_SHOW_TOOLTIPS_IN_IDE, value);
118: }
119:
120: /** Getter for ConfirmDelete
121: * @param true if the user should asked for confirmation of object delete, false otherwise */
122: public boolean getConfirmDelete() {
123: return getPreferences().getBoolean(PROP_CONFIRM_DELETE, true);
124: }
125:
126: /** Setter for ConfirmDelete
127: * @param value if true the user is asked for confirmation of object delete, not if false */
128: public void setConfirmDelete(boolean value) {
129: getPreferences().putBoolean(PROP_CONFIRM_DELETE, value);
130: }
131:
132: /** This method must be overriden. It returns display name of this options.
133: */
134: public String displayName() {
135: return NbBundle.getBundle(IDESettings.class).getString(
136: "CTL_IDESettings");
137: }
138:
139: public HelpCtx getHelpCtx() {
140: return new HelpCtx(IDESettings.class);
141: }
142:
143: /** Getter for home page used in html viewer.
144: */
145: public String getHomePage() {
146: return HtmlBrowser.getHomePage();
147: }
148:
149: /** Setter for home page used in html viewer.
150: */
151: public void setHomePage(String homePage) {
152: HtmlBrowser.setHomePage(homePage);
153: }
154:
155: /** Getter for showing file extensions.
156: * @return whether to show them
157: */
158: public boolean getShowFileExtensions() {
159: return DataNode.getShowFileExtensions();
160: }
161:
162: /** Setter for showing file extensions.
163: * @param s whether to show them
164: */
165: public void setShowFileExtensions(boolean s) {
166: DataNode.setShowFileExtensions(s);
167: }
168:
169: /** Getter for preffered web browser.
170: *
171: * First time when this function is called Lookup is used
172: * to find prefered browser factory in a browser registry.
173: *
174: * @return prefered browser,
175: * may return null if it is not possible to get the browser
176: */
177: public static HtmlBrowser.Factory getWWWBrowser() {
178: try {
179: Object obj = getPreferences().get(PROP_WWWBROWSER, null);
180:
181: if (obj instanceof String && !"".equals(obj)) {
182: // use new style
183: Lookup.Item<HtmlBrowser.Factory> item = Lookup
184: .getDefault()
185: .lookupItem(
186: new Lookup.Template<HtmlBrowser.Factory>(
187: HtmlBrowser.Factory.class,
188: (String) obj, null));
189: return item == null ? null : item.getInstance();
190: }
191:
192: // the browser is not set yet - find the first one
193: if (obj == null || "".equals(obj)) {
194: Lookup.Result<HtmlBrowser.Factory> res = Lookup
195: .getDefault().lookupResult(
196: HtmlBrowser.Factory.class);
197: java.util.Iterator<? extends HtmlBrowser.Factory> it = res
198: .allInstances().iterator();
199: while (it.hasNext()) {
200: HtmlBrowser.Factory brow = it.next();
201:
202: // check if it is not set to be hidden
203: FileObject fo = Repository.getDefault()
204: .getDefaultFileSystem().findResource(
205: "Services/Browsers"); // NOI18N
206:
207: DataFolder folder = DataFolder.findFolder(fo);
208: DataObject[] dobjs = folder.getChildren();
209: for (int i = 0; i < dobjs.length; i++) {
210: Object o = null;
211:
212: try {
213: if (Boolean.TRUE.equals(dobjs[i]
214: .getPrimaryFile().getAttribute(
215: "hidden")))
216: continue;
217: InstanceCookie cookie = (InstanceCookie) dobjs[i]
218: .getCookie(InstanceCookie.class);
219:
220: if (cookie == null)
221: continue;
222: o = cookie.instanceCreate();
223: if (o != null && o.equals(brow)) {
224: return brow;
225: }
226: }
227: // exceptions are thrown if module is uninstalled
228: catch (java.io.IOException ex) {
229: Logger.getLogger(
230: IDESettings.class.getName()).log(
231: Level.WARNING, null, ex);
232: } catch (ClassNotFoundException ex) {
233: Logger.getLogger(
234: IDESettings.class.getName()).log(
235: Level.WARNING, null, ex);
236: }
237: }
238:
239: }
240: return null;
241: }
242: } catch (Exception ex) {
243: Exceptions.printStackTrace(ex);
244: }
245: return null;
246: }
247:
248: /** Setter for preffered browser.
249: *
250: * Actually Node.Handle of node that represent browser in lookup folder is stored.
251: *
252: * @param brow prefered browser capable of providing implementation
253: */
254: public static void setWWWBrowser(HtmlBrowser.Factory brow) {
255: try {
256: if (brow == null) {
257: getPreferences().put(PROP_WWWBROWSER, "");//NOI18N
258: return;
259: }
260:
261: Lookup.Item<HtmlBrowser.Factory> item = Lookup.getDefault()
262: .lookupItem(
263: new Lookup.Template<HtmlBrowser.Factory>(
264: HtmlBrowser.Factory.class, null,
265: brow));
266: if (item != null) {
267: getPreferences().put(PROP_WWWBROWSER, item.getId());
268: } else {
269: // strange
270: Logger.getLogger(IDESettings.class.getName()).warning(
271: "IDESettings: Cannot find browser in lookup");// NOI18N
272: getPreferences().put(PROP_WWWBROWSER, "");//NOI18N
273: }
274: } catch (Exception ex) {
275: Exceptions.printStackTrace(ex);
276: }
277: }
278:
279: // PRIVATE METHODS
280:
281: public String getIgnoredFiles() {
282: return getPreferences()
283: .get(
284: PROP_IGNORED_FILES,
285: "^(CVS|SCCS|vssver.?\\.scc|#.*#|%.*%|\\.(cvsignore|svn|DS_Store)|_svn)$|~$|^\\..*$"); //NOI18N
286: }
287:
288: public void setIgnoredFiles(String ignoredFiles)
289: throws IllegalArgumentException {
290: try {
291: Pattern.compile(ignoredFiles);
292: getPreferences().put(PROP_IGNORED_FILES, ignoredFiles); //NOI18N
293: } catch (PatternSyntaxException e) {
294: IllegalArgumentException iae = new IllegalArgumentException();
295: iae.initCause(e);
296: UIExceptions.annotateUser(iae, e.getMessage(), e
297: .getLocalizedMessage(), null, null);
298: throw iae;
299: }
300: }
301:
302: private static BeanNode createViewNode()
303: throws java.beans.IntrospectionException {
304: return new BeanNode(IDESettings.getInstance());
305: }
306:
307: private static org.netbeans.beaninfo.editors.HtmlBrowser.FactoryEditor createHtmlBrowserFactoryEditor() {
308: return new org.netbeans.beaninfo.editors.HtmlBrowser.FactoryEditor() {
309: public void setValue(Object value) {
310: setWWWBrowser((HtmlBrowser.Factory) value);
311: }
312:
313: public Object getValue() {
314: return getWWWBrowser();
315: }
316: };
317: }
318: }
|