001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: PageBackgroundDrawable.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    
032    package org.jfree.report.modules.gui.swing.preview;
033    
034    import java.awt.Color;
035    import java.awt.Dimension;
036    import java.awt.Graphics2D;
037    import java.awt.RenderingHints;
038    import java.awt.geom.AffineTransform;
039    import java.awt.geom.Rectangle2D;
040    import java.awt.print.PageFormat;
041    import javax.swing.UIManager;
042    
043    import org.jfree.layouting.modules.output.graphics.PageDrawable;
044    import org.jfree.ui.ExtendedDrawable;
045    
046    /**
047     * Creation-Date: 17.11.2006, 20:31:36
048     *
049     * @author Thomas Morgner
050     */
051    public class PageBackgroundDrawable implements ExtendedDrawable
052    {
053      private PageDrawable backend;
054      private boolean borderPainted;
055      private float shadowSize;
056      private double zoom;
057    
058      public PageBackgroundDrawable()
059      {
060        this.shadowSize = 6;
061        this.borderPainted = false;
062        this.zoom = 1;
063      }
064    
065      public PageDrawable getBackend()
066      {
067        return backend;
068      }
069    
070      public void setBackend(final PageDrawable backend)
071      {
072        this.backend = backend;
073      }
074    
075      public boolean isBorderPainted()
076      {
077        return borderPainted;
078      }
079    
080      public void setBorderPainted(final boolean borderPainted)
081      {
082        this.borderPainted = borderPainted;
083      }
084    
085      public double getZoom()
086      {
087        return zoom;
088      }
089    
090      public void setZoom(final double zoom)
091      {
092        this.zoom = zoom;
093      }
094    
095      public Dimension getPreferredSize()
096      {
097        if (backend == null)
098        {
099          return new Dimension(0, 0);
100        }
101        final Dimension preferredSize = backend.getPreferredSize();
102    
103        return new Dimension
104            ((int) ((preferredSize.width + shadowSize) * zoom),
105                (int) ((preferredSize.height + shadowSize) * zoom));
106      }
107    
108      public boolean isPreserveAspectRatio()
109      {
110        return true;
111      }
112    
113      public float getShadowSize()
114      {
115        return shadowSize;
116      }
117    
118      public void setShadowSize(final float shadowSize)
119      {
120        this.shadowSize = shadowSize;
121      }
122    
123      /**
124       * Draws the object.
125       *
126       * @param g2   the graphics device.
127       * @param area the area inside which the object should be drawn.
128       */
129      public void draw(final Graphics2D g2, final Rectangle2D area)
130      {
131        if (backend == null)
132        {
133          return;
134        }
135    
136        final PageFormat pageFormat = backend.getPageFormat();
137        final float outerW = (float) pageFormat.getWidth();
138        final float outerH = (float) pageFormat.getHeight();
139    
140        final float innerX = (float) pageFormat.getImageableX();
141        final float innerY = (float) pageFormat.getImageableY();
142        final float innerW = (float) pageFormat.getImageableWidth();
143        final float innerH = (float) pageFormat.getImageableHeight();
144    
145        //double paperBorder = paperBorderPixel * zoomFactor;
146    
147        /** Prepare background **/
148        g2.transform(AffineTransform.getScaleInstance(getZoom(), getZoom()));
149        g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
150    
151        /** Prepare background **/
152        final Rectangle2D pageArea =
153            new Rectangle2D.Float(0, 0, outerW, outerH);
154    
155        g2.setPaint(Color.white);
156        g2.fill(pageArea);
157    
158    
159        final Graphics2D g22 = (Graphics2D) g2.create();
160        backend.draw(g22, new Rectangle2D.Double
161            (0, 0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight()));
162        g22.dispose();
163    
164        /**
165         * The border around the printable area is painted when the corresponding property is
166         * set to true.
167         */
168        final Rectangle2D printingArea = new Rectangle2D.Float(innerX, innerY, innerW, innerH);
169    
170        /** Paint Page Shadow */
171        final Rectangle2D southborder = new Rectangle2D.Float
172            (getShadowSize(), outerH,
173                outerW, getShadowSize());
174    
175        g2.setPaint(UIManager.getColor("controlShadow"));
176    
177        g2.fill(southborder);
178    
179        final Rectangle2D eastborder = new Rectangle2D.Float
180            (outerW, getShadowSize(),getShadowSize(), outerH);
181    
182        g2.fill(eastborder);
183        final Rectangle2D transPageArea = new Rectangle2D.Float(0, 0, outerW, outerH);
184    
185        g2.setPaint(Color.black);
186        g2.draw(transPageArea);
187        if (isBorderPainted())
188        {
189          g2.setPaint(Color.gray);
190          g2.draw(printingArea);
191        }
192    
193      }
194    }