Search in sources :

Example 1 with PictureWidget

use of org.csstudio.display.builder.model.widgets.PictureWidget 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 2 with PictureWidget

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

the class WidgetTransfer method installWidgetsFromURL.

private static void installWidgetsFromURL(final DragEvent event, final List<Widget> widgets, final List<Runnable> updates) {
    final String choice;
    final Dragboard db = event.getDragboard();
    String url = db.getUrl();
    // Fix URL, which on linux can contain the file name twice:
    // "http://some/path/to/file.xyz\nfile.xyz"
    int sep = url.indexOf('\n');
    if (sep > 0) {
        url = url.substring(0, sep);
    }
    // Check URL's extension
    sep = url.lastIndexOf('.');
    final String ext = sep > 0 ? url.substring(1 + sep).toUpperCase() : null;
    if (EMBEDDED_FILE_EXTENSIONS.contains(ext)) {
        choice = EmbeddedDisplayWidget.WIDGET_DESCRIPTOR.getName();
    } else if (IMAGE_FILE_EXTENSIONS.contains(ext)) {
        choice = PictureWidget.WIDGET_DESCRIPTOR.getName();
    } else {
        // Prompt user
        final List<String> choices = Arrays.asList(LabelWidget.WIDGET_DESCRIPTOR.getName(), EmbeddedDisplayWidget.WIDGET_DESCRIPTOR.getName(), PictureWidget.WIDGET_DESCRIPTOR.getName(), WebBrowserWidget.WIDGET_DESCRIPTOR.getName());
        final ChoiceDialog<String> dialog = new ChoiceDialog<>(choices.get(3), choices);
        // Position dialog
        dialog.setX(event.getScreenX());
        dialog.setY(event.getScreenY());
        dialog.setTitle(Messages.WT_FromURL_dialog_title);
        dialog.setHeaderText(NLS.bind(Messages.WT_FromURL_dialog_headerFMT, reduceString(url)));
        dialog.setContentText(Messages.WT_FromURL_dialog_content);
        final Optional<String> result = dialog.showAndWait();
        if (result.isPresent()) {
            choice = result.get();
        } else {
            return;
        }
    }
    if (LabelWidget.WIDGET_DESCRIPTOR.getName().equals(choice)) {
        logger.log(Level.FINE, "Creating LabelWidget for {0}", url);
        final LabelWidget widget = (LabelWidget) LabelWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propText().setValue(url);
        widgets.add(widget);
    } else if (WebBrowserWidget.WIDGET_DESCRIPTOR.getName().equals(choice)) {
        logger.log(Level.FINE, "Creating WebBrowserWidget for {0}", url);
        final WebBrowserWidget widget = (WebBrowserWidget) WebBrowserWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propWidgetURL().setValue(url);
        widgets.add(widget);
    } else if (PictureWidget.WIDGET_DESCRIPTOR.getName().equals(choice)) {
        logger.log(Level.FINE, "Creating PictureWidget for {0}", url);
        final PictureWidget widget = (PictureWidget) PictureWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propFile().setValue(url);
        widgets.add(widget);
        updates.add(() -> updatePictureWidgetSize(widget));
    } else if (EmbeddedDisplayWidget.WIDGET_DESCRIPTOR.getName().equals(choice)) {
        logger.log(Level.FINE, "Creating EmbeddedDisplayWidget for {0}", url);
        EmbeddedDisplayWidget widget = (EmbeddedDisplayWidget) EmbeddedDisplayWidget.WIDGET_DESCRIPTOR.createWidget();
        widget.propFile().setValue(url);
        widgets.add(widget);
        updates.add(() -> updateEmbeddedDisplayWidget(widget));
    }
}
Also used : ChoiceDialog(javafx.scene.control.ChoiceDialog) Optional(java.util.Optional) EmbeddedDisplayWidget(org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget) LabelWidget(org.csstudio.display.builder.model.widgets.LabelWidget) PictureWidget(org.csstudio.display.builder.model.widgets.PictureWidget) List(java.util.List) ArrayList(java.util.ArrayList) WebBrowserWidget(org.csstudio.display.builder.model.widgets.WebBrowserWidget) Dragboard(javafx.scene.input.Dragboard)

Example 3 with PictureWidget

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

the class WidgetTransfer method installPictureWidgetFromFile.

/**
 * @param image_file The image file used to create and preset a {@link PictureWidget}.
 * @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 installPictureWidgetFromFile(final String image_file, final SelectedWidgetUITracker selection_tracker, final List<Widget> widgets, final List<Runnable> updates) {
    logger.log(Level.FINE, "Creating PictureWidget for dropped image {0}", image_file);
    final DisplayModel model = selection_tracker.getModel();
    final PictureWidget widget = (PictureWidget) PictureWidget.WIDGET_DESCRIPTOR.createWidget();
    widget.propFile().setValue(image_file);
    final int index = widgets.size();
    widget.propX().setValue(model.propGridStepX().getValue() * index);
    widget.propY().setValue(model.propGridStepY().getValue() * index);
    widgets.add(widget);
    updates.add(() -> updatePictureWidgetSize(widget));
}
Also used : DisplayModel(org.csstudio.display.builder.model.DisplayModel) PictureWidget(org.csstudio.display.builder.model.widgets.PictureWidget)

Aggregations

PictureWidget (org.csstudio.display.builder.model.widgets.PictureWidget)3 DisplayModel (org.csstudio.display.builder.model.DisplayModel)2 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 ChoiceDialog (javafx.scene.control.ChoiceDialog)1 Image (javafx.scene.image.Image)1 WritableImage (javafx.scene.image.WritableImage)1 Dragboard (javafx.scene.input.Dragboard)1 EmbeddedDisplayWidget (org.csstudio.display.builder.model.widgets.EmbeddedDisplayWidget)1 LabelWidget (org.csstudio.display.builder.model.widgets.LabelWidget)1 WebBrowserWidget (org.csstudio.display.builder.model.widgets.WebBrowserWidget)1