Search in sources :

Example 1 with Scalar

use of org.yamcs.studio.data.vtype.Scalar in project yamcs-studio by yamcs.

the class VTypeHelper method formatScalarNumber.

/**
 * @param formatEnum
 * @param pmValue
 *            The PVManager V Value. It must be a scalar with number value.
 * @param numValue
 *            the number value. not necessary if pmValue is given.
 * @param precision
 * @return
 */
private static String formatScalarNumber(FormatEnum formatEnum, Object pmValue, Number numValue, int precision) {
    if (pmValue != null) {
        numValue = (Number) ((Scalar) pmValue).getValue();
    }
    NumberFormat numberFormat;
    var displayPrecision = calculatePrecision(pmValue, precision);
    double highDispLimit = 0.0, lowDispLimit = 0.0;
    switch(formatEnum) {
        case DECIMAL:
        case DEFAULT:
        default:
            if (precision == UNSET_PRECISION) {
                if (pmValue instanceof Display && ((Display) pmValue).getFormat() != null) {
                    if (numValue instanceof Float) {
                        // Use boxed float, to avoid the upcast to double
                        return ((Display) pmValue).getFormat().format(numValue);
                    } else if (numValue instanceof Byte || numValue instanceof Short || numValue instanceof Integer || numValue instanceof Long) {
                        return ((Display) pmValue).getFormat().format(numValue);
                    } else {
                        return ((Display) pmValue).getFormat().format(numValue.doubleValue());
                    }
                } else {
                    return formatScalarNumber(FormatEnum.COMPACT, numValue, displayPrecision);
                }
            } else {
                if (numValue instanceof Byte || numValue instanceof Short || numValue instanceof Integer || numValue instanceof Long) {
                    numberFormat = getDecimalFormat(precision);
                    return numberFormat.format(numValue);
                } else if (numValue instanceof Float) {
                    // consistent with the rest of CSS.
                    if (Float.isNaN(numValue.floatValue())) {
                        return Float.toString(Float.NaN);
                    }
                    // Also check for positive and negative infinity.
                    if (Float.isInfinite(numValue.floatValue())) {
                        return Float.toString(numValue.floatValue());
                    }
                    numberFormat = getDecimalFormat(precision);
                    // Use boxed Float, to avoid the upcast to double
                    return numberFormat.format(numValue);
                } else {
                    // consistent with the rest of CSS.
                    if (Double.isNaN(numValue.doubleValue())) {
                        return Double.toString(Double.NaN);
                    }
                    // Also check for positive and negative infinity.
                    if (Double.isInfinite(numValue.doubleValue())) {
                        return Double.toString(numValue.doubleValue());
                    }
                    numberFormat = getDecimalFormat(precision);
                    return numberFormat.format(numValue.doubleValue());
                }
            }
        case COMPACT:
            var dValue = numValue.doubleValue();
            if (((dValue > 0.0001) && (dValue < 10000)) || ((dValue < -0.0001) && (dValue > -10000)) || dValue == 0.0) {
                return formatScalarNumber(FormatEnum.DECIMAL, numValue, displayPrecision);
            } else {
                return formatScalarNumber(FormatEnum.EXP, numValue, displayPrecision);
            }
        case ENG:
            var value = numValue.doubleValue();
            if (value == 0) {
                return formatScalarNumber(FormatEnum.EXP, numValue, displayPrecision);
            }
            var log10 = Math.log10(Math.abs(value));
            var power = 3 * (int) Math.floor(log10 / 3);
            return String.format("%." + displayPrecision + "fE%d", value / Math.pow(10, power), power);
        case SEXA:
            if (pmValue instanceof Display) {
                highDispLimit = ((Display) pmValue).getUpperDisplayLimit();
                lowDispLimit = ((Display) pmValue).getLowerDisplayLimit();
            }
            return doubleToSexagesimal(numValue.doubleValue(), displayPrecision, highDispLimit, lowDispLimit);
        case SEXA_HMS:
            if (pmValue instanceof Display) {
                highDispLimit = ((Display) pmValue).getUpperDisplayLimit();
                lowDispLimit = ((Display) pmValue).getLowerDisplayLimit();
            }
            return doubleToSexagesimal(numValue.doubleValue() * 12.0 / Math.PI, displayPrecision, highDispLimit, lowDispLimit);
        case SEXA_DMS:
            if (pmValue instanceof Display) {
                highDispLimit = ((Display) pmValue).getUpperDisplayLimit();
                lowDispLimit = ((Display) pmValue).getLowerDisplayLimit();
            }
            return doubleToSexagesimal(numValue.doubleValue() * 180.0 / Math.PI, displayPrecision, highDispLimit, lowDispLimit);
        case EXP:
            // Exponential notation identified as 'negative' precision in cached
            numberFormat = getExponentialFormat(displayPrecision);
            return numberFormat.format(numValue.doubleValue());
        case HEX:
            return HEX_PREFIX + Integer.toHexString(numValue.intValue()).toUpperCase();
        case HEX64:
            return HEX_PREFIX + Long.toHexString(numValue.longValue());
        case STRING:
            return new String(new char[] { (char) numValue.intValue() });
    }
}
Also used : ListFloat(org.yamcs.studio.data.vtype.ListFloat) VFloat(org.yamcs.studio.data.vtype.VFloat) ListByte(org.yamcs.studio.data.vtype.ListByte) VByte(org.yamcs.studio.data.vtype.VByte) ListLong(org.yamcs.studio.data.vtype.ListLong) VString(org.yamcs.studio.data.vtype.VString) VShort(org.yamcs.studio.data.vtype.VShort) ListShort(org.yamcs.studio.data.vtype.ListShort) Scalar(org.yamcs.studio.data.vtype.Scalar) MultiScalar(org.yamcs.studio.data.vtype.MultiScalar) NumberFormat(java.text.NumberFormat) Display(org.yamcs.studio.data.vtype.Display)

Example 2 with Scalar

use of org.yamcs.studio.data.vtype.Scalar in project yamcs-studio by yamcs.

the class MenuButtonEditPart method registerPropertyChangeHandlers.

@Override
protected void registerPropertyChangeHandlers() {
    setPropertyChangeHandler(PROP_PVNAME, (oldValue, newValue, figure) -> {
        registerLoadActionsListener();
        return false;
    });
    setPropertyChangeHandler(PROP_PVVALUE, (oldValue, newValue, refreshableFigure) -> {
        if ((newValue != null) && (newValue instanceof Scalar)) {
            ((MenuButtonFigure) refreshableFigure).setText(VTypeHelper.getString((VType) newValue));
        }
        return true;
    });
    setPropertyChangeHandler(PROP_LABEL, (oldValue, newValue, refreshableFigure) -> {
        ((MenuButtonFigure) refreshableFigure).setText(newValue.toString());
        return true;
    });
    setPropertyChangeHandler(PROP_TRANSPARENT, (oldValue, newValue, refreshableFigure) -> {
        ((MenuButtonFigure) refreshableFigure).setOpaque(!(Boolean) newValue);
        return true;
    });
    setPropertyChangeHandler(PROP_SHOW_DOWN_ARROW, (oldValue, newValue, refreshableFigure) -> {
        ((MenuButtonFigure) refreshableFigure).setDownArrowVisible((boolean) newValue);
        return true;
    });
    IWidgetPropertyChangeHandler handler = (oldValue, newValue, refreshableFigure) -> {
        updatePropSheet((Boolean) newValue);
        return false;
    };
    getWidgetModel().getProperty(PROP_ACTIONS_FROM_PV).addPropertyChangeListener(evt -> handler.handleChange(evt.getOldValue(), evt.getNewValue(), getFigure()));
}
Also used : PROP_PVVALUE(org.csstudio.opibuilder.model.IPVWidgetModel.PROP_PVVALUE) WidgetActionMenuAction(org.csstudio.opibuilder.actions.WidgetActionMenuAction) PROP_TRANSPARENT(org.csstudio.opibuilder.widgets.model.MenuButtonModel.PROP_TRANSPARENT) PROP_ACTIONS(org.csstudio.opibuilder.model.AbstractWidgetModel.PROP_ACTIONS) AbstractWidgetModel(org.csstudio.opibuilder.model.AbstractWidgetModel) PROP_PVNAME(org.csstudio.opibuilder.model.IPVWidgetModel.PROP_PVNAME) RequestConstants(org.eclipse.gef.RequestConstants) CustomMediaFactory(org.csstudio.ui.util.CustomMediaFactory) IFigure(org.eclipse.draw2d.IFigure) ITextFigure(org.csstudio.swt.widgets.figures.ITextFigure) PROP_SHOW_DOWN_ARROW(org.csstudio.opibuilder.widgets.model.MenuButtonModel.PROP_SHOW_DOWN_ARROW) IPV(org.yamcs.studio.data.IPV) Request(org.eclipse.gef.Request) RGB(org.eclipse.swt.graphics.RGB) IPVListener(org.yamcs.studio.data.IPVListener) ActionsInput(org.csstudio.opibuilder.widgetActions.ActionsInput) Scalar(org.yamcs.studio.data.vtype.Scalar) MouseMotionListener(org.eclipse.draw2d.MouseMotionListener) ExecutionMode(org.csstudio.opibuilder.editparts.ExecutionMode) VTypeHelper(org.yamcs.studio.data.VTypeHelper) PlatformUI(org.eclipse.ui.PlatformUI) Point(org.eclipse.draw2d.geometry.Point) MenuManager(org.eclipse.jface.action.MenuManager) VEnum(org.yamcs.studio.data.vtype.VEnum) MouseEvent(org.eclipse.draw2d.MouseEvent) PROP_ACTIONS_FROM_PV(org.csstudio.opibuilder.widgets.model.MenuButtonModel.PROP_ACTIONS_FROM_PV) MouseListener(org.eclipse.draw2d.MouseListener) Display(org.eclipse.swt.widgets.Display) PROP_LABEL(org.csstudio.opibuilder.widgets.model.MenuButtonModel.PROP_LABEL) MenuButtonFigure(org.csstudio.opibuilder.widgets.figures.MenuButtonFigure) List(java.util.List) WritePVAction(org.csstudio.opibuilder.widgetActions.WritePVAction) VType(org.yamcs.studio.data.vtype.VType) AbstractPVWidgetEditPart(org.csstudio.opibuilder.editparts.AbstractPVWidgetEditPart) MenuButtonModel(org.csstudio.opibuilder.widgets.model.MenuButtonModel) EditPolicy(org.eclipse.gef.EditPolicy) IWidgetPropertyChangeHandler(org.csstudio.opibuilder.properties.IWidgetPropertyChangeHandler) GraphicsUtil(org.csstudio.swt.widgets.util.GraphicsUtil) MenuButtonFigure(org.csstudio.opibuilder.widgets.figures.MenuButtonFigure) VType(org.yamcs.studio.data.vtype.VType) IWidgetPropertyChangeHandler(org.csstudio.opibuilder.properties.IWidgetPropertyChangeHandler) Scalar(org.yamcs.studio.data.vtype.Scalar)

Aggregations

Scalar (org.yamcs.studio.data.vtype.Scalar)2 NumberFormat (java.text.NumberFormat)1 List (java.util.List)1 WidgetActionMenuAction (org.csstudio.opibuilder.actions.WidgetActionMenuAction)1 AbstractPVWidgetEditPart (org.csstudio.opibuilder.editparts.AbstractPVWidgetEditPart)1 ExecutionMode (org.csstudio.opibuilder.editparts.ExecutionMode)1 AbstractWidgetModel (org.csstudio.opibuilder.model.AbstractWidgetModel)1 PROP_ACTIONS (org.csstudio.opibuilder.model.AbstractWidgetModel.PROP_ACTIONS)1 PROP_PVNAME (org.csstudio.opibuilder.model.IPVWidgetModel.PROP_PVNAME)1 PROP_PVVALUE (org.csstudio.opibuilder.model.IPVWidgetModel.PROP_PVVALUE)1 IWidgetPropertyChangeHandler (org.csstudio.opibuilder.properties.IWidgetPropertyChangeHandler)1 ActionsInput (org.csstudio.opibuilder.widgetActions.ActionsInput)1 WritePVAction (org.csstudio.opibuilder.widgetActions.WritePVAction)1 MenuButtonFigure (org.csstudio.opibuilder.widgets.figures.MenuButtonFigure)1 MenuButtonModel (org.csstudio.opibuilder.widgets.model.MenuButtonModel)1 PROP_ACTIONS_FROM_PV (org.csstudio.opibuilder.widgets.model.MenuButtonModel.PROP_ACTIONS_FROM_PV)1 PROP_LABEL (org.csstudio.opibuilder.widgets.model.MenuButtonModel.PROP_LABEL)1 PROP_SHOW_DOWN_ARROW (org.csstudio.opibuilder.widgets.model.MenuButtonModel.PROP_SHOW_DOWN_ARROW)1 PROP_TRANSPARENT (org.csstudio.opibuilder.widgets.model.MenuButtonModel.PROP_TRANSPARENT)1 ITextFigure (org.csstudio.swt.widgets.figures.ITextFigure)1