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);
}
});
}
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));
}
}
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));
}
Aggregations