Search in sources :

Example 36 with DisplayModel

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

the class ActionUtil method openFile.

/**
 * Open a file
 *  @param source_widget Widget from which the action is invoked.
 *                       Used to resolve the potentially relative path of the
 *                       file specified in the action
 *  @param action        Information on which file to open
 */
private static void openFile(final Widget source_widget, final OpenFileActionInfo action) {
    if (action.getFile().isEmpty()) {
        logger.log(Level.WARNING, "Action without file: {0}", action);
        return;
    }
    try {
        final String resolved_name = resolve(source_widget, action.getFile());
        final DisplayModel top_model = source_widget.getTopDisplayModel();
        final ToolkitRepresentation<Object, Object> toolkit = ToolkitRepresentation.getToolkit(top_model);
        toolkit.execute(() -> {
            try {
                toolkit.openFile(resolved_name);
            } catch (Exception ex) {
                logger.log(Level.WARNING, "Cannot open " + action, ex);
                toolkit.showErrorDialog(source_widget, "Cannot open " + resolved_name);
            }
        });
    } catch (final Exception ex) {
        logger.log(Level.WARNING, "Error handling " + action, ex);
        ScriptUtil.showErrorDialog(source_widget, "Cannot open " + action.getFile() + ".\n\nSee log for details.");
    }
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel)

Example 37 with DisplayModel

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

the class ScriptUtil method closeDisplay.

/**
 * Close a display
 *
 *  @param widget Widget within the display to close
 */
public static void closeDisplay(final Widget widget) {
    try {
        final DisplayModel model = widget.getTopDisplayModel();
        final ToolkitRepresentation<Object, Object> toolkit = ToolkitRepresentation.getToolkit(model);
        toolkit.closeWindow(model);
    } catch (Throwable ex) {
        logger.log(Level.WARNING, "Cannot close display", ex);
    }
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel)

Example 38 with DisplayModel

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

the class RuntimeScriptHandler method compileScript.

/**
 * Helper to compile script
 *
 *  <p>Resolves script path based on macros and display,
 *  can be invoked by other code.
 *
 *  @param widget Widget on which the script is invoked
 *  @param macros
 *  @param script_info Script to compile
 *  @return Compiled script
 *  @throws Exception on error
 */
public static Script compileScript(final Widget widget, final MacroValueProvider macros, final ScriptInfo script_info) throws Exception {
    // Compile script
    final String script_name = MacroHandler.replace(macros, script_info.getPath());
    final ScriptSupport scripting = RuntimeUtil.getScriptSupport(widget);
    final InputStream stream;
    final DisplayModel model = widget.getDisplayModel();
    final String parent_display = model.getUserData(DisplayModel.USER_DATA_INPUT_FILE);
    final String path;
    if (script_info.getText() == null) {
        // Load external script
        final String resolved = ModelResourceUtil.resolveResource(parent_display, script_name);
        stream = ModelResourceUtil.openResourceStream(resolved);
        path = ModelResourceUtil.getDirectory(ModelResourceUtil.getLocalPath(resolved));
    } else {
        // Use script text that was embedded in display
        stream = new ByteArrayInputStream(script_info.getText().getBytes());
        path = ModelResourceUtil.getDirectory(ModelResourceUtil.getLocalPath(parent_display));
    }
    return scripting.compile(path, script_name, stream);
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 39 with DisplayModel

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

the class DisplayEditor method pasteFromClipboard.

/**
 * Paste widgets from clipboard
 *  @param x Desired coordinate of upper-left widget ..
 *  @param y .. when pasted
 */
public void pasteFromClipboard(int x, int y) {
    if (selection_tracker.isInlineEditorActive())
        return;
    final String xml = Clipboard.getSystemClipboard().getString();
    // Anything on clipboard?
    if (xml == null)
        return;
    // Does it look like widget XML?
    if (!(xml.startsWith("<?xml") && xml.contains("<display")))
        return;
    // Correct the y coordinate, measured inside this editor,
    // by height of toolbar
    y -= toolbar.getHeight();
    // Correct coordinates by zoom factor
    final double zoom = toolkit.getZoom();
    x = (int) Math.round(x / zoom);
    y = (int) Math.round(y / zoom);
    try {
        final DisplayModel model = ModelReader.parseXML(xml);
        final List<Widget> widgets = model.getChildren();
        logger.log(Level.FINE, "Pasted {0} widgets", widgets.size());
        GeometryTools.moveWidgets(x, y, widgets);
        final Rectangle2D bounds = GeometryTools.getBounds(widgets);
        // Potentially activate group at drop point
        group_handler.locateParent(x, y, bounds.getWidth(), bounds.getHeight());
        addWidgets(widgets);
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Failed to paste content of clipboard", ex);
    }
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) ArrayWidget(org.csstudio.display.builder.model.widgets.ArrayWidget) Widget(org.csstudio.display.builder.model.Widget) Rectangle2D(javafx.geometry.Rectangle2D)

Example 40 with DisplayModel

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

the class EditorDemoGUI method loadModel.

/**
 * Load model from file
 *  @param file File that contains the model
 */
public void loadModel(final File file) {
    EditorUtil.getExecutor().execute(() -> {
        try {
            final DisplayModel model = ModelLoader.loadModel(new FileInputStream(file), file.getCanonicalPath());
            setModel(model);
            this.file = file;
        } catch (final Exception ex) {
            logger.log(Level.SEVERE, "Cannot start", ex);
        }
    });
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) FileInputStream(java.io.FileInputStream)

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