use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class WidgetNaming method setDefaultName.
/**
* Set widget's name
*
* <p>Widget that already has unique name remains unchanged.
* Otherwise a "_123" instance index is added, using the next unused index.
* Name defaults to the widget type.
*
* @param model Model that will contain the widget (but doesn't, yet)
* @param widget Widget to name
*/
public void setDefaultName(final DisplayModel model, final Widget widget) {
// Check that widget is not already in the model, because otherwise
// a name lookup would find the widget itself and consider the name
// as already "in use".
DisplayModel widget_model;
try {
widget_model = widget.getDisplayModel();
} catch (Exception ex) {
// OK to not be in any model
widget_model = null;
}
if (widget_model == model)
throw new IllegalStateException(widget + " already in model " + model);
String name = widget.getName();
// Default to human-readable widget type
if (name.isEmpty())
name = WidgetFactory.getInstance().getWidgetDescriptor(widget.getType()).getName();
// Does the name match "SomeName_14"?
final Matcher matcher = pattern.matcher(name);
final String base;
int number;
if (matcher.matches()) {
base = matcher.group(1);
number = Integer.parseInt(matcher.group(2));
} else {
base = name;
number = 0;
}
// Check for the next unused instance of this widget name
synchronized (max_used_instance) {
final Integer used = max_used_instance.get(base);
if (used != null) {
number = Math.max(number, used + 1);
name = base + "_" + number;
}
// Locate next available "SomeName_14"
while (model.runtimeChildren().getChildByName(name) != null) {
++number;
name = base + "_" + number;
}
max_used_instance.put(base, number);
}
widget.setPropertyValue(CommonWidgetProperties.propName, name);
final ChildrenProperty children = ChildrenProperty.getChildren(widget);
if (children != null) {
for (Widget child : children.getValue()) setDefaultName(model, child);
}
}
use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class ImageRepresentation method loadROI_Image.
private void loadROI_Image(final RegionOfInterest plot_roi, final ROIWidgetProperty model_roi) {
String image_name = model_roi.file().getValue();
Image image = null;
try {
image_name = MacroHandler.replace(model_widget.getMacrosOrProperties(), image_name);
if (!image_name.isEmpty()) {
// Resolve new image file relative to the source widget model (not 'top'!)
// Get the display model from the widget tied to this representation
final DisplayModel widget_model = model_widget.getDisplayModel();
// Resolve the image path using the parent model file path
image_name = ModelResourceUtil.resolveResource(widget_model, image_name);
image = new Image(ModelResourceUtil.openResourceStream(image_name));
}
} catch (Exception ex) {
logger.log(Level.WARNING, "Cannot load ROI image " + image_name, ex);
}
plot_roi.setImage(image);
}
use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class RuntimeUtil method getScriptSupport.
/**
* Obtain script support
*
* <p>Script support is associated with the top-level display model
* and initialized on first access, i.e. each display has its own
* script support. Embedded displays use the script support of
* their parent display.
*
* @param widget Widget
* @return {@link ScriptSupport} for the widget's top-level display model
* @throws Exception on error
*/
public static ScriptSupport getScriptSupport(final Widget widget) throws Exception {
final DisplayModel model = widget.getTopDisplayModel();
// So sync'ing on the ScriptSupport class
synchronized (ScriptSupport.class) {
ScriptSupport scripting = model.getUserData(Widget.USER_DATA_SCRIPT_SUPPORT);
if (scripting == null) {
// This takes about 3 seconds
final long start = System.currentTimeMillis();
scripting = new ScriptSupport();
final long elapsed = System.currentTimeMillis() - start;
logger.log(Level.FINE, "ScriptSupport created for {0} by {1} in {2} ms", new Object[] { model, widget, elapsed });
model.setUserData(Widget.USER_DATA_SCRIPT_SUPPORT, scripting);
}
return scripting;
}
}
use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class WidgetRuntime method startScripts.
/**
* Start Scripts
*/
private void startScripts() {
// Start scripts triggered by PVs
final List<ScriptInfo> script_infos = widget.propScripts().getValue();
final List<RuleInfo> rule_infos = widget.propRules().getValue();
if ((script_infos.size() > 0) || (rule_infos.size() > 0)) {
final List<RuntimeScriptHandler> handlers = new ArrayList<>(script_infos.size() + rule_infos.size());
for (final ScriptInfo script_info : script_infos) {
try {
handlers.add(new RuntimeScriptHandler(widget, script_info));
} catch (final Exception ex) {
final StringBuilder buf = new StringBuilder();
buf.append("Script failed to compile\n");
try {
final DisplayModel model = widget.getDisplayModel();
buf.append("Display '").append(model.getDisplayName()).append("', ");
} catch (Exception ignore) {
// Skip display model
}
buf.append(widget).append(", ").append(script_info.getPath());
logger.log(Level.WARNING, buf.toString(), ex);
}
}
for (final RuleInfo rule_info : rule_infos) {
try {
handlers.add(new RuntimeScriptHandler(widget, rule_info));
} catch (final Exception ex) {
final StringBuilder buf = new StringBuilder();
buf.append("Rule failed to compile\n");
try {
final DisplayModel model = widget.getDisplayModel();
buf.append("Display '").append(model.getDisplayName()).append("', ");
} catch (Exception ignore) {
// Skip display model
}
buf.append(widget).append(", ").append(rule_info.getName());
logger.log(Level.WARNING, buf.toString(), ex);
}
}
script_handlers = handlers;
}
// Compile scripts invoked by actions
final List<ActionInfo> actions = widget.propActions().getValue().getActions();
if (actions.size() > 0) {
final Map<ExecuteScriptActionInfo, Script> scripts = new HashMap<>();
for (ActionInfo action_info : actions) {
if (!(action_info instanceof ExecuteScriptActionInfo))
continue;
final ExecuteScriptActionInfo script_action = (ExecuteScriptActionInfo) action_info;
try {
final MacroValueProvider macros = widget.getMacrosOrProperties();
final Script script = RuntimeScriptHandler.compileScript(widget, macros, script_action.getInfo());
scripts.put(script_action, script);
} catch (final Exception ex) {
final StringBuilder buf = new StringBuilder();
buf.append("Script for action failed to compile\n");
try {
final DisplayModel model = widget.getDisplayModel();
buf.append("Display '").append(model.getDisplayName()).append("', ");
} catch (Exception ignore) {
// Skip display model
}
buf.append(widget).append(", ").append(script_action);
logger.log(Level.WARNING, buf.toString(), ex);
}
}
if (scripts.size() > 0)
action_scripts = scripts;
}
// Signal that start() has completed
started.countDown();
}
use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class RepresentationDemoJavaFXinSWT method main.
public static void main(final String[] args) throws Exception {
// final DisplayModel model = ExampleModels.getModel(1);
final DisplayModel model = ExampleModels.createModel();
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText(model.getPropertyValue(propName));
shell.setLayout(new FillLayout());
final JFX_SWT_Wrapper wrapper = new JFX_SWT_Wrapper(shell, () -> {
toolkit = new JFXRepresentation(false);
return new Scene(toolkit.createModelRoot());
});
final Scene scene = wrapper.getScene();
JFXRepresentation.setSceneStyle(scene);
final Parent parent = toolkit.getModelParent();
toolkit.representModel(parent, model);
final DummyRuntime runtime = new DummyRuntime(model);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
runtime.shutdown();
toolkit.shutdown();
display.dispose();
}
Aggregations