Search in sources :

Example 1 with VectorNi

use of maspack.matrix.VectorNi 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 2 with VectorNi

use of maspack.matrix.VectorNi in project artisynth_core by artisynth.

the class VectoriField method setVectorSize.

/**
 * Sets the size of the vector associated with this vector field. Changing
 * the vector size will cause the result holder to be cleared, and the
 * current vector value to be reset to a zero vector of the indicated size.
 * The number of columns in the field will also be reset.
 *
 * @param size
 * new vector size
 */
public void setVectorSize(int size) {
    if (size < 1) {
        throw new IllegalArgumentException("size must be greater than 1");
    }
    if (size != myVectorSize) {
        setColumns(5 * size);
        myResultHolder = null;
        myVectorSize = size;
        setValue(new VectorNi(size));
    }
}
Also used : VectorNi(maspack.matrix.VectorNi)

Example 3 with VectorNi

use of maspack.matrix.VectorNi in project artisynth_core by artisynth.

the class VectoriField method textToValue.

public Object textToValue(String text, BooleanHolder corrected, StringHolder errMsg) {
    corrected.value = false;
    if (isBlank(text)) {
        return setVoidIfPossible(errMsg);
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new StringReader(text));
    VectorNi tmp = new VectorNi(myVectorSize);
    try {
        for (int i = 0; i < myVectorSize; i++) {
            if (rtok.nextToken() == ReaderTokenizer.TT_EOF) {
                return illegalValue("Missing elements (there should be " + myVectorSize + ")", errMsg);
            } else if (!rtok.tokenIsInteger()) {
                return illegalValue("Malformed integer for element " + i, errMsg);
            }
            tmp.set(i, (int) rtok.nval);
        }
        if (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
            if (rtok.tokenIsInteger()) {
                return illegalValue("Too many elements (there should be " + myVectorSize + ")", errMsg);
            } else {
                return illegalValue("Extra characters after last element", errMsg);
            }
        }
        return validValue(tmp, errMsg);
    } catch (Exception e) {
        return illegalValue("Improperly formed vector", errMsg);
    }
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) StringReader(java.io.StringReader) VectorNi(maspack.matrix.VectorNi)

Example 4 with VectorNi

use of maspack.matrix.VectorNi in project artisynth_core by artisynth.

the class NumericInterval method validate.

/**
 * Validates an integer vector by checking that its elements lie within this
 * range interval. For purposes of this method, the interval is treated as
 * closed.  If all elements are within range, the vector is returned
 * unchanged.  Otherwise, the method returns either a new clipped vector (if
 * <code>clip</code> is <code>true</code>), or the special value {@link
 * Range#IllegalValue}. In the latter two cases, an error message will also
 * be returned if the variable <code>errMsg</code> is non-null.
 *
 * @param vec
 * vector to validate
 * @param clip
 * if true, clip the vector to the range if possible
 * @param errMsg
 * if non-null, is used to return an error message if one or more elements
 * are out of range
 * @return either the original vector, a clipped version of it, or
 * Range.IllegalValue
 */
public Object validate(Vectori vec, boolean clip, StringHolder errMsg) {
    boolean vectorCopied = false;
    for (int i = 0; i < vec.size(); i++) {
        double num = vec.get(i);
        if (!withinRange(num)) {
            if (clip && canClipToRange(num)) {
                if (!vectorCopied) {
                    vec = new VectorNi(vec);
                    vectorCopied = true;
                }
                vec.set(i, (int) clipToRange(num));
            } else {
                setError(errMsg, "elements should be within the range " + this);
                return Range.IllegalValue;
            }
        }
    }
    if (vectorCopied) {
        setError(errMsg, "vector was clipped to range");
    }
    return vec;
}
Also used : VectorNi(maspack.matrix.VectorNi)

Aggregations

VectorNi (maspack.matrix.VectorNi)4 Color (java.awt.Color)1 Font (java.awt.Font)1 StringReader (java.io.StringReader)1 AffineTransform3d (maspack.matrix.AffineTransform3d)1 AxisAngle (maspack.matrix.AxisAngle)1 SymmetricMatrix3d (maspack.matrix.SymmetricMatrix3d)1 VectorBase (maspack.matrix.VectorBase)1 VectorNd (maspack.matrix.VectorNd)1 VectoriBase (maspack.matrix.VectoriBase)1 PropertyInfo (maspack.properties.PropertyInfo)1 EnumRange (maspack.util.EnumRange)1 InternalErrorException (maspack.util.InternalErrorException)1 NumericInterval (maspack.util.NumericInterval)1 Range (maspack.util.Range)1 ReaderTokenizer (maspack.util.ReaderTokenizer)1 StringRange (maspack.util.StringRange)1