Merge pull request #1797 from SiboVG/issue-1794

[#1794] Clamp IntegerModel to min and max on set
This commit is contained in:
Sibo Van Gool 2022-11-07 21:20:44 +01:00 committed by GitHub
commit 009cbbe17c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@ import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.sf.openrocket.util.MathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -210,9 +211,16 @@ public class IntegerModel implements StateChangeListener {
* Sets the value of the variable.
*/
public void setValue(int v) {
int clampedValue = MathUtil.clamp(v, minValue, maxValue);
if (clampedValue != v) {
log.debug("Clamped value " + v + " to " + clampedValue + " for " + this);
v = clampedValue;
}
log.debug("Setting value " + v + " for " + this);
try {
setMethod.invoke(source, v);
fireStateChanged();
} catch (IllegalArgumentException e) {
throw new BugException(e);
} catch (IllegalAccessException e) {