SDSU CS 696 Emerging Technologies: Distributed Objects
Spring Semester, 1998
Bean Property Editors & Persistence

To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 07-May-98

Contents of Doc 34, Bean Property Editors & Persistence

  1. Property Editors
    1. Finding Property Editors
    2. Example - PropertyEditor for a Type
    3. Example - Editor for individual Property
  2. Special Editors
  3. Customizers
  4. Persistence
CS 696 Doc 34 Bean Property Editors & Persistence


References

Java in a Nutshell: A Desktop Quick Reference, 2nd Ed. David Flanagan, O'Reilly, 1997, Chapter 10 Java Beans pp. 178-200

Developing Java Beans, Englander, O'Reilly, 1997, chapters 9-10, pp. 191-252


Doc 34, Bean Property Editors & Persistence Slide # 2

Property Editors

Finding Property Editors


The steps in order taken by a builder tool to find a property editor for a bean's property

1. BeanInfo class

In the BeanInfo class for the bean a PropertyDescriptor for the Property can specify the PropertyEditor for the property

This is done on the property level

2. java.beans.PropertyEditorManager.register
public static void registerEditor(Class targetType, Class editorClass)

You can use this method to register a PropertyEditor for all properties that return targetType objects

3. Naming Convention

The fully qualified class name is appended with Editor and look for that class

For example beanExamples.School becomes beanExample.SchoolEditor


Doc 34, Bean Property Editors & Persistence Slide # 3

4. Use the default packages

Take the class name (without the package), append "Editor" and prepend the default package names and look for that class

Use java.beans.PropertyEditorManager.setEditorSearchPath to change the packages
public static void setEditorSearchPath(String path[])

Change the list of package names that will be used for finding property editors



Doc 34, Bean Property Editors & Persistence Slide # 4

Example - PropertyEditor for a Type

package beanExamples;
import java.beans.*;
import java.io.Serializable;

public class SchoolReporter implements TimeAdvancedListener,  Serializable
   {
   School name = new School( School.SDSU);
   int count = 0;
      
   public void setName( School newName) 
      {
      School oldName = name;
      name = newName;

      //Just to show that property change
      System.out.println( "Old: " + oldName +
                        " New: " + name );
      }
   
   public School getName() { return name; }
      
   public void timeAdvanced( TimeAdvancedEvent
          timeEvent )
      {
      report();
      }
      
   public void report()
      {
      System.out.println( "Report number: " + (count++)  + 
         " from " + name );
      }
   }


Doc 34, Bean Property Editors & Persistence Slide # 5
package beanExamples;
import java.beans.*;
import java.io.Serializable;

public class School implements Serializable
   {
   public static final int SDSU = 1;
   public static final int USD = 2;
   public static final int UCSD = 3;
   
   private int aSchool;
   
   public School( int id )
      {
      aSchool = id;
      }
   
   public int getType()
      {
      return aSchool;
      }
   public String getName()
      {
      switch ( aSchool )
         {
         case 1:   return "SDSU";
         case 2:   return "USD";
         case 3:   return "UCSD";
         default:   return "Not Known";
         }
         
      }
   }


Doc 34, Bean Property Editors & Persistence Slide # 6
 package beanExamples;
import java.beans.*;

public class SchoolEditor extends PropertyEditorSupport
   {
   
   public String[] getTags()
      {
      return new String[] { "SDSU", "USD", "UCSD" };
      }
   
   public void setAsText( String input )
      {
      if ( input.equals( "SDSU" ) )
         setValue( new School( School.SDSU ));
      else if ( input.equals( "USD" ) )
         setValue( new School( School.USD ));
      else if  ( input.equals( "UCSD" ) )
         setValue( new School( School.UCSD ));
      else 
         throw new IllegalArgumentException( input );
      }
   
   public String getJavaInitializationString()
      {
      School value =  (School) getValue();
      int type = value.getType();
      
      switch ( type )
         {
         default:
         case 1: return "new School( School.SDSU )";
         case  2: return "new School( School.USD )";
         case 3: return "new School( School.UCSD )";
         }
      }
   }


Doc 34, Bean Property Editors & Persistence Slide # 7

Example - Editor for individual Property

package beanExamples;
import java.beans.*;
import java.io.Serializable;

public class SchoolReporter implements TimeAdvancedListener,  Serializable
   {
   String name = "SDSU";
   int count = 0;
      
   public void setName( String newName) 
      {
      String oldName = name;
      
      name = newName;
      System.out.println( "New name: " + name + " old " + oldName );
      }
   
   public String getName()   { return name; }
      
   public void timeAdvanced( TimeAdvancedEvent timeEvent )
      {
      report();
      }
      
   public void report()
      {
      System.out.println( "Report number: " + (count++)  + 
         " from " + name );
      }
   }


Doc 34, Bean Property Editors & Persistence Slide # 8

package beanExamples;
import java.beans.*;

public class SchoolEditor extends PropertyEditorSupport
   {
   
   public String[] getTags()
      {
      return new String[] { "SDSU", "USD", "UCSD" };
      }
   
   public void setAsText( String input )
      {
      setValue( input );
      }
   
   public String getJavaInitializationString()
      {
      String school = (String) getValue();
      
      if (school.equals( "SDSU" ))   return "SDSU";
      else if  (school.equals( "UCSD" ))   return "UCSD";
      else return "USD";
      }
   }


Doc 34, Bean Property Editors & Persistence Slide # 9
 package beanExamples;
import java.beans.*;

public class SchoolReporterBeanInfo extends SimpleBeanInfo
   {
   
   public PropertyDescriptor[] getPropertyDescriptors()
      {
      try
         {
         PropertyDescriptor nameProperty = 
            new PropertyDescriptor( "name", SchoolReporter.class );
         nameProperty.setDisplayName( "Reporter's School");

         nameProperty.setPropertyEditorClass( SchoolEditor.class );
         nameProperty.setShortDescription(
            "This is the name of your reporter");
         
         PropertyDescriptor[] allProperties = { nameProperty };
         return allProperties;
         }
      catch ( IntrospectionException error )
         {
         return super.getPropertyDescriptors();
         }
      }
   }


Doc 34, Bean Property Editors & Persistence Slide # 10

Special Editors


You can create your own AWT component to be used to edit a property

Subclass java.beans.PropertyEditorSupport

Have the method supportsCustomEditor() return true

Have the method getCustomEditor() return a subclass of java.awt.Component that you want to edit the Property

The Component uses the getValue and setValue of your subclass of PropertyEditorSupport to change the values of property

Customizers


Customizers are used to customize the entire bean, not just a property

Customizers are not limited to customizing properties

You must use the BeanInfo to use a customizer

There is no "design pattern" for Customizers

You need to use the BeanDescriptor constructor to specify the customizer class

public BeanDescriptor(Class beanClass, Class customizerClass)


Doc 34, Bean Property Editors & Persistence Slide # 11

Persistence


Persistence occurs at two levels in the bean world
The contents of the BeanBox can be stored on disk for later reuse
Individual objects can be stored on disk for use in the BeanBox


Builder tools have two possible ways to store the contents of the BeanBox
Use Serialization
Store code to regenerate the objects in the BeanBox


Doc 34, Bean Property Editors & Persistence Slide # 12
Persistent Objects

To use persistent objects in the BeanBox

Save the object in a file (You can do this with the BeanBox)

The file name should end in ".ser"

Add the X.ser file to the Bean's jar file

The persistent object can be instantiated in one of two ways:

1. If the X.ser file is in the same directory as the X.class file then when the BeanBox needs to create a Bean of type X is will serialize the object from X.ser
This means that X must be a name of a Class

2. Use the method java.beans.Beans.instantiate
public static Object instantiate(ClassLoader cls, 
   String beanName) throws IOException, ClassNotFoundException

If cls is null use the default class loader

beanName - the name of the bean within the class-loader. For example "sun.beanbox.foobah"

The ".ser" will be appended to the beanName

If the ".ser" file can not be found, ".class" will be appended to the beanName and that class will be used to instantiate a bean

Note the beanName does not have to be the name of a class

Copyright © 1998 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
All rights reserved.

visitors since 07-May-98