Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Custom Float PropertyEditor (Part 2)

DZone's Guide to

A Custom Float PropertyEditor (Part 2)

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In my last post I showed how to set a JSpinner as PropertyEditor for float properties and how to reuse it for different needs. Now I will further extend it to make the users' life a bit easier (yes, now and then, we should think about them ;-)). Let's keep our previous example of a 3D "bodies in space" application. So far, you can already set its dimensions and coordinates all around your universe. Now you also want to rotate it, say, on the XY plane (assuming Z-axis points upwards). How to do it is beyond this post; suffice to say that it (often) involves heavy trigonometrical calculations (and there are many libraries that handle it, if I may add). Anyway, these math libraries (e.g. java.lang.Math) commonly ask radians as parameters for their trigonometrical functions (and your application should do as well). However, again, think of the user who has to write "0.78539816339744830961566084581988" just to rotate 45º clockwise a bloody couch in a new living room plan. Not very likely I guess.

A angle property editor, along with the couch rotated 45º clockwise

Let's now set a PropertyEditor so the user can rotate a couch in a more user-friendly way. Extend the class FloatPropertyEditor and override the functions getAsText() and setAsText(), as well as the class FloatInPlaceEditor with the functions getValue() and setValue(). These are where the degrees to radians and viceversa conversion takes place.

public class AngleFloatPropertyEditor extends FloatPropertyEditor {

public AngleFloatPropertyEditor() {
super(new SpinnerNumberModel(0.0f, 0.0f, 360.0f, 1.0f));
}

public AngleFloatPropertyEditor(SpinnerNumberModel model) {
super(model);
}

@Override
public InplaceEditor getInplaceEditor() {
return new FloatInplaceEditor(model){

@Override
public Object getValue() {
Float f = (Float)super.getValue();
if(f == null)
f = Float.valueOf(0.0f);
return new Float(Math.toRadians(f));
}

@Override
public void setValue(Object object) {
Float f = (Float)object;
if(f == null)
f = Float.valueOf(0.0f);
super.setValue(new Float(Math.toDegrees((Float)object)));
}

};
}

@Override
public String getAsText() {
Float f = (Float)getValue();
if (f == null) {
return "0.00";
}
return NumberFormat.getNumberInstance().format(Math.toDegrees(f.doubleValue()));
}

@Override
public void setAsText(String s) {
try {
setValue(new Float(Math.toRadians(NumberFormat.getNumberInstance().parse(s).doubleValue())));
} catch (ParseException ex) {
setValue(Float.valueOf(0.0f));
}
}

}

You can now use radians internally, while your user can enter (and read) the angles in degrees. As always, you can extend the above code to fit your needs, like add a degree sign at the end. In a next post, I will show another way to extend FloatPropertyEditor to edit a different data type.

Thank  you very much if you've read this far.

Muchas gracias por leer hasta aquí.

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}