Search in sources :

Example 1 with VImage

use of org.diirt.vtype.VImage in project org.csstudio.display.builder by kasemir.

the class FormatOptionHandler method format.

/**
 * Format value as string
 *
 *  @param value Value to format
 *  @param option How to format the value
 *  @param precision Precision to use. -1 will try to fetch precision from VType
 *  @param show_units Include units?
 *  @return Formatted value
 */
public static String format(final VType value, final FormatOption option, int precision, final boolean show_units) {
    precision = actualPrecision(value, precision);
    if (value == null)
        return "<null>";
    if (value instanceof VNumber) {
        final VNumber number = (VNumber) value;
        final String text = formatNumber(number.getValue(), number, option, precision);
        if (show_units && !number.getUnits().isEmpty())
            return text + " " + number.getUnits();
        return text;
    } else if (value instanceof VString)
        return ((VString) value).getValue();
    else if (value instanceof VEnum)
        return formatEnum((VEnum) value, option);
    else if (value instanceof VNumberArray) {
        final VNumberArray array = (VNumberArray) value;
        if (option == FormatOption.STRING)
            return getLongString(array);
        final ListNumber data = array.getData();
        if (data.size() <= 0)
            return "[]";
        final StringBuilder buf = new StringBuilder("[");
        buf.append(formatNumber(data.getDouble(0), array, option, precision));
        for (int i = 1; i < data.size(); ++i) {
            buf.append(", ");
            buf.append(formatNumber(data.getDouble(i), array, option, precision));
        }
        buf.append("]");
        if (show_units && !array.getUnits().isEmpty())
            buf.append(" ").append(array.getUnits());
        return buf.toString();
    } else if (value instanceof VEnumArray) {
        final List<String> labels = ((VEnumArray) value).getLabels();
        final StringBuilder buf = new StringBuilder("[");
        for (int i = 0; i < labels.size(); ++i) {
            if (i > 0)
                buf.append(", ");
            buf.append(labels.get(i));
        }
        buf.append("]");
        return buf.toString();
    } else if (value instanceof VStringArray)
        return StringList.join(((VStringArray) value).getData());
    else if (value instanceof VImage) {
        final VImage image = (VImage) value;
        return "VImage(" + image.getWidth() + " x " + image.getHeight() + ")";
    } else if (value instanceof VTable)
        return formatTable((VTable) value);
    return "<" + value.getClass().getName() + ">";
}
Also used : VEnumArray(org.diirt.vtype.VEnumArray) VNumber(org.diirt.vtype.VNumber) VString(org.diirt.vtype.VString) VEnum(org.diirt.vtype.VEnum) VNumberArray(org.diirt.vtype.VNumberArray) ListNumber(org.diirt.util.array.ListNumber) VString(org.diirt.vtype.VString) VTable(org.diirt.vtype.VTable) VStringArray(org.diirt.vtype.VStringArray) VImage(org.diirt.vtype.VImage)

Aggregations

ListNumber (org.diirt.util.array.ListNumber)1 VEnum (org.diirt.vtype.VEnum)1 VEnumArray (org.diirt.vtype.VEnumArray)1 VImage (org.diirt.vtype.VImage)1 VNumber (org.diirt.vtype.VNumber)1 VNumberArray (org.diirt.vtype.VNumberArray)1 VString (org.diirt.vtype.VString)1 VStringArray (org.diirt.vtype.VStringArray)1 VTable (org.diirt.vtype.VTable)1