1 19 20 package org.netbeans.modules.form.editors; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.*; 25 26 import org.netbeans.modules.form.NamedPropertyEditor; 27 28 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 29 import org.openide.util.NbBundle; 30 31 35 public class StringArrayEditor implements XMLPropertyEditor, 36 StringArrayCustomizable, NamedPropertyEditor { 37 38 private static final String XML_STRING_ARRAY = "StringArray"; private static final String XML_STRING_ITEM = "StringItem"; private static final String ATTR_COUNT = "count"; private static final String ATTR_INDEX = "index"; private static final String ATTR_VALUE = "value"; 45 private String [] strings; 47 private PropertyChangeSupport support; 48 49 public StringArrayEditor() { 50 support = new PropertyChangeSupport (this); 51 } 52 53 public Object getValue () { 54 return strings; 55 } 56 57 public void setValue (Object value) { 58 strings = (String []) value; 59 support.firePropertyChange ("", null, null); } 61 62 65 68 public String [] getStringArray () { 69 return (String [])getValue (); 70 } 71 72 75 public void setStringArray (String [] value) { 76 setValue (value); 77 } 78 79 81 protected final String getStrings(boolean quoted) { 82 if (strings == null) return "null"; 84 StringBuffer buf = new StringBuffer (); 85 for (int i = 0; i < strings.length; i++) { 86 if (quoted) { 88 buf.append("\""); char[] chars = strings[i].toCharArray(); 90 for (int j = 0; j < chars.length; j++) { 91 char c = chars[j]; 92 switch (c) { 93 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: 101 if (c >= 0x0020 && c <= 0x007f) 102 buf.append(c); 103 else { 104 buf.append("\\u"); String hex = Integer.toHexString(c); 106 for (int k = 0; k < 4 - hex.length(); k++) 107 buf.append('0'); 108 buf.append(hex); 109 } 110 } 111 } 112 buf.append("\""); } else { 114 buf.append(strings[i]); 115 } 116 if (i != strings.length - 1) 117 buf.append (", "); } 119 120 return buf.toString (); 121 } 122 123 public String getAsText () { 124 return getStrings(false); 125 } 126 127 public void setAsText (String text) { 128 if (text.equals("null")) { setValue(null); 130 return; 131 } 132 StringTokenizer tok = new StringTokenizer(text, ","); java.util.List list = new LinkedList(); 134 while (tok.hasMoreTokens()) { 135 String s = tok.nextToken(); 136 list.add(s.trim()); 137 } 138 String [] a = (String [])list.toArray(new String [list.size()]); 139 setValue(a); 140 } 141 142 public String getJavaInitializationString () { 143 if (strings == null) return "null"; StringBuffer buf = new StringBuffer ("new String[] {"); buf.append (getStrings(true)); 147 buf.append ("}"); return buf.toString (); 149 } 150 151 public String [] getTags () { 152 return null; 153 } 154 155 public boolean isPaintable () { 156 return false; 157 } 158 159 public void paintValue (Graphics g, Rectangle rectangle) { 160 } 161 162 public boolean supportsCustomEditor () { 163 return true; 164 } 165 166 public Component getCustomEditor () { 167 return new StringArrayCustomEditor (this); 168 } 169 170 public void addPropertyChangeListener (PropertyChangeListener propertyChangeListener) { 171 support.addPropertyChangeListener (propertyChangeListener); 172 } 173 174 public void removePropertyChangeListener (PropertyChangeListener propertyChangeListener) { 175 support.removePropertyChangeListener (propertyChangeListener); 176 } 177 178 181 187 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 188 org.w3c.dom.Element arrayEl = doc.createElement(XML_STRING_ARRAY); 189 int count = strings != null ? strings.length : 0; 190 arrayEl.setAttribute(ATTR_COUNT, Integer.toString(count)); 191 192 for (int i=0; i < count; i++) { 193 org.w3c.dom.Element itemEl = doc.createElement(XML_STRING_ITEM); 194 itemEl.setAttribute(ATTR_INDEX, Integer.toString(i)); 195 itemEl.setAttribute(ATTR_VALUE, strings[i]); 196 arrayEl.appendChild(itemEl); 197 } 198 199 return arrayEl; 200 } 201 202 211 public void readFromXML(org.w3c.dom.Node element) throws java.io.IOException { 212 if (!XML_STRING_ARRAY.equals(element.getNodeName())) 213 throw new java.io.IOException (); 214 215 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 216 String [] stringArray; 217 org.w3c.dom.Node countNode = null; 218 int count = 0; 219 220 if ((countNode = attributes.getNamedItem(ATTR_COUNT)) != null 221 && (count = Integer.parseInt(countNode.getNodeValue())) > 0) { 222 stringArray = new String [count]; 223 org.w3c.dom.NodeList items = element.getChildNodes(); 224 org.w3c.dom.Element itemEl; 225 226 for (int i = 0; i < items.getLength(); i++) { 227 if (items.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 228 itemEl = (org.w3c.dom.Element )items.item(i); 229 if (itemEl.getNodeName().equals(XML_STRING_ITEM)) { 230 String indexStr = itemEl.getAttribute(ATTR_INDEX); 231 String valueStr = itemEl.getAttribute(ATTR_VALUE); 232 if (indexStr != null && valueStr != null) { 233 int index = Integer.parseInt(indexStr); 234 if (index >=0 && index < count){ 235 stringArray[index] = valueStr; 236 } 237 238 } 239 } 240 } 241 } 242 } 243 else stringArray = new String [0]; 244 245 setValue(stringArray); 246 } 247 248 public String getDisplayName() { 250 return NbBundle.getBundle(getClass()).getString("CTL_StringArrayEditor_DisplayName"); } 252 253 } 254 | Popular Tags |