KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > 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.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 /** A property editor for array of Strings.
32 * @author Ian Formanek
33 * @version 0.10, 17 Jun 1998
34 */

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

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

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

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

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

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

84         StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
85         for (int i = 0; i < strings.length; i++) {
86             // Handles in-string escapes if quoted
87
if (quoted) {
88                 buf.append("\""); // NOI18N
89
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; // NOI18N
94
case '\t': buf.append("\\t"); break; // NOI18N
95
case '\n': buf.append("\\n"); break; // NOI18N
96
case '\f': buf.append("\\f"); break; // NOI18N
97
case '\r': buf.append("\\r"); break; // NOI18N
98
case '\"': buf.append("\\\""); break; // NOI18N
99
case '\\': buf.append("\\\\"); break; // NOI18N
100
default:
101                         if (c >= 0x0020 && c <= 0x007f)
102                             buf.append(c);
103                         else {
104                             buf.append("\\u"); // NOI18N
105
String JavaDoc 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("\""); // NOI18N
113
} else {
114                 buf.append(strings[i]);
115             }
116             if (i != strings.length - 1)
117                 buf.append (", "); // NOI18N
118
}
119  
120         return buf.toString ();
121     }
122
123     public String JavaDoc getAsText () {
124         return getStrings(false);
125     }
126
127     public void setAsText (String JavaDoc text) {
128         if (text.equals("null")) { // NOI18N
129
setValue(null);
130             return;
131         }
132         StringTokenizer tok = new StringTokenizer(text, ","); // NOI18N
133
java.util.List JavaDoc list = new LinkedList();
134         while (tok.hasMoreTokens()) {
135             String JavaDoc s = tok.nextToken();
136             list.add(s.trim());
137         }
138         String JavaDoc [] a = (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
139         setValue(a);
140     }
141
142     public String JavaDoc getJavaInitializationString () {
143         if (strings == null) return "null"; // NOI18N
144
// [PENDING - wrap strings ???]
145
StringBuffer JavaDoc buf = new StringBuffer JavaDoc ("new String[] {"); // NOI18N
146
buf.append (getStrings(true));
147         buf.append ("}"); // NOI18N
148
return buf.toString ();
149     }
150
151     public String JavaDoc[] 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     // -------------------------------------------
179
// XMLPropertyEditor implementation
180

181     /** Called to store current property value into XML subtree.
182      * @param doc The XML document to store the XML in - should be used for
183      * creating nodes only
184      * @return the XML DOM element representing a subtree of XML from which
185                the value should be loaded
186      */

187     public org.w3c.dom.Node JavaDoc storeToXML(org.w3c.dom.Document JavaDoc doc) {
188         org.w3c.dom.Element JavaDoc 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 JavaDoc 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     /** Called to load property value from specified XML subtree.
203      * If succesfully loaded, the value should be available via getValue().
204      * An IOException should be thrown when the value cannot be restored from
205      * the specified XML element
206      * @param element the XML DOM element representing a subtree of XML from
207      * which the value should be loaded
208      * @exception IOException thrown when the value cannot be restored from
209                   the specified XML element
210      */

211     public void readFromXML(org.w3c.dom.Node JavaDoc element) throws java.io.IOException JavaDoc {
212         if (!XML_STRING_ARRAY.equals(element.getNodeName()))
213             throw new java.io.IOException JavaDoc();
214
215         org.w3c.dom.NamedNodeMap JavaDoc attributes = element.getAttributes();
216         String JavaDoc[] stringArray;
217         org.w3c.dom.Node JavaDoc 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 JavaDoc[count];
223             org.w3c.dom.NodeList JavaDoc items = element.getChildNodes();
224             org.w3c.dom.Element JavaDoc 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 JavaDoc)items.item(i);
229                     if (itemEl.getNodeName().equals(XML_STRING_ITEM)) {
230                         String JavaDoc indexStr = itemEl.getAttribute(ATTR_INDEX);
231                         String JavaDoc 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 JavaDoc[0];
244
245         setValue(stringArray);
246     }
247     
248     // NamedPropertyEditor implementation
249
public String JavaDoc getDisplayName() {
250         return NbBundle.getBundle(getClass()).getString("CTL_StringArrayEditor_DisplayName"); // NOI18N
251
}
252
253 }
254
Popular Tags