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