View | Details | Raw Unified | Return to bug 225173 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/gmf/runtime/diagram/ui/printing/render/internal/JPSDiagramPrinter.java (-59 / +113 lines)
Lines 12-17 Link Here
12
package org.eclipse.gmf.runtime.diagram.ui.printing.render.internal;
12
package org.eclipse.gmf.runtime.diagram.ui.printing.render.internal;
13
13
14
import java.awt.BasicStroke;
14
import java.awt.BasicStroke;
15
import java.awt.Graphics2D;
15
import java.awt.print.PageFormat;
16
import java.awt.print.PageFormat;
16
import java.awt.print.PrinterException;
17
import java.awt.print.PrinterException;
17
import java.util.Iterator;
18
import java.util.Iterator;
Lines 44-49 Link Here
44
import javax.print.attribute.standard.Sides;
45
import javax.print.attribute.standard.Sides;
45
46
46
import org.eclipse.draw2d.Graphics;
47
import org.eclipse.draw2d.Graphics;
48
import org.eclipse.draw2d.SWTGraphics;
47
import org.eclipse.draw2d.geometry.Rectangle;
49
import org.eclipse.draw2d.geometry.Rectangle;
48
import org.eclipse.gef.LayerConstants;
50
import org.eclipse.gef.LayerConstants;
49
import org.eclipse.gef.RootEditPart;
51
import org.eclipse.gef.RootEditPart;
Lines 81-86 Link Here
81
import org.eclipse.jface.preference.IPreferenceStore;
83
import org.eclipse.jface.preference.IPreferenceStore;
82
import org.eclipse.jface.resource.JFaceResources;
84
import org.eclipse.jface.resource.JFaceResources;
83
import org.eclipse.swt.graphics.FontData;
85
import org.eclipse.swt.graphics.FontData;
86
import org.eclipse.swt.graphics.GC;
87
import org.eclipse.swt.graphics.Image;
84
import org.eclipse.swt.graphics.Point;
88
import org.eclipse.swt.graphics.Point;
85
import org.eclipse.swt.widgets.Display;
89
import org.eclipse.swt.widgets.Display;
86
import org.eclipse.swt.widgets.Shell;
90
import org.eclipse.swt.widgets.Shell;
Lines 96-115 Link Here
96
 */
100
 */
97
public class JPSDiagramPrinter extends DiagramPrinter implements
101
public class JPSDiagramPrinter extends DiagramPrinter implements
98
		java.awt.print.Printable {
102
		java.awt.print.Printable {
103
99
	
104
	
105
	/**
106
	 * A specialized graphics adapter used in printing.
107
	 * 
108
	 * There are several issues with the base adapter such as incorrect line width settings
109
	 * and issues with gradient fill causing printing to be offset wich are concerns specific
110
	 * to printing.
111
	 * 
112
	 * @author James Bruck (jbruck)
113
	 *
114
	 */
115
	public class PrinterGraphicsToGraphics2DAdapter extends
116
			GraphicsToGraphics2DAdaptor {
117
118
		public PrinterGraphicsToGraphics2DAdapter(Graphics2D graphics,
119
				Rectangle viewPort) {
120
			super(graphics, viewPort);
121
		}
122
123
		/*
124
		 * (non-Javadoc)
125
		 * 
126
		 * @see org.eclipse.gmf.runtime.draw2d.ui.render.awt.internal.graphics.GraphicsToGraphics2DAdaptor#setLineWidth(int)
127
		 */
128
		public void setLineWidth(int width) {
129
			super.setLineWidth(width);
130
131
			BasicStroke scaledStroke = getStroke();
132
			//
133
			// Make a special case for line thickness to take the
134
			// printer resolution into account.
135
			//
136
			scaledStroke = new BasicStroke(
137
					(float) (width * AWT_DPI_CONST / 100), scaledStroke
138
							.getEndCap(), scaledStroke.getLineJoin(),
139
					scaledStroke.getMiterLimit(), scaledStroke.getDashArray(),
140
					0);
141
142
			getGraphics2D().setStroke(scaledStroke);
143
		}
144
145
		/*
146
		 * (non-Javadoc)
147
		 * 
148
		 * @see org.eclipse.gmf.runtime.draw2d.ui.render.awt.internal.graphics.GraphicsToGraphics2DAdaptor#fillGradient(int,
149
		 *      int, int, int, boolean)
150
		 */
151
		public void fillGradient(int x, int y, int w, int h, boolean vertical) {
152
			//
153
			// A bug in the draw2d layer causes printed output to be
154
			// offset if we use gradient fill. We will use an image
155
			// instead.
156
			//
157
			Image tempImage = new Image(Display.getDefault(),
158
					new org.eclipse.swt.graphics.Rectangle(x, y, w, h));
159
			GC gc = new GC(tempImage);
160
			SWTGraphics tempGraphics = new SWTGraphics(gc);
161
162
			tempGraphics.setForegroundColor(swtGraphics.getForegroundColor());
163
			tempGraphics.setBackgroundColor(swtGraphics.getBackgroundColor());
164
			tempGraphics.fillGradient(
165
					new org.eclipse.draw2d.geometry.Rectangle(0, 0, w, h),
166
					vertical);
167
			drawImage(tempImage, 0, 0, w, h, x, y, w, h);
168
169
			tempGraphics.dispose();
170
			gc.dispose();
171
			tempImage.dispose();
172
		}
173
	}
174
100
	// A constant that takes into account screen display DPI and the graphic DPI
175
	// A constant that takes into account screen display DPI and the graphic DPI
101
	// 72.0 DPI is an AWT constant @see java.awt.Graphics2D
176
	// 72.0 DPI is an AWT constant @see java.awt.Graphics2D
102
	private static double AWT_DPI_CONST = 72.0;
177
	private static double AWT_DPI_CONST = 72.0;
103
	
178
104
	// The print service used during printing.
179
	// The print service used during printing.
105
	private PrintService printService;
180
	private PrintService printService;
106
	
181
107
	// Page information that is collected up front and used during the async printing calls.
182
	// Page information that is collected up front and used during the async printing calls.
108
	private PageData[] pages;
183
	private PageData[] pages;
109
	
184
110
	// The print helper contains page information.
185
	// The print helper contains page information.
111
	private IPrintHelper printHelper;
186
	private IPrintHelper printHelper;
112
	
187
113
	public JPSDiagramPrinter(PreferencesHint preferencesHint, IMapMode mm) {
188
	public JPSDiagramPrinter(PreferencesHint preferencesHint, IMapMode mm) {
114
		super(preferencesHint, mm);
189
		super(preferencesHint, mm);
115
		this.preferencesHint = preferencesHint;
190
		this.preferencesHint = preferencesHint;
Lines 187-196 Link Here
187
	 * 
262
	 * 
188
	 * @param dgrmEP The diagram edit part to print
263
	 * @param dgrmEP The diagram edit part to print
189
	 * @param loadedPreferences true if existing prefs could be loaded
264
	 * @param loadedPreferences true if existing prefs could be loaded
190
     * 		successfully, false if not and defaults are being used.  This parameter
265
	 * 		successfully, false if not and defaults are being used.  This parameter
191
     * 		is important to obtain the correct page break bounds.
266
	 * 		is important to obtain the correct page break bounds.
192
	 * @param fPreferences the preferenceStore that could either contain
267
	 * @param fPreferences the preferenceStore that could either contain
193
     * 		existing preferences or defaults
268
	 * 		existing preferences or defaults
194
	 */
269
	 */
195
	protected void printToScale(DiagramEditPart dgrmEP,
270
	protected void printToScale(DiagramEditPart dgrmEP,
196
			boolean loadedPreferences, IPreferenceStore fPreferences) {
271
			boolean loadedPreferences, IPreferenceStore fPreferences) {
Lines 256-274 Link Here
256
			}
331
			}
257
			col = 1;
332
			col = 1;
258
		}
333
		}
259
		pages =  pageList.toArray(new PageData[pageList.size()]);
334
		pages = pageList.toArray(new PageData[pageList.size()]);
260
	}
335
	}
261
336
262
	/**
337
	/**
263
	 * Print the diagram figure to fit the number and rows and columns
338
	 * Print the diagram figure to fit the number and rows and columns
264
     * specified by the user.
339
	 * specified by the user.
265
	 * 
340
	 * 
266
	 * @param dgrmEP The diagram edit part to print
341
	 * @param dgrmEP The diagram edit part to print
267
	 * @param loadedPreferences true if existing prefs could be loaded
342
	 * @param loadedPreferences true if existing prefs could be loaded
268
     * 		successfully, false if not and defaults are being used.  This parameter
343
	 * 		successfully, false if not and defaults are being used.  This parameter
269
     * 		is important to obtain the correct page break bounds.
344
	 * 		is important to obtain the correct page break bounds.
270
	 * @param fPreferences the preferenceStore that could either contain
345
	 * @param fPreferences the preferenceStore that could either contain
271
     * 		existing preferences or defaults
346
	 * 		existing preferences or defaults
272
	 */
347
	 */
273
	protected void printToPages(DiagramEditPart dgrmEP,
348
	protected void printToPages(DiagramEditPart dgrmEP,
274
			boolean loadedPreferences, IPreferenceStore fPreferences) {
349
			boolean loadedPreferences, IPreferenceStore fPreferences) {
Lines 360-406 Link Here
360
			return java.awt.print.Printable.NO_SUCH_PAGE;
435
			return java.awt.print.Printable.NO_SUCH_PAGE;
361
		}
436
		}
362
437
363
		try {			
438
		try {
364
			swtGraphics = new GraphicsToGraphics2DAdaptor(
439
			swtGraphics = new PrinterGraphicsToGraphics2DAdapter(
365
					(java.awt.Graphics2D) printGraphics, new Rectangle(0, 0,
440
					(java.awt.Graphics2D) printGraphics, new Rectangle(0, 0,
366
							(int) pageFormat.getWidth(), (int) pageFormat
441
							(int) pageFormat.getWidth(), (int) pageFormat
367
									.getHeight())) {
442
									.getHeight()));
368
				/*
369
				 * (non-Javadoc)
370
				 * @see org.eclipse.gmf.runtime.draw2d.ui.render.awt.internal.graphics.GraphicsToGraphics2DAdaptor#setLineWidth(int)
371
				 */
372
				public void setLineWidth(int width) {
373
					super.setLineWidth(width);
374
375
					BasicStroke scaledStroke = getStroke();
376
					//
377
					// Make a special case for line thickness to take the printer
378
					// resolution into account.
379
					//
380
					scaledStroke = new BasicStroke(
381
							(float) (width * AWT_DPI_CONST / 100), 
382
							scaledStroke.getEndCap(),
383
							scaledStroke.getLineJoin(), 
384
							scaledStroke.getMiterLimit(), 
385
							scaledStroke.getDashArray(), 0);
386
443
387
					getGraphics2D().setStroke(scaledStroke);
388
				}
389
			};
390
			
391
			graphics = createMapModeGraphics(createPrinterGraphics(swtGraphics));
444
			graphics = createMapModeGraphics(createPrinterGraphics(swtGraphics));
392
			graphics.scale(AWT_DPI_CONST / display_dpi.x);
445
			graphics.scale(AWT_DPI_CONST / display_dpi.x);
393
			drawPage(pages[pageIndex]);
446
			drawPage(pages[pageIndex]);
394
			
447
		} finally {
395
		}  finally {
396
			dispose();
448
			dispose();
397
		}
449
		}
398
450
399
		return java.awt.print.Printable.PAGE_EXISTS;
451
		return java.awt.print.Printable.PAGE_EXISTS;
400
	}
452
	}
401
	
453
402
	/*
454
	/*
403
	 * (non-Javadoc)
455
	 * (non-Javadoc)
456
	 * 
404
	 * @see org.eclipse.gmf.runtime.diagram.ui.printing.internal.util.DiagramPrinter#createMapModeGraphics(org.eclipse.draw2d.Graphics)
457
	 * @see org.eclipse.gmf.runtime.diagram.ui.printing.internal.util.DiagramPrinter#createMapModeGraphics(org.eclipse.draw2d.Graphics)
405
	 */
458
	 */
406
	protected MapModeGraphics createMapModeGraphics(Graphics theGraphics) {
459
	protected MapModeGraphics createMapModeGraphics(Graphics theGraphics) {
Lines 410-416 Link Here
410
	protected ScaledGraphics createPrinterGraphics(Graphics theGraphics) {
463
	protected ScaledGraphics createPrinterGraphics(Graphics theGraphics) {
411
		return new ScaledGraphics(theGraphics);
464
		return new ScaledGraphics(theGraphics);
412
	}
465
	}
413
		
466
414
	/**
467
	/**
415
	 * Set printing options in a format that is suitable for the Java print
468
	 * Set printing options in a format that is suitable for the Java print
416
	 * service
469
	 * service
Lines 421-431 Link Here
421
	 *            obtain page information from preferences
474
	 *            obtain page information from preferences
422
	 * @return PrintRequestAttribute set suitable for Java print service
475
	 * @return PrintRequestAttribute set suitable for Java print service
423
	 */
476
	 */
424
	protected PrintRequestAttributeSet initializePrintOptions(DocPrintJob printJob,
477
	protected PrintRequestAttributeSet initializePrintOptions(
425
			String jobName,
478
			DocPrintJob printJob, String jobName, IPreferenceStore fPreferences) {
426
			IPreferenceStore fPreferences) {
427
479
428
		PrintOptions advancedOptions = ((PrintHelper) (printHelper)).getPrintOptions();
480
		PrintOptions advancedOptions = ((PrintHelper) (printHelper))
481
				.getPrintOptions();
429
482
430
		PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
483
		PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
431
484
Lines 537-553 Link Here
537
			DiagramEditPart diagramEditPart, boolean loadedPreferences,
590
			DiagramEditPart diagramEditPart, boolean loadedPreferences,
538
			IPreferenceStore fPreferences) {
591
			IPreferenceStore fPreferences) {
539
592
540
		
593
		PrintRequestAttributeSet printRequestAttributeSet = initializePrintOptions(
541
		PrintRequestAttributeSet printRequestAttributeSet = initializePrintOptions(printJob,
594
				printJob, diagramEditPart.getDiagramView().getName(),
542
				diagramEditPart
595
				fPreferences);
543
				.getDiagramView().getName(), fPreferences);
544
596
545
		if (isScaledPercent) {
597
		if (isScaledPercent) {
546
			printToScale(diagramEditPart, loadedPreferences, fPreferences);
598
			printToScale(diagramEditPart, loadedPreferences, fPreferences);
547
		} else {
599
		} else {
548
			printToPages(diagramEditPart, loadedPreferences, fPreferences);
600
			printToPages(diagramEditPart, loadedPreferences, fPreferences);
549
		}
601
		}
550
		
602
551
		Doc doc = new SimpleDoc(this, DocFlavor.SERVICE_FORMATTED.PRINTABLE,
603
		Doc doc = new SimpleDoc(this, DocFlavor.SERVICE_FORMATTED.PRINTABLE,
552
				new HashDocAttributeSet());
604
				new HashDocAttributeSet());
553
605
Lines 573-580 Link Here
573
	/**
625
	/**
574
	 * 
626
	 * 
575
	 * This method paints a portion of the diagram. (The area painted
627
	 * This method paints a portion of the diagram. (The area painted
576
     * representing one page.)
628
	 * representing one page.)
577
     * 
629
	 * 
578
	 * @param page indicates which page to print.
630
	 * @param page indicates which page to print.
579
	 */
631
	 */
580
	protected void drawPage(PageData page) {
632
	protected void drawPage(PageData page) {
Lines 582-588 Link Here
582
634
583
		internalDrawPage(page.diagram, page.bounds, page.preferences,
635
		internalDrawPage(page.diagram, page.bounds, page.preferences,
584
				page.margins, graphics, page.row, page.column, false);
636
				page.margins, graphics, page.row, page.column, false);
585
		
637
586
		// TODO: Re-enable printing of header and footer in phase 2
638
		// TODO: Re-enable printing of header and footer in phase 2
587
		this.graphics.popState();
639
		this.graphics.popState();
588
	}
640
	}
Lines 601-607 Link Here
601
		int width = pageSize.x, height = pageSize.y;
653
		int width = pageSize.x, height = pageSize.y;
602
654
603
		g.pushState();
655
		g.pushState();
604
		
656
605
		g.translate(translated.x, translated.y);
657
		g.translate(translated.x, translated.y);
606
		g.scale(userScale);
658
		g.scale(userScale);
607
659
Lines 626-642 Link Here
626
		g.translate(scaledTranslateX, scaledTranslateY);
678
		g.translate(scaledTranslateX, scaledTranslateY);
627
679
628
		Rectangle clip = new Rectangle(
680
		Rectangle clip = new Rectangle(
629
						(scaledWidth - margins.left - margins.right) * (colIndex - 1) + figureBounds.x, 
681
				(scaledWidth - margins.left - margins.right) * (colIndex - 1)
630
						(scaledHeight - margins.bottom - margins.top)* (rowIndex - 1) + figureBounds.y, 
682
						+ figureBounds.x,
631
						 scaledWidth - margins.right - margins.left, 
683
				(scaledHeight - margins.bottom - margins.top) * (rowIndex - 1)
632
						 scaledHeight - margins.top - margins.bottom);
684
						+ figureBounds.y, scaledWidth - margins.right
685
						- margins.left, scaledHeight - margins.top
686
						- margins.bottom);
633
		g.clipRect(clip);
687
		g.clipRect(clip);
634
688
635
		dgrmEP.getLayer(LayerConstants.PRINTABLE_LAYERS).paint(g);
689
		dgrmEP.getLayer(LayerConstants.PRINTABLE_LAYERS).paint(g);
636
690
637
		g.popState();
691
		g.popState();
638
	}
692
	}
639
	
693
640
	/**
694
	/**
641
	 *  Adjust the page margins to be compatible with the user scale.
695
	 *  Adjust the page margins to be compatible with the user scale.
642
	 *  
696
	 *  

Return to bug 225173