Search in sources :

Example 51 with DisplayModel

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

the class ActionUtil method openDisplay.

/**
 * Open a display
 *
 *  <p>Depending on the target of the action,
 *  this will open a new display or replace
 *  an existing display
 *
 *  @param source_widget Widget from which the action is invoked
 *                       Used to resolve the potentially relative path of the
 *                       display specified in the action
 *  @param action        Information on which display to open and how
 */
private static void openDisplay(final Widget source_widget, final OpenDisplayActionInfo action) {
    if (action.getFile().isEmpty()) {
        logger.log(Level.WARNING, "Action without file: {0}", action);
        return;
    }
    try {
        // Path to resolve, after expanding macros of source widget and action
        final Macros macros = Macros.merge(source_widget.getEffectiveMacros(), action.getMacros());
        final String expanded_path = MacroHandler.replace(macros, action.getFile());
        logger.log(Level.FINER, "{0}, effective macros {1} ({2})", new Object[] { action, macros, expanded_path });
        // Resolve new display file relative to the source widget model (not 'top'!)
        final DisplayModel widget_model = source_widget.getDisplayModel();
        final String parent_file = widget_model.getUserData(DisplayModel.USER_DATA_INPUT_FILE);
        // Load new model. If that fails, no reason to continue.
        final DisplayModel new_model = ModelLoader.resolveAndLoadModel(parent_file, expanded_path);
        // Model is standalone; source_widget (Action button, ..) is _not_ the parent,
        // but it does add macros to those already defined in the display file.
        final Macros combined_macros = Macros.merge(macros, new_model.propMacros().getValue());
        new_model.propMacros().setValue(combined_macros);
        // Schedule representation on UI thread...
        final DisplayModel top_model = source_widget.getTopDisplayModel();
        final ToolkitRepresentation<Object, Object> toolkit = ToolkitRepresentation.getToolkit(top_model);
        final Future<Object> wait_for_ui;
        if (action.getTarget() == OpenDisplayActionInfo.Target.TAB) {
            wait_for_ui = toolkit.submit(() -> {
                // Create new panel
                final ToolkitRepresentation<Object, Object> new_toolkit = toolkit.openPanel(new_model, ActionUtil::handleClose);
                RuntimeUtil.hookRepresentationListener(new_toolkit);
                return null;
            });
        } else if (action.getTarget() == OpenDisplayActionInfo.Target.WINDOW) {
            wait_for_ui = toolkit.submit(() -> {
                // Create new top-level window
                final ToolkitRepresentation<Object, Object> new_toolkit = toolkit.openNewWindow(new_model, ActionUtil::handleClose);
                RuntimeUtil.hookRepresentationListener(new_toolkit);
                return null;
            });
        } else if (action.getTarget() == OpenDisplayActionInfo.Target.STANDALONE) {
            wait_for_ui = toolkit.submit(() -> {
                final ToolkitRepresentation<Object, Object> new_toolkit = toolkit.openStandaloneWindow(new_model, ActionUtil::handleClose);
                RuntimeUtil.hookRepresentationListener(new_toolkit);
                return null;
            });
        } else {
            // Default to OpenDisplayActionInfo.Target.REPLACE
            // Stop old runtime.
            RuntimeUtil.stopRuntime(top_model);
            wait_for_ui = toolkit.submit(() -> {
                // Close old representation
                final Object parent = toolkit.disposeRepresentation(top_model);
                // Tell toolkit about new model to represent
                toolkit.representModel(parent, new_model);
                return null;
            });
        }
        wait_for_ui.get();
        // Back in background thread, create new runtime
        RuntimeUtil.startRuntime(new_model);
    } 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 : ToolkitRepresentation(org.csstudio.display.builder.representation.ToolkitRepresentation) DisplayModel(org.csstudio.display.builder.model.DisplayModel) Macros(org.csstudio.display.builder.model.macros.Macros)

Example 52 with DisplayModel

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

the class ActionUtil method resolve.

private static String resolve(final Widget source_widget, final String path) throws Exception {
    // Path to resolve, after expanding macros
    final Macros macros = source_widget.getEffectiveMacros();
    final String expanded_path = MacroHandler.replace(macros, path);
    // Resolve file relative to the source widget model (not 'top'!)
    final DisplayModel widget_model = source_widget.getDisplayModel();
    final String parent_file = widget_model.getUserData(DisplayModel.USER_DATA_INPUT_FILE);
    return ModelResourceUtil.resolveResource(parent_file, expanded_path);
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) Macros(org.csstudio.display.builder.model.macros.Macros)

Example 53 with DisplayModel

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

the class ScriptUtil method playAudio.

/**
 * Play audio
 *
 *  <p>Audio file can be a file
 *  in the same directory as the display,
 *  a workspace path if running under RCP,
 *  an absolute file system path,
 *  or a http URL.
 *
 *  <p>Result can be used to await end of playback via `get()`,
 *  to poll via 'isDone()',
 *  or to `cancel(true)` the playback.
 *  Caller should keep a reference to the future until
 *  and of playback, because otherwise garbage collection
 *  can end the playback.
 *
 *  @param widget Widget, used to coordinate with toolkit
 *  @param audio_file Audio file
 *  @return Handle for controlling the playback
 */
public static Future<Boolean> playAudio(final Widget widget, final String audio_file) {
    try {
        final DisplayModel widget_model = widget.getDisplayModel();
        final String resolved = ModelResourceUtil.resolveResource(widget_model, audio_file);
        String url;
        if (resolved.startsWith("http:") || resolved.startsWith("https:"))
            url = resolved;
        else {
            final String local = ModelResourceUtil.getLocalPath(resolved);
            if (local != null)
                url = new File(local).toURI().toString();
            else
                url = new File(resolved).toURI().toString();
        }
        return ToolkitRepresentation.getToolkit(widget.getDisplayModel()).playAudio(url);
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Error playing audio " + audio_file, ex);
    }
    return CompletableFuture.completedFuture(false);
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) File(java.io.File)

Example 54 with DisplayModel

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

the class RepresentationDemoJavaFX method start.

@Override
public void start(final Stage stage) {
    try {
        final DisplayModel model = ExampleModels.createModel();
        final JFXStageRepresentation toolkit = new JFXStageRepresentation(stage);
        final Parent parent = toolkit.configureStage(model, this::close);
        toolkit.representModel(parent, model);
        runtime = new DummyRuntime(model);
    } catch (final Exception ex) {
        ex.printStackTrace();
        Platform.exit();
    }
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) JFXStageRepresentation(org.csstudio.display.builder.representation.javafx.JFXStageRepresentation) Parent(javafx.scene.Parent)

Example 55 with DisplayModel

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

the class DisplayEditorPart method setModel.

private void setModel(final DisplayModel model) {
    final DisplayModel old_model = editor.getModel();
    if (old_model != null)
        old_model.propName().removePropertyListener(model_name_listener);
    if (model == null)
        return;
    // In UI thread..
    toolkit.execute(() -> {
        setPartName(model.getDisplayName());
        editor.setModel(model);
        if (outline_page != null)
            outline_page.setModel(model);
    });
    model.propName().addPropertyListener(model_name_listener);
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel)

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