1 19 package org.netbeans.modules.xml.tax.beans.editor; 20 21 import java.awt.Component ; 22 import java.beans.PropertyEditorSupport ; 23 import java.beans.FeatureDescriptor ; 24 25 import org.openide.nodes.Node; 26 import org.openide.explorer.propertysheet.ExPropertyEditor; 27 import org.openide.explorer.propertysheet.PropertyEnv; 28 29 34 public class NullStringEditor extends PropertyEditorSupport implements ExPropertyEditor { 35 36 37 protected static final String DEFAULT_NULL = Util.THIS.getString ("TEXT_DEFAULT"); 38 39 40 private boolean editable; 41 42 43 47 48 public NullStringEditor () { 49 super(); 50 editable = true; 51 } 52 53 54 58 60 public void setAsText (String text) throws IllegalArgumentException { 61 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("NullStringEditor::setAsText: text = " + text); 63 if ( DEFAULT_NULL.equals (text) ) { 64 setValue (null); 65 } else if ( text.length() == 0 ) { 66 setValue (null); 67 } else { 68 setValue (text); 69 } 70 } 71 72 74 public String getAsText () { 75 Object value = super.getValue(); 76 77 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("NullStringEditor::getAsText: value = " + value); 79 if ( value == null ) { 80 return DEFAULT_NULL; 81 } else { 82 String text = value.toString(); 83 if ( text.length() == 0) { 84 return DEFAULT_NULL; 85 } 86 return text; 87 } 88 } 89 90 92 public boolean supportsCustomEditor () { 93 return true; 94 } 95 96 98 public Component getCustomEditor () { 99 return new NullStringCustomEditor (this); 100 } 101 102 104 public String getJavaInitializationString () { 105 String s = (String ) getValue (); 106 return "\"" + toAscii (s) + "\""; } 108 109 110 114 116 public void attachEnv (PropertyEnv env) { 117 FeatureDescriptor desc = env.getFeatureDescriptor(); 118 119 if (desc instanceof Node.Property){ 120 Node.Property prop = (Node.Property)desc; 121 122 editable = prop.canWrite(); 123 } 124 } 125 126 127 131 133 public boolean hasInPlaceCustomEditor () { 134 return false; 135 } 136 137 139 public Component getInPlaceCustomEditor () { 140 return null; 141 } 142 143 145 public boolean supportsEditingTaggedValues () { 146 return false; 147 } 148 149 150 154 156 public boolean isEditable () { 157 return editable; 158 } 159 160 162 private static String toAscii (String str) { 163 StringBuffer buf = new StringBuffer (str.length() * 6); char[] chars = str.toCharArray(); 165 for (int i = 0; i < chars.length; i++) { 166 char c = chars[i]; 167 switch (c) { 168 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: 177 if (c >= 0x0020 && c <= 0x007f) 178 buf.append (c); 179 else { 180 buf.append ("\\u"); String hex = Integer.toHexString (c); 182 for (int j = 0; j < 4 - hex.length(); j++) 183 buf.append ('0'); 184 buf.append (hex); 185 } 186 } 187 } 188 return buf.toString(); 189 } 190 191 } 192 | Popular Tags |