PageRenderTime 111ms CodeModel.GetById 104ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/net.sf.paperclips.ui/src/net/sf/paperclips/ui/PrintPieceCanvas.java

https://code.google.com/p/swt-paperclips/
Java | 85 lines | 41 code | 10 blank | 34 comment | 4 complexity | 0e68153f0da21a1f9448d5b13b382585 MD5 | raw file
 1/*
 2 * Copyright (c) 2005 Matthew Hall and others.
 3 * All rights reserved. This program and the accompanying materials
 4 * are made available under the terms of the Eclipse Public License v1.0
 5 * which accompanies this distribution, and is available at
 6 * http://www.eclipse.org/legal/epl-v10.html
 7 * 
 8 * Contributors:
 9 *     Matthew Hall - initial API and implementation
10 */
11package net.sf.paperclips.ui;
12
13import net.sf.paperclips.PrintPiece;
14
15import org.eclipse.swt.SWT;
16import org.eclipse.swt.graphics.Rectangle;
17import org.eclipse.swt.widgets.Canvas;
18import org.eclipse.swt.widgets.Composite;
19import org.eclipse.swt.widgets.Event;
20import org.eclipse.swt.widgets.Listener;
21
22/**
23 * A canvas for displaying Print objects.
24 * 
25 * @author Matthew
26 */
27public class PrintPieceCanvas extends Canvas {
28	PrintPiece piece = null;
29
30	/**
31	 * Constructs a PrintCanvas with the given parent and style.
32	 * 
33	 * @param parent
34	 *            the parent Composite.
35	 * @param style
36	 *            the style parameter.
37	 */
38	public PrintPieceCanvas(Composite parent, int style) {
39		super(parent, style);
40
41		setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
42		setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
43
44		addListener(SWT.Paint, new Listener() {
45			public void handleEvent(Event event) {
46				if (piece == null)
47					return;
48
49				Rectangle client = getClientArea();
50				piece.paint(event.gc, client.x, client.y);
51			}
52		});
53		addListener(SWT.Dispose, new Listener() {
54			public void handleEvent(Event event) {
55				disposePrintPiece();
56			}
57		});
58	}
59
60	/**
61	 * Displays the given Print in this PrintCanvas.
62	 * 
63	 * @param piece
64	 *            the PrintPiece to display.
65	 */
66	public void setPrintPiece(PrintPiece piece) {
67		disposePrintPiece();
68		this.piece = piece;
69		redraw();
70	}
71
72	/**
73	 * Returns the PrintPiece being displayed by this PrintCanvas.
74	 * 
75	 * @return the PrintPiece being displayed by this PrintCanvas.
76	 */
77	public PrintPiece getPrintPiece() {
78		return piece;
79	}
80
81	private void disposePrintPiece() {
82		if (piece != null)
83			piece.dispose();
84	}
85}