Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 67b94f662dceee7b737e4728c59704e4b367d4d6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*****************************************************************************
 * Copyright (c) 2017 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 		IBM Corporation - initial API and implementation
 * 		Fanch BONNABESSE (ALL4TEC) fanch.bonnabesse@all4tec.net - Bug 424803
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.common.providers;

import java.util.Collections;

import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
import org.eclipse.gmf.runtime.diagram.ui.printing.internal.l10n.DiagramUIPrintingMessages;
import org.eclipse.gmf.runtime.diagram.ui.printing.internal.util.SWTDiagramPrinter;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.ui.IEditorPart;

/**
 * Provides basic printing functionality. This does a print from a default print
 * dialog.
 * Customization of <code>DefaultPrintActionHelper</code>
 * @since 3.0
 *
 */
public class CustomPrintActionHelper {

	/**
	 * Prints the diagram after a default print dialog is opened for the user.
	 *
	 * @param editorPart
	 *            the IEditorPart containing the diagram to print
	 * @param diagramPrinter
	 *            the diagram printer that does the work of actually printing
	 *            the diagrams
	 */
	public static void doRun(final IEditorPart editorPart, final SWTDiagramPrinter diagramPrinter) {
		// print the editor contents.
		final PrintDialog dialog = new PrintDialog(editorPart.getSite().getShell(), SWT.NULL);
		final PrinterData data = dialog.open();

		if (null != data) {
			boolean isPrintToFit = MessageDialog.openQuestion(null,
					DiagramUIPrintingMessages.Print_MessageDialogTitle,
					DiagramUIPrintingMessages.Print_MessageDialogMessage);

			final Printer printer = new Printer(data);

			diagramPrinter.setPrinter(printer);
			diagramPrinter.setDisplayDPI(dialog.getParent().getDisplay().getDPI());

			DiagramEditPart diagramEditPart = null;
			if (editorPart instanceof IDiagramWorkbenchPart) {
				diagramEditPart = ((IDiagramWorkbenchPart) editorPart).getDiagramEditPart();
			} else if (editorPart instanceof IMultiDiagramEditor) {
				IEditorPart activeEditor = ((IMultiDiagramEditor) editorPart).getActiveEditor();
				if (activeEditor instanceof IDiagramWorkbenchPart) {
					diagramEditPart = ((IDiagramWorkbenchPart) activeEditor).getDiagramEditPart();
				}
			}

			if (null != diagramEditPart) {
				diagramPrinter.setDiagrams(Collections.singletonList(diagramEditPart.getDiagramView().getDiagram()));

				if (isPrintToFit) {
					diagramPrinter.setColumns(1);
					diagramPrinter.setRows(1);
				} else {
					diagramPrinter.setScaledPercent(100);
				}

				diagramPrinter.run();
				printer.dispose();
			}
		}
	}

	/**
	 * Private constructor prevents instantiation
	 */
	private CustomPrintActionHelper() {
		// utility class
	}

}

Back to the top