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

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

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

}
}

 

 

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}