This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 20736
Collapse All | Expand All

(-)core/src/org/netbeans/beaninfo/editors/IntEditor.java (+186 lines)
Added Link Here
1
/*
2
 * IntEditor.java
3
 *
4
 * Created on February 28, 2003, 2:15 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
import java.util.Arrays;
12
/** An editor for primitive integer types which allows hinting of tag
13
 *  values and handles whitespace in setAsText better than the default
14
 *  one.  Hints may be supplied by the standard method of returning them
15
 *  from getValue() on the feature descriptor supplied by the PropertyEnv.
16
 *  To use hinting, return a String[] from getValue ("stringKeys")
17
 *  and an int[] from getValue ("intValues").  These two
18
 *  arrays must have the same number of entries and the number of entries
19
 *  must be at least 1.  Key matching in setAsText is case sensitive.
20
 *  <P>To provide specific initialization strings (support for generating
21
 *  code that uses constants instead of their literal value), also supply
22
 *  from <code>getValue(&quot;codeValues&quot;) an array of initialization
23
 *  strings.  This array must be the same size as the keys array.
24
 *
25
 * @author  Tim Boudreau
26
 */
27
public class IntEditor extends PropertyEditorSupport implements 
28
    ExPropertyEditor {
29
    String[] keys=null;
30
    String[] code=null;
31
    int[] values=null;
32
    /** Creates a new instance of IntEditor */
33
    public IntEditor() {
34
    }
35
    
36
    public void attachEnv(org.openide.explorer.propertysheet.PropertyEnv env) {
37
        keys = (String[]) env.getFeatureDescriptor().getValue(
38
            "stringKeys"); //NOI18N
39
        values = (int[]) env.getFeatureDescriptor().getValue(
40
            "intValues"); //NOI18N
41
        code = (String[]) env.getFeatureDescriptor().getValue(
42
            "codeValues"); //NOI18N
43
        System.out.println("Env attached.  keys= " + arrToStr(keys) 
44
            + " values=" + arrToStr(values) + " codes=" + arrToStr(code));
45
        validateEnv();
46
    }
47
    
48
    /** Validate that the values supplied by the PropertyEnv are proper,
49
     *  so there's not an obscure ArrayIndexOutOfBoundsException some time
50
     *  later because bad information was supplied.  */
51
    private void validateEnv() {
52
        //fail fast validation of illegal values
53
        boolean valid = keys == null && values == null && code == null;
54
        System.out.println("checked all null - " + valid);
55
        if (!valid) {
56
            valid = keys != null && values != null;
57
            System.out.println("checked both non-null - " + valid);
58
            if (!valid) {
59
                throw new EnvException (
60
                    "You must specify both an array of keys and an array of values if you specify one. Keys=" +   //NOI18N
61
                    arrToStr(keys) + " Values=" + arrToStr(values));  //NOI18N
62
            } else {
63
                valid = keys.length == values.length;
64
                System.out.println("checked matching lengths - " + valid);
65
                if (valid) {
66
                    valid = keys.length > 0 && values.length > 0;
67
                }
68
                System.out.println("checked > 0 lengths - " + valid);
69
                
70
                if (!valid) {
71
                throw new EnvException (
72
                    "The arrays of keys and values must have the same length and the length must be > 0. keys.length =" +   //NOI18N
73
                    keys.length + " values.length=" + values.length + " Keys=" + //NOI18N
74
                    arrToStr(keys) + " Values=" + arrToStr(values));  //NOI18N
75
                } else {
76
                    if (code != null) {
77
                        valid = code.length == keys.length;
78
                        System.out.println("checked code length - " + valid);
79
                        if (valid) {
80
                            valid = code.length > 0;
81
                        }
82
                        System.out.println("checked code length > 0 - " + valid);
83
                        if (!valid) {
84
                            throw new EnvException (
85
                                "The arrays of keys and values and codes must all have the same length, > 0. keys.length =" +   //NOI18N
86
                                keys.length + " values.length=" + values.length + //NOI18N
87
                                " Code.length=" + code.length + " Keys=" + //NOI18N
88
                                arrToStr(keys) + " Values=" + arrToStr(values));  //NOI18N
89
                        }
90
                    }
91
                }
92
            }
93
        }
94
    }
95
    
96
    private static final String arrToStr (String[] s) {
97
        if (s == null) return "null"; //NOI18N
98
        StringBuffer out = new StringBuffer (s.length * 10);
99
        for (int i=0; i < s.length; i++) {
100
            out.append (s[i]);
101
            if (i != s.length-1) {
102
                out.append (","); //NOI18N
103
            }
104
        }
105
        return out.toString();
106
    }
107
        
108
    private static final String arrToStr (int[] s) {
109
        if (s == null) return "null"; //NOI18N
110
        StringBuffer out = new StringBuffer (s.length * 3);
111
        for (int i=0; i < s.length; i++) {
112
            out.append (s[i]);
113
            if (i != s.length-1) {
114
                out.append (","); //NOI18N
115
            }
116
        }
117
        return out.toString();
118
    }
119
    
120
    private String getStringRep (int i) {
121
        if (keys != null) {
122
            try {
123
                return keys[i];
124
            } catch (ArrayIndexOutOfBoundsException ae) {
125
                throw new IllegalArgumentException (
126
                    "This property editor uses a set of keyed values, " +  //NOI18N
127
                    "and the value " //NOI18N
128
                    + i + " is out of range."); //NOI18N
129
            }
130
        } else {
131
            return Integer.toString(i);
132
        }
133
    }
134
    
135
    public String getAsText() {
136
        return getStringRep (((Integer) getValue()).intValue());
137
    }
138
    
139
    public void setAsText(String s) {
140
        s = s.trim();
141
        if (keys == null) {
142
            //standard int handling
143
            setValue (new Integer (Integer.parseInt (s)));
144
        } else {
145
            //use the keys
146
            int idx = Arrays.asList (keys).indexOf(s);
147
            if ((idx == -1) || (idx > values.length-1)) {
148
                StringBuffer sb = new StringBuffer (40);
149
                //if something goes wrong, give a clue what it was
150
                sb.append ("Illegal value: \"" + s + "\" - valid values are: "); //NOI18N
151
                for (int i=0; i < keys.length; i ++) {
152
                    sb.append (keys[i]);
153
                    if (i != keys.length - 1) {
154
                        sb.append (','); //NOI18N
155
                    }
156
                }
157
                throw new IllegalArgumentException 
158
                    (sb.toString()); //NOI18N
159
            } else {
160
                setValue (new Integer (values[idx]));
161
            }
162
        }
163
    }
164
    
165
    public String[] getTags () {
166
        return keys;
167
    }
168
    
169
    public String getJavaInitializationString() {
170
        String result;
171
        if (code == null) {
172
            result = getValue().toString();
173
        } else {
174
            result = code[((Integer) getValue()).intValue()];
175
        }
176
        return result;
177
    }
178
    
179
    /** This class exists to enable unit tests to differentiate
180
     *  between code bugs in the editors and invalid values from
181
     *  the propertyEnv.  */
182
    public static class EnvException extends IllegalArgumentException {  
183
        public EnvException (String s) { super (s); }
184
    }
185
    
186
}
(-)core/src/org/netbeans/beaninfo/editors/WrappersEditor.java (-3 / +11 lines)
Lines 14-27 Link Here
14
package org.netbeans.beaninfo.editors;
14
package org.netbeans.beaninfo.editors;
15
15
16
import java.beans.*;
16
import java.beans.*;
17
17
import org.openide.explorer.propertysheet.ExPropertyEditor;
18
import org.openide.explorer.propertysheet.PropertyEnv;
18
/**
19
/**
19
 * Abstract class represents Editor for Wrappers of 8 known primitive types
20
 * Abstract class represents Editor for Wrappers of 8 known primitive types
20
 * (Byte, Short, Integer, Long, Boolean, Float, Double, Character)
21
 * (Byte, Short, Integer, Long, Boolean, Float, Double, Character)
21
 *
22
 *
22
 * @author  Josef Kozak
23
 * @author  Josef Kozak
23
 */
24
 */
24
public abstract class WrappersEditor implements PropertyEditor {
25
public abstract class WrappersEditor implements ExPropertyEditor {
25
    
26
    
26
    private PropertyEditor pe = null;
27
    private PropertyEditor pe = null;
27
    
28
    
Lines 189-193 Link Here
189
     *   	current value.
190
     *   	current value.
190
     */    
191
     */    
191
    public abstract String getJavaInitializationString();
192
    public abstract String getJavaInitializationString();
192
    
193
194
    public void attachEnv(PropertyEnv env) {
195
        //Delegate if the primitive editor is an ExPropertyEditor -
196
        //new boolean and int editors will be
197
        if (pe instanceof ExPropertyEditor) {
198
            ((ExPropertyEditor) pe).attachEnv (env);
199
        }
200
    }
193
}
201
}
(-)core/src/org/netbeans/beaninfo/editors/BoolEditor.java (+95 lines)
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
}
(-)core/test/unit/src/org/openide/explorer/propertysheet/EnvProvider.java (+26 lines)
Added Link Here
1
/*
2
 * EnvProvider.java
3
 *
4
 * Created on March 10, 2003, 4:14 PM
5
 */
6
7
package org.openide.explorer.propertysheet;
8
import org.openide.nodes.Node.Property;
9
/** Trivial class to manufacture PropertyEnv instances without having
10
 *to create visual components.
11
 *
12
 * @author  Tim Boudreau
13
 */
14
public class EnvProvider {
15
    
16
    /** Creates a new instance of EnvProvider */
17
    public EnvProvider() {
18
    }
19
    
20
    public static final PropertyEnv getEnv (Property p) {
21
        PropertyEnv result = new PropertyEnv();
22
        result.setFeatureDescriptor (p);
23
        return result;
24
    }
25
    
26
}
(-)core/test/unit/src/org/netbeans/beaninfo/editors/IntEditorTest.java (+186 lines)
Added Link Here
1
/*
2
 * IntEditorTest.java
3
 * JUnit based test
4
 *
5
 * Created on March 10, 2003, 1:10 PM
6
 */
7
8
package org.netbeans.beaninfo.editors;
9
10
import java.beans.*;
11
import junit.framework.*;
12
import org.openide.explorer.propertysheet.*;
13
import org.openide.nodes.*;
14
import org.openide.util.NbBundle;
15
import java.util.Arrays;
16
17
/** Unit test for priimitive int property editor
18
 *
19
 * @author Tim Boudreau
20
 */
21
public class IntEditorTest extends TestCase {
22
    String[] goodKeys = new String[] {"zero","one","two","three"};
23
    int[] goodValues = new int[] {0,1,2,3};
24
    String [] goodCode = new String[] {"0", "0+1", "1+1", "1+2"};
25
    
26
    IntProp goodProp = new IntProp (0, goodKeys, goodValues, goodCode);
27
    
28
    String[] badKeys = new String[] {"blah"};
29
    int[] badValues = new int[] {1,15};
30
    
31
    String[] emptyKeys = new String[0];
32
    
33
    IntProp badMismatchProp = new IntProp (1, emptyKeys, goodValues, null);
34
    IntProp badMismatchProp2 = new IntProp (1, goodKeys, goodValues, emptyKeys);
35
    
36
    public IntEditorTest(java.lang.String testName) {
37
        super(testName);
38
        
39
    }
40
    
41
    public static Test suite() {
42
        TestSuite suite = new TestSuite(IntEditorTest.class);
43
        return suite;
44
    }
45
    
46
    /** Test of attachEnv method, of class org.netbeans.beaninfo.editors.IntEditor. */
47
    public void testAttachEnv() {
48
        System.out.println("testAttachEnv");
49
        PropertyEditor ped = goodProp.getPropertyEditor();
50
        if (!(ped instanceof ExPropertyEditor)) {
51
            fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName());
52
        } else {
53
            ExPropertyEditor ex = (ExPropertyEditor) ped;
54
            prepareEnv (goodProp, ex, false);
55
        }
56
        
57
        ped = badMismatchProp.getPropertyEditor();
58
        if (!(ped instanceof ExPropertyEditor)) {
59
            fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName());
60
        } else {
61
            ExPropertyEditor ex = (ExPropertyEditor) ped;
62
            prepareEnv (badMismatchProp, ex, true);
63
        }
64
        
65
        ped = badMismatchProp2.getPropertyEditor();
66
        if (!(ped instanceof ExPropertyEditor)) {
67
            fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName());
68
        } else {
69
            ExPropertyEditor ex = (ExPropertyEditor) ped;
70
            prepareEnv (badMismatchProp2, ex, true);
71
        }
72
    }
73
    
74
    /** Test of getAsText method, of class org.netbeans.beaninfo.editors.IntEditor. */
75
    public void testGetAsText() {
76
        System.out.println("testGetAsText");
77
        
78
        // Add your test code below by replacing the default call to fail.
79
        ExPropertyEditor ped = getConfiguredEditor (goodProp);
80
        ped.setValue (new Integer(0));
81
        String s = ped.getAsText();
82
        if ("zero".equals(s)) return;
83
    }
84
    
85
    /** Test of setAsText method, of class org.netbeans.beaninfo.editors.IntEditor. */
86
    public void testSetAsText() {
87
        System.out.println("testSetAsText");
88
        ExPropertyEditor ped = getConfiguredEditor (goodProp);
89
        ped.setAsText ("two");
90
        int i = ((Integer) ped.getValue()).intValue();
91
        if (i != 2) {
92
            fail ("Set text value of propertyeditor to \"two\" but getValue() returns " + i);
93
        }
94
    }
95
    
96
    /** Test of getTags method, of class org.netbeans.beaninfo.editors.IntEditor. */
97
    public void testGetTags() {
98
        System.out.println("testGetTags");
99
        ExPropertyEditor ped = getConfiguredEditor (goodProp);
100
        String[] s = ped.getTags();
101
        if (s.length != 4) fail ("Keys length should be 4 but is " + s.length);
102
        if ("zero".equals(s[0])) return;
103
        fail ("First key should be \"zero\" but is " + s[0]);
104
    }
105
    
106
    /** Test of getJavaInitializationString method, of class org.netbeans.beaninfo.editors.IntEditor. */
107
    public void testGetJavaInitializationString() {
108
        System.out.println("testGetJavaInitializationString");
109
        ExPropertyEditor ped = getConfiguredEditor (goodProp);
110
        ped.setValue (new Integer(3));
111
        String s = ped.getJavaInitializationString();
112
        if (!("1+2".equals (s))) {
113
            fail ("GetJavaInitializationString returns " + s + " but should return \"1+1\"");
114
        }
115
    }
116
    
117
    private ExPropertyEditor getConfiguredEditor (IntProp p) {
118
        ExPropertyEditor result = (ExPropertyEditor) p.getPropertyEditor();
119
        try {
120
            result.setValue ((Integer) p.getValue());
121
            prepareEnv (p, result, false);
122
        } catch (Exception e) {
123
            //Other tests will fail if an exception is thrown, ignore
124
        }
125
        return result;
126
    }
127
    
128
    private void prepareEnv (IntProp prop, ExPropertyEditor pe, boolean shouldFail) {
129
        PropertyEnv env = org.openide.explorer.propertysheet.EnvProvider.getEnv(prop);
130
        org.netbeans.beaninfo.editors.IntEditor.EnvException e=null;
131
        try {
132
            pe.attachEnv (env);
133
            
134
        } catch (org.netbeans.beaninfo.editors.IntEditor.EnvException ie) {
135
            e = ie;
136
        } finally {
137
            if (shouldFail && e == null) {
138
                fail ("EnvException should have been thrown but wasn't.");
139
            }
140
        }
141
    }
142
    
143
    private class IntProp extends Node.Property {
144
        String[] k;
145
        int[] v;
146
        String[] c;
147
        int val;
148
        public IntProp (int val, String[] keys, int[] vals, String[] codeVals) {
149
            super (Integer.TYPE);
150
            this.val = val;
151
            v = vals;
152
            k = keys;
153
            c = codeVals;
154
        }
155
        
156
        public Object getValue (String key) {
157
            if ("stringKeys".equals(key)) return k;
158
            if ("intValues".equals(key)) return v;
159
            if ("codeValues".equals(key)) return c;
160
            Object result = super.getValue(key);
161
            return result;
162
        }
163
        
164
        public boolean canRead() {
165
            return true;
166
        }
167
        
168
        public boolean canWrite() {
169
            return true;
170
        }
171
        
172
        public Object getValue() throws IllegalAccessException, java.lang.reflect.InvocationTargetException {
173
            return new Integer(val);
174
        }
175
        
176
        public void setValue(Object val) throws IllegalAccessException, IllegalArgumentException, java.lang.reflect.InvocationTargetException {
177
            this.val = ((Integer) val).intValue();
178
        }
179
        
180
        public PropertyEditor getPropertyEditor () {
181
            return new org.netbeans.beaninfo.editors.IntEditor ();
182
        }
183
        
184
    }
185
    
186
}
(-)openide/api/doc/org/openide/explorer/doc-files/api.html (+50 lines)
Lines 742-747 Link Here
742
            <TD> org.openide.util.Lookup </TD>
742
            <TD> org.openide.util.Lookup </TD>
743
            <TD> A lookup to use to query for results.</TD>
743
            <TD> A lookup to use to query for results.</TD>
744
        </TR>
744
        </TR>
745
	<TR> 
746
	    <TD rowspan='3'>java.lang.String </TD>
747
	    <TD>instructions</TD>
748
	    <TD>String</TD>
749
	    <TD>Should contain localized instructions to the user, to be displayed in the
750
                String custom editor</TD>
751
        </TR>
752
        <TR>
753
            <TD>oneline</TD>
754
            <TD>Boolean</TD>
755
            <TD>Instructs the property editor that the custom editor should present
756
                a one-line text control (JTextField or equivalent) rather than
757
                a multi-line text control (JTextArea or equivalent).
758
            </TD>
759
        </TR>
760
        <TR>
761
	    <TD>suppressCustomEditor</TD>
762
            <TD>Boolean</TD>
763
            <TD>Instructs the property editor to suppress the custom editor 
764
                button on the property sheet.</TD>
765
        </TR>
766
	<TR>
767
	    <TD rowspan='3'> java.lang.Integer and primitive integer </TD>
768
            <TD>stringKeys</TD>
769
            <TD>String[]</TD>
770
            <TD> Should contain internationalized strings that should be used as tags in
771
                 a combo box.  Number of elements must be >= 1.  If this hint is 
772
                 supplied, the hint <code>intKeyValues</code> must also be supplied
773
            </TD>
774
        </TR>
775
        <TR>
776
            <TD>intValues</TD>
777
            <TD> int[] </TD>
778
            <TD> The values the string keys correspond to. Must have the same number
779
                 of elements as the string array </TD>
780
        </TR>
781
        <TR>
782
            <TD>codeValues</TD>
783
            <TD> String[] </TD>
784
            <TD> Values to use in <code>getJavaInitializationString()</code> in place
785
                 of hardcoding numbers (useful for constants)</TD>
786
        </TR>
787
        <TR>
788
           <TD> java.lang.Boolean </TD>
789
           <TD> stringValues </TD>
790
           <TD> String[] </TD>
791
           <TD> Should contain an array of two strings to be used for user
792
                displayable text for the boolean values, in the order
793
                false, true - for example, <code>new String[] {"off","on"}</code>.</TD
794
        </TR>
745
    </TABLE>
795
    </TABLE>
746
796
747
<H4> Custom parameters that are not tied with particular editor </H4>
797
<H4> Custom parameters that are not tied with particular editor </H4>

Return to bug 20736