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.beaninfo.editors;
043:
044: import java.beans.PropertyEditorSupport;
045: import java.text.MessageFormat;
046: import java.util.ArrayList;
047: import org.netbeans.core.UIExceptions;
048: import org.openide.cookies.InstanceCookie;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileStateInvalidException;
051: import org.openide.filesystems.Repository;
052: import org.openide.loaders.DataFolder;
053: import org.openide.loaders.DataObject;
054: import org.openide.util.Exceptions;
055: import org.openide.util.Lookup;
056: import org.openide.util.NbBundle;
057:
058: /**
059: * Defines editor for choosing of Web browser.
060: *
061: * @author Radim Kubacki
062: */
063: public class HtmlBrowser extends Object {
064:
065: public static class FactoryEditor extends PropertyEditorSupport {
066:
067: /** extended attribute that signals that this object should not be visible to the user */
068: private static final String EA_HIDDEN = "hidden"; // NOI18N
069:
070: private static final String BROWSER_FOLDER = "Services/Browsers"; // NOI18N
071:
072: /** Creates new FactoryEditor */
073: public FactoryEditor() {
074: }
075:
076: public String getAsText() {
077: try {
078: org.openide.awt.HtmlBrowser.Factory f = (org.openide.awt.HtmlBrowser.Factory) getValue();
079:
080: Lookup.Item<org.openide.awt.HtmlBrowser.Factory> i = Lookup
081: .getDefault()
082: .lookupItem(
083: new Lookup.Template<org.openide.awt.HtmlBrowser.Factory>(
084: org.openide.awt.HtmlBrowser.Factory.class,
085: null, f));
086: if (i != null)
087: return i.getDisplayName();
088: } catch (Exception ex) {
089: Exceptions.printStackTrace(ex);
090: }
091: return NbBundle.getMessage(FactoryEditor.class,
092: "CTL_UnspecifiedBrowser"); //NOI18N
093: }
094:
095: public void setAsText(java.lang.String str)
096: throws java.lang.IllegalArgumentException {
097: try {
098: if (NbBundle.getMessage(FactoryEditor.class,
099: "CTL_UnspecifiedBrowser").equals(str) //NOI18N
100: || str == null) {
101: setValue(null);
102: return;
103: }
104: Lookup.Result<org.openide.awt.HtmlBrowser.Factory> r = Lookup
105: .getDefault()
106: .lookupResult(
107: org.openide.awt.HtmlBrowser.Factory.class);
108: for (Lookup.Item<org.openide.awt.HtmlBrowser.Factory> i : r
109: .allItems()) {
110: if (str.equals(i.getDisplayName())) {
111: setValue(i.getInstance());
112: return;
113: }
114: }
115: } catch (Exception e) {
116: IllegalArgumentException iae = new IllegalArgumentException(
117: e.getMessage());
118: String msg = e.getLocalizedMessage();
119: if (msg == null) {
120: msg = MessageFormat.format(NbBundle.getMessage(
121: HtmlBrowser.class,
122: "FMT_EXC_GENERIC_BAD_VALUE"), //NOI18N
123: new Object[] { str });
124: }
125: UIExceptions.annotateUser(iae, str, msg, e,
126: new java.util.Date());
127: throw iae;
128: }
129: }
130:
131: public java.lang.String[] getTags() {
132: ArrayList<String> list = new ArrayList<String>(6);
133: Lookup.Result<org.openide.awt.HtmlBrowser.Factory> r = Lookup
134: .getDefault().lookupResult(
135: org.openide.awt.HtmlBrowser.Factory.class);
136: for (Lookup.Item<org.openide.awt.HtmlBrowser.Factory> i : r
137: .allItems()) {
138: list.add(i.getDisplayName());
139: }
140:
141: // PENDING need to get rid of this filtering
142: FileObject fo = Repository.getDefault()
143: .getDefaultFileSystem()
144: .findResource(BROWSER_FOLDER);
145: if (fo != null) {
146: DataFolder folder = DataFolder.findFolder(fo);
147: DataObject[] dobjs = folder.getChildren();
148: for (int i = 0; i < dobjs.length; i++) {
149: // Must not be hidden and have to provide instances (we assume instance is HtmlBrowser.Factory)
150: if (Boolean.TRUE.equals(dobjs[i].getPrimaryFile()
151: .getAttribute(EA_HIDDEN))
152: || dobjs[i].getCookie(InstanceCookie.class) == null) {
153: FileObject fo2 = dobjs[i].getPrimaryFile();
154: String n = fo2.getName();
155: try {
156: n = fo2.getFileSystem().getStatus()
157: .annotateName(n, dobjs[i].files());
158: } catch (FileStateInvalidException e) {
159: // Never mind.
160: }
161: list.remove(n);
162: }
163: }
164: }
165: String[] retValue = new String[list.size()];
166:
167: list.toArray(retValue);
168: return retValue;
169: }
170:
171: }
172:
173: }
|