Search in sources :

Example 6 with ListNumber

use of org.diirt.util.array.ListNumber in project yamcs-studio by yamcs.

the class PVUtil method getStringArray.

/**
 * Get string array from pv.
 *
 * @param pv
 *            The PV.
 * @return String array. For string array, it's the actual strings. For numeric arrays, the numbers are formatted as
 *         strings. For enum array, the labels are returned. For scalar PVs, an array with a single string is
 *         returned.
 */
public static final String[] getStringArray(IPV pv) {
    final VType value = checkPVValue(pv);
    if (value instanceof VStringArray) {
        final List<String> list = ((VStringArray) value).getData();
        return list.toArray(new String[list.size()]);
    } else if (value instanceof VDoubleArray) {
        final ListNumber list = ((VNumberArray) value).getData();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) text[i] = Double.toString(list.getDouble(i));
        return text;
    } else if (value instanceof VNumberArray) {
        final ListNumber list = ((VNumberArray) value).getData();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) text[i] = Long.toString(list.getLong(i));
        return text;
    } else if (value instanceof VEnumArray) {
        final List<String> labels = ((VEnumArray) value).getLabels();
        final ListInt list = ((VEnumArray) value).getIndexes();
        final String[] text = new String[list.size()];
        for (int i = 0; i < text.length; ++i) {
            final int index = list.getInt(i);
            if (index >= 0 && index <= labels.size())
                text[i] = labels.get(index);
            else
                text[i] = "<" + index + ">";
        }
        return text;
    }
    return new String[] { getString(pv) };
}
Also used : VDoubleArray(org.diirt.vtype.VDoubleArray) VNumberArray(org.diirt.vtype.VNumberArray) VEnumArray(org.diirt.vtype.VEnumArray) ListNumber(org.diirt.util.array.ListNumber) ListInt(org.diirt.util.array.ListInt) VType(org.diirt.vtype.VType) List(java.util.List) VStringArray(org.diirt.vtype.VStringArray)

Example 7 with ListNumber

use of org.diirt.util.array.ListNumber in project org.csstudio.display.builder by kasemir.

the class FormatOptionHandlerTest method testString.

@Test
public void testString() throws Exception {
    // Actual String
    VType value = ValueFactory.newVString("Test1", ValueFactory.alarmNone(), ValueFactory.timeNow());
    String text = FormatOptionHandler.format(value, FormatOption.DEFAULT, -1, true);
    System.out.println(text);
    assertThat(text, equalTo("Test1"));
    text = FormatOptionHandler.format(value, FormatOption.STRING, -1, true);
    System.out.println(text);
    assertThat(text, equalTo("Test1"));
    text = FormatOptionHandler.format(value, FormatOption.EXPONENTIAL, -1, true);
    System.out.println(text);
    assertThat(text, equalTo("Test1"));
    // Number interpreted as char
    value = ValueFactory.newVDouble(65.0, display);
    text = FormatOptionHandler.format(value, FormatOption.STRING, -1, true);
    System.out.println(text);
    assertThat(text, equalTo("A V"));
    // Number array interpreted as long string
    // UTF-8 for 'Hello'
    ListNumber data = new ArrayInt(72, 101, 108, 108, 111);
    value = ValueFactory.newVNumberArray(data, ValueFactory.alarmNone(), ValueFactory.timeNow(), display);
    System.out.println(value);
    text = FormatOptionHandler.format(value, FormatOption.STRING, -1, true);
    System.out.println(text);
    assertThat(text, equalTo("Hello"));
    data = new ArrayInt(/* Dollar */
    0x24, /* Euro */
    0xE2, 0x82, 0xAC);
    value = ValueFactory.newVNumberArray(data, ValueFactory.alarmNone(), ValueFactory.timeNow(), display);
    System.out.println(value);
    text = FormatOptionHandler.format(value, FormatOption.STRING, -1, true);
    System.out.println(text);
    // For this to work, Eclipse IDE Preferences -> General -> Workspace -> Text file encoding
    // must be set to UTF-8, which is the default on Linux but not necessarily Windows
    assertThat(text, equalTo("$€"));
}
Also used : ListNumber(org.diirt.util.array.ListNumber) VType(org.diirt.vtype.VType) ArrayInt(org.diirt.util.array.ArrayInt) Test(org.junit.Test)

Example 8 with ListNumber

use of org.diirt.util.array.ListNumber in project org.csstudio.display.builder by kasemir.

the class FormatOptionHandler method formatTable.

/**
 * Format table as text
 *
 *  <p>A single-row table is formatted as "Col1: value, Col2: value, ...".
 *  Otherwise, table is formatted aligned by width of each column:
 *
 *  <pre>
 *  X     Column 2 Names
 *  3     On       Fred
 *  3.14  Off      Jane
 *  3.345 On       Alan
 *  </pre>
 *
 *  @param table {@link VTable}
 *  @return Textual dump of the table
 */
private static String formatTable(final VTable table) {
    final int rows = table.getRowCount(), cols = table.getColumnCount();
    final String[][] cell = new String[rows + 1][cols];
    final int[] width = new int[cols];
    // Determine string for headers and each table cell
    for (int c = 0; c < cols; ++c) {
        cell[0][c] = table.getColumnName(c);
        // Table columns use ListNumber for ListDouble or an integer-typed ListNumber
        // Otherwise it's a List<?> for String, Instant, alarm, ...
        final Object col = table.getColumnData(c);
        if (col instanceof ListNumber) {
            final ListNumber list = (ListNumber) col;
            if (table.getColumnType(c).equals(Integer.TYPE))
                for (int r = 0; r < list.size(); ++r) cell[1 + r][c] = Integer.toString(list.getInt(r));
            else
                for (int r = 0; r < list.size(); ++r) cell[1 + r][c] = Double.toString(list.getDouble(r));
            for (int r = list.size(); r < rows; ++r) cell[1 + r][c] = "";
        } else {
            final List<?> list = (List<?>) col;
            for (int r = 0; r < list.size(); ++r) // handle null
            cell[1 + r][c] = Objects.toString(list.get(r));
            for (int r = list.size(); r < rows; ++r) cell[1 + r][c] = "";
        }
        // Determine maximum width of all cells in this column
        for (int r = 0; r < rows + 1; ++r) width[c] = Math.max(width[c], cell[r][c].length());
    }
    // Format cells into one big string
    final StringBuilder buf = new StringBuilder();
    if (rows == 1) {
        // Single-row
        for (int c = 0; c < cols; ++c) {
            if (c > 0)
                buf.append(", ");
            buf.append(cell[0][c]);
            buf.append(": ");
            buf.append(cell[1][c]);
        }
    } else {
        // Table header followed by rows
        for (int c = 0; c < cols; ++c) {
            if (c > 0)
                buf.append(' ');
            buf.append(pad(cell[0][c], width[c]));
        }
        for (int r = 1; r < rows + 1; ++r) {
            buf.append('\n');
            for (int c = 0; c < cols; ++c) {
                if (c > 0)
                    buf.append(' ');
                buf.append(pad(cell[r][c], width[c]));
            }
        }
    }
    return buf.toString();
}
Also used : ListNumber(org.diirt.util.array.ListNumber) List(java.util.List) VString(org.diirt.vtype.VString)

Example 9 with ListNumber

use of org.diirt.util.array.ListNumber in project org.csstudio.display.builder by kasemir.

the class ImagePlot method setCrosshairLocation.

/**
 * Set location of crosshair
 *  @param x_val
 *  @param y_val
 */
public void setCrosshairLocation(final double x_val, final double y_val, final RTImagePlotListener listener) {
    if (Double.isNaN(x_val) || Double.isNaN(y_val)) {
        if (crosshair_position == null)
            return;
        crosshair_position = null;
        if (listener != null)
            listener.changedCursorInfo(Double.NaN, Double.NaN, -1, -1, Double.NaN);
        requestRedraw();
        return;
    }
    final Point2D pos = new Point2D(x_val, y_val);
    if (pos.equals(crosshair_position))
        return;
    crosshair_position = pos;
    if (listener != null)
        listener.changedCrosshair(x_val, y_val);
    // Location as coordinate in image
    // No "+0.5" rounding! Truncate to get full pixel offsets,
    // don't jump to next pixel when mouse moves beyond 'half' of the current pixel.
    // Use -1 to mark location outside of data width resp. height.
    int image_x = (int) (data_width * (x_val - min_x) / (max_x - min_x));
    if (image_x < 0)
        image_x = -1;
    else if (image_x >= data_width)
        image_x = -1;
    // Mouse and image coords for Y go 'down'
    int image_y = (int) (data_height * (max_y - y_val) / (max_y - min_y));
    if (image_y < 0)
        image_y = -1;
    else if (image_y >= data_height)
        image_y = -1;
    final ListNumber data = image_data;
    double pixel = Double.NaN;
    if (data != null && image_x >= 0 && image_y >= 0) {
        final int offset = image_x + image_y * data_width;
        try {
            if (unsigned_data) {
                if (data instanceof ArrayByte)
                    pixel = Byte.toUnsignedInt(data.getByte(offset));
                else if (data instanceof ArrayShort)
                    pixel = Short.toUnsignedInt(data.getShort(offset));
                else if (data instanceof ArrayInt)
                    pixel = Integer.toUnsignedLong(data.getInt(offset));
                else
                    pixel = data.getDouble(offset);
            } else
                pixel = data.getDouble(offset);
        } catch (Throwable ex) {
            // Catch ArrayIndexOutOfBoundsException or other internal errors of ListNumber
            logger.log(Level.WARNING, "Error accessing pixel " + image_x + ", " + image_y + " of data with size " + data.size());
        // leave pixel == Double.NaN;
        }
    }
    if (listener != null)
        listener.changedCursorInfo(x_val, y_val, image_x, image_y, pixel);
    requestRedraw();
}
Also used : ListNumber(org.diirt.util.array.ListNumber) Point2D(javafx.geometry.Point2D) ArrayByte(org.diirt.util.array.ArrayByte) ArrayInt(org.diirt.util.array.ArrayInt) ArrayShort(org.diirt.util.array.ArrayShort)

Example 10 with ListNumber

use of org.diirt.util.array.ListNumber in project org.csstudio.display.builder by kasemir.

the class ValueUtil method getDoubleArray.

/**
 * Try to get a 'double' type array from a value.
 *  @param value Value of a PV
 *  @return Current value as double[].
 *          Will return single-element array for scalar value,
 *          including <code>{ Double.NaN }</code> in case the value type
 *          does not decode into a number.
 */
public static double[] getDoubleArray(final VType value) {
    if (value instanceof VNumberArray) {
        final ListNumber list = ((VNumberArray) value).getData();
        final Object wrapped = CollectionNumbers.wrappedArray(list);
        if (wrapped instanceof double[])
            return (double[]) wrapped;
        final double[] result = new double[list.size()];
        for (int i = 0; i < result.length; i++) result[i] = list.getDouble(i);
        return result;
    }
    return new double[] { getDouble(value) };
}
Also used : VNumberArray(org.diirt.vtype.VNumberArray) ListNumber(org.diirt.util.array.ListNumber)

Aggregations

ListNumber (org.diirt.util.array.ListNumber)29 VNumberArray (org.diirt.vtype.VNumberArray)13 List (java.util.List)10 VString (org.diirt.vtype.VString)9 ArrayList (java.util.ArrayList)6 ArrayDouble (org.diirt.util.array.ArrayDouble)5 ArrayInt (org.diirt.util.array.ArrayInt)5 ListInt (org.diirt.util.array.ListInt)5 VEnumArray (org.diirt.vtype.VEnumArray)5 VNumber (org.diirt.vtype.VNumber)4 VTable (org.diirt.vtype.VTable)4 VType (org.diirt.vtype.VType)4 Test (org.junit.Test)4 AbstractList (java.util.AbstractList)3 ListDouble (org.diirt.util.array.ListDouble)3 VEnum (org.diirt.vtype.VEnum)3 VStringArray (org.diirt.vtype.VStringArray)3 BufferedImage (java.awt.image.BufferedImage)2 Background (javafx.scene.layout.Background)2 BackgroundFill (javafx.scene.layout.BackgroundFill)2