SFB Property Editors

The easy way to implement text based Java Property Editors.

The tiny SFB Property Editors library helps you in two ways:

Use a Mapper for Creating a Property Editor

Start with implementing the interface com.github.stefanbirkner.editors.mapper.Mapper<T>. This interface is parameterized with the type that is mapped and it has two methods: getTextForValue(T value) and getValueForText(String text). The first one returns the text for an object and the second returns an object based on the text. The following example shows a Mapper for Longs.

class LongMapper implements Mapper<Long> {
  @Override
  public String getTextForValue(Long value) {
    if (value == null)
      return "";
    else
      return value.toString();
  }

  @Override
  public Long getValueForText(String text) {
    if ((text == null) || text.isEmpty())
      return null;
    else
      return Long.parseLong(text);
  }
}

The new mapper can be used to create a property editor. You just need to create an instance of com.github.stefanbirkner.editors.SfbPropertyEditorSupport<T>. Listener support and storing of the value is already done by SfbPropertyEditorSupport.

PropertyEditor editor = new SfbPropertyEditorSupport<Long>(Long.class, new LongMapper());

Maybe you want to create an dedicated property editor for java.lang.Long:

public class LongPropertyEditor extends SfbPropertyEditorSupport<Long> {
  public LongPropertyEditor() {
    super(Long.class, new LongMapper());
  }
}

Author

Stefan Birkner (mail@stefan-birkner.de)