/*
* UFO Saved Game Editor
* Copyright (C) 2010 Christopher Davoren
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.rubikscomplex.ufosge.gui;
import java.awt.Toolkit;
import java.text.ParseException;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.text.NumberFormatter;
import net.rubikscomplex.ufosge.util.Util;
/**
*
* @author Dav
*/
public class NumericInputVerifier extends InputVerifier {
long maxValue;
long minValue;
public NumericInputVerifier(long minValue, long maxValue) {
super();
this.maxValue = maxValue;
this.minValue = minValue;
}
public boolean verify(JComponent input) {
JFormattedTextField component = (JFormattedTextField)input;
String text = component.getText();
// System.out.println("Text: " + component.getText());
long num;
if (component.getFormatter() != null) {
NumberFormatter formatter = (NumberFormatter)component.getFormatter();
try {
num = ((Number)formatter.stringToValue(text)).longValue();
}
catch (ParseException e) {
Util.displayTooltip(input);
Toolkit.getDefaultToolkit().beep();
return false;
}
}
else {
try {
num = Long.parseLong(text);
}
catch (NumberFormatException e) {
Util.displayTooltip(input);
Toolkit.getDefaultToolkit().beep();
return false;
}
}
if (num < minValue || num > maxValue) {
// JOptionPane.showMessageDialog(null, "Value must be between " + formatter.valueToString(minValue) + " and " + formatter.valueToString(maxValue));
Util.displayTooltip(input);
Toolkit.getDefaultToolkit().beep();
return false;
}
return true;
}
}