1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.*; 23 import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor; 24 25 29 public class CharEditor extends PropertyEditorSupport implements EnhancedPropertyEditor { 30 31 35 public String getAsText () { 36 char value = ((Character )getValue()).charValue(); 37 final StringBuffer buf = new StringBuffer (6); 38 switch (value) { 39 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; default: 46 if (value >= 0x0020 && value <= 0x007f) 47 buf.append(value); 48 else { 49 buf.append("\\u"); String hex = Integer.toHexString(value); 51 for (int j = 0; j < 4 - hex.length(); j++) 52 buf.append('0'); 53 buf.append(hex); 54 } 55 } 56 return buf.toString() ; 57 } 58 62 public void setAsText(String text) throws IllegalArgumentException { 63 if (text.length() < 1) { 64 return; 66 } 67 char value = 0; 68 if (text.charAt(0) == '\\') { 69 char ch = text.length() >=2 ? text.charAt(1) : '\\'; 71 switch (ch) { 72 case 'b': value = '\b'; break; 73 case 't': value = '\t'; break; 74 case 'n': value = '\n'; break; 75 case 'f': value = '\f'; break; 76 case 'r': value = '\r'; break; 77 case '\\': value = '\\' ; break; 78 case 'u' : 79 String num = text.substring(2,text.length()); 80 if (num.length () > 4) { 81 return; 83 } 84 try { 85 int intValue = Integer.parseInt(num,16); 86 value = (char) intValue; 87 break; 88 } catch (NumberFormatException nfe) { 89 return; 91 } 92 default: 93 return; 95 96 } 97 } else { 98 value = text.charAt(0); 99 } 100 setValue(Character.valueOf(value)); 101 } 102 103 108 public void setValue(Object newValue) throws IllegalArgumentException { 109 if (newValue instanceof Character ) { 110 super.setValue(newValue); 111 return; 112 } 113 if (newValue instanceof String ) { 114 String text = (String ) newValue; 115 if (text.length() >= 1) { 116 super.setValue(Character.valueOf(text.charAt(0))); 117 return; 118 } 119 } 120 if (newValue == null ) { 121 super.setValue( Character.valueOf( '\u0000' ) ); return; 123 } 124 125 throw new IllegalArgumentException (); 126 } 127 128 139 public String getJavaInitializationString() { 140 if ( ((Character )getValue()).charValue() == '\'' ) 141 return "'\\''"; else 143 return "'" + getAsText() + "'"; } 145 146 151 public java.awt.Component getInPlaceCustomEditor () { 152 return null; 153 } 154 155 160 public boolean hasInPlaceCustomEditor () { 161 return false; 162 } 163 164 168 public boolean supportsEditingTaggedValues () { 169 return true; 170 } 171 } 172 | Popular Tags |