Search in sources :

Example 1 with Screenshot

use of org.csstudio.javafx.Screenshot in project org.csstudio.display.builder by kasemir.

the class SnapshotAction method run.

// 
@Override
public void run() {
    if (shell == null) {
        // $NON-NLS-1$
        Logger.getLogger("SnapshotAction").log(Level.SEVERE, "Cannot make snapshot. Shell not set");
        return;
    }
    final FileDialog dialog = new FileDialog(shell, SWT.SAVE);
    dialog.setOverwrite(true);
    dialog.setFilterNames(new String[] { "PNG Files", "All Files (*.*)" });
    dialog.setFilterExtensions(new String[] { "*.png", "*.*" });
    final String path = dialog.open();
    if (path == null)
        return;
    final Screenshot screenshot;
    try {
        screenshot = plot.getScreenshot();
    } catch (Exception ex) {
        ExceptionDetailsErrorDialog.openError(shell, "Cannot obtain screenshot", ex);
        return;
    }
    new Job("Save Image") {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            try {
                screenshot.writeToFile(new File(path));
            } catch (Exception ex) {
                shell.getDisplay().asyncExec(() -> ExceptionDetailsErrorDialog.openError(shell, "Cannot write screenshot", ex));
            }
            return Status.OK_STATUS;
        }
    }.schedule();
    ;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) Screenshot(org.csstudio.javafx.Screenshot) Job(org.eclipse.core.runtime.jobs.Job) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File)

Example 2 with Screenshot

use of org.csstudio.javafx.Screenshot in project org.csstudio.display.builder by kasemir.

the class SendLogbookAction method run.

@Override
public void run() {
    File image_file = null;
    try {
        final Screenshot screenshot = new Screenshot(scene);
        image_file = screenshot.writeToTempfile("display");
    } catch (Exception ex) {
        ExceptionDetailsErrorDialog.openError(shell, "Cannot obtain screenshot", ex);
        image_file = null;
    }
    try {
        final LogEntryBuilder entry = LogEntryBuilder.withText(Messages.DefaultLogbookText);
        if (image_file != null)
            entry.attach(attachment(image_file.getAbsolutePath()).inputStream(new FileInputStream(image_file)));
        final LogEntryBuilderDialog dialog = new LogEntryBuilderDialog(shell, entry);
        dialog.setBlockOnOpen(true);
        dialog.open();
    } catch (Exception ex) {
        ExceptionDetailsErrorDialog.openError(shell, "Cannot create log entry", ex);
    }
}
Also used : Screenshot(org.csstudio.javafx.Screenshot) LogEntryBuilderDialog(org.csstudio.logbook.ui.LogEntryBuilderDialog) LogEntryBuilder(org.csstudio.logbook.LogEntryBuilder) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with Screenshot

use of org.csstudio.javafx.Screenshot in project org.csstudio.display.builder by kasemir.

the class PrintAction method run.

@Override
public void run() {
    // Not optimal:
    // Creates screenshot file for JFX scene,
    // then loads that file as SWT image..
    final Image snapshot;
    try {
        final Screenshot screenshot = new Screenshot(scene);
        final File image_file = screenshot.writeToTempfile("display");
        final ImageLoader loader = new ImageLoader();
        final ImageData[] data = loader.load(new FileInputStream(image_file));
        snapshot = new Image(shell.getDisplay(), data[0]);
    } catch (Exception ex) {
        ExceptionDetailsErrorDialog.openError(shell, "Cannot obtain screenshot", ex);
        return;
    }
    // SWT Printer GUI
    final PrintDialog dlg = new PrintDialog(shell);
    PrinterData data = dlg.open();
    if (data == null) {
        logger.fine("Cannot obtain printer");
        return;
    }
    // Access to SWT Printer must be on UI thread.
    // Printing in other thread can deadlock with UI thread.
    final Printer printer = new Printer(data);
    try {
        if (!printer.startJob("Display Builder")) {
            Logger.getLogger(getClass().getName()).fine("Cannot start print job");
            return;
        }
        try {
            // Printer page info
            final Rectangle area = printer.getClientArea();
            final Rectangle trim = printer.computeTrim(0, 0, 0, 0);
            final Point dpi = printer.getDPI();
            // Compute layout
            final Rectangle image_rect = snapshot.getBounds();
            // Leave one inch on each border.
            // (copied the computeTrim stuff from an SWT example.
            // Really no clue...)
            final int left_right = dpi.x + trim.x;
            final int top_bottom = dpi.y + trim.y;
            final int printed_width = area.width - 2 * left_right;
            // Try to scale height according to on-screen aspect ratio.
            final int max_height = area.height - 2 * top_bottom;
            final int printed_height = Math.min(max_height, image_rect.height * printed_width / image_rect.width);
            // Print one page
            printer.startPage();
            final GC gc = new GC(printer);
            gc.drawImage(snapshot, 0, 0, image_rect.width, image_rect.height, left_right, top_bottom, printed_width, printed_height);
            printer.endPage();
        } finally {
            printer.endJob();
        }
    } finally {
        printer.dispose();
    }
    // Image used by printer must only be disposed after the printer that used it.
    // Otherwise crash, https://github.com/ControlSystemStudio/cs-studio/issues/1937
    snapshot.dispose();
}
Also used : PrintDialog(org.eclipse.swt.printing.PrintDialog) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) Printer(org.eclipse.swt.printing.Printer) FileInputStream(java.io.FileInputStream) Point(org.eclipse.swt.graphics.Point) ImageData(org.eclipse.swt.graphics.ImageData) PrinterData(org.eclipse.swt.printing.PrinterData) Screenshot(org.csstudio.javafx.Screenshot) ImageLoader(org.eclipse.swt.graphics.ImageLoader) GC(org.eclipse.swt.graphics.GC) File(java.io.File)

Example 4 with Screenshot

use of org.csstudio.javafx.Screenshot in project org.csstudio.display.builder by kasemir.

the class SaveSnapshotAction method run.

@Override
public void run() {
    final FileDialog dialog = new FileDialog(shell, SWT.SAVE);
    dialog.setOverwrite(true);
    dialog.setFilterNames(new String[] { "PNG Files", "All Files (*.*)" });
    dialog.setFilterExtensions(new String[] { "*.png", "*.*" });
    final String path = dialog.open();
    if (path == null)
        return;
    final Screenshot screenshot;
    try {
        screenshot = new Screenshot(scene);
    } catch (Exception ex) {
        ExceptionDetailsErrorDialog.openError(shell, "Cannot obtain screenshot", ex);
        return;
    }
    new Job("Save Image") {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            try {
                screenshot.writeToFile(new File(path));
            } catch (Exception ex) {
                shell.getDisplay().asyncExec(() -> ExceptionDetailsErrorDialog.openError(shell, "Cannot write screenshot", ex));
            }
            return Status.OK_STATUS;
        }
    }.schedule();
    ;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) Screenshot(org.csstudio.javafx.Screenshot) Job(org.eclipse.core.runtime.jobs.Job) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File)

Aggregations

File (java.io.File)4 Screenshot (org.csstudio.javafx.Screenshot)4 FileInputStream (java.io.FileInputStream)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 Job (org.eclipse.core.runtime.jobs.Job)2 FileDialog (org.eclipse.swt.widgets.FileDialog)2 LogEntryBuilder (org.csstudio.logbook.LogEntryBuilder)1 LogEntryBuilderDialog (org.csstudio.logbook.ui.LogEntryBuilderDialog)1 GC (org.eclipse.swt.graphics.GC)1 Image (org.eclipse.swt.graphics.Image)1 ImageData (org.eclipse.swt.graphics.ImageData)1 ImageLoader (org.eclipse.swt.graphics.ImageLoader)1 Point (org.eclipse.swt.graphics.Point)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1 PrintDialog (org.eclipse.swt.printing.PrintDialog)1 Printer (org.eclipse.swt.printing.Printer)1 PrinterData (org.eclipse.swt.printing.PrinterData)1