1 19 24 25 package org.netbeans.beaninfo.editors; 26 import java.util.Arrays ; 27 import org.netbeans.beaninfo.editors.ExPropertyEditorSupport.EnvException; 28 import org.netbeans.core.UIExceptions; 29 import org.openide.explorer.propertysheet.*; 30 import org.openide.util.NbBundle; 31 46 public class IntEditor extends ExPropertyEditorSupport { 47 48 public static final String KEYS = "stringKeys"; public static final String VALS = "intValues"; public static final String CODE_VALS = "codeValues"; String [] keys=null; 52 String [] code=null; 53 int[] values=null; 54 55 public IntEditor() { 56 } 57 58 protected void attachEnvImpl(PropertyEnv env) { 59 keys = (String []) env.getFeatureDescriptor().getValue(KEYS); 60 values = (int[]) env.getFeatureDescriptor().getValue(VALS); 61 code = (String []) env.getFeatureDescriptor().getValue(CODE_VALS); 62 } 63 64 67 protected void validateEnv(PropertyEnv env) { 68 boolean valid = keys == null && values == null && code == null; 70 if (!valid) { 71 valid = keys != null && values != null; 72 if (!valid) { 73 throw new EnvException( 74 "You must specify both an array of keys and an " + "array of values if you specify one. Keys=" + arrToStr(keys) + " Values=" + arrToStr(values)); } else { 78 valid = keys.length == values.length; 79 if (valid) { 80 valid = keys.length > 0 && values.length > 0; 81 } 82 83 if (!valid) { 84 throw new EnvException( 85 "The arrays of keys and values must have the same " + "length and the length must be > 0. keys.length =" + keys.length + " values.length=" + values.length + " Keys=" + arrToStr(keys) + " Values=" + arrToStr(values)); } else { 90 if (code != null) { 91 valid = code.length == keys.length; 92 if (valid) { 93 valid = code.length > 0; 94 } 95 if (!valid) { 96 throw new EnvException( 97 "The arrays of keys and values and codes must all" + " have the same length, > 0. keys.length =" + keys.length + " values.length=" + values.length + " Code.length=" + code.length + " Keys=" + arrToStr(keys) + " Values=" + arrToStr(values) + 102 " Code=" + arrToStr(code)); } 104 } 105 } 106 } 107 } 108 } 109 110 private static final String arrToStr(int[] s) { 111 if (s == null) return "null"; StringBuffer out = new StringBuffer (s.length * 3); 113 for (int i=0; i < s.length; i++) { 114 out.append(s[i]); 115 if (i != s.length-1) { 116 out.append(','); } 118 } 119 return out.toString(); 120 } 121 122 public String getAsText() { 123 Integer i = (Integer ) getValue(); 124 String result; 125 if (i != null) { 126 if (keys != null) { 127 int intVal = i.intValue(); 128 int idx = -1; 129 for (int j=0; j < values.length; j++) { 130 if (values[j] == intVal) { 131 idx = j; 132 break; 133 } 134 } 135 if (idx != -1) { 136 result = keys [((Integer ) super.getValue()).intValue()]; 137 } else { 138 throw new IllegalArgumentException ( 139 "This property editor uses a set of keyed values, " + "and the current value, " + i + ", is not specified."); } 143 } else { 144 result = getValue().toString(); 145 } 146 } else { 147 result = NbBundle.getMessage (IntEditor.class, "NULL"); } 149 return result; 150 } 151 152 private void doSetAsText(String s) { 153 try { 157 setValue(Integer.valueOf(s)); 158 } catch (NumberFormatException nfe) { 159 String msg = NbBundle.getMessage( 160 IntEditor.class, "EXC_ILLEGAL_VALUE_TEXT") + s; RuntimeException iae = new IllegalArgumentException (msg); UIExceptions.annotateUser(iae, msg, msg, nfe, new java.util.Date ()); 163 throw iae; 164 } 165 } 166 167 public void setAsText(String s) { 168 s = s.trim(); 169 if (keys == null) { 170 doSetAsText(s); 171 } else { 172 int idx = Arrays.asList(keys).indexOf(s); 174 if ((idx == -1) || (idx > values.length-1)) { 175 StringBuffer msg = new StringBuffer (); 176 msg.append(NbBundle.getMessage(IntEditor.class, 177 "EXC_ILLEGAL_STRING_TEXT_FIRST")); msg.append(s); 179 msg.append(NbBundle.getMessage(IntEditor.class, 180 "EXC_ILLEGAL_STRING_TEXT_SECOND")); msg.append(arrToStr(keys)); 182 String message = msg.toString(); 183 RuntimeException iae = new IllegalArgumentException (message); 184 UIExceptions.annotateUser(iae, message, message, iae, 185 new java.util.Date ()); 186 throw iae; 187 } else { 188 setValue(Integer.valueOf(idx)); 189 } 190 } 191 } 192 193 public Object getValue () { 194 Integer v = (Integer ) super.getValue(); 195 if (values != null) { 196 v = Integer.valueOf (values[v.intValue()]); 197 } 198 return v; 199 } 200 201 public void setValue (Object value) { 203 if ((value instanceof Integer ) || (value == null)) { 204 super.setValue (value); 205 } else { 206 throw new IllegalArgumentException ( 207 "Argument to IntEditor.setValue() must be Integer, but was " + value.getClass().getName() + "(=" + value.toString() + ")"); } 211 } 212 213 public String [] getTags() { 214 return keys; 215 } 216 217 public String getJavaInitializationString() { 218 String result; 219 if (code == null) { 220 result = getValue().toString(); 221 } else { 222 result = code[((Integer ) getValue()).intValue()]; 223 } 224 return result; 225 } 226 } 227 | Popular Tags |