1   /****************************************************************************
2    **
3    ** This file is part of the yFiles extension package yExport-1.3.
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2007-2012 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  
15  package demo.yext.export;
16  
17  
18  import y.view.BackgroundRenderer;
19  import y.view.Graph2DView;
20  
21  import javax.print.attribute.standard.MediaSize;
22  import javax.print.attribute.standard.MediaSizeName;
23  import java.awt.BasicStroke;
24  import java.awt.Color;
25  import java.awt.Graphics2D;
26  import java.awt.Rectangle;
27  import java.awt.Stroke;
28  import java.awt.Insets;
29  import java.awt.print.PageFormat;
30  
31  
32  /**
33   * Renders the view background as greyed out area with a zoom level independent
34   * white rectangle, whose size corresponds to that of a specified page
35   * format, to achieve an impression similar to that known from document viewers
36   * and word processors.
37   *
38   */
39  public class PageBackgroundRenderer implements BackgroundRenderer {
40    private final Graph2DView view;
41    private PageFormat pageFormat;
42  
43    /**
44     * Constructs a new <code>PageBackgroundRenderer</code> for the specified
45     * view.
46     */
47    public PageBackgroundRenderer( final Graph2DView view ) {
48      this.view = view;
49      pageFormat = new PageFormat();
50      PageFormatHelper.assignMediaSize(
51              pageFormat,
52              MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4)
53      );
54    }
55  
56    public void paint(Graphics2D gfx, int x, int y, int w, int h) {
57      final Color oldColor = gfx.getColor();
58      final Stroke oldStroke = gfx.getStroke();
59  
60      final Rectangle rect = gfx.getClipBounds();
61      gfx.setColor(Color.LIGHT_GRAY);
62      gfx.fillRect(rect.x, rect.y, rect.width, rect.height);
63  
64      final double z = view.getZoom();
65      final int paperWidth = (int)Math.ceil(pageFormat.getWidth());
66      final int paperHeight = (int)Math.ceil(pageFormat.getHeight());
67  
68      gfx.setColor(Color.WHITE);
69      gfx.fillRect(0, 0, paperWidth, paperHeight);
70      gfx.setColor(Color.BLACK);
71      gfx.setStroke(new BasicStroke((float) (1.0 / z)));
72      gfx.drawRect(0, 0, paperWidth, paperHeight);
73      gfx.setColor(Color.LIGHT_GRAY);
74  
75      final Insets margin = PageFormatHelper.getMargin(pageFormat);
76      if (margin.top > 0) {
77        final int _y = margin.top;
78        gfx.drawLine(margin.left, _y, paperWidth - margin.right, _y);
79      }
80      if (margin.bottom > 0) {
81        final int _y = paperHeight - margin.bottom;
82        gfx.drawLine(margin.left, _y, paperWidth - margin.right, _y);
83      }
84      if (margin.left > 0) {
85        final int _x = margin.left;
86        gfx.drawLine(_x, margin.top, _x, paperHeight - margin.bottom);
87      }
88      if (margin.right > 0) {
89        final int _x = paperWidth - margin.right;
90        gfx.drawLine(_x, margin.top, _x, paperHeight - margin.bottom);
91      }
92  
93      gfx.setStroke(oldStroke);
94      gfx.setColor(oldColor);
95    }
96  
97    /**
98     * Specifies the reference page format for this renderer.
99     */
100   public void setPageFormat( final PageFormat pageFormat ) {
101     if (pageFormat == null) {
102       throw new IllegalArgumentException("null");
103     }
104 
105     this.pageFormat = pageFormat;
106 
107     if (view != null) {
108       view.updateView();
109     }
110   }
111 
112   /**
113    * Returns the reference page format for this renderer.
114    */
115   public PageFormat getPageFormat() {
116     return pageFormat;
117   }
118 }
119