Search in sources :

Example 16 with WidgetProperty

use of org.csstudio.display.builder.model.WidgetProperty in project org.csstudio.display.builder by kasemir.

the class WidgetRuntime method start.

/**
 * Start: Connect to PVs, start scripts
 *  @throws Exception on error
 */
public void start() throws Exception {
    // Update "value" property from primary PV, if defined
    final Optional<WidgetProperty<String>> name = widget.checkProperty(propPVName);
    final Optional<WidgetProperty<VType>> value = widget.checkProperty(runtimePropPVValue);
    if (name.isPresent() && value.isPresent())
        pv_name_binding.set(new PVNameToValueBinding(this, name.get(), value.get(), true));
    // Prepare action-related PVs
    final List<ActionInfo> actions = widget.propActions().getValue().getActions();
    if (actions.size() > 0) {
        final List<RuntimePV> action_pvs = new ArrayList<>();
        for (final ActionInfo action : actions) {
            if (action instanceof WritePVActionInfo) {
                final String pv_name = ((WritePVActionInfo) action).getPV();
                final String expanded = MacroHandler.replace(widget.getMacrosOrProperties(), pv_name);
                final RuntimePV pv = PVFactory.getPV(expanded);
                action_pvs.add(pv);
                addPV(pv, true);
            }
        }
        if (action_pvs.size() > 0)
            this.writable_pvs = action_pvs;
    }
    widget.propClass().addPropertyListener(update_widget_class);
    // Start scripts in pool because Jython setup is expensive
    RuntimeUtil.getExecutor().execute(this::startScripts);
}
Also used : WidgetProperty(org.csstudio.display.builder.model.WidgetProperty) RuntimePV(org.csstudio.display.builder.runtime.pv.RuntimePV) ArrayList(java.util.ArrayList) ActionInfo(org.csstudio.display.builder.model.properties.ActionInfo) WritePVActionInfo(org.csstudio.display.builder.model.properties.WritePVActionInfo) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) WritePVActionInfo(org.csstudio.display.builder.model.properties.WritePVActionInfo)

Example 17 with WidgetProperty

use of org.csstudio.display.builder.model.WidgetProperty in project org.csstudio.display.builder by kasemir.

the class ScriptUtil method getWidgetValueByName.

// ==================
// Workspace helpers
/**
 * Locate a widget by name, then fetch its value
 *
 *  <p>Value is the content of the "value" property.
 *  If there is no such property, the primary PV
 *  of the widget is read.
 *
 *  @param widget Widget used to locate the widget model
 *  @param name Name of widget to find
 *  @return Value of the widget
 *  @throws Exception on error
 */
public static VType getWidgetValueByName(final Widget widget, final String name) throws Exception {
    final Widget w = findWidgetByName(widget, name);
    final Optional<WidgetProperty<Object>> value_prop = w.checkProperty("value");
    if (value_prop.isPresent()) {
        final Object value = value_prop.get().getValue();
        if (value == null || value instanceof VType)
            return (VType) value;
    }
    return getPrimaryPV(w).read();
}
Also used : WidgetProperty(org.csstudio.display.builder.model.WidgetProperty) VType(org.diirt.vtype.VType) Widget(org.csstudio.display.builder.model.Widget)

Example 18 with WidgetProperty

use of org.csstudio.display.builder.model.WidgetProperty in project org.csstudio.display.builder by kasemir.

the class PropertyPanel method updatePropertiesView.

private void updatePropertiesView(final Set<WidgetProperty<?>> properties, final List<Widget> other) {
    final String search = searchField.getText().trim();
    final Set<WidgetProperty<?>> filtered;
    if (search.trim().isEmpty())
        filtered = properties;
    else {
        // Filter properties, but preserve order (LinkedHashSet)
        filtered = new LinkedHashSet<>();
        for (WidgetProperty<?> prop : properties) if (prop.getDescription().toLowerCase().contains(search.toLowerCase()))
            filtered.add(prop);
    }
    final DisplayModel model = editor.getModel();
    section.clear();
    section.setClassMode(model != null && model.isClassModel());
    section.fill(editor.getUndoableActionManager(), filtered, other);
}
Also used : WidgetProperty(org.csstudio.display.builder.model.WidgetProperty) DisplayModel(org.csstudio.display.builder.model.DisplayModel)

Example 19 with WidgetProperty

use of org.csstudio.display.builder.model.WidgetProperty in project org.csstudio.display.builder by kasemir.

the class PropertyPanel method setSelectedWidgets.

/**
 * Populate UI with properties of widgets
 *  @param widgets Widgets to configure
 */
private void setSelectedWidgets(final List<Widget> widgets) {
    final DisplayModel model = editor.getModel();
    searchField.setDisable(widgets.isEmpty() && model == null);
    if (widgets.isEmpty()) {
        // Use the DisplayModel
        if (model != null)
            updatePropertiesView(model.getProperties(), Collections.emptyList());
    } else {
        // Determine common properties
        final List<Widget> other = new ArrayList<>(widgets);
        final Widget primary = other.remove(0);
        final Set<WidgetProperty<?>> properties = commonProperties(primary, other);
        updatePropertiesView(properties, other);
    }
}
Also used : WidgetProperty(org.csstudio.display.builder.model.WidgetProperty) DisplayModel(org.csstudio.display.builder.model.DisplayModel) Widget(org.csstudio.display.builder.model.Widget) ArrayList(java.util.ArrayList)

Example 20 with WidgetProperty

use of org.csstudio.display.builder.model.WidgetProperty in project org.csstudio.display.builder by kasemir.

the class RuleToScript method generatePy.

public static String generatePy(final Widget attached_widget, final RuleInfo rule) {
    final WidgetProperty<?> prop = attached_widget.getProperty(rule.getPropID());
    PropFormat pform = PropFormat.STRING;
    if (prop.getDefaultValue() instanceof Number)
        pform = PropFormat.NUMERIC;
    else if (prop.getDefaultValue() instanceof Boolean)
        pform = PropFormat.BOOLEAN;
    else if (prop.getDefaultValue() instanceof WidgetColor)
        pform = PropFormat.COLOR;
    final StringBuilder script = new StringBuilder();
    script.append("## Script for Rule: ").append(rule.getName()).append("\n\n");
    script.append("from org.csstudio.display.builder.runtime.script import PVUtil\n");
    if (pform == PropFormat.COLOR)
        script.append("from org.csstudio.display.builder.model.properties import WidgetColor\n");
    script.append("\n## Process variable extraction\n");
    script.append("## Use any of the following valid variable names in an expression:\n");
    final Map<String, String> pvm = pvNameOptions(rule.getPVs().size());
    for (Map.Entry<String, String> entry : pvm.entrySet()) {
        script.append("##     ").append(entry.getKey());
        if (entry.getKey().contains("Legacy"))
            script.append("  [DEPRECATED]");
        script.append("\n");
    }
    script.append("\n");
    // Check which pv* variables are actually used
    final Map<String, String> output_pvm = new HashMap<String, String>();
    for (ExpressionInfo<?> expr : rule.getExpressions()) {
        // Check the boolean expressions.
        // In principle, should parse an expression like
        // pv0 > 10
        // to see if it refers to the variable "pv0".
        // Instead of implementing a full parser, we
        // just check for "pv0" anywhere in the expression.
        // This will erroneously detect a variable reference in
        // len("Text with pv0")>4
        // which doesn't actually reference "pv0" as a variable,
        // but it doesn't matter if the script creates some
        // extra variable "pv0" which is then left unused.
        String expr_to_check = expr.getBoolExp();
        // simply including them in the string to check
        if (rule.getPropAsExprFlag())
            expr_to_check += " " + expr.getPropVal().toString();
        for (Map.Entry<String, String> entry : pvm.entrySet()) {
            final String varname = entry.getKey();
            if (expr_to_check.contains(varname))
                output_pvm.put(varname, entry.getValue());
        }
    }
    // Generate code that reads the required pv* variables from PVs
    for (Map.Entry<String, String> entry : output_pvm.entrySet()) script.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n");
    if (pform == PropFormat.COLOR) {
        // If property is a color, create variables for all the used colors
        script.append("\n## Define Colors\n");
        WidgetColor col = (WidgetColor) prop.getValue();
        script.append("colorCurrent = ").append("WidgetColor(").append(col.getRed()).append(", ").append(col.getGreen()).append(", ").append(col.getBlue()).append(")\n");
        if (!rule.getPropAsExprFlag()) {
            int idx = 0;
            for (ExpressionInfo<?> expr : rule.getExpressions()) {
                if (expr.getPropVal() instanceof WidgetProperty<?>) {
                    final Object value = ((WidgetProperty<?>) expr.getPropVal()).getValue();
                    if (value instanceof WidgetColor) {
                        col = (WidgetColor) value;
                        script.append("colorVal").append(idx).append(" = ").append("WidgetColor(").append(col.getRed()).append(", ").append(col.getGreen()).append(", ").append(col.getBlue()).append(")\n");
                    }
                }
                idx++;
            }
        }
    }
    script.append("\n## Script Body\n");
    String indent = "    ";
    final String setPropStr = "widget.setPropertyValue('" + rule.getPropID() + "', ";
    int idx = 0;
    for (ExpressionInfo<?> expr : rule.getExpressions()) {
        script.append((idx == 0) ? "if" : "elif");
        script.append(" ").append(javascriptToPythonLogic(expr.getBoolExp())).append(":\n");
        script.append(indent).append(setPropStr);
        if (rule.getPropAsExprFlag())
            script.append(javascriptToPythonLogic(expr.getPropVal().toString())).append(")\n");
        else
            script.append(formatPropVal((WidgetProperty<?>) expr.getPropVal(), idx, pform)).append(")\n");
        idx++;
    }
    if (idx > 0) {
        script.append("else:\n");
        script.append(indent).append(setPropStr).append(formatPropVal(prop, -1, pform)).append(")\n");
    } else
        script.append(setPropStr).append(formatPropVal(prop, -1, pform)).append(")\n");
    return script.toString();
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) WidgetColor(org.csstudio.display.builder.model.properties.WidgetColor) WidgetProperty(org.csstudio.display.builder.model.WidgetProperty) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

WidgetProperty (org.csstudio.display.builder.model.WidgetProperty)20 ArrayList (java.util.ArrayList)7 Map (java.util.Map)6 Widget (org.csstudio.display.builder.model.Widget)6 WidgetPropertyListener (org.csstudio.display.builder.model.WidgetPropertyListener)6 List (java.util.List)5 Level (java.util.logging.Level)5 DisplayModel (org.csstudio.display.builder.model.DisplayModel)5 VType (org.diirt.vtype.VType)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 RuntimePV (org.csstudio.display.builder.runtime.pv.RuntimePV)4 Collection (java.util.Collection)3 Label (javafx.scene.control.Label)3 ArrayWidgetProperty (org.csstudio.display.builder.model.ArrayWidgetProperty)3 RuntimePVListener (org.csstudio.display.builder.runtime.pv.RuntimePVListener)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Platform (javafx.application.Platform)2 Insets (javafx.geometry.Insets)2 Pos (javafx.geometry.Pos)2 TextField (javafx.scene.control.TextField)2