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.net.URL;
046: import java.net.MalformedURLException;
047: import java.text.MessageFormat;
048: import org.netbeans.core.UIExceptions;
049: import org.openide.util.NbBundle;
050:
051: /** A property editor for java.net.URL class.
052: *
053: * @author Ian Formanek
054: */
055: public class URLEditor extends PropertyEditorSupport implements
056: org.openide.explorer.propertysheet.editors.XMLPropertyEditor {
057:
058: /** sets new value */
059: public void setAsText(String s) {
060: if ("null".equals(s)) { // NOI18N
061: setValue(null);
062: return;
063: }
064:
065: try {
066: URL url = new URL(s);
067: setValue(url);
068: } catch (MalformedURLException e) {
069: IllegalArgumentException iae = new IllegalArgumentException(
070: e.getMessage());
071: String msg = MessageFormat.format(NbBundle.getMessage(
072: URLEditor.class, "FMT_EXC_BAD_URL"),
073: new Object[] { s }); //NOI18N
074: UIExceptions.annotateUser(iae, e.getMessage(), msg, e,
075: new java.util.Date());
076: throw iae;
077: }
078: }
079:
080: /** @return the current value as String */
081: public String getAsText() {
082: URL url = (URL) getValue();
083: return url != null ? url.toString() : "null"; // NOI18N
084: }
085:
086: public String getJavaInitializationString() {
087: URL url = (URL) getValue();
088: return "new java.net.URL(\"" + url.toString() + "\")"; // NOI18N
089: }
090:
091: public boolean supportsCustomEditor() {
092: return false;
093: }
094:
095: //--------------------------------------------------------------------------
096: // XMLPropertyEditor implementation
097:
098: public static final String XML_URL = "Url"; // NOI18N
099:
100: public static final String ATTR_VALUE = "value"; // NOI18N
101:
102: /** Called to load property value from specified XML subtree. If succesfully loaded,
103: * the value should be available via the getValue method.
104: * An IOException should be thrown when the value cannot be restored from the specified XML element
105: * @param element the XML DOM element representing a subtree of XML from which the value should be loaded
106: * @exception IOException thrown when the value cannot be restored from the specified XML element
107: */
108: public void readFromXML(org.w3c.dom.Node element)
109: throws java.io.IOException {
110: if (!XML_URL.equals(element.getNodeName())) {
111: throw new java.io.IOException();
112: }
113: org.w3c.dom.NamedNodeMap attributes = element.getAttributes();
114: try {
115: String value = attributes.getNamedItem(ATTR_VALUE)
116: .getNodeValue();
117: setAsText(value);
118: } catch (Exception e) {
119: throw new java.io.IOException();
120: }
121: }
122:
123: /** Called to store current property value into XML subtree. The property value should be set using the
124: * setValue method prior to calling this method.
125: * @param doc The XML document to store the XML in - should be used for creating nodes only
126: * @return the XML DOM element representing a subtree of XML from which the value should be loaded
127: */
128: public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) {
129: org.w3c.dom.Element el = doc.createElement(XML_URL);
130: el.setAttribute(ATTR_VALUE, getAsText());
131: return el;
132: }
133: }
|