
nl.cloudfarming.client.util.swing.beanbinding.DoubleConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of util-swing Show documentation
Show all versions of util-swing Show documentation
AgroSense util swing - Swing specific utility classes
The newest version!
/**
* Copyright (C) 2008-2013 LimeTri. All rights reserved.
*
* AgroSense is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
* this software, see the FLOSS License Exception
* .
*
* AgroSense is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AgroSense. If not, see .
*/
package nl.cloudfarming.client.util.swing.beanbinding;
import java.text.DecimalFormat;
import org.jdesktop.beansbinding.Converter;
/**
* Converts String to double If String cannot be converted set's the value to
* {@link Double#MIN_VALUE} This is used by the {@link DoubleValidator}
*
* Works in conjunction with {@link DoubleConverter}
*
* Usage:
*
* yourBinding.setConverter(new {@link DoubleConverter}());
* yourBinding.setValidator(new {@link DoubleValidator}());
*
* @author Merijn Zengers
*/
public class DoubleConverter extends Converter {
private Integer numberOfFractions;
public DoubleConverter() {
}
/**
* Constructs a Double converter with the number of digits after the
* separator specified. Will use this in the convertForward
*
* @param numberOfFractions
*/
public DoubleConverter(Integer numberOfFractions) {
this.numberOfFractions = numberOfFractions;
}
/**
* Convert a Double to a String
*
* @param arg
* @return
*/
@Override
public String convertForward(Double arg) {
if (numberOfFractions != null) {
DecimalFormat format = new DecimalFormat();
format.setMaximumFractionDigits(numberOfFractions);
return format.format(arg);
}
return String.valueOf(arg);
}
/**
* Convert a String to a {@link Double}
*
* If not able to convert the string to {@link Double} will return {@link Double#MIN_VALUE}
*
* @param arg
* @return converted String value or {@link Double#MIN_VALUE} if a {@link NumberFormatException}
* occurs
*/
@Override
public Double convertReverse(String arg) {
double value;
try {
value = (arg == null || arg.isEmpty()) ? 0 : Double.parseDouble(arg);
} catch (NumberFormatException ex) {
value = Double.MIN_VALUE;
}
return value;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy