Search in sources :

Example 16 with DisplayModel

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();
    });
}
Also used : MouseEvent(javafx.scene.input.MouseEvent) DisplayModel(org.csstudio.display.builder.model.DisplayModel) ClipboardContent(javafx.scene.input.ClipboardContent) PictureWidget(org.csstudio.display.builder.model.widgets.PictureWidget) PVWidget(org.csstudio.display.builder.model.widgets.PVWidget) WebBrowserWidget(org.csstudio.display.builder.model.widgets.WebBrowserWidget) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) EmbeddedDisplayWidget(org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget) SymbolWidget(org.csstudio.display.builder.model.widgets.SymbolWidget) Widget(org.csstudio.display.builder.model.Widget) Dragboard(javafx.scene.input.Dragboard)

Example 17 with DisplayModel

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);
}
Also used : XYPlotWidget(org.csstudio.display.builder.model.widgets.plots.XYPlotWidget) DisplayModel(org.csstudio.display.builder.model.DisplayModel) XYPlotWidget(org.csstudio.display.builder.model.widgets.plots.XYPlotWidget) Widget(org.csstudio.display.builder.model.Widget) Test(org.junit.Test)

Example 18 with DisplayModel

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"));
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) GroupWidget(org.csstudio.display.builder.model.widgets.GroupWidget) Test(org.junit.Test)

Example 19 with DisplayModel

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());
        });
    });
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) TreeItem(javafx.scene.control.TreeItem) TabsWidget(org.csstudio.display.builder.model.widgets.TabsWidget) Widget(org.csstudio.display.builder.model.Widget)

Example 20 with DisplayModel

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));
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) ByteArrayInputStream(java.io.ByteArrayInputStream) CustomWidget(org.csstudio.display.builder.model.CustomWidget) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) GroupWidget(org.csstudio.display.builder.model.widgets.GroupWidget) Widget(org.csstudio.display.builder.model.Widget) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WidgetFactoryUnitTest(org.csstudio.display.builder.model.WidgetFactoryUnitTest) Test(org.junit.Test)

Aggregations

DisplayModel (org.csstudio.display.builder.model.DisplayModel)73 Widget (org.csstudio.display.builder.model.Widget)15 Test (org.junit.Test)10 LabelWidget (org.csstudio.display.builder.model.widgets.LabelWidget)9 GroupWidget (org.csstudio.display.builder.model.widgets.GroupWidget)7 EmbeddedDisplayRepresentationUtil.loadDisplayModel (org.csstudio.display.builder.representation.EmbeddedDisplayRepresentationUtil.loadDisplayModel)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 EmbeddedDisplayWidget (org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget)5 Image (javafx.scene.image.Image)4 File (java.io.File)3 InputStream (java.io.InputStream)3 Parent (javafx.scene.Parent)3 Macros (org.csstudio.display.builder.model.macros.Macros)3 PictureWidget (org.csstudio.display.builder.model.widgets.PictureWidget)3 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 Rectangle2D (javafx.geometry.Rectangle2D)2 WidgetFactoryUnitTest (org.csstudio.display.builder.model.WidgetFactoryUnitTest)2 WidgetProperty (org.csstudio.display.builder.model.WidgetProperty)2 ModelWriter (org.csstudio.display.builder.model.persist.ModelWriter)2