Search in sources :

Example 1 with MacroValueProvider

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

the class TooltipSupport method attach.

/**
 * Attach tool tip
 *  @param node Node that should have the tool tip
 *  @param tooltip_property Tool tip to show
 */
public static void attach(final Node node, final WidgetProperty<String> tooltip_property) {
    // Patch legacy tool tips that defaulted to pv name & value,
    // even for static widgets
    final StringWidgetProperty ttp = (StringWidgetProperty) tooltip_property;
    if (legacy_tooltip.matcher(ttp.getSpecification()).matches() && !tooltip_property.getWidget().checkProperty("pv_name").isPresent())
        ttp.setSpecification("");
    // Avoid listener and code to remove/add tooltip at runtime.
    if (tooltip_property.getValue().isEmpty())
        return;
    final Tooltip tooltip = new Tooltip();
    tooltip.setWrapText(true);
    // Evaluate the macros in tool tip specification each time
    // the tool tip is about to show
    tooltip.setOnShowing(event -> {
        final String spec = ((MacroizedWidgetProperty<?>) tooltip_property).getSpecification();
        final Widget widget = tooltip_property.getWidget();
        final MacroValueProvider macros = widget.getMacrosOrProperties();
        String expanded;
        try {
            expanded = MacroHandler.replace(macros, spec);
            tooltip.setText(expanded);
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Cannot evaluate tooltip of " + widget, ex);
            tooltip.setText(spec);
        }
    });
    Tooltip.install(node, tooltip);
    if (!initialized_behavior) {
        // Unfortunately, no API to control when tooltop shows, and for how long.
        // http://stackoverflow.com/questions/26854301/control-javafx-tooltip-delay
        // has the hack used in here, which only needs to be applied once
        // because it changes a static BEHAVIOR inside the Tooltip.
        // Java 9 will offer API, https://bugs.openjdk.java.net/browse/JDK-8090477
        hack_behavior(tooltip);
        initialized_behavior = true;
    }
}
Also used : MacroizedWidgetProperty(org.csstudio.display.builder.model.MacroizedWidgetProperty) MacroValueProvider(org.csstudio.display.builder.model.macros.MacroValueProvider) StringWidgetProperty(org.csstudio.display.builder.model.properties.StringWidgetProperty) Tooltip(javafx.scene.control.Tooltip) Widget(org.csstudio.display.builder.model.Widget)

Example 2 with MacroValueProvider

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

the class ActionButtonRepresentation method makeActionText.

private String makeActionText(final ActionInfo action) {
    String action_str = action.getDescription();
    if (action_str.isEmpty())
        action_str = action.toString();
    String expanded;
    try {
        final MacroValueProvider macros = model_widget.getMacrosOrProperties();
        expanded = MacroHandler.replace(macros, action_str);
    } catch (final Exception ex) {
        logger.log(Level.WARNING, model_widget + " action " + action + " cannot expand macros for " + action_str, ex);
        expanded = action_str;
    }
    return expanded;
}
Also used : MacroValueProvider(org.csstudio.display.builder.model.macros.MacroValueProvider)

Example 3 with MacroValueProvider

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

the class MacroizedWidgetProperty method getValue.

/**
 * Evaluates value based on specification
 *  @return Current value of the property
 */
@Override
public synchronized T getValue() {
    if (value == null) {
        final MacroValueProvider macros = widget.getMacrosOrProperties();
        String expanded;
        try {
            expanded = MacroHandler.replace(macros, specification);
        } catch (final Exception ex) {
            logger.log(Level.WARNING, widget + " property " + getName() + " cannot expand macros for '" + specification + "'", ex);
            expanded = specification;
        }
        // as there is just one escaped macro, as in "$(MISSING_AND_IGNORED) \\$(ESCAPED)"
        if (MacroHandler.containsMacros(expanded) && !specification.contains("\\$"))
            logger.log(Level.INFO, widget + " '" + getName() + "' is not fully resolved: " + expanded);
        try {
            // Do NOT notify listeners.
            // Otherwise, if property.getValue() is called within a listener
            // and the listener is registered to fire when setValue() is called
            // then listener -> getValue() -> setValue() -> call listener again ..
            doSetValue(parseExpandedSpecification(expanded), false);
        } catch (final Exception ex) {
            logger.log(Level.WARNING, widget + " property " + getName() + " cannot evaluate '" + expanded + "'", ex);
            value = default_value;
        }
    }
    return value;
}
Also used : MacroValueProvider(org.csstudio.display.builder.model.macros.MacroValueProvider)

Example 4 with MacroValueProvider

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

the class RuntimeScriptHandler method createPVs.

private void createPVs() throws Exception {
    // Create PVs
    final WidgetRuntime<Widget> runtime = WidgetRuntime.ofWidget(widget);
    final MacroValueProvider macros = widget.getMacrosOrProperties();
    for (int i = 0; i < pvs.length; ++i) {
        final String pv_name = MacroHandler.replace(macros, infos.get(i).getName());
        pvs[i] = PVFactory.getPV(pv_name);
        subscribed[i] = new AtomicBoolean(true);
        runtime.addPV(pvs[i]);
    }
    // Will later unsubscribe from non-trigger PVs
    for (int i = 0; i < pvs.length; ++i) pvs[i].addListener(this);
    // disconnected
    if (!check_connections)
        script.submit(widget, pvs);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MacroValueProvider(org.csstudio.display.builder.model.macros.MacroValueProvider) Widget(org.csstudio.display.builder.model.Widget)

Example 5 with MacroValueProvider

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

the class WidgetRuntime method startScripts.

/**
 * Start Scripts
 */
private void startScripts() {
    // Start scripts triggered by PVs
    final List<ScriptInfo> script_infos = widget.propScripts().getValue();
    final List<RuleInfo> rule_infos = widget.propRules().getValue();
    if ((script_infos.size() > 0) || (rule_infos.size() > 0)) {
        final List<RuntimeScriptHandler> handlers = new ArrayList<>(script_infos.size() + rule_infos.size());
        for (final ScriptInfo script_info : script_infos) {
            try {
                handlers.add(new RuntimeScriptHandler(widget, script_info));
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Script failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(script_info.getPath());
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        for (final RuleInfo rule_info : rule_infos) {
            try {
                handlers.add(new RuntimeScriptHandler(widget, rule_info));
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Rule failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(rule_info.getName());
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        script_handlers = handlers;
    }
    // Compile scripts invoked by actions
    final List<ActionInfo> actions = widget.propActions().getValue().getActions();
    if (actions.size() > 0) {
        final Map<ExecuteScriptActionInfo, Script> scripts = new HashMap<>();
        for (ActionInfo action_info : actions) {
            if (!(action_info instanceof ExecuteScriptActionInfo))
                continue;
            final ExecuteScriptActionInfo script_action = (ExecuteScriptActionInfo) action_info;
            try {
                final MacroValueProvider macros = widget.getMacrosOrProperties();
                final Script script = RuntimeScriptHandler.compileScript(widget, macros, script_action.getInfo());
                scripts.put(script_action, script);
            } catch (final Exception ex) {
                final StringBuilder buf = new StringBuilder();
                buf.append("Script for action failed to compile\n");
                try {
                    final DisplayModel model = widget.getDisplayModel();
                    buf.append("Display '").append(model.getDisplayName()).append("', ");
                } catch (Exception ignore) {
                // Skip display model
                }
                buf.append(widget).append(", ").append(script_action);
                logger.log(Level.WARNING, buf.toString(), ex);
            }
        }
        if (scripts.size() > 0)
            action_scripts = scripts;
    }
    // Signal that start() has completed
    started.countDown();
}
Also used : Script(org.csstudio.display.builder.runtime.script.internal.Script) HashMap(java.util.HashMap) ScriptInfo(org.csstudio.display.builder.model.properties.ScriptInfo) 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) ExecuteScriptActionInfo(org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo) MacroValueProvider(org.csstudio.display.builder.model.macros.MacroValueProvider) DisplayModel(org.csstudio.display.builder.model.DisplayModel) RuntimeScriptHandler(org.csstudio.display.builder.runtime.script.internal.RuntimeScriptHandler) RuleInfo(org.csstudio.display.builder.model.rules.RuleInfo)

Aggregations

MacroValueProvider (org.csstudio.display.builder.model.macros.MacroValueProvider)6 HashMap (java.util.HashMap)2 Widget (org.csstudio.display.builder.model.Widget)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Tooltip (javafx.scene.control.Tooltip)1 IMacroTableProvider (org.csstudio.apputil.macros.IMacroTableProvider)1 InfiniteLoopException (org.csstudio.apputil.macros.InfiniteLoopException)1 MacroTable (org.csstudio.apputil.macros.MacroTable)1 MacroUtil (org.csstudio.apputil.macros.MacroUtil)1 DisplayModel (org.csstudio.display.builder.model.DisplayModel)1 MacroizedWidgetProperty (org.csstudio.display.builder.model.MacroizedWidgetProperty)1 ActionInfo (org.csstudio.display.builder.model.properties.ActionInfo)1 ExecuteScriptActionInfo (org.csstudio.display.builder.model.properties.ExecuteScriptActionInfo)1