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.");
}
}
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);
}
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);
}
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();
}
}
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);
}
Aggregations