KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BoolEditor.java
21  *
22  * Created on February 28, 2003, 1:13 PM
23  */

24
25 package org.netbeans.beaninfo.editors;
26 import java.beans.*;
27 import org.openide.explorer.propertysheet.ExPropertyEditor;
28 import org.openide.util.NbBundle;
29 /** Replacement editor for boolean primitive values which supports
30  * internationalization and alternate string values that
31  * can be supplied to the property editor via adding an array
32  * returning an array of two Strings (false then true) from
33  * <code>env.getFeatureDescriptor().getValue()</code>. These
34  * string values will then be used for getAsText, setAsText, and getTags.
35  * These strings should be correctly internationalized if supplied
36  * by a module. String value matching in setAsText is non-case-sensitive
37  * ("TRUE" and "tRue" are equivalent).
38  *
39  * @author Tim Boudreau
40  */

41 public class BoolEditor extends ExPropertyEditorSupport {
42     String JavaDoc[] stringValues = null;
43     /** Creates a new instance of BoolEditor */
44     public BoolEditor() {
45     }
46     
47     protected void attachEnvImpl(org.openide.explorer.propertysheet.PropertyEnv env) {
48         stringValues = (String JavaDoc[]) env.getFeatureDescriptor().getValue(
49         "stringValues"); //NOI18N
50
}
51     
52     /** Throws an EnvException if the stringValues key is not 2 items in length. */
53     protected void validateEnv(org.openide.explorer.propertysheet.PropertyEnv env) {
54         if (stringValues != null) {
55             if (stringValues.length != 2) {
56                 throw new EnvException(
57                 "String value hint for boolean editor must contain exactly 2 "
58                 + "items. The supplied value contains " + stringValues.length +
59                 " items: " + arrToStr(stringValues));
60             }
61         }
62     }
63     
64     private String JavaDoc getStringRep(boolean val) {
65         if (stringValues != null) {
66             return stringValues [val ? 0 : 1];
67         }
68         String JavaDoc result;
69         if (val) {
70             result = NbBundle.getMessage(BoolEditor.class, "TRUE"); //NOI18N
71
} else {
72             result = NbBundle.getMessage(BoolEditor.class, "FALSE"); //NOI18N
73
}
74         return result;
75     }
76     
77     /** Returns Boolean.TRUE, Boolean.FALSE or null in the case of an
78      * unrecognized string. */

79     private Boolean JavaDoc stringVal(String JavaDoc val) {
80         String JavaDoc valToTest = val.trim().toUpperCase();
81         String JavaDoc test = getStringRep(true).toUpperCase();
82         if (test.equals(valToTest)) return Boolean.TRUE;
83         test = getStringRep(false).toUpperCase();
84         if (test.equals(valToTest)) return Boolean.FALSE;
85         return null;
86     }
87     
88     public String JavaDoc getJavaInitializationString() {
89         Boolean JavaDoc val = (Boolean JavaDoc) getValue();
90         if (val == null) return "null"; //NOI18N
91
return Boolean.TRUE.equals(getValue()) ? "true" : "false"; //NOI18N
92
}
93     
94     public String JavaDoc[] getTags() {
95         return new String JavaDoc[] {
96             getStringRep(true), getStringRep(false)
97         };
98     }
99     
100     public String JavaDoc getAsText() {
101         Boolean JavaDoc val = (Boolean JavaDoc) getValue();
102         if (val == null) return NbBundle.getMessage(BoolEditor.class, "NULL");
103         return getStringRep(Boolean.TRUE.equals(getValue()));
104     }
105     
106     public void setAsText(String JavaDoc txt) {
107         Boolean JavaDoc val = stringVal(txt);
108         boolean newVal = val == null ? false : val.booleanValue();
109         setValue(newVal ? Boolean.TRUE : Boolean.FALSE);
110     }
111 }
112
Popular Tags