Source for org.jfree.report.modules.gui.swing.preview.PageBackgroundDrawable

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: PageBackgroundDrawable.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.modules.gui.swing.preview;
  33: 
  34: import java.awt.Color;
  35: import java.awt.Dimension;
  36: import java.awt.Graphics2D;
  37: import java.awt.RenderingHints;
  38: import java.awt.geom.AffineTransform;
  39: import java.awt.geom.Rectangle2D;
  40: import java.awt.print.PageFormat;
  41: import javax.swing.UIManager;
  42: 
  43: import org.jfree.layouting.modules.output.graphics.PageDrawable;
  44: import org.jfree.ui.ExtendedDrawable;
  45: 
  46: /**
  47:  * Creation-Date: 17.11.2006, 20:31:36
  48:  *
  49:  * @author Thomas Morgner
  50:  */
  51: public class PageBackgroundDrawable implements ExtendedDrawable
  52: {
  53:   private PageDrawable backend;
  54:   private boolean borderPainted;
  55:   private float shadowSize;
  56:   private double zoom;
  57: 
  58:   public PageBackgroundDrawable()
  59:   {
  60:     this.shadowSize = 6;
  61:     this.borderPainted = false;
  62:     this.zoom = 1;
  63:   }
  64: 
  65:   public PageDrawable getBackend()
  66:   {
  67:     return backend;
  68:   }
  69: 
  70:   public void setBackend(final PageDrawable backend)
  71:   {
  72:     this.backend = backend;
  73:   }
  74: 
  75:   public boolean isBorderPainted()
  76:   {
  77:     return borderPainted;
  78:   }
  79: 
  80:   public void setBorderPainted(final boolean borderPainted)
  81:   {
  82:     this.borderPainted = borderPainted;
  83:   }
  84: 
  85:   public double getZoom()
  86:   {
  87:     return zoom;
  88:   }
  89: 
  90:   public void setZoom(final double zoom)
  91:   {
  92:     this.zoom = zoom;
  93:   }
  94: 
  95:   public Dimension getPreferredSize()
  96:   {
  97:     if (backend == null)
  98:     {
  99:       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: }