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

A Custom Float PropertyEditor (Part 3)

DZone's Guide to

A Custom Float PropertyEditor (Part 3)

· 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 the first post of this series I showed how to create a custom property editor for float values. Now, I will show how to reuse this property editor for other numeric non-primitive types, such as BigDecimal. This also enables you to have a common user interface for numerical values.

The only thing needed is to wrap the BigDecimal into the float property editor, while taking care to convert back and forth, with some error handling too:

 Notice that there is no visual difference between the BigDecimal Editor...

BigDecimal JSpin PropertyEditor

...and the Float Editor:

 

Notice also that the upper editor has a value, while the lower has a null value. It's up to you to take care of it. In this example, I use the NullableSpinner and the NullableSpinnerNumberModel from SweetHome3d:

public class BigDecimalEditor extends FloatPropertyEditor {

public BigDecimalEditor() {
this(new NullableSpinnerNumberModel(0f, 0f, Float.MAX_VALUE, 0.01f));
}


public BigDecimalEditor(NullableSpinnerNumberModel model) {
super(model);
model.setNullable(true);
}

public BigDecimalEditor(Object source, NullableSpinnerNumberModel model) {
super(source, model);
model.setNullable(true);
}


@Override
public String getAsText() {
BigDecimal d = (BigDecimal) getValue();
if (d == null) {
return "(no value)";
}
return NumberFormat.getNumberInstance().format(d.floatValue());
}

@Override
public void setAsText(String s) {
try {
setValue(new BigDecimal(NumberFormat.getNumberInstance().parse(s).floatValue()));
} catch (ParseException ex) {
setValue(null);
}
}

@Override
public void attachEnv(PropertyEnv env) {
env.registerInplaceEditorFactory(this);
}

@Override
public InplaceEditor getInplaceEditor() {
if (ed == null) {
ed = new BigDecimalInplaceEditor(model);
}
return ed;
}

protected static class BigDecimalInplaceEditor extends FloatInplaceEditor{

public BigDecimalInplaceEditor(NullableSpinnerNumberModel model) {
super(model);
}

@Override
public Object getValue() {
Float result = (Float) BigDecimalInplaceEditor.super.getValue();
return new BigDecimal(result.floatValue());
}

@Override
public void setValue(Object object) {
Float value = null;
try {
value = new Float(((BigDecimal) object).floatValue());
} catch (NullPointerException e) {
value = null;
}
BigDecimalInplaceEditor.super.setValue(value);
}

@Override
public boolean supportsTextEntry() {
return true;
}

@Override
public void reset() {
BigDecimal d = (BigDecimal)getPropertyEditor().getValue();
setValue(d);
}

}
}

 

 

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 }}