Search in sources :

Example 1 with IntegerInterval

use of maspack.util.IntegerInterval in project artisynth_core by artisynth.

the class SetHandler method getNumericRange.

public static NumericInterval getNumericRange(Property prop) {
    NumericInterval nrange = null;
    PropertyInfo info = prop.getInfo();
    if (info.isReadOnly()) {
        return null;
    }
    Class<?> type = info.getValueClass();
    if (type == double.class || type == float.class || type == Double.class || type == Float.class) {
        Object value = prop.get();
        // i.e., if the value is not Void
        if (value instanceof Number) {
            double x = ((Number) value).doubleValue();
            if (info.hasRestrictedRange()) {
                Range range = prop.getRange();
                NumericInterval defaultRange = info.getDefaultNumericRange();
                if (range instanceof NumericInterval) {
                    nrange = new DoubleInterval((NumericInterval) range);
                    if (!nrange.isBounded()) {
                        if (defaultRange != null) {
                            nrange.intersect(defaultRange);
                        }
                    }
                    nrange = SliderRange.estimateBoundsIfNecessary(nrange, x);
                } else if (defaultRange != null) {
                    nrange = SliderRange.estimateBoundsIfNecessary(defaultRange, x);
                }
            }
            if (nrange == null) {
                // try to calculate range a from the value
                if (x == -1) {
                    nrange = new DoubleInterval(-1, 1);
                } else {
                    // interval is unbounded
                    nrange = new DoubleInterval();
                    nrange = SliderRange.estimateBoundsIfNecessary(nrange, x);
                }
            }
        }
    } else if (type == int.class || type == Integer.class) {
        Object value = prop.get();
        // i.e., if the value is not Void
        if (value instanceof Number) {
            int x = ((Number) value).intValue();
            if (info.hasRestrictedRange()) {
                Range range = prop.getRange();
                NumericInterval defaultRange = info.getDefaultNumericRange();
                if (range instanceof NumericInterval) {
                    nrange = new IntegerInterval((NumericInterval) range);
                    if (!nrange.isBounded()) {
                        if (defaultRange != null) {
                            nrange.intersect(defaultRange);
                        }
                    }
                    nrange = SliderRange.estimateBoundsIfNecessary(nrange, x);
                } else if (defaultRange != null) {
                    nrange = SliderRange.estimateBoundsIfNecessary(defaultRange, x);
                }
            }
        }
    }
    return nrange;
}
Also used : IntegerInterval(maspack.util.IntegerInterval) DoubleInterval(maspack.util.DoubleInterval) PropertyInfo(maspack.properties.PropertyInfo) EnumRange(maspack.util.EnumRange) StringRange(maspack.util.StringRange) Range(maspack.util.Range) NumericInterval(maspack.util.NumericInterval)

Example 2 with IntegerInterval

use of maspack.util.IntegerInterval in project artisynth_core by artisynth.

the class IntegerIntervalField method valueToText.

@Override
protected String[] valueToText(Object obj) {
    String[] strs = new String[2];
    if (obj instanceof IntegerInterval) {
        IntegerInterval rng = (IntegerInterval) obj;
        strs[0] = IntegerField.valueToText(rng.getLowerBound(), myFmt);
        strs[1] = IntegerField.valueToText(rng.getUpperBound(), myFmt);
        return strs;
    } else {
        strs[0] = strs[1] = "";
    }
    return strs;
}
Also used : IntegerInterval(maspack.util.IntegerInterval)

Example 3 with IntegerInterval

use of maspack.util.IntegerInterval in project artisynth_core by artisynth.

the class IntegerIntervalField method textToValue.

@Override
public Object textToValue(String[] text, boolean[] corrected, StringHolder errMsg) {
    IntegerInterval tmp = new IntegerInterval();
    BooleanHolder clipped = new BooleanHolder();
    if (LabeledTextField.isBlank(text[0]) || LabeledTextField.isBlank(text[1])) {
        return setVoidIfPossible(errMsg);
    }
    try {
        tmp.setLowerBound(IntegerField.parseInt(text[0], clipped));
        corrected[0] = clipped.value;
    } catch (Exception e) {
        return illegalValue("Improperly formed number for minimum", errMsg);
    }
    try {
        tmp.setUpperBound(IntegerField.parseInt(text[1], clipped));
        corrected[1] = clipped.value;
    } catch (Exception e) {
        return illegalValue("Improperly formed number for maximum", errMsg);
    }
    return validValue(tmp, errMsg);
}
Also used : IntegerInterval(maspack.util.IntegerInterval) BooleanHolder(maspack.util.BooleanHolder) IOException(java.io.IOException)

Example 4 with IntegerInterval

use of maspack.util.IntegerInterval in project artisynth_core by artisynth.

the class SetHandler method create.

public static LabeledComponentBase create(Property prop, double min, double max) {
    String name = prop.getName();
    PropertyInfo info = prop.getInfo();
    Class<?> type = info.getValueClass();
    LabeledComponentBase widget = null;
    if (type == double.class || type == float.class || type == Double.class || type == Float.class) {
        DoubleFieldSlider doubleSlider = new DoubleFieldSlider(name, min, max);
        if (info.getPrintFormat() != null) {
            doubleSlider.setFormat(info.getPrintFormat());
        }
        Range range = prop.getRange();
        if (range instanceof NumericInterval) {
            NumericInterval nrange = (NumericInterval) range;
            doubleSlider.setRange(nrange);
            if (nrange.isBounded()) {
                doubleSlider.setSliderRange(nrange);
            }
        }
        GuiUtils.setFixedWidth(doubleSlider.getTextField(), 100);
        doubleSlider.addValueChangeListener(new PropChangeListener(prop));
        widget = doubleSlider;
    } else if (type == int.class || type == Integer.class) {
        IntegerFieldSlider intSlider = new IntegerFieldSlider(name, (int) min, (int) max);
        Range range = prop.getRange();
        if (range instanceof IntegerInterval) {
            IntegerInterval irange = (IntegerInterval) range;
            intSlider.setRange(irange);
            if (irange.isBounded()) {
                intSlider.setSliderRange(irange);
            }
        // intSlider.setSliderRange ((IntRange)range);
        }
        GuiUtils.setFixedWidth(intSlider.getTextField(), 100);
        intSlider.addValueChangeListener(new PropChangeListener(prop));
        widget = intSlider;
    }
    if (widget != null) {
        widget.setToolTipText(info.getDescription());
        finishWidget(widget, prop);
    }
    return widget;
}
Also used : IntegerInterval(maspack.util.IntegerInterval) PropertyInfo(maspack.properties.PropertyInfo) EnumRange(maspack.util.EnumRange) StringRange(maspack.util.StringRange) Range(maspack.util.Range) NumericInterval(maspack.util.NumericInterval)

Example 5 with IntegerInterval

use of maspack.util.IntegerInterval in project artisynth_core by artisynth.

the class IntegerFieldSlider method updateInternalValue.

protected boolean updateInternalValue(Object value) {
    if (super.updateInternalValue(value)) {
        if (value instanceof Number && mySliderRange != null) {
            int newValue = ((Number) value).intValue();
            int max = (int) mySliderRange.getUpperBound();
            int min = (int) mySliderRange.getLowerBound();
            if (myAutoRangingP) {
                IntegerInterval newRange = null;
                if (newValue > max) {
                    newRange = new IntegerInterval(min, newValue);
                } else if (newValue < min) {
                    newRange = new IntegerInterval(newValue, max);
                }
                if (newRange != null) {
                    DoubleInterval dNewRange = SliderRange.roundBoundsTo125(newRange);
                    // make sure hard range OK
                    dNewRange.intersect(getRange());
                    setSliderRange(dNewRange);
                }
                // and if this widget does not have hard bounds
                if (!myRange.isBounded() && mySlider != null) {
                    int halfPixel = halfPixelValue();
                    if (newValue < halfPixel && newValue >= min) {
                        int newMax = (int) SliderRange.roundUp125(min + 4 * (newValue - min));
                        setSliderRange(new IntegerInterval(min, newMax));
                    }
                }
            }
            if (newValue < min || newValue > max) {
                mySlider.setBackground(Color.GRAY);
            } else {
                mySlider.setBackground(null);
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : IntegerInterval(maspack.util.IntegerInterval) DoubleInterval(maspack.util.DoubleInterval)

Aggregations

IntegerInterval (maspack.util.IntegerInterval)5 PropertyInfo (maspack.properties.PropertyInfo)2 DoubleInterval (maspack.util.DoubleInterval)2 EnumRange (maspack.util.EnumRange)2 NumericInterval (maspack.util.NumericInterval)2 Range (maspack.util.Range)2 StringRange (maspack.util.StringRange)2 IOException (java.io.IOException)1 BooleanHolder (maspack.util.BooleanHolder)1