001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.uml.print;
053:
054: import java.awt.Dimension;
055: import java.awt.Color;
056: import java.awt.Image;
057: import java.awt.Graphics;
058: import java.awt.image.BufferedImage;
059: import java.awt.print.*;
060: import org.acm.seguin.uml.UMLPackage;
061: import java.awt.Graphics2D;
062: import org.acm.seguin.print.PagePrinter;
063:
064: /**
065: * Handles printing the page
066: *
067: *@author Chris Seguin
068: *@created August 6, 1999
069: */
070: public class UMLPagePrinter extends PagePrinter {
071: private UMLPackage currentPackage;
072:
073: /**
074: * Constructor for the UMLPagePrinter object
075: *
076: *@param panel the current package
077: */
078: public UMLPagePrinter(UMLPackage panel) {
079: currentPackage = panel;
080: }
081:
082: /**
083: * Guess the number of pages
084: *
085: *@param pf Description of Parameter
086: *@return Description of the Returned Value
087: */
088: public int calculatePageCount(PageFormat pf) {
089: Dimension size = currentPackage.getPreferredSize();
090: int pageHeight = (int) pf.getImageableHeight() - headerHeight;
091: int pageWidth = (int) pf.getImageableWidth();
092:
093: int pagesWide = (int) (1 + getScale() * size.width / pageWidth);
094: int pagesHigh = (int) (1 + getScale() * size.height
095: / pageHeight);
096:
097: return pagesWide * pagesHigh;
098: }
099:
100: /**
101: * Print the page
102: *
103: *@param g the graphics object
104: *@param pf the page format
105: *@param pageNumber the page number
106: *@return Description of the Returned Value
107: */
108: public int print(Graphics g, PageFormat pf, int pageNumber) {
109: Dimension size = currentPackage.getPreferredSize();
110: int pageHeight = (int) pf.getImageableHeight() - headerHeight;
111: int pageWidth = (int) pf.getImageableWidth();
112:
113: int pagesWide = (int) (1 + getScale() * size.width / pageWidth);
114: int pagesHigh = (int) (1 + getScale() * size.height
115: / pageHeight);
116: if (pageNumber > pagesWide * pagesHigh) {
117: return Printable.NO_SUCH_PAGE;
118: }
119:
120: int row = pageNumber / pagesWide;
121: int col = pageNumber % pagesWide;
122:
123: /*
124: * if (panelBuffer == null) {
125: * panelBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
126: * Graphics tempGraphics = panelBuffer.getGraphics();
127: * tempGraphics.setColor(Color.white);
128: * tempGraphics.fillRect(0, 0, size.width, size.height);
129: * currentPackage.print(tempGraphics, 0, 0);
130: * }
131: * g.drawImage(panelBuffer,
132: * ((int) pf.getImageableX()) - col * pageWidth,
133: * ((int) pf.getImageableY())- row * pageHeight, null);
134: */
135: ((Graphics2D) g).translate(
136: pf.getImageableX() - col * pageWidth, pf
137: .getImageableY()
138: - row * pageHeight + headerHeight);
139: ((Graphics2D) g).scale(getScale(), getScale());
140: currentPackage.print(g, 0, 0);
141:
142: ((Graphics2D) g).scale(1 / getScale(), 1 / getScale());
143: ((Graphics2D) g)
144: .translate(
145: -(pf.getImageableX() - col * pageWidth),
146: -(pf.getImageableY() - row * pageHeight + headerHeight));
147:
148: String packageName = currentPackage.getSummary().getName();
149: if ((packageName == null) || (packageName.length() == 0)) {
150: packageName = "Top Level Package";
151: }
152: printHeader(g, packageName, "(" + (1 + col) + ", " + (1 + row)
153: + ")", "(" + pagesWide + ", " + pagesHigh + ")");
154:
155: return Printable.PAGE_EXISTS;
156: }
157:
158: /**
159: * Returns the page
160: *
161: *@param dialog present a dialog screen if none
162: *@return the current page format
163: */
164: public static PageFormat getPageFormat(boolean dialog) {
165: PageFormat pf = PagePrinter.getPageFormat(dialog);
166: setScale(0.8);
167: return pf;
168: }
169:
170: /**
171: * Return the width of the page
172: *
173: *@return Description of the Returned Value
174: */
175: public static int getPageHeight() {
176: int result = PagePrinter.getPageHeight();
177: if (result == -1) {
178: return -1;
179: }
180:
181: return (result - headerHeight);
182: }
183: }
|