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

[5753 byte] By [TheFida] at [2007-11-27 2:40:21]
# 1

I can't believe no one in the JavaBeans news group has ever implemented an enum property. Am I in the wrong forum?

I've looked at all the tutorials and samples I can find and cannot seem to grok the method in this madness. I've searched high and low for how the existing Swing beans implement their enum properties but I guess the source for them isn't available?

An example of what I am trying to do would be the JLabel horizontalAlignment property.

Can anyone point me to an example of how to implement an enum property for a JavaBean?

Please?

With respect and thanks,

Mike

TheFida at 2007-7-12 3:03:10 > top of Java-index,Desktop,Developing for the Desktop...
# 2

Strictly speaking, I think you're out of luck. A PropertyEditor may only return an Object from its getValue() method, so that means that ultimately you would have to return an Integer, not an int.

Having said that, and assuming that you have some code somewhere to wrap/unwrap primitives and their Object types, here are some tips towards helping you do what you want.

Let's just assume for the moment that we're going to talk about PropertyEditors that don't have custom editors, and that aren't paintable.

Such a PropertyEditor has to do the following things:

It needs to be able to take in a String and turn it into an Object of the right kind.

It needs to be able to derive a String representation of an Object of the right kind from such an Object.

Finally, usually PropertyEditors are defined for particular classes (like Boolean, Color, Class, etc.).

So in your case if I'm reading you right you want something that is capable of turning "LEFT" into SwingConstants.LEFT, "RIGHT" into SwingConstants.RIGHT and so on.

So let's start with the getAsText() method:

final Object value = this.getValue();

// TODO: what if getValue() returns null? What String do you want to return? ""? Let's assume value is not null for this exercise.

if (value instanceof Number) {

return value.toString();

} else {

// this is probably an error condition

}

return null;

The corresponding setAsText() method:

final String[] tags = this.getTags();

if (tags != null && tags.length > 0) {

for (int i = 0; i < tags.length; i++) {

final String tag = tags[i];

if (tag != null && tag.equals(text)) {

// Valid text input.

final Integer number = (Integer)someInternalHashMap.get(text);

this.setValue(number);

return;

}

}

}

throw new IllegalArgumentException("Unrecognized text: " + text);

That's all I have time for at the moment; I'll post more later if you still need more help. I also coded this here in this pathetic text editor, so I'm sure it breaks, but hopefully it will get you going in the right direction.

Laird

ljnelsona at 2007-7-12 3:03:11 > top of Java-index,Desktop,Developing for the Desktop...
# 3

Sorry, I didn't look closely enough at what you did. You've obviously got the gist. My apologies for inadvertently patronizing.

I don't see anything obviously wrong (from a quick glance) with what you're doing. In general, the get and setValue() calls are built in to PropertyEditorSupport, complete with property change firings, so instead of maintaining your own instance variable for the Integer, I'd simply use the getters and setters whenever you need to get it or set it. I suspect that someone is not getting notified in the way that you think they are, and that the PropertyChangeEvents are responsible for the ultimate problem.

Good luck,

Laird

ljnelsona at 2007-7-12 3:03:11 > top of Java-index,Desktop,Developing for the Desktop...
# 4

Sorry, yes, I did notice something you're doing wrong.

You mentioned that the problem occurs when you're using this PropertyEditor to actually tweak a form? From the problem, it looks like the IDE is using the return value of your PropertyEditor's getJavaInitializationString, which, in your implementation, is always going to return the same thing (Left).

What you want to do here is return a Java string that reflects the current state of the editor. So your getJavaInitializationString() method shouldn't just return Left, but should return:

"new Integer(" + ((Integer)this.getValue()).intValue() + ")"

Hope that helps.

Laird

ljnelsona at 2007-7-12 3:03:11 > top of Java-index,Desktop,Developing for the Desktop...
# 5

It was the getJavaInitializationString() implementation!

I changed it to:

public String getJavaInitializationString() {

return "SwingConstants."+getAsText();

}

...and it worked exactly as I needed.

THANK YOU!!!!

Cheers,

Mike

TheFida at 2007-7-12 3:03:11 > top of Java-index,Desktop,Developing for the Desktop...
# 6
P.S. I also changed the string constants to all uppercase.
TheFida at 2007-7-12 3:03:11 > top of Java-index,Desktop,Developing for the Desktop...