Index: core/src/org/netbeans/beaninfo/editors/IntEditor.java =================================================================== RCS file: core/src/org/netbeans/beaninfo/editors/IntEditor.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ core/src/org/netbeans/beaninfo/editors/IntEditor.java 10 Mar 2003 17:12:40 -0000 @@ -0,0 +1,186 @@ +/* + * IntEditor.java + * + * Created on February 28, 2003, 2:15 PM + */ + +package org.netbeans.beaninfo.editors; +import java.beans.*; +import org.openide.explorer.propertysheet.ExPropertyEditor; +import org.openide.util.NbBundle; +import java.util.Arrays; +/** An editor for primitive integer types which allows hinting of tag + * values and handles whitespace in setAsText better than the default + * one. Hints may be supplied by the standard method of returning them + * from getValue() on the feature descriptor supplied by the PropertyEnv. + * To use hinting, return a String[] from getValue ("stringKeys") + * and an int[] from getValue ("intValues"). These two + * arrays must have the same number of entries and the number of entries + * must be at least 1. Key matching in setAsText is case sensitive. + *
To provide specific initialization strings (support for generating
+ * code that uses constants instead of their literal value), also supply
+ * from getValue("codeValues") an array of initialization
+ * strings. This array must be the same size as the keys array.
+ *
+ * @author Tim Boudreau
+ */
+public class IntEditor extends PropertyEditorSupport implements
+ ExPropertyEditor {
+ String[] keys=null;
+ String[] code=null;
+ int[] values=null;
+ /** Creates a new instance of IntEditor */
+ public IntEditor() {
+ }
+
+ public void attachEnv(org.openide.explorer.propertysheet.PropertyEnv env) {
+ keys = (String[]) env.getFeatureDescriptor().getValue(
+ "stringKeys"); //NOI18N
+ values = (int[]) env.getFeatureDescriptor().getValue(
+ "intValues"); //NOI18N
+ code = (String[]) env.getFeatureDescriptor().getValue(
+ "codeValues"); //NOI18N
+ System.out.println("Env attached. keys= " + arrToStr(keys)
+ + " values=" + arrToStr(values) + " codes=" + arrToStr(code));
+ validateEnv();
+ }
+
+ /** Validate that the values supplied by the PropertyEnv are proper,
+ * so there's not an obscure ArrayIndexOutOfBoundsException some time
+ * later because bad information was supplied. */
+ private void validateEnv() {
+ //fail fast validation of illegal values
+ boolean valid = keys == null && values == null && code == null;
+ System.out.println("checked all null - " + valid);
+ if (!valid) {
+ valid = keys != null && values != null;
+ System.out.println("checked both non-null - " + valid);
+ if (!valid) {
+ throw new EnvException (
+ "You must specify both an array of keys and an array of values if you specify one. Keys=" + //NOI18N
+ arrToStr(keys) + " Values=" + arrToStr(values)); //NOI18N
+ } else {
+ valid = keys.length == values.length;
+ System.out.println("checked matching lengths - " + valid);
+ if (valid) {
+ valid = keys.length > 0 && values.length > 0;
+ }
+ System.out.println("checked > 0 lengths - " + valid);
+
+ if (!valid) {
+ throw new EnvException (
+ "The arrays of keys and values must have the same length and the length must be > 0. keys.length =" + //NOI18N
+ keys.length + " values.length=" + values.length + " Keys=" + //NOI18N
+ arrToStr(keys) + " Values=" + arrToStr(values)); //NOI18N
+ } else {
+ if (code != null) {
+ valid = code.length == keys.length;
+ System.out.println("checked code length - " + valid);
+ if (valid) {
+ valid = code.length > 0;
+ }
+ System.out.println("checked code length > 0 - " + valid);
+ if (!valid) {
+ throw new EnvException (
+ "The arrays of keys and values and codes must all have the same length, > 0. keys.length =" + //NOI18N
+ keys.length + " values.length=" + values.length + //NOI18N
+ " Code.length=" + code.length + " Keys=" + //NOI18N
+ arrToStr(keys) + " Values=" + arrToStr(values)); //NOI18N
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private static final String arrToStr (String[] s) {
+ if (s == null) return "null"; //NOI18N
+ StringBuffer out = new StringBuffer (s.length * 10);
+ for (int i=0; i < s.length; i++) {
+ out.append (s[i]);
+ if (i != s.length-1) {
+ out.append (","); //NOI18N
+ }
+ }
+ return out.toString();
+ }
+
+ private static final String arrToStr (int[] s) {
+ if (s == null) return "null"; //NOI18N
+ StringBuffer out = new StringBuffer (s.length * 3);
+ for (int i=0; i < s.length; i++) {
+ out.append (s[i]);
+ if (i != s.length-1) {
+ out.append (","); //NOI18N
+ }
+ }
+ return out.toString();
+ }
+
+ private String getStringRep (int i) {
+ if (keys != null) {
+ try {
+ return keys[i];
+ } catch (ArrayIndexOutOfBoundsException ae) {
+ throw new IllegalArgumentException (
+ "This property editor uses a set of keyed values, " + //NOI18N
+ "and the value " //NOI18N
+ + i + " is out of range."); //NOI18N
+ }
+ } else {
+ return Integer.toString(i);
+ }
+ }
+
+ public String getAsText() {
+ return getStringRep (((Integer) getValue()).intValue());
+ }
+
+ public void setAsText(String s) {
+ s = s.trim();
+ if (keys == null) {
+ //standard int handling
+ setValue (new Integer (Integer.parseInt (s)));
+ } else {
+ //use the keys
+ int idx = Arrays.asList (keys).indexOf(s);
+ if ((idx == -1) || (idx > values.length-1)) {
+ StringBuffer sb = new StringBuffer (40);
+ //if something goes wrong, give a clue what it was
+ sb.append ("Illegal value: \"" + s + "\" - valid values are: "); //NOI18N
+ for (int i=0; i < keys.length; i ++) {
+ sb.append (keys[i]);
+ if (i != keys.length - 1) {
+ sb.append (','); //NOI18N
+ }
+ }
+ throw new IllegalArgumentException
+ (sb.toString()); //NOI18N
+ } else {
+ setValue (new Integer (values[idx]));
+ }
+ }
+ }
+
+ public String[] getTags () {
+ return keys;
+ }
+
+ public String getJavaInitializationString() {
+ String result;
+ if (code == null) {
+ result = getValue().toString();
+ } else {
+ result = code[((Integer) getValue()).intValue()];
+ }
+ return result;
+ }
+
+ /** This class exists to enable unit tests to differentiate
+ * between code bugs in the editors and invalid values from
+ * the propertyEnv. */
+ public static class EnvException extends IllegalArgumentException {
+ public EnvException (String s) { super (s); }
+ }
+
+}
Index: core/src/org/netbeans/beaninfo/editors/WrappersEditor.java
===================================================================
RCS file: /cvs/core/src/org/netbeans/beaninfo/editors/WrappersEditor.java,v
--- core/src/org/netbeans/beaninfo/editors/WrappersEditor.java 6 Feb 2002 10:07:38 -0000 1.3
+++ core/src/org/netbeans/beaninfo/editors/WrappersEditor.java 10 Mar 2003 17:12:40 -0000
@@ -14,14 +14,15 @@
package org.netbeans.beaninfo.editors;
import java.beans.*;
-
+import org.openide.explorer.propertysheet.ExPropertyEditor;
+import org.openide.explorer.propertysheet.PropertyEnv;
/**
* Abstract class represents Editor for Wrappers of 8 known primitive types
* (Byte, Short, Integer, Long, Boolean, Float, Double, Character)
*
* @author Josef Kozak
*/
-public abstract class WrappersEditor implements PropertyEditor {
+public abstract class WrappersEditor implements ExPropertyEditor {
private PropertyEditor pe = null;
@@ -189,5 +190,12 @@
* current value.
*/
public abstract String getJavaInitializationString();
-
+
+ public void attachEnv(PropertyEnv env) {
+ //Delegate if the primitive editor is an ExPropertyEditor -
+ //new boolean and int editors will be
+ if (pe instanceof ExPropertyEditor) {
+ ((ExPropertyEditor) pe).attachEnv (env);
+ }
+ }
}
Index: core/src/org/netbeans/beaninfo/editors/BoolEditor.java
===================================================================
RCS file: core/src/org/netbeans/beaninfo/editors/BoolEditor.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ core/src/org/netbeans/beaninfo/editors/BoolEditor.java 10 Mar 2003 17:12:40 -0000
@@ -0,0 +1,95 @@
+/*
+ * BoolEditor.java
+ *
+ * Created on February 28, 2003, 1:13 PM
+ */
+
+package org.netbeans.beaninfo.editors;
+import java.beans.*;
+import org.openide.explorer.propertysheet.ExPropertyEditor;
+import org.openide.util.NbBundle;
+/** Replacement editor for boolean primitive values which supports
+ * internationalization and alternate string values that
+ * can be supplied to the property editor via adding an array
+ * returning an array of two Strings (false then true) from
+ *
env.getFeatureDescriptor().getValue()
. These
+ * string values will then be used for getAsText, setAsText, and getTags.
+ * These strings should be correctly internationalized if supplied
+ * by a module. String value matching in setAsText is non-case-sensitive
+ * ("TRUE" and "tRue" are equivalent).
+ *
+ * @author Tim Boudreau
+ */
+public class BoolEditor extends PropertyEditorSupport implements ExPropertyEditor {
+ String[] stringValues = null;
+ /** Creates a new instance of BoolEditor */
+ public BoolEditor() {
+ }
+
+ public void attachEnv(org.openide.explorer.propertysheet.PropertyEnv env) {
+ stringValues = (String[]) env.getFeatureDescriptor().getValue(
+ "stringValues"); //NOI18N
+ }
+
+ private String getStringRep (boolean val) {
+ if (stringValues != null) {
+ return stringValues [val ? 0 : 1];
+ }
+ String result;
+ if (val) {
+ result = NbBundle.getMessage(BoolEditor.class, "TRUE"); //NOI18N
+ } else {
+ result = NbBundle.getMessage(BoolEditor.class, "FALSE"); //NOI18N
+ }
+ return result;
+ }
+
+ /** Returns Boolean.TRUE, Boolean.FALSE or null in the case of an
+ * unrecognized string. */
+ private Boolean stringVal (String val) {
+ String valToTest = val.trim().toUpperCase();
+ String test = getStringRep (true).toUpperCase();
+ if (test.equals(valToTest)) return Boolean.TRUE;
+ test = getStringRep (false).toUpperCase();
+ if (test.equals(valToTest)) return Boolean.FALSE;
+ return null;
+ }
+
+ public String getJavaInitializationString() {
+ Boolean val = (Boolean) getValue();
+ if (val == null) return "null"; //NOI18N
+ return Boolean.TRUE.equals (getValue()) ? "true" : "false"; //NOI18N
+ }
+
+ public String[] getTags () {
+ return new String[] {
+ getStringRep (true), getStringRep (false)
+ };
+ }
+
+ public String getAsText() {
+ Boolean val = (Boolean) getValue();
+ if (val == null) return NbBundle.getMessage (BoolEditor.class, "NULL");
+ return getStringRep (Boolean.TRUE.equals (getValue()));
+ }
+
+ public void setAsText(String txt) {
+ Boolean val = stringVal (txt);
+ boolean newVal = val == null ? false : val.booleanValue();
+ setValue (newVal ? Boolean.TRUE : Boolean.FALSE);
+ }
+
+ static BoolEditor ed = new BoolEditor();
+ public static void main (String[] args) {
+ Boolean val = Boolean.TRUE;
+ ed.setValue (val);
+ checkValue (val.booleanValue());
+ val = Boolean.FALSE;
+ ed.setValue (val);
+ checkValue (val.booleanValue());
+ }
+
+ public static boolean checkValue (boolean val) {
+ return val == ((Boolean) ed.getValue()).booleanValue();
+ }
+}
Index: core/test/unit/src/org/openide/explorer/propertysheet/EnvProvider.java
===================================================================
RCS file: core/test/unit/src/org/openide/explorer/propertysheet/EnvProvider.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ core/test/unit/src/org/openide/explorer/propertysheet/EnvProvider.java 10 Mar 2003 17:12:40 -0000
@@ -0,0 +1,26 @@
+/*
+ * EnvProvider.java
+ *
+ * Created on March 10, 2003, 4:14 PM
+ */
+
+package org.openide.explorer.propertysheet;
+import org.openide.nodes.Node.Property;
+/** Trivial class to manufacture PropertyEnv instances without having
+ *to create visual components.
+ *
+ * @author Tim Boudreau
+ */
+public class EnvProvider {
+
+ /** Creates a new instance of EnvProvider */
+ public EnvProvider() {
+ }
+
+ public static final PropertyEnv getEnv (Property p) {
+ PropertyEnv result = new PropertyEnv();
+ result.setFeatureDescriptor (p);
+ return result;
+ }
+
+}
Index: core/test/unit/src/org/netbeans/beaninfo/editors/IntEditorTest.java
===================================================================
RCS file: core/test/unit/src/org/netbeans/beaninfo/editors/IntEditorTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ core/test/unit/src/org/netbeans/beaninfo/editors/IntEditorTest.java 10 Mar 2003 17:12:40 -0000
@@ -0,0 +1,186 @@
+/*
+ * IntEditorTest.java
+ * JUnit based test
+ *
+ * Created on March 10, 2003, 1:10 PM
+ */
+
+package org.netbeans.beaninfo.editors;
+
+import java.beans.*;
+import junit.framework.*;
+import org.openide.explorer.propertysheet.*;
+import org.openide.nodes.*;
+import org.openide.util.NbBundle;
+import java.util.Arrays;
+
+/** Unit test for priimitive int property editor
+ *
+ * @author Tim Boudreau
+ */
+public class IntEditorTest extends TestCase {
+ String[] goodKeys = new String[] {"zero","one","two","three"};
+ int[] goodValues = new int[] {0,1,2,3};
+ String [] goodCode = new String[] {"0", "0+1", "1+1", "1+2"};
+
+ IntProp goodProp = new IntProp (0, goodKeys, goodValues, goodCode);
+
+ String[] badKeys = new String[] {"blah"};
+ int[] badValues = new int[] {1,15};
+
+ String[] emptyKeys = new String[0];
+
+ IntProp badMismatchProp = new IntProp (1, emptyKeys, goodValues, null);
+ IntProp badMismatchProp2 = new IntProp (1, goodKeys, goodValues, emptyKeys);
+
+ public IntEditorTest(java.lang.String testName) {
+ super(testName);
+
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(IntEditorTest.class);
+ return suite;
+ }
+
+ /** Test of attachEnv method, of class org.netbeans.beaninfo.editors.IntEditor. */
+ public void testAttachEnv() {
+ System.out.println("testAttachEnv");
+ PropertyEditor ped = goodProp.getPropertyEditor();
+ if (!(ped instanceof ExPropertyEditor)) {
+ fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName());
+ } else {
+ ExPropertyEditor ex = (ExPropertyEditor) ped;
+ prepareEnv (goodProp, ex, false);
+ }
+
+ ped = badMismatchProp.getPropertyEditor();
+ if (!(ped instanceof ExPropertyEditor)) {
+ fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName());
+ } else {
+ ExPropertyEditor ex = (ExPropertyEditor) ped;
+ prepareEnv (badMismatchProp, ex, true);
+ }
+
+ ped = badMismatchProp2.getPropertyEditor();
+ if (!(ped instanceof ExPropertyEditor)) {
+ fail ("Not an instance of ExPropertyEditor: " + ped.getClass().getName());
+ } else {
+ ExPropertyEditor ex = (ExPropertyEditor) ped;
+ prepareEnv (badMismatchProp2, ex, true);
+ }
+ }
+
+ /** Test of getAsText method, of class org.netbeans.beaninfo.editors.IntEditor. */
+ public void testGetAsText() {
+ System.out.println("testGetAsText");
+
+ // Add your test code below by replacing the default call to fail.
+ ExPropertyEditor ped = getConfiguredEditor (goodProp);
+ ped.setValue (new Integer(0));
+ String s = ped.getAsText();
+ if ("zero".equals(s)) return;
+ }
+
+ /** Test of setAsText method, of class org.netbeans.beaninfo.editors.IntEditor. */
+ public void testSetAsText() {
+ System.out.println("testSetAsText");
+ ExPropertyEditor ped = getConfiguredEditor (goodProp);
+ ped.setAsText ("two");
+ int i = ((Integer) ped.getValue()).intValue();
+ if (i != 2) {
+ fail ("Set text value of propertyeditor to \"two\" but getValue() returns " + i);
+ }
+ }
+
+ /** Test of getTags method, of class org.netbeans.beaninfo.editors.IntEditor. */
+ public void testGetTags() {
+ System.out.println("testGetTags");
+ ExPropertyEditor ped = getConfiguredEditor (goodProp);
+ String[] s = ped.getTags();
+ if (s.length != 4) fail ("Keys length should be 4 but is " + s.length);
+ if ("zero".equals(s[0])) return;
+ fail ("First key should be \"zero\" but is " + s[0]);
+ }
+
+ /** Test of getJavaInitializationString method, of class org.netbeans.beaninfo.editors.IntEditor. */
+ public void testGetJavaInitializationString() {
+ System.out.println("testGetJavaInitializationString");
+ ExPropertyEditor ped = getConfiguredEditor (goodProp);
+ ped.setValue (new Integer(3));
+ String s = ped.getJavaInitializationString();
+ if (!("1+2".equals (s))) {
+ fail ("GetJavaInitializationString returns " + s + " but should return \"1+1\"");
+ }
+ }
+
+ private ExPropertyEditor getConfiguredEditor (IntProp p) {
+ ExPropertyEditor result = (ExPropertyEditor) p.getPropertyEditor();
+ try {
+ result.setValue ((Integer) p.getValue());
+ prepareEnv (p, result, false);
+ } catch (Exception e) {
+ //Other tests will fail if an exception is thrown, ignore
+ }
+ return result;
+ }
+
+ private void prepareEnv (IntProp prop, ExPropertyEditor pe, boolean shouldFail) {
+ PropertyEnv env = org.openide.explorer.propertysheet.EnvProvider.getEnv(prop);
+ org.netbeans.beaninfo.editors.IntEditor.EnvException e=null;
+ try {
+ pe.attachEnv (env);
+
+ } catch (org.netbeans.beaninfo.editors.IntEditor.EnvException ie) {
+ e = ie;
+ } finally {
+ if (shouldFail && e == null) {
+ fail ("EnvException should have been thrown but wasn't.");
+ }
+ }
+ }
+
+ private class IntProp extends Node.Property {
+ String[] k;
+ int[] v;
+ String[] c;
+ int val;
+ public IntProp (int val, String[] keys, int[] vals, String[] codeVals) {
+ super (Integer.TYPE);
+ this.val = val;
+ v = vals;
+ k = keys;
+ c = codeVals;
+ }
+
+ public Object getValue (String key) {
+ if ("stringKeys".equals(key)) return k;
+ if ("intValues".equals(key)) return v;
+ if ("codeValues".equals(key)) return c;
+ Object result = super.getValue(key);
+ return result;
+ }
+
+ public boolean canRead() {
+ return true;
+ }
+
+ public boolean canWrite() {
+ return true;
+ }
+
+ public Object getValue() throws IllegalAccessException, java.lang.reflect.InvocationTargetException {
+ return new Integer(val);
+ }
+
+ public void setValue(Object val) throws IllegalAccessException, IllegalArgumentException, java.lang.reflect.InvocationTargetException {
+ this.val = ((Integer) val).intValue();
+ }
+
+ public PropertyEditor getPropertyEditor () {
+ return new org.netbeans.beaninfo.editors.IntEditor ();
+ }
+
+ }
+
+}
Index: openide/api/doc/org/openide/explorer/doc-files/api.html
===================================================================
RCS file: /cvs/openide/api/doc/org/openide/explorer/doc-files/api.html,v
--- openide/api/doc/org/openide/explorer/doc-files/api.html 26 Feb 2003 06:09:40 -0000 1.32
+++ openide/api/doc/org/openide/explorer/doc-files/api.html 10 Mar 2003 17:12:40 -0000
@@ -742,6 +742,56 @@
org.openide.util.Lookup
A lookup to use to query for results.
+
+
+ java.lang.String
+ instructions
+ String
+ Should contain localized instructions to the user, to be displayed in the
+ String custom editor
+
+
+ oneline
+ Boolean
+ Instructs the property editor that the custom editor should present
+ a one-line text control (JTextField or equivalent) rather than
+ a multi-line text control (JTextArea or equivalent).
+
+
+
+ suppressCustomEditor
+ Boolean
+ Instructs the property editor to suppress the custom editor
+ button on the property sheet.
+
+
+ java.lang.Integer and primitive integer
+ stringKeys
+ String[]
+ Should contain internationalized strings that should be used as tags in
+ a combo box. Number of elements must be >= 1. If this hint is
+ supplied, the hint
+ intKeyValues
must also be supplied
+
+
+ intValues
+ int[]
+ The values the string keys correspond to. Must have the same number
+ of elements as the string array
+
+
+ codeValues
+ String[]
+ Values to use in
+ getJavaInitializationString()
in place
+ of hardcoding numbers (useful for constants)
+ java.lang.Boolean
+ stringValues
+ String[]
+ Should contain an array of two strings to be used for user
+ displayable text for the boolean values, in the order
+ false, true - for example,
new String[] {"off","on"}
. Custom parameters that are not tied with particular editor