1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.PropertyEditorSupport ; 23 24 import org.openide.explorer.propertysheet.ExPropertyEditor; 26 import org.openide.explorer.propertysheet.PropertyEnv; 27 import java.beans.FeatureDescriptor ; 28 import org.openide.nodes.Node; 29 30 31 35 public class StringEditor extends PropertyEditorSupport implements ExPropertyEditor 36 { 37 private static boolean useRaw = Boolean.getBoolean("netbeans.stringEditor.useRawCharacters"); 38 39 private boolean editable=true; 41 42 public boolean isEditable(){ 43 return (editable); 44 } 45 46 47 public void setAsText(String s) { 48 if ( "null".equals( s ) && getValue() == null ) return; 50 setValue(s); 51 } 52 53 public String getJavaInitializationString () { 54 String s = (String ) getValue (); 55 return "\"" + toAscii(s) + "\""; } 57 58 public boolean supportsCustomEditor () { 59 return customEd; 60 } 61 62 public java.awt.Component getCustomEditor () { 63 Object val = getValue(); 64 String s = ""; if (val != null) { 66 s = val instanceof String ? (String ) val : val.toString(); 67 } 68 return new StringCustomEditor (s, isEditable(), oneline, instructions, this, env); } 70 71 private static String toAscii(String str) { 72 StringBuffer buf = new StringBuffer (str.length() * 6); char[] chars = str.toCharArray(); 74 for (int i = 0; i < chars.length; i++) { 75 char c = chars[i]; 76 switch (c) { 77 case '\b': buf.append("\\b"); break; case '\t': buf.append("\\t"); break; case '\n': buf.append("\\n"); break; case '\f': buf.append("\\f"); break; case '\r': buf.append("\\r"); break; case '\"': buf.append("\\\""); break; case '\\': buf.append("\\\\"); break; default: 86 if (c >= 0x0020 && (useRaw || c <= 0x007f)) 87 buf.append(c); 88 else { 89 buf.append("\\u"); String hex = Integer.toHexString(c); 91 for (int j = 0; j < 4 - hex.length(); j++) 92 buf.append('0'); 93 buf.append(hex); 94 } 95 } 96 } 97 return buf.toString(); 98 } 99 100 private String instructions=null; 101 private boolean oneline=false; 102 private boolean customEd=true; 103 private PropertyEnv env; 104 105 public void attachEnv(PropertyEnv env) { 107 this.env = env; 108 109 FeatureDescriptor desc = env.getFeatureDescriptor(); 110 if (desc instanceof Node.Property){ 111 Node.Property prop = (Node.Property)desc; 112 editable = prop.canWrite(); 113 instructions = (String ) prop.getValue ("instructions"); oneline = Boolean.TRUE.equals (prop.getValue ("oneline")); customEd = !Boolean.TRUE.equals (prop.getValue 118 ("suppressCustomEditor")); } 120 } 121 } 122 | Popular Tags |