KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.StringTokenizer JavaDoc;
23 import java.text.MessageFormat JavaDoc;
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 /** Support for property editors for several integers.
30 * for example: Point - [2,4], Insets [2,3,4,4],...
31 *
32 * @author Petr Hamernik
33 * @version 0.14, Jul 20, 1998
34 */

35 abstract class ArrayOfIntSupport extends java.beans.PropertyEditorSupport JavaDoc
36 implements XMLPropertyEditor, ExPropertyEditor {
37     private static final String JavaDoc VALUE_FORMAT = org.openide.util.NbBundle.getBundle(
38                 ArrayOfIntSupport.class).getString("EXC_BadFormatValue");
39
40     /** Length of the array of the integers */
41     private int count;
42
43     /** Class Name of the edited property. It is used in getJavaInitializationString
44     * method.
45     */

46     private String JavaDoc className;
47
48     /** associated env, accessible by package private subclasses */
49     PropertyEnv env;
50
51
52     /** constructs new property editor.
53     * @param className Name of the class which is this editor for. (e.g. "java.awt.Point")
54     * @param count Length of the array of int
55     */

56     public ArrayOfIntSupport(String JavaDoc 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     /** This method is intended for use when generating Java code to set
67     * the value of the property. It should return a fragment of Java code
68     * that can be used to initialize a variable with the current property
69     * value.
70     * <p>
71     * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
72     *
73     * @return A fragment of Java code representing an initializer for the
74     * current value.
75     */

76     public String JavaDoc getJavaInitializationString() {
77         int[] val = getValues();
78         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("new "); // NOI18N
79

80         buf.append(className);
81         buf.append("("); // NOI18N
82
addArray(buf, val);
83         buf.append(")"); // NOI18N
84
return buf.toString();
85     }
86
87     /** Abstract method for translating the value from getValue() method to array of int. */
88     abstract int[] getValues();
89
90     /** Abstract method for translating the array of int to value
91     * which is set to method setValue(XXX)
92     */

93     abstract void setValues(int[] val);
94
95     //----------------------------------------------------------------------
96

97     /**
98     * @return The property value as a human editable string.
99     * <p> Returns null if the value can't be expressed as an editable string.
100     * <p> If a non-null value is returned, then the PropertyEditor should
101     * be prepared to parse that string back in setAsText().
102     */

103     public String JavaDoc getAsText() {
104         if (getValue() == null)
105             return "null"; // NOI18N
106

107         int[] val = getValues();
108
109         if (val == null)
110             return null;
111         else {
112             StringBuffer JavaDoc buf = new StringBuffer JavaDoc("["); // NOI18N
113
addArray(buf, val);
114             buf.append("]"); // NOI18N
115
return buf.toString();
116         }
117     }
118
119     /** Add array of integers to the StringBuffer. Numbers are separated by ", " string */ // NOI18N
120
private void addArray(StringBuffer JavaDoc buf, int[] arr) {
121         for (int i = 0; i < count; i++) {
122             if (arr == null)
123                 buf.append("0"); // NOI18N
124
else
125                 buf.append(arr[i]);
126
127             if (i < count - 1)
128                 buf.append(", "); // NOI18N
129
}
130     }
131
132     /** Set the property value by parsing a given String. May raise
133     * java.lang.IllegalArgumentException if either the String is
134     * badly formatted or if this kind of property can't be expressed
135     * as text.
136     * @param text The string to be parsed.
137     */

138     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
139         if ("null".equals(text) || "".equals(text)) { // NOI18N
140
setValue(null);
141             return;
142         }
143         int[] newVal = new int[count];
144         int nextNumber = 0;
145
146         StringTokenizer JavaDoc tuk = new StringTokenizer JavaDoc(text, "[] ,;", false); // NOI18N
147
while (tuk.hasMoreTokens()) {
148             String JavaDoc token = tuk.nextToken();
149             if (nextNumber >= count)
150                 badFormat(null);
151
152             try {
153                 newVal[nextNumber] = new Integer JavaDoc(token).intValue();
154                 nextNumber++;
155             }
156             catch (NumberFormatException JavaDoc e) {
157                 badFormat(e);
158             }
159         }
160
161         // if less numbers are entered, copy the last entered number into the rest
162
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     /** Always throws the new exception */
173     private void badFormat(Exception JavaDoc e) throws IllegalArgumentException JavaDoc {
174         String JavaDoc msg = new MessageFormat JavaDoc(VALUE_FORMAT).format(new Object JavaDoc[]
175             { className , getHintFormat() } );
176         IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(msg);
177         UIExceptions.annotateUser(iae, e == null ? ""
178                                                 : e.getMessage(), msg, e,
179                                  new java.util.Date JavaDoc()); //NOI18N
180
throw iae;
181     }
182
183     /** @return the format info for the user. Can be rewritten in subclasses. */
184     String JavaDoc getHintFormat() {
185         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("["); // NOI18N
186
for (int i = 0; i < count; i++) {
187             buf.append("<n"); // NOI18N
188
buf.append(i);
189             buf.append(">"); // NOI18N
190

191             if (i < count - 1)
192                 buf.append(", "); // NOI18N
193
}
194         buf.append("]"); // NOI18N
195

196         return buf.toString();
197     }
198
199     //--------------------------------------------------------------------------
200
// XMLPropertyEditor implementation
201

202     public static final String JavaDoc ATTR_VALUE = "value"; // NOI18N
203

204     /** Provides name of XML tag to use for XML persistence of the property value */
205     protected abstract String JavaDoc getXMLValueTag ();
206
207     /** Called to load property value from specified XML subtree. If succesfully loaded,
208     * the value should be available via the getValue method.
209     * An IOException should be thrown when the value cannot be restored from the specified XML element
210     * @param element the XML DOM element representing a subtree of XML from which the value should be loaded
211     * @exception IOException thrown when the value cannot be restored from the specified XML element
212     */

213     public void readFromXML (org.w3c.dom.Node JavaDoc element) throws java.io.IOException JavaDoc {
214         if (!getXMLValueTag ().equals (element.getNodeName ())) {
215             throw new java.io.IOException JavaDoc ();
216         }
217         org.w3c.dom.NamedNodeMap JavaDoc attributes = element.getAttributes ();
218         try {
219             String JavaDoc value = attributes.getNamedItem (ATTR_VALUE).getNodeValue ();
220             setAsText (value);
221         } catch (Exception JavaDoc e) {
222             throw new java.io.IOException JavaDoc ();
223         }
224     }
225
226     /** Called to store current property value into XML subtree. The property value should be set using the
227     * setValue method prior to calling this method.
228     * @param doc The XML document to store the XML in - should be used for creating nodes only
229     * @return the XML DOM element representing a subtree of XML from which the value should be loaded
230     */

231     public org.w3c.dom.Node JavaDoc storeToXML(org.w3c.dom.Document JavaDoc doc) {
232         org.w3c.dom.Element JavaDoc el = doc.createElement (getXMLValueTag ());
233         el.setAttribute (ATTR_VALUE, getAsText ());
234         return el;
235     }
236
237
238 }
239
Popular Tags