KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > PrintScreenPainter


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps;
15
16 import java.awt.*;
17 import java.awt.print.*;
18 import javax.swing.*;
19 import java.util.*;
20
21 import org.compiere.util.*;
22 import org.compiere.print.*;
23
24 /**
25  * PrintScreen Painter
26  *
27  * @author Jorg Janke
28  * @version $Id: PrintScreenPainter.java,v 1.6 2002/08/09 04:14:45 jjanke Exp $
29  */

30 public class PrintScreenPainter implements Pageable, Printable
31 {
32     /**
33      * PrintScreen Painter
34      * @param element Window to print
35      */

36     public PrintScreenPainter (Window element)
37     {
38         m_element = element;
39     } // PrintScreenPainter
40

41     /** Element */
42     private Window m_element;
43
44     /**
45      * Get Number of pages
46      * @return 1
47      */

48     public int getNumberOfPages()
49     {
50         return 1;
51     } // getNumberOfPages
52

53     /**
54      * Get Printable
55      * @param pageIndex page index
56      * @return this
57      * @throws java.lang.IndexOutOfBoundsException
58      */

59     public Printable getPrintable(int pageIndex) throws java.lang.IndexOutOfBoundsException JavaDoc
60     {
61         return this;
62     } // getPrintable
63

64     /**
65      * Get Page Format
66      * @param pageIndex page index
67      * @return Portrait
68      * @throws java.lang.IndexOutOfBoundsException
69      */

70     public PageFormat getPageFormat(int pageIndex) throws java.lang.IndexOutOfBoundsException JavaDoc
71     {
72         CPaper paper = new CPaper(false);
73         return paper.getPageFormat();
74     } // getPageFormat
75

76     /**
77      * Print
78      * @param graphics graphics
79      * @param pageFormat page format
80      * @param pageIndex page index
81      * @return NO_SUCH_PAGE or PAGE_EXISTS
82      * @throws PrinterException
83      */

84     public int print (Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
85     {
86     // Log.trace(Log.l4_Data, "PrintScreenPainter.print " + pageIndex, "ClipBounds=" + graphics.getClipBounds());
87
if (pageIndex > 0)
88             return Printable.NO_SUCH_PAGE;
89         //
90
Graphics2D g2 = (Graphics2D) graphics;
91
92         // Start position - top of page
93
g2.translate (pageFormat.getImageableX(), pageFormat.getImageableY());
94
95         // Print Header
96
String JavaDoc header = Msg.getMsg(Env.getCtx(), "PrintScreen") + " - "
97             + DisplayType.getDateFormat(DisplayType.DateTime).format(new Date());
98         int y = g2.getFontMetrics().getHeight(); // leading + ascent + descent
99
g2.drawString(header, 0, y);
100         // Leave one row free
101
g2.translate (0, 2*y);
102
103         double xRatio = pageFormat.getImageableWidth() / m_element.getSize().width;
104         double yRatio = (pageFormat.getImageableHeight() - 2*y) / m_element.getSize().height;
105         // Sacle evenly, but don't inflate
106
double ratio = Math.min(Math.min(xRatio, yRatio), 1.0);
107         g2.scale (ratio, ratio);
108         // Print Element
109
m_element.printAll (g2);
110
111         return Printable.PAGE_EXISTS;
112     } // print
113

114     /*************************************************************************/
115
116     /**
117      * Static print start
118      * @param element window
119      */

120     public static void printScreen (Window element)
121     {
122         PrintUtil.print(new PrintScreenPainter(element), null, "PrintScreen", 1, false);
123     } // printScreen
124

125 } // PrintScreenPainter
126
Popular Tags