Added
Link Here
|
1 |
/* |
2 |
* BoolEditor.java |
3 |
* |
4 |
* Created on February 28, 2003, 1:13 PM |
5 |
*/ |
6 |
|
7 |
package org.netbeans.beaninfo.editors; |
8 |
import java.beans.*; |
9 |
import org.openide.explorer.propertysheet.ExPropertyEditor; |
10 |
import org.openide.util.NbBundle; |
11 |
/** Replacement editor for boolean primitive values which supports |
12 |
* internationalization and alternate string values that |
13 |
* can be supplied to the property editor via adding an array |
14 |
* returning an array of two Strings (false then true) from |
15 |
* <code>env.getFeatureDescriptor().getValue()</code>. These |
16 |
* string values will then be used for getAsText, setAsText, and getTags. |
17 |
* These strings should be correctly internationalized if supplied |
18 |
* by a module. String value matching in setAsText is non-case-sensitive |
19 |
* ("TRUE" and "tRue" are equivalent). |
20 |
* |
21 |
* @author Tim Boudreau |
22 |
*/ |
23 |
public class BoolEditor extends PropertyEditorSupport implements ExPropertyEditor { |
24 |
String[] stringValues = null; |
25 |
/** Creates a new instance of BoolEditor */ |
26 |
public BoolEditor() { |
27 |
} |
28 |
|
29 |
public void attachEnv(org.openide.explorer.propertysheet.PropertyEnv env) { |
30 |
stringValues = (String[]) env.getFeatureDescriptor().getValue( |
31 |
"stringValues"); //NOI18N |
32 |
} |
33 |
|
34 |
private String getStringRep (boolean val) { |
35 |
if (stringValues != null) { |
36 |
return stringValues [val ? 0 : 1]; |
37 |
} |
38 |
String result; |
39 |
if (val) { |
40 |
result = NbBundle.getMessage(BoolEditor.class, "TRUE"); //NOI18N |
41 |
} else { |
42 |
result = NbBundle.getMessage(BoolEditor.class, "FALSE"); //NOI18N |
43 |
} |
44 |
return result; |
45 |
} |
46 |
|
47 |
/** Returns Boolean.TRUE, Boolean.FALSE or null in the case of an |
48 |
* unrecognized string. */ |
49 |
private Boolean stringVal (String val) { |
50 |
String valToTest = val.trim().toUpperCase(); |
51 |
String test = getStringRep (true).toUpperCase(); |
52 |
if (test.equals(valToTest)) return Boolean.TRUE; |
53 |
test = getStringRep (false).toUpperCase(); |
54 |
if (test.equals(valToTest)) return Boolean.FALSE; |
55 |
return null; |
56 |
} |
57 |
|
58 |
public String getJavaInitializationString() { |
59 |
Boolean val = (Boolean) getValue(); |
60 |
if (val == null) return "null"; //NOI18N |
61 |
return Boolean.TRUE.equals (getValue()) ? "true" : "false"; //NOI18N |
62 |
} |
63 |
|
64 |
public String[] getTags () { |
65 |
return new String[] { |
66 |
getStringRep (true), getStringRep (false) |
67 |
}; |
68 |
} |
69 |
|
70 |
public String getAsText() { |
71 |
Boolean val = (Boolean) getValue(); |
72 |
if (val == null) return NbBundle.getMessage (BoolEditor.class, "NULL"); |
73 |
return getStringRep (Boolean.TRUE.equals (getValue())); |
74 |
} |
75 |
|
76 |
public void setAsText(String txt) { |
77 |
Boolean val = stringVal (txt); |
78 |
boolean newVal = val == null ? false : val.booleanValue(); |
79 |
setValue (newVal ? Boolean.TRUE : Boolean.FALSE); |
80 |
} |
81 |
|
82 |
static BoolEditor ed = new BoolEditor(); |
83 |
public static void main (String[] args) { |
84 |
Boolean val = Boolean.TRUE; |
85 |
ed.setValue (val); |
86 |
checkValue (val.booleanValue()); |
87 |
val = Boolean.FALSE; |
88 |
ed.setValue (val); |
89 |
checkValue (val.booleanValue()); |
90 |
} |
91 |
|
92 |
public static boolean checkValue (boolean val) { |
93 |
return val == ((Boolean) ed.getValue()).booleanValue(); |
94 |
} |
95 |
} |