1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.InputStream ; 24 import java.io.IOException ; 25 import java.beans.PropertyEditorSupport ; 26 import java.text.MessageFormat ; 27 import java.util.Date ; 28 import java.util.Enumeration ; 29 import java.util.Properties ; 30 import org.netbeans.core.UIExceptions; 31 import org.openide.util.NbBundle; 32 33 34 37 public class PropertiesEditor extends PropertyEditorSupport { 38 39 40 public String getAsText() { 41 Object value = getValue(); 42 43 if(value instanceof Properties ) { 44 Properties prop = (Properties )value; 45 46 StringBuffer buff = new StringBuffer (); 47 48 for(Enumeration e = prop.keys(); e.hasMoreElements(); ) { 49 if(buff.length() > 0) { 50 buff.append("; "); } 52 53 Object key = e.nextElement(); 54 55 buff.append(key).append('=').append(prop.get(key)); } 57 58 return buff.toString(); 59 } 60 61 return String.valueOf(value); } 63 64 67 public void setAsText(String text) throws IllegalArgumentException { 68 try { 69 if(text == null) { 70 throw new IllegalArgumentException ("Inserted value can't be null."); } 72 Properties prop = new Properties (); 73 InputStream is = new ByteArrayInputStream ( 74 text.replace(';', '\n').getBytes("ISO8859_1") ); 76 prop.load(is); 77 setValue(prop); 78 } catch(IOException ioe) { 79 IllegalArgumentException iae = new IllegalArgumentException (ioe.getMessage()); 80 String msg = ioe.getLocalizedMessage(); 81 if (msg == null) { 82 msg = MessageFormat.format( 83 NbBundle.getMessage( 84 PropertiesEditor.class, "FMT_EXC_GENERIC_BAD_VALUE"), new Object [] {text}); } 86 UIExceptions.annotateUser(iae, iae.getMessage(), msg, ioe, new Date ()); 87 throw iae; 88 } 89 } 90 91 public String getJavaInitializationString () { 92 return null; } 94 95 public boolean supportsCustomEditor () { 96 return true; 97 } 98 99 public java.awt.Component getCustomEditor () { 100 return new PropertiesCustomEditor (this); 101 } 102 103 } 104 | Popular Tags |