1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.util.StringTokenizer ; 23 import java.text.MessageFormat ; 24 import org.netbeans.core.UIExceptions; 25 import org.openide.explorer.propertysheet.ExPropertyEditor; 26 import org.openide.explorer.propertysheet.PropertyEnv; 27 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 28 29 35 abstract class ArrayOfIntSupport extends java.beans.PropertyEditorSupport 36 implements XMLPropertyEditor, ExPropertyEditor { 37 private static final String VALUE_FORMAT = org.openide.util.NbBundle.getBundle( 38 ArrayOfIntSupport.class).getString("EXC_BadFormatValue"); 39 40 41 private int count; 42 43 46 private String className; 47 48 49 PropertyEnv env; 50 51 52 56 public ArrayOfIntSupport(String className, int count) { 57 this.className = className; 58 this.count = count; 59 } 60 61 public void attachEnv(PropertyEnv env) { 62 this.env = env; 63 } 64 65 66 76 public String getJavaInitializationString() { 77 int[] val = getValues(); 78 StringBuffer buf = new StringBuffer ("new "); 80 buf.append(className); 81 buf.append("("); addArray(buf, val); 83 buf.append(")"); return buf.toString(); 85 } 86 87 88 abstract int[] getValues(); 89 90 93 abstract void setValues(int[] val); 94 95 97 103 public String getAsText() { 104 if (getValue() == null) 105 return "null"; 107 int[] val = getValues(); 108 109 if (val == null) 110 return null; 111 else { 112 StringBuffer buf = new StringBuffer ("["); addArray(buf, val); 114 buf.append("]"); return buf.toString(); 116 } 117 } 118 119 private void addArray(StringBuffer buf, int[] arr) { 121 for (int i = 0; i < count; i++) { 122 if (arr == null) 123 buf.append("0"); else 125 buf.append(arr[i]); 126 127 if (i < count - 1) 128 buf.append(", "); } 130 } 131 132 138 public void setAsText(String text) throws IllegalArgumentException { 139 if ("null".equals(text) || "".equals(text)) { setValue(null); 141 return; 142 } 143 int[] newVal = new int[count]; 144 int nextNumber = 0; 145 146 StringTokenizer tuk = new StringTokenizer (text, "[] ,;", false); while (tuk.hasMoreTokens()) { 148 String token = tuk.nextToken(); 149 if (nextNumber >= count) 150 badFormat(null); 151 152 try { 153 newVal[nextNumber] = new Integer (token).intValue(); 154 nextNumber++; 155 } 156 catch (NumberFormatException e) { 157 badFormat(e); 158 } 159 } 160 161 if (nextNumber != count) { 163 if (nextNumber > 0) { 164 int copyValue = newVal [nextNumber - 1]; 165 for (int i = nextNumber; i < count; i++) 166 newVal [i] = copyValue; 167 } 168 } 169 setValues(newVal); 170 } 171 172 173 private void badFormat(Exception e) throws IllegalArgumentException { 174 String msg = new MessageFormat (VALUE_FORMAT).format(new Object [] 175 { className , getHintFormat() } ); 176 IllegalArgumentException iae = new IllegalArgumentException (msg); 177 UIExceptions.annotateUser(iae, e == null ? "" 178 : e.getMessage(), msg, e, 179 new java.util.Date ()); throw iae; 181 } 182 183 184 String getHintFormat() { 185 StringBuffer buf = new StringBuffer ("["); for (int i = 0; i < count; i++) { 187 buf.append("<n"); buf.append(i); 189 buf.append(">"); 191 if (i < count - 1) 192 buf.append(", "); } 194 buf.append("]"); 196 return buf.toString(); 197 } 198 199 202 public static final String ATTR_VALUE = "value"; 204 205 protected abstract String getXMLValueTag (); 206 207 213 public void readFromXML (org.w3c.dom.Node element) throws java.io.IOException { 214 if (!getXMLValueTag ().equals (element.getNodeName ())) { 215 throw new java.io.IOException (); 216 } 217 org.w3c.dom.NamedNodeMap attributes = element.getAttributes (); 218 try { 219 String value = attributes.getNamedItem (ATTR_VALUE).getNodeValue (); 220 setAsText (value); 221 } catch (Exception e) { 222 throw new java.io.IOException (); 223 } 224 } 225 226 231 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 232 org.w3c.dom.Element el = doc.createElement (getXMLValueTag ()); 233 el.setAttribute (ATTR_VALUE, getAsText ()); 234 return el; 235 } 236 237 238 } 239 | Popular Tags |