1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.PropertyEditorSupport ; 23 import java.net.URL ; 24 import java.net.MalformedURLException ; 25 import java.text.MessageFormat ; 26 import org.netbeans.core.UIExceptions; 27 import org.openide.util.NbBundle; 28 29 33 public class URLEditor extends PropertyEditorSupport implements org.openide.explorer.propertysheet.editors.XMLPropertyEditor { 34 35 36 public void setAsText(String s) { 37 if ("null".equals(s)) { setValue(null); 39 return; 40 } 41 42 try { 43 URL url = new URL (s); 44 setValue(url); 45 } catch (MalformedURLException e) { 46 IllegalArgumentException iae = new IllegalArgumentException (e.getMessage()); 47 String msg = MessageFormat.format( 48 NbBundle.getMessage( 49 URLEditor.class, "FMT_EXC_BAD_URL"), new Object [] {s}); UIExceptions.annotateUser(iae, e.getMessage(), msg, e, 51 new java.util.Date ()); 52 throw iae; 53 } 54 } 55 56 57 public String getAsText() { 58 URL url = (URL )getValue(); 59 return url != null ? url.toString() : "null"; } 61 62 public String getJavaInitializationString () { 63 URL url = (URL ) getValue (); 64 return "new java.net.URL(\""+url.toString ()+"\")"; } 66 67 public boolean supportsCustomEditor () { 68 return false; 69 } 70 71 74 public static final String XML_URL = "Url"; 76 public static final String ATTR_VALUE = "value"; 78 84 public void readFromXML (org.w3c.dom.Node element) throws java.io.IOException { 85 if (!XML_URL.equals (element.getNodeName ())) { 86 throw new java.io.IOException (); 87 } 88 org.w3c.dom.NamedNodeMap attributes = element.getAttributes (); 89 try { 90 String value = attributes.getNamedItem (ATTR_VALUE).getNodeValue (); 91 setAsText (value); 92 } catch (Exception e) { 93 throw new java.io.IOException (); 94 } 95 } 96 97 102 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 103 org.w3c.dom.Element el = doc.createElement (XML_URL); 104 el.setAttribute (ATTR_VALUE, getAsText ()); 105 return el; 106 } 107 } 108 | Popular Tags |