Search in sources :

Example 46 with DisplayModel

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

the class WidgetNaming method setDefaultName.

/**
 * Set widget's name
 *
 *  <p>Widget that already has unique name remains unchanged.
 *  Otherwise a "_123" instance index is added, using the next unused index.
 *  Name defaults to the widget type.
 *
 *  @param model  Model that will contain the widget (but doesn't, yet)
 *  @param widget Widget to name
 */
public void setDefaultName(final DisplayModel model, final Widget widget) {
    // Check that widget is not already in the model, because otherwise
    // a name lookup would find the widget itself and consider the name
    // as already "in use".
    DisplayModel widget_model;
    try {
        widget_model = widget.getDisplayModel();
    } catch (Exception ex) {
        // OK to not be in any model
        widget_model = null;
    }
    if (widget_model == model)
        throw new IllegalStateException(widget + " already in model " + model);
    String name = widget.getName();
    // Default to human-readable widget type
    if (name.isEmpty())
        name = WidgetFactory.getInstance().getWidgetDescriptor(widget.getType()).getName();
    // Does the name match "SomeName_14"?
    final Matcher matcher = pattern.matcher(name);
    final String base;
    int number;
    if (matcher.matches()) {
        base = matcher.group(1);
        number = Integer.parseInt(matcher.group(2));
    } else {
        base = name;
        number = 0;
    }
    // Check for the next unused instance of this widget name
    synchronized (max_used_instance) {
        final Integer used = max_used_instance.get(base);
        if (used != null) {
            number = Math.max(number, used + 1);
            name = base + "_" + number;
        }
        // Locate next available "SomeName_14"
        while (model.runtimeChildren().getChildByName(name) != null) {
            ++number;
            name = base + "_" + number;
        }
        max_used_instance.put(base, number);
    }
    widget.setPropertyValue(CommonWidgetProperties.propName, name);
    final ChildrenProperty children = ChildrenProperty.getChildren(widget);
    if (children != null) {
        for (Widget child : children.getValue()) setDefaultName(model, child);
    }
}
Also used : ChildrenProperty(org.csstudio.display.builder.model.ChildrenProperty) DisplayModel(org.csstudio.display.builder.model.DisplayModel) Matcher(java.util.regex.Matcher) Widget(org.csstudio.display.builder.model.Widget)

Example 47 with DisplayModel

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

the class ImageRepresentation method loadROI_Image.

private void loadROI_Image(final RegionOfInterest plot_roi, final ROIWidgetProperty model_roi) {
    String image_name = model_roi.file().getValue();
    Image image = null;
    try {
        image_name = MacroHandler.replace(model_widget.getMacrosOrProperties(), image_name);
        if (!image_name.isEmpty()) {
            // Resolve new image file relative to the source widget model (not 'top'!)
            // Get the display model from the widget tied to this representation
            final DisplayModel widget_model = model_widget.getDisplayModel();
            // Resolve the image path using the parent model file path
            image_name = ModelResourceUtil.resolveResource(widget_model, image_name);
            image = new Image(ModelResourceUtil.openResourceStream(image_name));
        }
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Cannot load ROI image " + image_name, ex);
    }
    plot_roi.setImage(image);
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) VImage(org.diirt.vtype.VImage) Image(javafx.scene.image.Image)

Example 48 with DisplayModel

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

the class RuntimeUtil method getScriptSupport.

/**
 * Obtain script support
 *
 *  <p>Script support is associated with the top-level display model
 *  and initialized on first access, i.e. each display has its own
 *  script support. Embedded displays use the script support of
 *  their parent display.
 *
 *  @param widget Widget
 *  @return {@link ScriptSupport} for the widget's top-level display model
 *  @throws Exception on error
 */
public static ScriptSupport getScriptSupport(final Widget widget) throws Exception {
    final DisplayModel model = widget.getTopDisplayModel();
    // So sync'ing on the ScriptSupport class
    synchronized (ScriptSupport.class) {
        ScriptSupport scripting = model.getUserData(Widget.USER_DATA_SCRIPT_SUPPORT);
        if (scripting == null) {
            // This takes about 3 seconds
            final long start = System.currentTimeMillis();
            scripting = new ScriptSupport();
            final long elapsed = System.currentTimeMillis() - start;
            logger.log(Level.FINE, "ScriptSupport created for {0} by {1} in {2} ms", new Object[] { model, widget, elapsed });
            model.setUserData(Widget.USER_DATA_SCRIPT_SUPPORT, scripting);
        }
        return scripting;
    }
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) ScriptSupport(org.csstudio.display.builder.runtime.script.internal.ScriptSupport)

Example 49 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel 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)

Example 50 with DisplayModel

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

the class RepresentationDemoJavaFXinSWT method main.

public static void main(final String[] args) throws Exception {
    // final DisplayModel model = ExampleModels.getModel(1);
    final DisplayModel model = ExampleModels.createModel();
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText(model.getPropertyValue(propName));
    shell.setLayout(new FillLayout());
    final JFX_SWT_Wrapper wrapper = new JFX_SWT_Wrapper(shell, () -> {
        toolkit = new JFXRepresentation(false);
        return new Scene(toolkit.createModelRoot());
    });
    final Scene scene = wrapper.getScene();
    JFXRepresentation.setSceneStyle(scene);
    final Parent parent = toolkit.getModelParent();
    toolkit.representModel(parent, model);
    final DummyRuntime runtime = new DummyRuntime(model);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    runtime.shutdown();
    toolkit.shutdown();
    display.dispose();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) DisplayModel(org.csstudio.display.builder.model.DisplayModel) Parent(javafx.scene.Parent) JFXRepresentation(org.csstudio.display.builder.representation.javafx.JFXRepresentation) FillLayout(org.eclipse.swt.layout.FillLayout) JFX_SWT_Wrapper(org.csstudio.javafx.swt.JFX_SWT_Wrapper) Scene(javafx.scene.Scene) Display(org.eclipse.swt.widgets.Display)

Aggregations

DisplayModel (org.csstudio.display.builder.model.DisplayModel)73 Widget (org.csstudio.display.builder.model.Widget)15 Test (org.junit.Test)10 LabelWidget (org.csstudio.display.builder.model.widgets.LabelWidget)9 GroupWidget (org.csstudio.display.builder.model.widgets.GroupWidget)7 EmbeddedDisplayRepresentationUtil.loadDisplayModel (org.csstudio.display.builder.representation.EmbeddedDisplayRepresentationUtil.loadDisplayModel)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 EmbeddedDisplayWidget (org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget)5 Image (javafx.scene.image.Image)4 File (java.io.File)3 InputStream (java.io.InputStream)3 Parent (javafx.scene.Parent)3 Macros (org.csstudio.display.builder.model.macros.Macros)3 PictureWidget (org.csstudio.display.builder.model.widgets.PictureWidget)3 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 Rectangle2D (javafx.geometry.Rectangle2D)2 WidgetFactoryUnitTest (org.csstudio.display.builder.model.WidgetFactoryUnitTest)2 WidgetProperty (org.csstudio.display.builder.model.WidgetProperty)2 ModelWriter (org.csstudio.display.builder.model.persist.ModelWriter)2