Tag-Based Editor for the Person Bean

This topic discusses classes found in the com.ibm.ivj.examples.vc.propertyeditors package. You can try this editor from either PersonTester or PaintTester.

The incomeRange property has a tag-based editor class associated with it. This editor extends java.beans.PropertyEditorSupport, a concrete implementation class for the java.beans.PropertyEditor interface. As a result, setValue( ) does not have to be implemented locally.

public class IncomeRangeEditor extends java.beans.PropertyEditorSupport {
   String[] stringValues = {
      "0 - 20,000",
      "20,000 - 50,000",
      "50,000 - 100,000",
      "100,000+" };
   int[] intValues = {1, 2, 3, 4};
   String[] codeGenStrings = {
      "propertyeditors.Person.belowTwenty",
      "propertyeditors.Person.twentyToFifty",
      "propertyeditors.Person.fiftyToOneHundred",
      "propertyeditors.Person.aboveOneHundred"};
 
   public String getAsText() {
      for (int i=0; i< intValues.length; i++) {
         if (intValues[i] == ((Integer) getValue()).intValue())
            return stringValues[i];
      }
      return "";
   }
   public String getJavaInitializationString() {
      for (int i=0; i< intValues.length; i++) {
         if (intValues[i] == ((Integer) getValue()).intValue())
            return codeGenStrings[i];
      }
      return "0";
   }
   public String[] getTags() {
      return stringValues;
   }
   public void setAsText(String text) throws java.lang.IllegalArgumentException {
      for (int i=0; i< stringValues.length; i++) {
         if (stringValues[i].equals(text)) {
            setValue(new Integer(intValues[i]));
            return;
         }
      }
      throw new java.lang.IllegalArgumentException(text);
   }
}

The getTags( ) method holds allowable property values in an array. IncomeRangeEditor uses a set of parallel arrays to manage the allowable income ranges. Note that the sole purpose of the codeGenStrings array is to address incomeRange constants in the Person bean through the getJavaInitializationString( ) method. The return value from this method appears in the getPerson1( ) method of the PersonTester class:

private Person getPerson1() {
	if (ivjPerson1 == null) {
		try {
			ivjPerson1 = new PropertyEditors.Person();
			ivjPerson1.setSex("Female");
			ivjPerson1.setName(new PropertyEditors.Name("Mrs.", "Susan", "Gail", "Carpenter"));
			ivjPerson1.setPhoneNumber("555-1212");
			ivjPerson1.setIncomeRange(propertyeditors.Person.belowTwenty);
			// user code begin {1}
			// user code end
		} catch (java.lang.Throwable ivjExc) {
			// user code begin {2}
			// user code end
			handleException(ivjExc);
		}
	};
	return ivjPerson1;
}

Related references
Property Editor Examples
Text-Based Editor for the Person Bean
Custom Editor for the Person Bean
Paintable Editor for the Person Bean