/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. */
  11. package net.sf.paperclips.ui;
  12. import net.sf.paperclips.PrintPiece;
  13. import org.eclipse.swt.SWT;
  14. import org.eclipse.swt.graphics.Rectangle;
  15. import org.eclipse.swt.widgets.Canvas;
  16. import org.eclipse.swt.widgets.Composite;
  17. import org.eclipse.swt.widgets.Event;
  18. import org.eclipse.swt.widgets.Listener;
  19. /**
  20. * A canvas for displaying Print objects.
  21. *
  22. * @author Matthew
  23. */
  24. public class PrintPieceCanvas extends Canvas {
  25. PrintPiece piece = null;
  26. /**
  27. * Constructs a PrintCanvas with the given parent and style.
  28. *
  29. * @param parent
  30. * the parent Composite.
  31. * @param style
  32. * the style parameter.
  33. */
  34. public PrintPieceCanvas(Composite parent, int style) {
  35. super(parent, style);
  36. setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
  37. setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
  38. addListener(SWT.Paint, new Listener() {
  39. public void handleEvent(Event event) {
  40. if (piece == null)
  41. return;
  42. Rectangle client = getClientArea();
  43. piece.paint(event.gc, client.x, client.y);
  44. }
  45. });
  46. addListener(SWT.Dispose, new Listener() {
  47. public void handleEvent(Event event) {
  48. disposePrintPiece();
  49. }
  50. });
  51. }
  52. /**
  53. * Displays the given Print in this PrintCanvas.
  54. *
  55. * @param piece
  56. * the PrintPiece to display.
  57. */
  58. public void setPrintPiece(PrintPiece piece) {
  59. disposePrintPiece();
  60. this.piece = piece;
  61. redraw();
  62. }
  63. /**
  64. * Returns the PrintPiece being displayed by this PrintCanvas.
  65. *
  66. * @return the PrintPiece being displayed by this PrintCanvas.
  67. */
  68. public PrintPiece getPrintPiece() {
  69. return piece;
  70. }
  71. private void disposePrintPiece() {
  72. if (piece != null)
  73. piece.dispose();
  74. }
  75. }