Search in sources :

Example 1 with PropertyInfo

use of maspack.properties.PropertyInfo 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 PropertyInfo

use of maspack.properties.PropertyInfo 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 PropertyInfo

use of maspack.properties.PropertyInfo in project artisynth_core by artisynth.

the class CompositePropertyPanel method findSelectableClasses.

private Class<?>[] findSelectableClasses(Property prop) {
    PropertyInfo info = prop.getInfo();
    // try to determine the selectable classes by querying the property's
    // main value class
    Class<?>[] classes = PropertyUtils.findCompositePropertySubclasses(info.getValueClass());
    if (classes == null) {
        classes = new Class[] { info.getValueClass() };
    }
    return classes;
}
Also used : PropertyInfo(maspack.properties.PropertyInfo)

Example 4 with PropertyInfo

use of maspack.properties.PropertyInfo in project artisynth_core by artisynth.

the class ComponentPropertyField method updatePropertySelector.

protected void updatePropertySelector() {
    // mask value change listeners since othewise the
    // property selector will fire one to announce the new
    // value we already know about ...
    myPropertySelector.maskValueChangeListeners(true);
    HasProperties host = getHost();
    if (myLastSelectorHost != host) {
        if (host != null) {
            PropertyInfoList list = host.getAllPropertyInfo();
            LinkedList<String> nameList = new LinkedList<String>();
            nameList.add(nullString);
            for (PropertyInfo info : list) {
                // need to prune
                if (myNumericOnly && !isNumeric(info)) {
                    continue;
                }
                if (myWidgetableOnly && !PropertyWidget.canCreate(info)) {
                    continue;
                }
                nameList.add(info.getName());
            }
            myPropertySelector.setSelections(nameList.toArray(new String[0]), nullString);
        } else {
            myPropertySelector.setSelections(new String[] { nullString }, nullString);
        }
        myLastSelectorHost = host;
    }
    myPropertySelector.setValue(getLeafPropName());
    myPropertySelector.maskValueChangeListeners(false);
}
Also used : PropertyInfoList(maspack.properties.PropertyInfoList) HasProperties(maspack.properties.HasProperties) PropertyInfo(maspack.properties.PropertyInfo) LinkedList(java.util.LinkedList)

Example 5 with PropertyInfo

use of maspack.properties.PropertyInfo in project artisynth_core by artisynth.

the class ComponentPropertyField method getPropertyForComposite.

private Property getPropertyForComposite(CompositeProperty cprop) {
    PropertyInfo info = cprop.getPropertyInfo();
    HasProperties host = cprop.getPropertyHost();
    Property prop = host.getProperty(info.getName());
    if (prop == null) {
        throw new InternalErrorException("Property not found in composite property host");
    }
    return prop;
}
Also used : HasProperties(maspack.properties.HasProperties) InternalErrorException(maspack.util.InternalErrorException) PropertyInfo(maspack.properties.PropertyInfo) CompositeProperty(maspack.properties.CompositeProperty) Property(maspack.properties.Property)

Aggregations

PropertyInfo (maspack.properties.PropertyInfo)12 HasProperties (maspack.properties.HasProperties)4 EnumRange (maspack.util.EnumRange)4 Range (maspack.util.Range)4 StringRange (maspack.util.StringRange)4 IntegerInterval (maspack.util.IntegerInterval)3 NumericInterval (maspack.util.NumericInterval)3 ModelComponent (artisynth.core.modelbase.ModelComponent)2 VectorNd (maspack.matrix.VectorNd)2 CompositeProperty (maspack.properties.CompositeProperty)2 HostList (maspack.properties.HostList)2 PropTreeCell (maspack.properties.PropTreeCell)2 InternalErrorException (maspack.util.InternalErrorException)2 RenderableComponent (artisynth.core.modelbase.RenderableComponent)1 Traceable (artisynth.core.modelbase.Traceable)1 Color (java.awt.Color)1 Component (java.awt.Component)1 Font (java.awt.Font)1 Point (java.awt.Point)1 LinkedList (java.util.LinkedList)1