Enum properties
I am trying to learn how to create a bean with an enum property. As an exercise, I have implemented the following...
/*
* TitleHorizontalAlignmentEditor.java
*
* Created on April 23, 2007, 3:53 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.iws.EPIBuilder.beans;
import java.beans.PropertyEditorSupport;
import javax.swing.*;
/**
*
* @author MFidler
*/
publicclass TitleHorizontalAlignmentEditorextendsPropertyEditorSupport
implements SwingConstants
{
privatestatic String CENTER_STR="Center";
privatestatic String LEADING_STR="Leading";
privatestatic String LEFT_STR="Left";
privatestatic String RIGHT_STR="Right";
privatestatic String TRAILING_STR ="Trailing";
privatestatic String[] Tags=new String[]{ CENTER_STR
, LEADING_STR
, LEFT_STR
, RIGHT_STR
, TRAILING_STR
};
protected Integer HorizAlign =new Integer(LEFT);
publicvoid setValue(Object o){
HorizAlign = (Integer)o;
firePropertyChange();
}
public Object getValue(){
return HorizAlign;
}
public String getJavaInitializationString(){
return LEFT_STR;
}
public String getAsText(){
String sResult ="";
switch (HorizAlign.intValue()){
case CENTER:sResult = CENTER_STR;break;
case LEADING:sResult = LEADING_STR;break;
case LEFT:sResult = LEFT_STR;break;
case RIGHT:sResult = RIGHT_STR;break;
case TRAILING:sResult = TRAILING_STR;break;
}
return sResult;
}
publicvoid setAsText(String val)throws IllegalArgumentException{
if (CENTER_STR.equalsIgnoreCase(val))HorizAlign =new Integer(CENTER);
elseif (LEADING_STR.equalsIgnoreCase(val)) HorizAlign =new Integer(LEADING);
elseif (LEFT_STR.equalsIgnoreCase(val))HorizAlign =new Integer(LEFT);
elseif (RIGHT_STR.equalsIgnoreCase(val))HorizAlign =new Integer(RIGHT);
elseif (TRAILING_STR.equalsIgnoreCase(val)) HorizAlign =new Integer(TRAILING);
else
thrownew IllegalArgumentException();
firePropertyChange();
}
public String[] getTags(){
return Tags;
}
}
The JComboBox appears for the property in the property sheet and lists the tags fine. Everything is great... except when I try to compile the form I dropped the bean onto.
If I modify the titleHorizontalAlignment property off the default value, the IDE adds the following to the source (for example):myBean.setTitleHorizontalAlignment(Left);
Of course,Left means nothing. What ought to be there is:myBean.setTitleHorizontalAlignment(javax.swing.SwingConstants.LEFT);
There is something I'm missing about this whole thing... how to I bind the SwingConstants to the property?
TIA,
Mike

