Search in sources :

Example 1 with ToolkitRepresentation

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

Aggregations

DisplayModel (org.csstudio.display.builder.model.DisplayModel)1 Macros (org.csstudio.display.builder.model.macros.Macros)1 ToolkitRepresentation (org.csstudio.display.builder.representation.ToolkitRepresentation)1