KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > URLEditor


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.beaninfo.editors;
21
22 import java.beans.PropertyEditorSupport JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import org.netbeans.core.UIExceptions;
27 import org.openide.util.NbBundle;
28
29 /** A property editor for java.net.URL class.
30 *
31 * @author Ian Formanek
32 */

33 public class URLEditor extends PropertyEditorSupport JavaDoc implements org.openide.explorer.propertysheet.editors.XMLPropertyEditor {
34
35     /** sets new value */
36     public void setAsText(String JavaDoc s) {
37         if ("null".equals(s)) { // NOI18N
38
setValue(null);
39             return;
40         }
41
42         try {
43             URL JavaDoc url = new URL JavaDoc (s);
44             setValue(url);
45         } catch (MalformedURLException JavaDoc e) {
46             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc (e.getMessage());
47             String JavaDoc msg = MessageFormat.format(
48                 NbBundle.getMessage(
49                     URLEditor.class, "FMT_EXC_BAD_URL"), new Object JavaDoc[] {s}); //NOI18N
50
UIExceptions.annotateUser(iae, e.getMessage(), msg, e,
51                                       new java.util.Date JavaDoc());
52             throw iae;
53         }
54     }
55
56     /** @return the current value as String */
57     public String JavaDoc getAsText() {
58         URL JavaDoc url = (URL JavaDoc)getValue();
59         return url != null ? url.toString() : "null"; // NOI18N
60
}
61
62     public String JavaDoc getJavaInitializationString () {
63         URL JavaDoc url = (URL JavaDoc) getValue ();
64         return "new java.net.URL(\""+url.toString ()+"\")"; // NOI18N
65
}
66
67     public boolean supportsCustomEditor () {
68         return false;
69     }
70
71     //--------------------------------------------------------------------------
72
// XMLPropertyEditor implementation
73

74     public static final String JavaDoc XML_URL = "Url"; // NOI18N
75

76     public static final String JavaDoc ATTR_VALUE = "value"; // NOI18N
77

78     /** Called to load property value from specified XML subtree. If succesfully loaded,
79     * the value should be available via the getValue method.
80     * An IOException should be thrown when the value cannot be restored from the specified XML element
81     * @param element the XML DOM element representing a subtree of XML from which the value should be loaded
82     * @exception IOException thrown when the value cannot be restored from the specified XML element
83     */

84     public void readFromXML (org.w3c.dom.Node JavaDoc element) throws java.io.IOException JavaDoc {
85         if (!XML_URL.equals (element.getNodeName ())) {
86             throw new java.io.IOException JavaDoc ();
87         }
88         org.w3c.dom.NamedNodeMap JavaDoc attributes = element.getAttributes ();
89         try {
90             String JavaDoc value = attributes.getNamedItem (ATTR_VALUE).getNodeValue ();
91             setAsText (value);
92         } catch (Exception JavaDoc e) {
93             throw new java.io.IOException JavaDoc ();
94         }
95     }
96
97     /** Called to store current property value into XML subtree. The property value should be set using the
98     * setValue method prior to calling this method.
99     * @param doc The XML document to store the XML in - should be used for creating nodes only
100     * @return the XML DOM element representing a subtree of XML from which the value should be loaded
101     */

102     public org.w3c.dom.Node JavaDoc storeToXML(org.w3c.dom.Document JavaDoc doc) {
103         org.w3c.dom.Element JavaDoc el = doc.createElement (XML_URL);
104         el.setAttribute (ATTR_VALUE, getAsText ());
105         return el;
106     }
107 }
108
Popular Tags