KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IntEditor.java
21  *
22  * Created on February 28, 2003, 2:15 PM
23  */

24
25 package org.netbeans.beaninfo.editors;
26 import java.util.Arrays JavaDoc;
27 import org.netbeans.beaninfo.editors.ExPropertyEditorSupport.EnvException;
28 import org.netbeans.core.UIExceptions;
29 import org.openide.explorer.propertysheet.*;
30 import org.openide.util.NbBundle;
31 /** An editor for primitive integer types which allows hinting of tag
32  * values and handles whitespace in setAsText better than the default
33  * JDK one. The following hints are supported:
34  * <UL><LI><B>stringKeys</B> - an array of Strings that should be supplied
35  * from getTags()</LI><LI><B>intValues - an array of ints corresponding to
36  * the values represented by stringKeys. <I>This hint must be present if
37  * the stringKeys hint is used, and the arrays must be of the same length.</LI>
38  * <LI><B>codeValues</B> - an array of strings that should be returned from
39  * getJavaInitializationString. This hint is optional when using the
40  * aforementioned hints. <P>These hints will also work for the Integer
41  * editor, which wraps an instance of this editor.
42  *
43  * @author Tim Boudreau
44  * @version 1.0
45  */

46 public class IntEditor extends ExPropertyEditorSupport {
47     
48     public static final String JavaDoc KEYS = "stringKeys"; //NOI18N
49
public static final String JavaDoc VALS = "intValues"; //NOI18N
50
public static final String JavaDoc CODE_VALS = "codeValues"; //NOI18N
51
String JavaDoc[] keys=null;
52     String JavaDoc[] code=null;
53     int[] values=null;
54     /** Creates a new instance of IntEditor */
55     public IntEditor() {
56     }
57     
58     protected void attachEnvImpl(PropertyEnv env) {
59         keys = (String JavaDoc[]) env.getFeatureDescriptor().getValue(KEYS);
60         values = (int[]) env.getFeatureDescriptor().getValue(VALS);
61         code = (String JavaDoc[]) env.getFeatureDescriptor().getValue(CODE_VALS);
62     }
63     
64     /** Validate that the values supplied by the PropertyEnv are proper,
65      * so there's not an obscure ArrayIndexOutOfBoundsException some time
66      * later because bad information was supplied. */

67     protected void validateEnv(PropertyEnv env) {
68         //fail fast validation of illegal values
69
boolean valid = keys == null && values == null && code == null;
70         if (!valid) {
71             valid = keys != null && values != null;
72             if (!valid) {
73                 throw new EnvException(
74                 "You must specify both an array of keys and an " + //NOI18N
75
"array of values if you specify one. Keys=" + //NOI18N
76
arrToStr(keys) + " Values=" + arrToStr(values)); //NOI18N
77
} else {
78                 valid = keys.length == values.length;
79                 if (valid) {
80                     valid = keys.length > 0 && values.length > 0;
81                 }
82                 
83                 if (!valid) {
84                     throw new EnvException(
85                     "The arrays of keys and values must have the same " + //NOI18N
86
"length and the length must be > 0. keys.length =" + //NOI18N
87
keys.length + " values.length=" + values.length + " Keys=" + //NOI18N
88
arrToStr(keys) + " Values=" + arrToStr(values)); //NOI18N
89
} else {
90                     if (code != null) {
91                         valid = code.length == keys.length;
92                         if (valid) {
93                             valid = code.length > 0;
94                         }
95                         if (!valid) {
96                             throw new EnvException(
97                             "The arrays of keys and values and codes must all" + //NOI18N
98
" have the same length, > 0. keys.length =" + //NOI18N
99
keys.length + " values.length=" + values.length + //NOI18N
100
" Code.length=" + code.length + " Keys=" + //NOI18N
101
arrToStr(keys) + " Values=" + arrToStr(values) +
102                             " Code=" + arrToStr(code)); //NOI18N
103
}
104                     }
105                 }
106             }
107         }
108     }
109     
110     private static final String JavaDoc arrToStr(int[] s) {
111         if (s == null) return "null"; //NOI18N
112
StringBuffer JavaDoc out = new StringBuffer JavaDoc(s.length * 3);
113         for (int i=0; i < s.length; i++) {
114             out.append(s[i]);
115             if (i != s.length-1) {
116                 out.append(','); //NOI18N
117
}
118         }
119         return out.toString();
120     }
121     
122     public String JavaDoc getAsText() {
123         Integer JavaDoc i = (Integer JavaDoc) getValue();
124         String JavaDoc result;
125         if (i != null) {
126             if (keys != null) {
127                 int intVal = i.intValue();
128                 int idx = -1;
129                 for (int j=0; j < values.length; j++) {
130                     if (values[j] == intVal) {
131                         idx = j;
132                         break;
133                     }
134                 }
135                 if (idx != -1) {
136                     result = keys [((Integer JavaDoc) super.getValue()).intValue()];
137                 } else {
138                     throw new IllegalArgumentException JavaDoc(
139                     "This property editor uses a set of keyed values, " + //NOI18N
140
"and the current value, " //NOI18N
141
+ i + ", is not specified."); //NOI18N
142
}
143             } else {
144                 result = getValue().toString();
145             }
146         } else {
147             result = NbBundle.getMessage (IntEditor.class, "NULL"); //NOI18N
148
}
149         return result;
150     }
151     
152     private void doSetAsText(String JavaDoc s) {
153         //fixes issue 23077, confusing error message from NFE.
154
//IllegalArgumentException is a more correct exception to throw
155
//anyway
156
try {
157             setValue(Integer.valueOf(s));
158         } catch (NumberFormatException JavaDoc nfe) {
159             String JavaDoc msg = NbBundle.getMessage(
160                 IntEditor.class, "EXC_ILLEGAL_VALUE_TEXT") + s; //NOI18N
161
RuntimeException JavaDoc iae = new IllegalArgumentException JavaDoc(msg); //NOI18N
162
UIExceptions.annotateUser(iae, msg, msg, nfe, new java.util.Date JavaDoc());
163             throw iae;
164         }
165     }
166     
167     public void setAsText(String JavaDoc s) {
168         s = s.trim();
169         if (keys == null) {
170             doSetAsText(s);
171         } else {
172             //use the keys, translate to an int value
173
int idx = Arrays.asList(keys).indexOf(s);
174             if ((idx == -1) || (idx > values.length-1)) {
175                 StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
176                 msg.append(NbBundle.getMessage(IntEditor.class,
177                     "EXC_ILLEGAL_STRING_TEXT_FIRST")); //NOI18N
178
msg.append(s);
179                 msg.append(NbBundle.getMessage(IntEditor.class,
180                     "EXC_ILLEGAL_STRING_TEXT_SECOND")); //NOI18N
181
msg.append(arrToStr(keys));
182                 String JavaDoc message = msg.toString();
183                 RuntimeException JavaDoc iae = new IllegalArgumentException JavaDoc(message);
184                 UIExceptions.annotateUser(iae, message, message, iae,
185                                          new java.util.Date JavaDoc());
186                 throw iae;
187             } else {
188                 setValue(Integer.valueOf(idx));
189             }
190         }
191     }
192     
193     public Object JavaDoc getValue () {
194         Integer JavaDoc v = (Integer JavaDoc) super.getValue();
195         if (values != null) {
196             v = Integer.valueOf (values[v.intValue()]);
197         }
198         return v;
199     }
200     
201     //issue 34037 - make setValue calls with illegal values fail-fast
202
public void setValue (Object JavaDoc value) {
203         if ((value instanceof Integer JavaDoc) || (value == null)) {
204             super.setValue (value);
205         } else {
206             throw new IllegalArgumentException JavaDoc (
207                 "Argument to IntEditor.setValue() must be Integer, but was " + //NOI18N
208
value.getClass().getName() + "(=" + //NOI18N
209
value.toString() + ")"); //NOI18N
210
}
211     }
212     
213     public String JavaDoc[] getTags() {
214         return keys;
215     }
216     
217     public String JavaDoc getJavaInitializationString() {
218         String JavaDoc result;
219         if (code == null) {
220             result = getValue().toString();
221         } else {
222             result = code[((Integer JavaDoc) getValue()).intValue()];
223         }
224         return result;
225     }
226 }
227
Popular Tags