package de.fhswf.in.inf.se.notepadMinusMinus.util; import javafx.util.StringConverter; /** * An {@link Integer} converter that checks the boundaries. * * @author Stefan Suhren * @version 1.0 */ public class OverLimitIntegerStringConverter extends StringConverter { private int minAllowedValue; /** * Create and {@link OverLimitIntegerStringConverter} that uses the given * limit. * * @param minAllowedValue * The smallest values that is still valid. */ public OverLimitIntegerStringConverter(int minAllowedValue) { this.minAllowedValue = minAllowedValue; } /* * (non-Javadoc) * * @see javafx.util.StringConverter#toString(java.lang.Object) */ @Override public String toString(Integer object) { // If the specified value is null or not positive, return a zero-length // String if (object == null || object.intValue() < minAllowedValue) { return ""; } return (Integer.toString(object.intValue())); } /* * (non-Javadoc) * * @see javafx.util.StringConverter#fromString(java.lang.String) */ @Override public Integer fromString(String string) { // If the specified value is null or zero-length, return null if (string == null) { return null; } string = string.trim(); if (string.length() < 1) { return null; } // If the specified value is 0 or negative, return null Integer object = Integer.valueOf(string); if (object.intValue() < minAllowedValue) { return null; } return object; } }