use of org.csstudio.display.builder.model.widgets.GroupWidget in project org.csstudio.display.builder by kasemir.
the class MacroHierarchyUnitTest method testMacroHierarchy.
/**
* Test Macro Hierarchy
* @throws Exception on error
*/
@Test
public void testMacroHierarchy() throws Exception {
// Macros start out empty
MacroValueProvider macros = new Macros();
System.out.println(macros);
assertThat(macros.toString(), equalTo("[]"));
// Preferences (at least in test setup where there is no preferences service)
macros = Preferences.getMacros();
System.out.println(macros);
assertThat(macros.getValue("EXAMPLE_MACRO"), equalTo("Value from Preferences"));
// Display model uses preferences
final DisplayModel model = new DisplayModel();
macros = model.getEffectiveMacros();
System.out.println(macros);
assertThat(macros.getValue("EXAMPLE_MACRO"), equalTo("Value from Preferences"));
// .. but display can replace this value
model.propMacros().getValue().add("EXAMPLE_MACRO", "Value from Display");
macros = model.getEffectiveMacros();
System.out.println(macros);
assertThat(macros.getValue("EXAMPLE_MACRO"), equalTo("Value from Display"));
// Similar, groups can replace macros
final LabelWidget child = new LabelWidget();
final GroupWidget group2 = new GroupWidget();
group2.propMacros().getValue().add("EXAMPLE_MACRO", "In Group 2");
group2.runtimeChildren().addChild(child);
final GroupWidget group1 = new GroupWidget();
group1.propMacros().getValue().add("EXAMPLE_MACRO", "In Group 1");
group1.runtimeChildren().addChild(group2);
model.runtimeChildren().addChild(group1);
macros = group1.getEffectiveMacros();
System.out.println(macros);
assertThat(macros.getValue("EXAMPLE_MACRO"), equalTo("In Group 1"));
macros = group2.getEffectiveMacros();
System.out.println(macros);
assertThat(macros.getValue("EXAMPLE_MACRO"), equalTo("In Group 2"));
macros = child.getEffectiveMacros();
System.out.println(macros);
assertThat(macros.getValue("EXAMPLE_MACRO"), equalTo("In Group 2"));
// Finally, the EmbeddedDisplayWidget can replace macros,
// but testing that requires the runtime to load the embedded content
// --> Leaving that to examples/macros
}
use of org.csstudio.display.builder.model.widgets.GroupWidget in project org.csstudio.display.builder by kasemir.
the class WidgetPropertySubscriptionUnitTest method testMacroizedValueChanges.
@Test
public void testMacroizedValueChanges() {
// Group widget supports macros
final GroupWidget widget = new GroupWidget();
final Macros macros = new Macros();
macros.add("NAME", "Fred");
widget.propMacros().setValue(macros);
final MacroizedWidgetProperty<String> name_prop = (MacroizedWidgetProperty<String>) widget.propName();
final AtomicInteger updates = new AtomicInteger();
final AtomicReference<String> received_value = new AtomicReference<String>(null);
name_prop.addPropertyListener((prop, old_value, new_value) -> {
System.out.println(prop.getName() + " changes from " + old_value + " to " + new_value);
updates.incrementAndGet();
received_value.set(new_value);
});
assertThat(updates.get(), equalTo(0));
// Setting the specification triggers a notification
name_prop.setSpecification("$(NAME)");
assertThat(updates.get(), equalTo(1));
// The listener received null, because only the specification
// was set, and the value has not been resolved
assertThat(received_value.get(), nullValue());
// Fetching the value will resolve macros,
// but that does _not_ trigger another update
assertThat(name_prop.getValue(), equalTo("Fred"));
assertThat(updates.get(), equalTo(1));
// Setting the value (not the description) to something
// that doesn't contain macros will just set the value.
name_prop.setValue("New Name");
assertThat(updates.get(), equalTo(2));
assertThat(received_value.get(), equalTo("New Name"));
// Fetching that value does not trigger macro resolution
// and another value update
assertThat(name_prop.getValue(), equalTo("New Name"));
assertThat(updates.get(), equalTo(2));
// Setting the specification to something that does
// not contain macros will set the value right away
name_prop.setSpecification("Plain Text");
assertThat(updates.get(), equalTo(3));
assertThat(name_prop.getSpecification(), equalTo("Plain Text"));
// Fetching the value does _not_ trigger another update
assertThat(name_prop.getValue(), equalTo("Plain Text"));
assertThat(updates.get(), equalTo(3));
}
use of org.csstudio.display.builder.model.widgets.GroupWidget in project org.csstudio.display.builder by kasemir.
the class CreateGroupAction method run.
@Override
public void run() {
editor.getWidgetSelectionHandler().clear();
// Create group that surrounds the original widget boundaries
final GroupWidget group = new GroupWidget();
// Get bounds of widgets relative to their container,
// which might be a group within the display
// or the display itself
final Rectangle2D rect = GeometryTools.getBounds(widgets);
// Inset depends on representation and changes with group style and font.
// Can be obtained via group.runtimePropInsets() _after_ the group has
// been represented. For this reason Style.NONE is used, where the inset
// is always 0. An alternative could be Style.LINE, with an inset of 1.
final int inset = 0;
group.propStyle().setValue(Style.NONE);
group.propTransparent().setValue(true);
group.propX().setValue((int) rect.getMinX() - inset);
group.propY().setValue((int) rect.getMinY() - inset);
group.propWidth().setValue((int) rect.getWidth() + 2 * inset);
group.propHeight().setValue((int) rect.getHeight() + 2 * inset);
group.propName().setValue(org.csstudio.display.builder.model.Messages.GroupWidget_Name);
final ChildrenProperty parent_children = ChildrenProperty.getParentsChildren(widgets.get(0));
final UndoableActionManager undo = editor.getUndoableActionManager();
undo.execute(new GroupWidgetsAction(parent_children, group, widgets, (int) rect.getMinX(), (int) rect.getMinY()));
editor.getWidgetSelectionHandler().toggleSelection(group);
}
use of org.csstudio.display.builder.model.widgets.GroupWidget in project org.csstudio.display.builder by kasemir.
the class ArrayRepresentation method copyProperties.
private void copyProperties(final Widget original, final Widget copy) {
if (original.equals(copy))
return;
// Copy (most) properties onto matching name in copy
for (WidgetProperty<?> prop : copy.getProperties()) {
// Don't change the 'name' property
if (prop.getName().equals(original.propName().getName()))
continue;
// Don't copy the 'children' of a GroupWidget
if (prop.getName().equals(ChildrenProperty.DESCRIPTOR.getName()))
continue;
try {
final String prop_name = prop.getName();
prop.setValue(original.getPropertyValue(prop_name));
} catch (Exception ex) {
logger.log(Level.WARNING, "Cannot copy " + original + " " + prop, ex);
}
}
// For a Group widget, copy the child widgets
if (original instanceof GroupWidget) {
final GroupWidget orig_group = (GroupWidget) original;
final GroupWidget copy_group = (GroupWidget) copy;
// Remove existing child elements
while (copy_group.runtimeChildren().getValue().size() > 0) copy_group.runtimeChildren().removeChild(copy_group.runtimeChildren().getValue().get(0));
// Set child elements from original group
for (Widget child : orig_group.runtimeChildren().getValue()) copy_group.runtimeChildren().addChild(copyWidget(child));
}
}
use of org.csstudio.display.builder.model.widgets.GroupWidget in project org.csstudio.display.builder by kasemir.
the class PersistenceUnitTest method testWidgetWriting.
/**
* Writing widgets as XML
* @throws Exception on error
*/
@Test
public void testWidgetWriting() throws Exception {
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
try (final ModelWriter writer = new ModelWriter(stream)) {
final Widget widget = new CustomWidget();
widget.setPropertyValue(propName, "Demo");
widget.getProperty(CustomWidget.propZeroTen).setValue(7);
writer.writeWidget(widget);
final GroupWidget group = new GroupWidget();
group.setPropertyValue(propName, "My Group");
final Widget child = new Widget("base");
child.setPropertyValue(propName, "Jänner");
group.runtimeChildren().addChild(child);
writer.writeWidget(group);
}
final String xml = stream.toString();
System.out.println(xml);
final String desired = getExampleFile();
assertThat(xml.replace("\r", ""), equalTo(desired));
}
Aggregations