1 19 20 package org.netbeans.modules.form.editors; 21 22 import java.beans.*; 23 import org.netbeans.modules.form.NamedPropertyEditor; 24 import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor; 25 import org.openide.util.NbBundle; 26 27 31 public class MnemonicEditor extends PropertyEditorSupport 32 implements EnhancedPropertyEditor, NamedPropertyEditor { 33 34 38 public String getAsText () { 39 Object ovalue = getValue(); 40 char value = (char)0; 41 42 if (java.lang.Character .class.isInstance(ovalue)) 43 value = ((Character )ovalue).charValue(); 44 else if (java.lang.Integer .class.isInstance(ovalue)) 45 value = (char)(((Integer )ovalue).intValue()); 46 47 if (value == 0) return ""; 48 49 final StringBuffer buf = new StringBuffer (6); 50 switch (value) { 51 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: 58 if (value >= 0x0020 && value <= 0x007f) 59 buf.append(value); 60 else { 61 buf.append("\\u"); String hex = Integer.toHexString(value); 63 for (int j = 0; j < 4 - hex.length(); j++) 64 buf.append('0'); 65 buf.append(hex); 66 } 67 } 68 return buf.toString() ; 69 } 70 74 public void setAsText(String text) throws IllegalArgumentException { 75 if (text.length() < 1) { 76 setValue(new Integer (0)); 77 return; 78 } 79 80 if (text.length() == 1 && text.charAt(0) != '\\') { 81 setValue(new Character (text.charAt(0))); 82 return; 83 } 84 85 if (text.charAt(0) == '\\') { 86 char value = 0; 88 char ch = text.length() >=2 ? text.charAt(1) : '\\'; 89 switch (ch) { 90 case 'b': value = '\b'; break; 91 case 't': value = '\t'; break; 92 case 'n': value = '\n'; break; 93 case 'f': value = '\f'; break; 94 case 'r': value = '\r'; break; 95 case '\\': value = '\\' ; break; 96 case 'u' : 97 String num = text.substring(2,text.length()); 98 if (num.length () > 4) { 99 return; 101 } 102 try { 103 int intValue = Integer.parseInt(num,16); 104 value = (char) intValue; 105 break; 106 } catch (NumberFormatException nfe) { 107 return; 109 } 110 default: 111 return; 113 114 } 115 setValue(new Character (value)); 116 return; 117 } 118 119 try { 120 setValue(new Integer (text)); 121 return; 122 } catch (NumberFormatException e) { 123 setValue(text); 124 return; 125 } 126 127 } 128 129 134 public void setValue(Object newValue) throws IllegalArgumentException { 135 if (newValue instanceof Integer ) { 136 super.setValue(newValue); 137 return; 138 } 139 if (newValue instanceof Character ) { 140 super.setValue(new Integer ((int)(((Character )newValue).charValue()))); 141 return; 142 } 143 if (newValue instanceof String ) { 144 String text = (String ) newValue; 145 if (text.length() >= 1) { 146 super.setValue(new Integer ((int)text.charAt(0))); 147 return; 148 } 149 } 150 throw new IllegalArgumentException (); 151 } 152 153 164 public String getJavaInitializationString() { 165 return "'" + getAsText() + "'"; } 167 168 173 public java.awt.Component getInPlaceCustomEditor () { 174 return null; 175 } 176 177 182 public boolean hasInPlaceCustomEditor () { 183 return false; 184 } 185 186 190 public boolean supportsEditingTaggedValues () { 191 return true; 192 } 193 194 public String getDisplayName() { 196 return NbBundle.getBundle(getClass()).getString("CTL_MnemonicsEditor_DisplayName"); } 198 199 } 200 | Popular Tags |