Search in sources :

Example 11 with PrinterData

use of org.eclipse.swt.printing.PrinterData in project yamcs-studio by yamcs.

the class PrintDisplayAction method run.

/**
 * @see org.eclipse.jface.action.Action#run()
 */
@Override
public void run() {
    final GraphicalViewer viewer = getWorkbenchPart().getAdapter(GraphicalViewer.class);
    viewer.getControl().getDisplay().asyncExec(new Runnable() {

        @Override
        public void run() {
            final ImageLoader loader = new ImageLoader();
            ImageData[] imageData;
            try {
                imageData = loader.load(ResourceUtil.getScreenshotFile(viewer));
                if (imageData.length > 0) {
                    PrintDialog dialog = new PrintDialog(viewer.getControl().getShell(), SWT.NULL);
                    final PrinterData data = dialog.open();
                    if (data != null) {
                        Printer printer = new Printer(data);
                        // Calculate the scale factor between the screen resolution
                        // and printer
                        // resolution in order to correctly size the image for the
                        // printer
                        Point screenDPI = viewer.getControl().getDisplay().getDPI();
                        Point printerDPI = printer.getDPI();
                        int scaleFactor = printerDPI.x / screenDPI.x;
                        // Determine the bounds of the entire area of the printer
                        Rectangle trim = printer.computeTrim(0, 0, 0, 0);
                        Image printerImage = new Image(printer, imageData[0]);
                        if (printer.startJob("Printing OPI")) {
                            if (printer.startPage()) {
                                GC gc = new GC(printer);
                                Rectangle printArea = printer.getClientArea();
                                if (imageData[0].width * scaleFactor <= printArea.width) {
                                    printArea.width = imageData[0].width * scaleFactor;
                                    printArea.height = imageData[0].height * scaleFactor;
                                } else {
                                    printArea.height = printArea.width * imageData[0].height / imageData[0].width;
                                }
                                gc.drawImage(printerImage, 0, 0, imageData[0].width, imageData[0].height, -trim.x, -trim.y, printArea.width, printArea.height);
                                gc.dispose();
                                printer.endPage();
                            }
                        }
                        printer.endJob();
                        printer.dispose();
                        printerImage.dispose();
                    }
                }
            } catch (Exception e) {
                ErrorHandlerUtil.handleError("Failed to print OPI", e);
                return;
            }
        }
    });
}
Also used : GraphicalViewer(org.eclipse.gef.GraphicalViewer) PrintDialog(org.eclipse.swt.printing.PrintDialog) PrinterData(org.eclipse.swt.printing.PrinterData) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) ImageLoader(org.eclipse.swt.graphics.ImageLoader) Printer(org.eclipse.swt.printing.Printer) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC)

Example 12 with PrinterData

use of org.eclipse.swt.printing.PrinterData 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 13 with PrinterData

use of org.eclipse.swt.printing.PrinterData in project dbeaver by serge-rider.

the class ERDEditorPart method printDiagram.

public void printDiagram() {
    GraphicalViewer viewer = getGraphicalViewer();
    PrintDialog dialog = new PrintDialog(viewer.getControl().getShell(), SWT.NULL);
    PrinterData data = dialog.open();
    if (data != null) {
        IFigure rootFigure = rootPart.getLayer(ScalableFreeformRootEditPart.PRINTABLE_LAYERS);
        // EntityDiagramFigure diagramFigure = findFigure(rootFigure, EntityDiagramFigure.class);
        if (rootFigure != null) {
            PrintFigureOperation printOp = new PrintFigureOperation(new Printer(data), rootFigure);
            // Set print preferences
            DBPPreferenceStore store = ERDUIActivator.getDefault().getPreferences();
            printOp.setPrintMode(store.getInt(ERDUIConstants.PREF_PRINT_PAGE_MODE));
            printOp.setPrintMargin(new Insets(store.getInt(ERDUIConstants.PREF_PRINT_MARGIN_TOP), store.getInt(ERDUIConstants.PREF_PRINT_MARGIN_LEFT), store.getInt(ERDUIConstants.PREF_PRINT_MARGIN_BOTTOM), store.getInt(ERDUIConstants.PREF_PRINT_MARGIN_RIGHT)));
            // Run print
            printOp.run("Print ER diagram");
        }
    }
// new PrintAction(this).run();
}
Also used : Insets(org.eclipse.draw2d.geometry.Insets) PrintDialog(org.eclipse.swt.printing.PrintDialog) PrinterData(org.eclipse.swt.printing.PrinterData) Printer(org.eclipse.swt.printing.Printer) PrintFigureOperation(org.eclipse.draw2d.PrintFigureOperation) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore) IFigure(org.eclipse.draw2d.IFigure)

Example 14 with PrinterData

use of org.eclipse.swt.printing.PrinterData in project dbeaver by serge-rider.

the class ERDEditorPart method printDiagram.

public void printDiagram() {
    GraphicalViewer viewer = getGraphicalViewer();
    PrintDialog dialog = new PrintDialog(viewer.getControl().getShell(), SWT.NULL);
    PrinterData data = dialog.open();
    if (data != null) {
        IFigure rootFigure = rootPart.getLayer(ScalableFreeformRootEditPart.PRINTABLE_LAYERS);
        //EntityDiagramFigure diagramFigure = findFigure(rootFigure, EntityDiagramFigure.class);
        if (rootFigure != null) {
            PrintFigureOperation printOp = new PrintFigureOperation(new Printer(data), rootFigure);
            // Set print preferences
            IPreferenceStore store = ERDActivator.getDefault().getPreferenceStore();
            printOp.setPrintMode(store.getInt(ERDConstants.PREF_PRINT_PAGE_MODE));
            printOp.setPrintMargin(new Insets(store.getInt(ERDConstants.PREF_PRINT_MARGIN_TOP), store.getInt(ERDConstants.PREF_PRINT_MARGIN_LEFT), store.getInt(ERDConstants.PREF_PRINT_MARGIN_BOTTOM), store.getInt(ERDConstants.PREF_PRINT_MARGIN_RIGHT)));
            // Run print
            printOp.run("Print ER diagram");
        }
    }
//new PrintAction(this).run();
}
Also used : Insets(org.eclipse.draw2d.geometry.Insets) PrintDialog(org.eclipse.swt.printing.PrintDialog) PrinterData(org.eclipse.swt.printing.PrinterData) Printer(org.eclipse.swt.printing.Printer) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Example 15 with PrinterData

use of org.eclipse.swt.printing.PrinterData in project eclipse.platform.text by eclipse.

the class TextViewer method print.

/**
 * {@inheritDoc}
 *
 * @since 3.4
 */
@Override
public void print(StyledTextPrintOptions options) {
    final Shell shell = fTextWidget.getShell();
    if (Printer.getPrinterList().length == 0) {
        // $NON-NLS-1$
        String title = JFaceTextMessages.getString("TextViewer.warning.noPrinter.title");
        // $NON-NLS-1$
        String msg = JFaceTextMessages.getString("TextViewer.warning.noPrinter.message");
        MessageDialog.openWarning(shell, title, msg);
        return;
    }
    final PrintDialog dialog = new PrintDialog(shell, SWT.PRIMARY_MODAL);
    dialog.setPrinterData(fgPrinterData);
    final PrinterData data = dialog.open();
    if (data != null) {
        final Printer printer = new Printer(data);
        final Runnable styledTextPrinter = fTextWidget.print(printer, options);
        Thread printingThread = new // $NON-NLS-1$
        Thread(// $NON-NLS-1$
        "Printing") {

            @Override
            public void run() {
                styledTextPrinter.run();
                printer.dispose();
            }
        };
        printingThread.start();
        /*
			 * FIXME:
			 * 	Should copy the printer data to avoid threading issues,
			 *	but this is currently not possible, see http://bugs.eclipse.org/297957
			 */
        fgPrinterData = data;
        fgPrinterData.startPage = 1;
        fgPrinterData.endPage = 1;
        fgPrinterData.scope = PrinterData.ALL_PAGES;
        fgPrinterData.copyCount = 1;
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) PrintDialog(org.eclipse.swt.printing.PrintDialog) PrinterData(org.eclipse.swt.printing.PrinterData) Printer(org.eclipse.swt.printing.Printer)

Aggregations

PrinterData (org.eclipse.swt.printing.PrinterData)26 Printer (org.eclipse.swt.printing.Printer)22 PrintDialog (org.eclipse.swt.printing.PrintDialog)16 Test (org.junit.Test)9 Point (org.eclipse.swt.graphics.Point)6 Rectangle (org.eclipse.swt.graphics.Rectangle)6 Shell (org.eclipse.swt.widgets.Shell)4 Insets (org.eclipse.draw2d.geometry.Insets)3 GraphicalViewer (org.eclipse.gef.GraphicalViewer)3 StyledTextPrintOptions (org.eclipse.swt.custom.StyledTextPrintOptions)3 GC (org.eclipse.swt.graphics.GC)3 Image (org.eclipse.swt.graphics.Image)3 IFigure (org.eclipse.draw2d.IFigure)2 PrintFigureOperation (org.eclipse.draw2d.PrintFigureOperation)2 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 ImageLoader (org.eclipse.swt.graphics.ImageLoader)2 JaretTablePrinter (de.jaret.util.ui.table.JaretTablePrinter)1 JaretTablePrintConfiguration (de.jaret.util.ui.table.print.JaretTablePrintConfiguration)1 JaretTablePrintDialog (de.jaret.util.ui.table.print.JaretTablePrintDialog)1 File (java.io.File)1