use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class WidgetTransfer method addDragSupport.
// Could create custom data format, or use "application/xml".
// Transferring as DataFormat("text/plain"), however, allows exchange
// with basic text editor, which can be very convenient.
/**
* Add support for 'dragging' a widget out of a node
*
* @param source Source {@link Node}
* @param selection
* @param desc Description of widget type to drag
* @param image Image to represent the widget, or <code>null</code>
*/
public static void addDragSupport(final Node source, final DisplayEditor editor, final Palette palette, final WidgetDescriptor descriptor, final Image image) {
source.setOnDragDetected((MouseEvent event) -> {
logger.log(Level.FINE, "Starting drag for {0}", descriptor);
editor.getWidgetSelectionHandler().clear();
final Widget widget = descriptor.createWidget();
// In class editor mode, create widget with some class name.
// In display editor mode, apply the class settings.
final DisplayModel model = editor.getModel();
if (model != null && model.isClassModel()) {
widget.propName().setValue("MY_CLASS");
} else {
WidgetClassesService.getWidgetClasses().apply(widget);
}
final String xml;
try {
xml = ModelWriter.getXML(Arrays.asList(widget));
} catch (Exception ex) {
logger.log(Level.WARNING, "Cannot drag-serialize", ex);
return;
}
final Dragboard db = source.startDragAndDrop(TransferMode.COPY);
final ClipboardContent content = new ClipboardContent();
content.putString(xml);
db.setContent(content);
final int width = widget.propWidth().getValue();
final int height = widget.propHeight().getValue();
db.setDragView(createDragImage(widget, image, width, height), 0, 0);
event.consume();
});
source.setOnDragDone(event -> {
// Widget was dropped
// -> Stop scrolling, clear the selected palette entry
editor.getAutoScrollHandler().canceTimeline();
palette.clearSelectedWidgetType();
});
}
use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class XYPlotWidgetTest method testXML.
/**
* Check if XML out/in preserves the Y axis
* @throws Exception on error
*/
@Test
public void testXML() throws Exception {
XYPlotWidget plot = new XYPlotWidget();
assertYAxis(plot);
final String xml = ModelWriter.getXML(Arrays.asList(plot));
System.out.println(xml);
final DisplayModel model = ModelReader.parseXML(xml);
final List<Widget> widgets = model.getChildren();
assertThat(widgets.size(), equalTo(1));
assertThat(widgets.get(0), instanceOf(XYPlotWidget.class));
plot = (XYPlotWidget) widgets.get(0);
assertYAxis(plot);
}
use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class MacroHierarchyUnitTest method testTimeOfExpansion.
/**
* Test when macros get expanded
* @throws Exception on error
*/
@Test
public void testTimeOfExpansion() throws Exception {
// model -> group -> subgroup -> label
final DisplayModel model = new DisplayModel();
final GroupWidget group = new GroupWidget();
model.runtimeChildren().addChild(group);
final GroupWidget subgroup = new GroupWidget();
group.runtimeChildren().addChild(subgroup);
final LabelWidget label = new LabelWidget();
subgroup.runtimeChildren().addChild(label);
// Sanity check: Straight forward macro value replacement.
// Display value replaced by group,
// then replaced by subgroup,
// so label sees "subgroup"
model.propMacros().getValue().add("P", "display");
group.propMacros().getValue().add("P", "group");
subgroup.propMacros().getValue().add("P", "subgroup");
Macros macros = label.getEffectiveMacros();
System.out.println(macros);
assertThat(macros.getValue("P"), equalTo("subgroup"));
// When are macros expanded?
// In BOY, they were mostly expanded when set,
// except the following example would fail if all widgets
// were within one display.
model.propMacros().getValue().add("P", "display");
// If macros are expanded early on,
// this sets SAVE=display,
// then redefines P
group.propMacros().getValue().add("SAVE", "$(P)");
group.propMacros().getValue().add("P", "group");
// .. and this would restore P='display', since that's what's im $(SAVE):
subgroup.propMacros().getValue().add("P", "$(SAVE)");
// With lazy macro expansion,
// the label widget would have P=$(SAVE), SAVE=$(P),
// so $(P) results in a "recursive macro" error.
// When macros are expanded as the runtime starts..
DisplayMacroExpander.expandDisplayMacros(model);
// ..you get $(P)="display"
macros = label.getEffectiveMacros();
System.out.println(macros);
assertThat(macros.getValue("P"), equalTo("display"));
assertThat(MacroHandler.replace(macros, "$(P)"), equalTo("display"));
}
use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class WidgetTree method setModel.
/**
* @param model Model to display as widget tree
*/
public void setModel(final DisplayModel model) {
// Could recursively remove all old model tree elements,
// on UI thread, one by one.
// Faster: Unlink listeners and then replace the whole
// tree model which was created in background.
final DisplayModel old_model = this.model;
if (old_model != null) {
old_model.runtimeChildren().removePropertyListener(children_listener);
for (Widget widget : old_model.runtimeChildren().getValue()) removeWidgetListeners(widget);
widget2tree.clear();
tab_name2tree.clear();
}
this.model = model;
// Might be called on UI thread, move off
EditorUtil.getExecutor().execute(() -> {
final TreeItem<WidgetOrTab> root = new TreeItem<WidgetOrTab>(WidgetOrTab.of(model));
if (model != null) {
widget2tree.put(model, root);
for (Widget widget : model.runtimeChildren().getValue()) addWidget(widget, -1);
root.setExpanded(true);
model.runtimeChildren().addPropertyListener(children_listener);
}
logger.log(Level.FINE, "Computed new tree on {0}, updating UI", Thread.currentThread().getName());
Platform.runLater(() -> {
tree_view.setRoot(root);
setSelectedWidgets(editor.getWidgetSelectionHandler().getSelection());
});
});
}
use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.
the class PersistenceUnitTest method testDisplayModelPersistence.
/**
* Test persistence of display model's properties
* @throws Exception on error
*/
@Test
public void testDisplayModelPersistence() throws Exception {
final DisplayModel model = new DisplayModel();
model.getProperty("width").setValueFromObject(400);
model.getProperty("height").setValueFromObject(800);
final Widget widget = new Widget("base");
widget.setPropertyValue(propName, "Test");
widget.getProperty("x").setValueFromObject(42);
model.runtimeChildren().addChild(widget);
final String xml = toXML(model);
System.out.println(xml);
final ModelReader reader = new ModelReader(new ByteArrayInputStream(xml.getBytes()));
final DisplayModel readback = reader.readModel();
assertThat(readback.getPropertyValue(CommonWidgetProperties.propWidth), equalTo(400));
assertThat(readback.getPropertyValue(CommonWidgetProperties.propHeight), equalTo(800));
assertThat(readback.getChildren().size(), equalTo(1));
assertThat(readback.getChildren().get(0).getPropertyValue(CommonWidgetProperties.propX), equalTo(42));
}
Aggregations