KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > StringArrayEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
26 import javax.swing.JScrollPane JavaDoc;
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 /** A property editor for array of Strings.
33  * @author Ian Formanek
34  */

35 public class StringArrayEditor implements XMLPropertyEditor, StringArrayCustomizable, ExPropertyEditor {
36
37     // constants for XML persistence
38
private static final String JavaDoc XML_STRING_ARRAY = "StringArray"; // NOI18N
39
private static final String JavaDoc XML_STRING_ITEM = "StringItem"; // NOI18N
40
private static final String JavaDoc ATTR_COUNT = "count"; // NOI18N
41
private static final String JavaDoc ATTR_INDEX = "index"; // NOI18N
42
private static final String JavaDoc ATTR_VALUE = "value"; // NOI18N
43

44     // private fields
45
private String JavaDoc[] strings;
46     private PropertyChangeSupport support;
47     private boolean editable = true;
48     private String JavaDoc separator = ",";
49
50     public StringArrayEditor() {
51         support = new PropertyChangeSupport (this);
52     }
53
54     public Object JavaDoc getValue () {
55         return strings;
56     }
57
58     public void setValue (Object JavaDoc value) {
59         strings = (String JavaDoc[]) value;
60         support.firePropertyChange("", null, null); // NOI18N
61
}
62
63     // -----------------------------------------------------------------------------
64
// StringArrayCustomizable implementation
65

66     /** Used to acquire the current value from the PropertyEditor
67     * @return the current value of the property
68     */

69     public String JavaDoc[] getStringArray () {
70         return (String JavaDoc[])getValue ();
71     }
72
73     /** Used to modify the current value in the PropertyEditor
74     * @param value the new value of the property
75     */

76     public void setStringArray (String JavaDoc[] value) {
77         setValue (value);
78     }
79
80     // end of StringArrayCustomizable implementation
81

82     protected final String JavaDoc getStrings(boolean quoted) {
83         if (strings == null) return "null"; // NOI18N
84

85         StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
86         for (int i = 0; i < strings.length; i++) {
87             // XXX handles in-string escapes if quoted
88
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 (' '); // NOI18N
97
}
98         }
99
100         return buf.toString ();
101     }
102
103     public String JavaDoc getAsText () {
104         return getStrings(false);
105     }
106
107     public void setAsText (String JavaDoc text) {
108         if ("null".equals(text)) { // NOI18N
109
setValue(null);
110             return;
111         }
112         StringTokenizer tok = new StringTokenizer(text, separator);
113         java.util.List JavaDoc<String JavaDoc> list = new LinkedList<String JavaDoc>();
114         while (tok.hasMoreTokens()) {
115             String JavaDoc s = tok.nextToken();
116             list.add(s.trim());
117         }
118         String JavaDoc [] a = list.toArray(new String JavaDoc[list.size()]);
119         setValue(a);
120     }
121
122     public String JavaDoc getJavaInitializationString () {
123         if (strings == null) return "null"; // NOI18N
124
// [PENDING - wrap strings ???]
125
StringBuilder JavaDoc buf = new StringBuilder JavaDoc ("new String[] {"); // NOI18N
126
buf.append (getStrings(true));
127         buf.append ('}'); // NOI18N
128
return buf.toString ();
129     }
130
131     public String JavaDoc[] 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         //Don't show custom editor if it's just going to show
144
//an empty component
145
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 JavaDoc(new JList JavaDoc(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     // -------------------------------------------
169
// XMLPropertyEditor implementation
170

171     /** Called to store current property value into XML subtree.
172      * @param doc The XML document to store the XML in - should be used for
173      * creating nodes only
174      * @return the XML DOM element representing a subtree of XML from which
175                the value should be loaded
176      */

177     public org.w3c.dom.Node JavaDoc storeToXML(org.w3c.dom.Document JavaDoc doc) {
178         org.w3c.dom.Element JavaDoc 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 JavaDoc 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     /** Called to load property value from specified XML subtree.
193      * If succesfully loaded, the value should be available via getValue().
194      * An IOException should be thrown when the value cannot be restored from
195      * the specified XML element
196      * @param element the XML DOM element representing a subtree of XML from
197      * which the value should be loaded
198      * @exception IOException thrown when the value cannot be restored from
199                   the specified XML element
200      */

201     public void readFromXML(org.w3c.dom.Node JavaDoc element) throws java.io.IOException JavaDoc {
202         if (!XML_STRING_ARRAY.equals(element.getNodeName()))
203             throw new java.io.IOException JavaDoc();
204
205         org.w3c.dom.NamedNodeMap JavaDoc attributes = element.getAttributes();
206         String JavaDoc[] stringArray;
207         org.w3c.dom.Node JavaDoc 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 JavaDoc[count];
213             org.w3c.dom.NodeList JavaDoc items = element.getChildNodes();
214             org.w3c.dom.Element JavaDoc 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 JavaDoc)items.item(i);
219                     if (XML_STRING_ITEM.equals(itemEl.getNodeName())) {
220                         String JavaDoc indexStr = itemEl.getAttribute(ATTR_INDEX);
221                         String JavaDoc 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 JavaDoc[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 JavaDoc v = d.getValue ("item.separator"); // NOI18N
251
if (v instanceof String JavaDoc) {
252             separator = (String JavaDoc)v;
253         }
254     }
255 }
256
Popular Tags