| 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 10756 2009-12-02 15:58:24Z 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 |
|
| 45 |
/** |
| 46 |
* Creation-Date: 17.11.2006, 20:31:36 |
| 47 |
* |
| 48 |
* @author Thomas Morgner |
| 49 |
*/ |
| 50 |
public class PageBackgroundDrawable |
| 51 |
{ |
| 52 |
private PageDrawable backend; |
| 53 |
private boolean borderPainted; |
| 54 |
private float shadowSize; |
| 55 |
private double zoom; |
| 56 |
|
| 57 |
public PageBackgroundDrawable() |
| 58 |
{ |
| 59 |
this.shadowSize = 6; |
| 60 |
this.borderPainted = false; |
| 61 |
this.zoom = 1; |
| 62 |
} |
| 63 |
|
| 64 |
public PageDrawable getBackend() |
| 65 |
{ |
| 66 |
return backend; |
| 67 |
} |
| 68 |
|
| 69 |
public void setBackend(final PageDrawable backend) |
| 70 |
{ |
| 71 |
this.backend = backend; |
| 72 |
} |
| 73 |
|
| 74 |
public boolean isBorderPainted() |
| 75 |
{ |
| 76 |
return borderPainted; |
| 77 |
} |
| 78 |
|
| 79 |
public void setBorderPainted(final boolean borderPainted) |
| 80 |
{ |
| 81 |
this.borderPainted = borderPainted; |
| 82 |
} |
| 83 |
|
| 84 |
public double getZoom() |
| 85 |
{ |
| 86 |
return zoom; |
| 87 |
} |
| 88 |
|
| 89 |
public void setZoom(final double zoom) |
| 90 |
{ |
| 91 |
this.zoom = zoom; |
| 92 |
} |
| 93 |
|
| 94 |
public Dimension getPreferredSize() |
| 95 |
{ |
| 96 |
if (backend == null) |
| 97 |
{ |
| 98 |
return new Dimension(0, 0); |
| 99 |
} |
| 100 |
final Dimension preferredSize = backend.getPreferredSize(); |
| 101 |
|
| 102 |
return new Dimension |
| 103 |
((int) ((preferredSize.width + shadowSize) * zoom), |
| 104 |
(int) ((preferredSize.height + shadowSize) * zoom)); |
| 105 |
} |
| 106 |
|
| 107 |
public boolean isPreserveAspectRatio() |
| 108 |
{ |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
public float getShadowSize() |
| 113 |
{ |
| 114 |
return shadowSize; |
| 115 |
} |
| 116 |
|
| 117 |
public void setShadowSize(final float shadowSize) |
| 118 |
{ |
| 119 |
this.shadowSize = shadowSize; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Draws the object. |
| 124 |
* |
| 125 |
* @param g2 the graphics device. |
| 126 |
* @param area the area inside which the object should be drawn. |
| 127 |
*/ |
| 128 |
public void draw(Graphics2D g2, Rectangle2D area) |
| 129 |
{ |
| 130 |
if (backend == null) |
| 131 |
{ |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
final PageFormat pageFormat = backend.getPageFormat(); |
| 136 |
final float outerW = (float) pageFormat.getWidth(); |
| 137 |
final float outerH = (float) pageFormat.getHeight(); |
| 138 |
|
| 139 |
final float innerX = (float) pageFormat.getImageableX(); |
| 140 |
final float innerY = (float) pageFormat.getImageableY(); |
| 141 |
final float innerW = (float) pageFormat.getImageableWidth(); |
| 142 |
final float innerH = (float) pageFormat.getImageableHeight(); |
| 143 |
|
| 144 |
//double paperBorder = paperBorderPixel * zoomFactor; |
| 145 |
|
| 146 |
/** Prepare background **/ |
| 147 |
g2.transform(AffineTransform.getScaleInstance(getZoom(), getZoom())); |
| 148 |
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); |
| 149 |
|
| 150 |
/** Prepare background **/ |
| 151 |
Rectangle2D pageArea = |
| 152 |
new Rectangle2D.Float(0, 0, outerW, outerH); |
| 153 |
|
| 154 |
g2.setPaint(Color.white); |
| 155 |
g2.fill(pageArea); |
| 156 |
|
| 157 |
|
| 158 |
Graphics2D g22 = (Graphics2D) g2.create(); |
| 159 |
backend.draw(g22, new Rectangle2D.Double |
| 160 |
(0, 0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight())); |
| 161 |
g22.dispose(); |
| 162 |
|
| 163 |
/** |
| 164 |
* The border around the printable area is painted when the corresponding property is |
| 165 |
* set to true. |
| 166 |
*/ |
| 167 |
final Rectangle2D printingArea = new Rectangle2D.Float(innerX, innerY, innerW, innerH); |
| 168 |
|
| 169 |
/** Paint Page Shadow */ |
| 170 |
final Rectangle2D southborder = new Rectangle2D.Float |
| 171 |
(getShadowSize(), outerH, |
| 172 |
outerW, getShadowSize()); |
| 173 |
|
| 174 |
g2.setPaint(UIManager.getColor("controlShadow")); |
| 175 |
|
| 176 |
g2.fill(southborder); |
| 177 |
|
| 178 |
final Rectangle2D eastborder = new Rectangle2D.Float |
| 179 |
(outerW, getShadowSize(),getShadowSize(), outerH); |
| 180 |
|
| 181 |
g2.fill(eastborder); |
| 182 |
final Rectangle2D transPageArea = new Rectangle2D.Float(0, 0, outerW, outerH); |
| 183 |
|
| 184 |
g2.setPaint(Color.black); |
| 185 |
g2.draw(transPageArea); |
| 186 |
if (isBorderPainted()) |
| 187 |
{ |
| 188 |
g2.setPaint(Color.gray); |
| 189 |
g2.draw(printingArea); |
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
} |