Search in sources :

Example 11 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class EmbeddedDisplayRepresentationUtil method loadDisplayModel.

/**
 * Load display model, optionally trimmed to group
 *  @param display_file
 *  @param group_name
 *  @return {@link DisplayModel}
 */
public static DisplayModel loadDisplayModel(final VisibleWidget model_widget, final DisplayAndGroup display_and_group) {
    DisplayModel embedded_model;
    if (display_and_group.getDisplayFile().isEmpty()) {
        // Empty model for empty file name
        embedded_model = new DisplayModel();
        embedded_model.setUserData(DisplayModel.USER_DATA_EMBEDDING_WIDGET, model_widget);
        model_widget.runtimePropConnected().setValue(true);
    } else {
        try {
            // Load model for displayFile, allowing lookup relative to this widget's model
            final DisplayModel display = model_widget.getDisplayModel();
            final String parent_display = display.getUserData(DisplayModel.USER_DATA_INPUT_FILE);
            embedded_model = ModelLoader.resolveAndLoadModel(parent_display, display_and_group.getDisplayFile());
            // Didn't honor the display size of legacy files,
            // always shrunk those to wrap their widgets
            final Version input_version = embedded_model.getUserData(DisplayModel.USER_DATA_INPUT_VERSION);
            if (input_version.getMajor() < 2)
                shrinkModelToWidgets(embedded_model);
            // Tell embedded model that it is held by this widget,
            // which provides access to macros of model_widget.
            embedded_model.setUserData(DisplayModel.USER_DATA_EMBEDDING_WIDGET, model_widget);
            if (!display_and_group.getGroupName().isEmpty())
                reduceDisplayModelToGroup(model_widget, embedded_model, display_and_group);
            // Adjust model name to reflect source file
            embedded_model.propName().setValue("EmbeddedDisplay " + display_and_group.getDisplayFile());
            model_widget.runtimePropConnected().setValue(true);
        } catch (final Throwable ex) {
            // Log error and show message in pseudo model
            final String message = "Failed to load embedded display '" + display_and_group + "'";
            logger.log(Level.WARNING, message, ex);
            embedded_model = createErrorModel(model_widget, message);
            model_widget.runtimePropConnected().setValue(false);
        }
    }
    return embedded_model;
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) Version(org.osgi.framework.Version)

Example 12 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class EmbeddedDisplayRepresentation method updatePendingDisplay.

/**
 * Update to the next pending display
 *
 *  <p>Synchronized to serialize the background threads.
 *
 *  <p>Example: Displays A, B, C are requested in quick succession.
 *
 *  <p>pending_display_and_group=A is submitted to executor thread A.
 *
 *  <p>While handling A, pending_display_and_group=B is submitted to executor thread B.
 *  Thread B will be blocked in synchronized method.
 *
 *  <p>Then pending_display_and_group=C is submitted to executor thread C.
 *  As thread A finishes, thread B finds pending_display_and_group==C.
 *  As thread C finally continues, it finds pending_display_and_group empty.
 *  --> Showing A, then C, skipping B.
 */
private synchronized void updatePendingDisplay() {
    final DisplayAndGroup handle = pending_display_and_group.getAndSet(null);
    if (handle == null)
        return;
    try {
        // Load new model (potentially slow)
        final DisplayModel new_model = loadDisplayModel(model_widget, handle);
        // Atomically update the 'active' model
        final DisplayModel old_model = active_content_model.getAndSet(new_model);
        if (old_model != null) {
            // Dispose old model
            final Future<Object> completion = toolkit.submit(() -> {
                toolkit.disposeRepresentation(old_model);
                return null;
            });
            checkCompletion(model_widget, completion, "timeout disposing old representation");
        }
        // Represent new model on UI thread
        final Future<Object> completion = toolkit.submit(() -> {
            representContent(new_model);
            return null;
        });
        checkCompletion(model_widget, completion, "timeout representing new content");
        model_widget.runtimePropEmbeddedModel().setValue(new_model);
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Failed to handle embedded display " + handle, ex);
    }
}
Also used : DisplayAndGroup(org.csstudio.display.builder.representation.EmbeddedDisplayRepresentationUtil.DisplayAndGroup) DisplayModel(org.csstudio.display.builder.model.DisplayModel) EmbeddedDisplayRepresentationUtil.loadDisplayModel(org.csstudio.display.builder.representation.EmbeddedDisplayRepresentationUtil.loadDisplayModel)

Example 13 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class RepresentationDemoSWT 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.setSize(model.getPropertyValue(propWidth), model.getPropertyValue(propHeight));
    final ToolkitRepresentation<Composite, Control> toolkit = new SWTRepresentation(display);
    toolkit.representModel(shell, model);
    final DummyRuntime runtime = new DummyRuntime(model);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    runtime.shutdown();
    toolkit.shutdown();
    display.dispose();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) Control(org.eclipse.swt.widgets.Control) Composite(org.eclipse.swt.widgets.Composite) DisplayModel(org.csstudio.display.builder.model.DisplayModel) SWTRepresentation(org.csstudio.display.builder.representation.swt.SWTRepresentation) Display(org.eclipse.swt.widgets.Display)

Example 14 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class WidgetTransfer method installPictureWidgetFromImage.

/**
 * @param db The {@link Dragboard} containing the dragged data.
 * @param selection_tracker Used to get the display model.
 * @param widgets The container of the created widgets.
 */
private static void installPictureWidgetFromImage(final Dragboard db, final SelectedWidgetUITracker selection_tracker, final List<Widget> widgets) {
    logger.log(Level.FINE, "Dropped image: creating PictureWidget");
    final DisplayModel model = selection_tracker.getModel();
    final ToolkitRepresentation<?, ?> toolkit = ToolkitRepresentation.getToolkit(selection_tracker.getModel());
    final String filename = toolkit.showSaveAsDialog(model, null);
    if (filename == null) {
        return;
    }
    final Image image = db.getImage();
    if (image == null) {
        return;
    }
    final PictureWidget widget = (PictureWidget) PictureWidget.WIDGET_DESCRIPTOR.createWidget();
    widget.propWidth().setValue((int) image.getWidth());
    widget.propHeight().setValue((int) image.getHeight());
    widgets.add(widget);
    // File access should not be on UI thread, but we need to return the widget right away.
    // -> Return the widget now, create the image file later, and then update the widget's file property
    EditorUtil.getExecutor().execute(() -> {
        try {
            BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
            ImageIO.write(bImage, "png", new File(filename));
            widget.propFile().setValue(ModelResourceUtil.getRelativePath(model.getUserData(DisplayModel.USER_DATA_INPUT_FILE), filename));
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Cannot save image as " + filename, ex);
        }
    });
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) PictureWidget(org.csstudio.display.builder.model.widgets.PictureWidget) BufferedImage(java.awt.image.BufferedImage) WritableImage(javafx.scene.image.WritableImage) Image(javafx.scene.image.Image) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 15 with DisplayModel

use of org.csstudio.display.builder.model.DisplayModel in project org.csstudio.display.builder by kasemir.

the class WidgetTransfer method installSymbolWidgetFromImageFiles.

/**
 * @param files The pathnames of the image files used by the symbol widget.
 * @param selection_tracker Used to get the grid steps from its model to be
 *            used in offsetting multiple widgets.
 * @param widgets The container of the created widgets.
 * @param updates Updates to perform on widgets
 */
private static void installSymbolWidgetFromImageFiles(final List<String> fileNames, final SelectedWidgetUITracker selection_tracker, final List<Widget> widgets, final List<Runnable> updates) {
    logger.log(Level.FINE, "Creating SymbolWidget for {0,number,#########0} dropped images", fileNames.size());
    final DisplayModel model = selection_tracker.getModel();
    final SymbolWidget widget = (SymbolWidget) SymbolWidget.WIDGET_DESCRIPTOR.createWidget();
    ArrayWidgetProperty<WidgetProperty<String>> propSymbols = widget.propSymbols();
    for (int i = 0; i < fileNames.size(); i++) {
        if (i < propSymbols.size()) {
            propSymbols.getElement(i).setValue(fileNames.get(i));
        } else {
            widget.addSymbol(fileNames.get(i));
        }
    }
    final int index = widgets.size();
    widget.propX().setValue(model.propGridStepX().getValue() * index);
    widget.propY().setValue(model.propGridStepY().getValue() * index);
    widgets.add(widget);
    updates.add(() -> updateSymbolWidgetSize(widget));
}
Also used : WidgetProperty(org.csstudio.display.builder.model.WidgetProperty) ArrayWidgetProperty(org.csstudio.display.builder.model.ArrayWidgetProperty) SymbolWidget(org.csstudio.display.builder.model.widgets.SymbolWidget) DisplayModel(org.csstudio.display.builder.model.DisplayModel)

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