Search in sources :

Example 1 with Range

use of maspack.util.Range 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 Range

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

the class SetHandler method initializeWidget.

public static boolean initializeWidget(LabeledComponentBase widget, Property prop) {
    String name = prop.getName();
    PropertyInfo info = prop.getInfo();
    Class<?> type = info.getValueClass();
    if (widget instanceof LabeledWidget) {
        LabeledWidget lwidget = (LabeledWidget) widget;
        if (textIsEmpty(lwidget.getLabelText())) {
            lwidget.setLabelText(name);
        }
        if (textIsEmpty(lwidget.getToolTipText())) {
            lwidget.setToolTipText(info.getDescription());
        }
    }
    if (widget instanceof LabeledControl) {
        removeOldListeners((LabeledControl) widget);
    }
    try {
        if (String.class.isAssignableFrom(type)) {
            // if String has a range, then use StringSelector
            // otherwise, use a simple StringField
            Range stringRange = prop.getRange();
            if (info.isInheritable() || stringRange == null || !(stringRange instanceof StringRange) || ((StringRange) stringRange).isWildcard()) {
                StringField stringField = (StringField) widget;
                stringField.setColumns(20);
                stringField.addValueChangeListener(new PropChangeListener(prop) {

                    public void valueChange(ValueChangeEvent e) {
                        if (e.getValue() == null || e.getValue().equals("")) {
                            super.valueChange(new ValueChangeEvent(e.getSource(), ""));
                        } else {
                            super.valueChange(e);
                        }
                    }
                });
                stringField.setStretchable(true);
            } else {
                String[] constants = ((StringRange) stringRange).getValidStrings();
                StringSelector selector = (StringSelector) widget;
                selector.setSelections(constants, null);
                selector.addValueChangeListener(new PropChangeListener(prop));
            }
        } else if (type == double.class || type == float.class || type == Double.class || type == Float.class) {
            DoubleField doubleField = (DoubleField) widget;
            Range range = prop.getRange();
            if (range instanceof NumericInterval) {
                doubleField.setRange((NumericInterval) range);
            }
            // }
            if (info.getPrintFormat() != null && formatIsDefault(doubleField)) {
                doubleField.setFormat(info.getPrintFormat());
            }
            GuiUtils.setFixedWidth(doubleField.getTextField(), 100);
            doubleField.addValueChangeListener(new PropChangeListener(prop));
        } else if (type == int.class || type == Integer.class) {
            IntegerField intField = (IntegerField) widget;
            GuiUtils.setFixedWidth(intField.getTextField(), 100);
            intField.addValueChangeListener(new PropChangeListener(prop));
        } else if (type == boolean.class || type == Boolean.class) {
            if (info.isReadOnly()) {
                StringField stringField = (StringField) widget;
                stringField.setColumns(5);
            } else {
                BooleanSelector selector = (BooleanSelector) widget;
                selector.addValueChangeListener(new PropChangeListener(prop));
            }
        } else if (VectorBase.class.isAssignableFrom(type) && info.getDimension() != -1) {
            VectorBase resultVec;
            try {
                resultVec = (VectorBase) type.newInstance();
            } catch (Exception e) {
                throw new InternalErrorException("Error creating no-args instance of " + type);
            }
            if (resultVec instanceof VectorNd) {
                ((VectorNd) resultVec).setSize(info.getDimension());
            }
            VectorField vectorField = (VectorField) widget;
            // from scratch)
            if (vectorField.getVectorSize() != info.getDimension()) {
                vectorField.setVectorSize(info.getDimension());
            } else {
                VectorNd existingValue = vectorField.getVectorValue();
                if (existingValue != null) {
                    resultVec.set(existingValue);
                }
            }
            vectorField.setResultHolder(resultVec);
            vectorField.addValueChangeListener(new PropChangeListener(prop));
            if (info.getPrintFormat() != null && formatIsDefault(vectorField)) {
                vectorField.setFormat(info.getPrintFormat());
            }
            vectorField.setStretchable(true);
        } else if (VectoriBase.class.isAssignableFrom(type) && info.getDimension() != -1) {
            VectoriBase resultVec;
            try {
                resultVec = (VectoriBase) type.newInstance();
            } catch (Exception e) {
                throw new InternalErrorException("Error creating no-args instance of " + type);
            }
            if (resultVec instanceof VectorNi) {
                ((VectorNi) resultVec).setSize(info.getDimension());
            }
            VectoriField vectorField = (VectoriField) widget;
            // from scratch)
            if (vectorField.getVectorSize() != info.getDimension()) {
                vectorField.setVectorSize(info.getDimension());
            } else {
                VectorNi existingValue = vectorField.getVectorValue();
                if (existingValue != null) {
                    resultVec.set(existingValue);
                }
            }
            vectorField.setResultHolder(resultVec);
            vectorField.addValueChangeListener(new PropChangeListener(prop));
            if (info.getPrintFormat() != null && formatIsDefault(vectorField)) {
                vectorField.setFormat(info.getPrintFormat());
            }
            vectorField.setStretchable(true);
        } else if (VectorBase.class.isAssignableFrom(type) && info.getDimension() == -1) {
            VectorBase resultVec;
            try {
                resultVec = (VectorBase) type.newInstance();
            } catch (Exception e) {
                throw new InternalErrorException("Error creating no-args instance of " + type);
            }
            VariableVectorField vectorField = (VariableVectorField) widget;
            VectorNd existingValue = vectorField.getVectorValue();
            if (vectorField.getVectorSize() != resultVec.size()) {
                resultVec.setSize(vectorField.getVectorSize());
            }
            if (existingValue != null) {
                resultVec.set(existingValue);
            }
            vectorField.setResultHolder(resultVec);
            vectorField.addValueChangeListener(new PropChangeListener(prop));
            if (info.getPrintFormat() != null && formatIsDefault(vectorField)) {
                vectorField.setFormat(info.getPrintFormat());
            }
            vectorField.setStretchable(true);
        } else if (SymmetricMatrix3d.class.isAssignableFrom(type)) {
            SymmetricMatrix3dField matrixField = (SymmetricMatrix3dField) widget;
            matrixField.addValueChangeListener(new PropChangeListener(prop));
            if (info.getPrintFormat() != null && formatIsDefault(matrixField)) {
                matrixField.setFormat(info.getPrintFormat());
            }
            matrixField.setStretchable(true);
        } else if (RigidTransform3d.class.isAssignableFrom(type)) {
            RigidTransformWidget transformField = (RigidTransformWidget) widget;
            transformField.addValueChangeListener(new PropChangeListener(prop));
            transformField.setStretchable(true);
        } else if (AffineTransform3d.class.isAssignableFrom(type)) {
            AffineTransformWidget transformField = (AffineTransformWidget) widget;
            transformField.addValueChangeListener(new PropChangeListener(prop));
            transformField.setStretchable(true);
        } else if (Rectangle2d.class.isAssignableFrom(type)) {
            RectangleField rectField = (RectangleField) widget;
            rectField.addValueChangeListener(new PropChangeListener(prop));
            if (info.getPrintFormat() != null && formatIsDefault(rectField)) {
                rectField.setFormat(info.getPrintFormat());
            }
            rectField.setStretchable(true);
        } else // }
        if (AxisAngle.class.isAssignableFrom(type)) {
            AxisAngleField orientationField = (AxisAngleField) widget;
            orientationField.addValueChangeListener(new PropChangeListener(prop));
            orientationField.setStretchable(true);
        } else if (Enum.class.isAssignableFrom(type)) {
            Enum<?>[] constants = null;
            Range range = prop.getRange();
            if (range != null && range instanceof EnumRange) {
                constants = ((EnumRange<?>) range).getValidEnums();
            } else {
                constants = (Enum[]) type.getEnumConstants();
            }
            if (info.isReadOnly()) {
                StringField stringField = (StringField) widget;
                int ncols = 0;
                for (int i = 0; i < constants.length; i++) {
                    int len = constants[i].toString().length();
                    if (len > ncols) {
                        ncols = len;
                    }
                }
                stringField.setColumns(ncols);
            } else {
                EnumSelector selector = (EnumSelector) widget;
                selector.setSelections(constants, null);
                selector.addValueChangeListener(new PropChangeListener(prop));
            }
        } else if (Color.class.isAssignableFrom(type)) {
            ColorSelector selector = (ColorSelector) widget;
            if (info.getNullValueOK()) {
                selector.enableNullColors();
            }
            selector.addValueChangeListener(new PropChangeListener(prop));
        } else if (IntegerInterval.class.isAssignableFrom(type)) {
            IntegerIntervalField rangeField = (IntegerIntervalField) widget;
            rangeField.addValueChangeListener(new PropChangeListener(prop));
            rangeField.setStretchable(true);
        } else if (NumericInterval.class.isAssignableFrom(type)) {
            DoubleIntervalField rangeField = (DoubleIntervalField) widget;
            rangeField.addValueChangeListener(new PropChangeListener(prop));
            rangeField.setStretchable(true);
        } else if (GLGridResolution.class.isAssignableFrom(type)) {
            GridResolutionField resField = (GridResolutionField) widget;
            resField.addValueChangeListener(new PropChangeListener(prop));
        } else if (Font.class.isAssignableFrom(type)) {
            FontField fontField = (FontField) widget;
            fontField.addValueChangeListener(new PropChangeListener(prop));
        } else if (CompositeProperty.class.isAssignableFrom(type)) {
            if (widget instanceof CompositePropertyWidget) {
                CompositePropertyWidget compProp = (CompositePropertyWidget) widget;
                compProp.setProperty(prop);
            } else {
                CompositePropertyPanel compProp = (CompositePropertyPanel) widget;
                compProp.setExpandState(info.getWidgetExpandState());
                compProp.initializeSelection(prop);
            }
        } else {
            return false;
        }
    } catch (ClassCastException e) {
        throw new IllegalArgumentException("widget type " + widget.getClass() + " inappropriate for property type " + type);
    }
    // finishWidget (widget, prop);
    return true;
}
Also used : EnumRange(maspack.util.EnumRange) NumericInterval(maspack.util.NumericInterval) SymmetricMatrix3d(maspack.matrix.SymmetricMatrix3d) Color(java.awt.Color) EnumRange(maspack.util.EnumRange) StringRange(maspack.util.StringRange) Range(maspack.util.Range) StringRange(maspack.util.StringRange) VectorBase(maspack.matrix.VectorBase) AffineTransform3d(maspack.matrix.AffineTransform3d) VectoriBase(maspack.matrix.VectoriBase) Font(java.awt.Font) InternalErrorException(maspack.util.InternalErrorException) InternalErrorException(maspack.util.InternalErrorException) AxisAngle(maspack.matrix.AxisAngle) VectorNd(maspack.matrix.VectorNd) PropertyInfo(maspack.properties.PropertyInfo) VectorNi(maspack.matrix.VectorNi)

Example 3 with Range

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

the class SetHandler method updateValue.

public static void updateValue(LabeledControl widget, Property prop) {
    widget.maskValueChangeListeners(true);
    // mask value checks because (a) we assume updated values are valid,
    // and (b) Void values, if present, may not pass the checks
    widget.maskValueChecks(true);
    if (widget instanceof NumericFieldSlider) {
        // do this because we don't want the slider to adjust its range
        // if there is a slight difference between the set and get values.
        ((NumericFieldSlider) widget).setAutoRangingEnabled(false);
    }
    widget.setValue(prop.get());
    if (widget instanceof NumericFieldSlider) {
        ((NumericFieldSlider) widget).setAutoRangingEnabled(true);
    }
    widget.maskValueChecks(false);
    widget.maskValueChangeListeners(false);
    if (prop.getInfo().isInheritable()) {
        PropertyModeButton button = getModeButton(widget);
        if (button != null) {
            button.setMode(((InheritableProperty) prop).getMode());
        }
    }
    // update the slider to reflect any changes in the range
    if (prop.getInfo().hasRestrictedRange()) {
        if (widget instanceof NumericFieldSlider) {
            Range range = prop.getRange();
            if (range instanceof NumericInterval) {
                ((NumericFieldSlider) widget).setRange((NumericInterval) range);
            }
        } else if (widget instanceof EnumSelector) {
            Range range = prop.getRange();
            if (range instanceof EnumRange) {
                ((EnumSelector) widget).setSelections(((EnumRange<?>) range).getValidEnums());
            }
        } else if (widget instanceof StringSelector) {
            Range range = prop.getRange();
            if (range instanceof StringRange) {
                String[] values = ((StringRange) range).getValidStrings();
                if (values != null) {
                    ((StringSelector) widget).setSelections(values);
                }
            }
        }
    }
}
Also used : EnumRange(maspack.util.EnumRange) EnumRange(maspack.util.EnumRange) StringRange(maspack.util.StringRange) Range(maspack.util.Range) NumericInterval(maspack.util.NumericInterval) StringRange(maspack.util.StringRange)

Example 4 with Range

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

the class HostList method getCommonRange.

public Range getCommonRange(PropTreeCell cell) {
    int[] indexPath = cell.getIndexPath();
    if (indexPath.length == 0) {
        throw new InternalErrorException("getCommonRange cannot be called for top-level cell");
    }
    Range commonRange = null;
    for (int i = 0; i < myHosts.size(); i++) {
        PropTreeData data = myDataList[i];
        HasProperties host = null;
        for (int level = 0; level < indexPath.length; level++) {
            host = data.myHost;
            if (data.getSubData() == null) {
                throw new InternalErrorException("data tree not initialized for " + cell.pathString());
            }
            // XXSystem.out.println ("25");
            data = data.getSubData()[indexPath[level]];
        }
        Range range = PropertyUtils.getRange(data.myInfo, host);
        if (range != null) {
            if (commonRange == null) {
                try {
                    commonRange = (Range) range.clone();
                } catch (Exception e) {
                    throw new InternalErrorException("Can't clone " + range.getClass());
                }
            } else {
                commonRange.intersect(range);
            }
        }
    }
    return commonRange;
}
Also used : InternalErrorException(maspack.util.InternalErrorException) Range(maspack.util.Range) InternalErrorException(maspack.util.InternalErrorException)

Example 5 with Range

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

the class PropCheckListener method validateValue.

public Object validateValue(ValueChangeEvent e, StringHolder errMsg) {
    // return myProp.validate (e.getValue(), errMsg);
    Range range = myProp.getRange();
    Object value = e.getValue();
    if (range != null && !range.isValid(value, errMsg)) {
        Object corrected = range.makeValid(value);
        if (corrected == Range.IllegalValue) {
            return Property.IllegalValue;
        } else {
            return corrected;
        }
    } else {
        if (errMsg != null) {
            // probably don't need to do this ...make
            errMsg.value = null;
        }
        return value;
    }
}
Also used : Range(maspack.util.Range)

Aggregations

Range (maspack.util.Range)7 EnumRange (maspack.util.EnumRange)5 StringRange (maspack.util.StringRange)5 PropertyInfo (maspack.properties.PropertyInfo)4 NumericInterval (maspack.util.NumericInterval)4 IntegerInterval (maspack.util.IntegerInterval)3 VectorNd (maspack.matrix.VectorNd)2 InternalErrorException (maspack.util.InternalErrorException)2 Color (java.awt.Color)1 Font (java.awt.Font)1 Rectangle2d (maspack.geometry.Rectangle2d)1 AffineTransform3d (maspack.matrix.AffineTransform3d)1 AxisAngle (maspack.matrix.AxisAngle)1 RigidTransform3d (maspack.matrix.RigidTransform3d)1 SymmetricMatrix3d (maspack.matrix.SymmetricMatrix3d)1 VectorBase (maspack.matrix.VectorBase)1 VectorNi (maspack.matrix.VectorNi)1 VectoriBase (maspack.matrix.VectoriBase)1 CompositeProperty (maspack.properties.CompositeProperty)1 GLGridResolution (maspack.render.GL.GLGridResolution)1