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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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í.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}