1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.*; 25 import javax.swing.JList ; 26 import javax.swing.JScrollPane ; 27 import org.openide.explorer.propertysheet.ExPropertyEditor; 28 import org.openide.explorer.propertysheet.PropertyEnv; 29 import org.openide.explorer.propertysheet.editors.XMLPropertyEditor; 30 import org.openide.nodes.Node; 31 32 35 public class StringArrayEditor implements XMLPropertyEditor, StringArrayCustomizable, ExPropertyEditor { 36 37 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"; 44 private String [] strings; 46 private PropertyChangeSupport support; 47 private boolean editable = true; 48 private String separator = ","; 49 50 public StringArrayEditor() { 51 support = new PropertyChangeSupport (this); 52 } 53 54 public Object getValue () { 55 return strings; 56 } 57 58 public void setValue (Object value) { 59 strings = (String []) value; 60 support.firePropertyChange("", null, null); } 62 63 66 69 public String [] getStringArray () { 70 return (String [])getValue (); 71 } 72 73 76 public void setStringArray (String [] value) { 77 setValue (value); 78 } 79 80 82 protected final String getStrings(boolean quoted) { 83 if (strings == null) return "null"; 85 StringBuilder buf = new StringBuilder (); 86 for (int i = 0; i < strings.length; i++) { 87 if (quoted) { 89 buf.append('"').append(strings[i]).append('"'); 90 } 91 else { 92 buf.append(strings[i]); 93 } 94 if (i != strings.length - 1) { 95 buf.append (separator); 96 buf.append (' '); } 98 } 99 100 return buf.toString (); 101 } 102 103 public String getAsText () { 104 return getStrings(false); 105 } 106 107 public void setAsText (String text) { 108 if ("null".equals(text)) { setValue(null); 110 return; 111 } 112 StringTokenizer tok = new StringTokenizer(text, separator); 113 java.util.List <String > list = new LinkedList<String >(); 114 while (tok.hasMoreTokens()) { 115 String s = tok.nextToken(); 116 list.add(s.trim()); 117 } 118 String [] a = list.toArray(new String [list.size()]); 119 setValue(a); 120 } 121 122 public String getJavaInitializationString () { 123 if (strings == null) return "null"; StringBuilder buf = new StringBuilder ("new String[] {"); buf.append (getStrings(true)); 127 buf.append ('}'); return buf.toString (); 129 } 130 131 public String [] getTags () { 132 return null; 133 } 134 135 public boolean isPaintable () { 136 return false; 137 } 138 139 public void paintValue (Graphics g, Rectangle rectangle) { 140 } 141 142 public boolean supportsCustomEditor () { 143 if (!editable && (strings==null || strings.length==0)) { 146 return false; 147 } else { 148 return true; 149 } 150 } 151 152 public Component getCustomEditor () { 153 if (editable) { 154 return new StringArrayCustomEditor(this); 155 } else { 156 return new JScrollPane (new JList (getStringArray())); 157 } 158 } 159 160 public void addPropertyChangeListener (PropertyChangeListener propertyChangeListener) { 161 support.addPropertyChangeListener (propertyChangeListener); 162 } 163 164 public void removePropertyChangeListener (PropertyChangeListener propertyChangeListener) { 165 support.removePropertyChangeListener (propertyChangeListener); 166 } 167 168 171 177 public org.w3c.dom.Node storeToXML(org.w3c.dom.Document doc) { 178 org.w3c.dom.Element arrayEl = doc.createElement(XML_STRING_ARRAY); 179 int count = strings != null ? strings.length : 0; 180 arrayEl.setAttribute(ATTR_COUNT, Integer.toString(count)); 181 182 for (int i=0; i < count; i++) { 183 org.w3c.dom.Element itemEl = doc.createElement(XML_STRING_ITEM); 184 itemEl.setAttribute(ATTR_INDEX, Integer.toString(i)); 185 itemEl.setAttribute(ATTR_VALUE, strings[i]); 186 arrayEl.appendChild(itemEl); 187 } 188 189 return arrayEl; 190 } 191 192 201 public void readFromXML(org.w3c.dom.Node element) throws java.io.IOException { 202 if (!XML_STRING_ARRAY.equals(element.getNodeName())) 203 throw new java.io.IOException (); 204 205 org.w3c.dom.NamedNodeMap attributes = element.getAttributes(); 206 String [] stringArray; 207 org.w3c.dom.Node countNode = null; 208 int count = 0; 209 210 if ((countNode = attributes.getNamedItem(ATTR_COUNT)) != null 211 && (count = Integer.parseInt(countNode.getNodeValue())) > 0) { 212 stringArray = new String [count]; 213 org.w3c.dom.NodeList items = element.getChildNodes(); 214 org.w3c.dom.Element itemEl; 215 216 for (int i = 0; i < items.getLength(); i++) { 217 if (items.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 218 itemEl = (org.w3c.dom.Element )items.item(i); 219 if (XML_STRING_ITEM.equals(itemEl.getNodeName())) { 220 String indexStr = itemEl.getAttribute(ATTR_INDEX); 221 String valueStr = itemEl.getAttribute(ATTR_VALUE); 222 if (indexStr != null && valueStr != null) { 223 int index = Integer.parseInt(indexStr); 224 if (index >=0 && index < count) 225 stringArray[index] = valueStr; 226 } 227 } 228 } 229 } 230 } 231 else stringArray = new String [0]; 232 233 setValue(stringArray); 234 } 235 236 public void attachEnv(PropertyEnv env) { 237 FeatureDescriptor d = env.getFeatureDescriptor(); 238 readEnv (env.getFeatureDescriptor ()); 239 } 240 241 final void readEnv (FeatureDescriptor d) { 242 if (d instanceof Node.Property) { 243 editable = ((Node.Property)d).canWrite(); 244 } else if (d instanceof PropertyDescriptor) { 245 editable = ((PropertyDescriptor)d).getWriteMethod() != null; 246 } else { 247 editable = true; 248 } 249 250 Object v = d.getValue ("item.separator"); if (v instanceof String ) { 252 separator = (String )v; 253 } 254 } 255 } 256 | Popular Tags |