Source Code Cross Referenced for Printout.java in  » XML-UI » xui32 » com » xoetrope » print » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML UI » xui32 » com.xoetrope.print 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.xoetrope.print;
002:
003:        import java.awt.Container;
004:        import java.awt.FontMetrics;
005:        import java.awt.Graphics2D;
006:        import java.awt.PageAttributes;
007:        import java.awt.geom.Rectangle2D;
008:        import java.awt.print.Book;
009:        import java.awt.print.PageFormat;
010:        import java.awt.print.PrinterJob;
011:
012:        import net.xoetrope.xui.XPage;
013:        import java.awt.Container;
014:        import java.awt.print.Paper;
015:
016:        /**
017:         * A print engine for printing pages
018:         *
019:         * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
020:         * the GNU Public License (GPL), please see license.txt for more details. If
021:         * you make commercial use of this software you must purchase a commercial
022:         * license from Xoetrope.</p>
023:         * <p> $Revision: 1.3 $</p>
024:         */
025:        public class Printout {
026:            private PrinterJob printerJob;
027:            private PageFormat pageFormat;
028:            private Book book;
029:            private int pageNumber;
030:            private String headerStyle;
031:
032:            private Object headers[] = new Object[3];
033:            private Object footers[] = new Object[3];
034:
035:            /**
036:             * Position the header or footer to the left of the page
037:             */
038:            public static final int LEFT = 0;
039:            /**
040:             * Position the header or footer to the center of the page
041:             */
042:            public static final int CENTER = 1;
043:            /**
044:             * Position the header or footer to the right of the page
045:             */
046:            public static final int RIGHT = 2;
047:
048:            /**
049:             * Create a new printout
050:             */
051:            public Printout() {
052:                printerJob = PrinterJob.getPrinterJob();
053:                pageFormat = printerJob.defaultPage();
054:                book = new Book();
055:                headerStyle = "base";
056:
057:                pageFormat.setOrientation(pageFormat.LANDSCAPE);
058:            }
059:
060:            /**
061:             * Create a new printout
062:             * @param paper the paper specification for this printout
063:             */
064:            public Printout(Paper paper) {
065:                this ();
066:                pageFormat.setPaper(paper);
067:            }
068:
069:            /**
070:             * Get the default page format
071:             * @return the default page format 
072:             */
073:            public static PageFormat getDefaultPage() {
074:                return PrinterJob.getPrinterJob().defaultPage();
075:            }
076:
077:            /**
078:             * Add a page or container to the book being printed
079:             * @param page the page or content
080:             */
081:            public void addPage(Container page) {
082:                book.append(new PrintablePage(page, this ), pageFormat);
083:            }
084:
085:            /**
086:             * Add a frameset to the printout. Each frame in the frameset will be printed
087:             * @param frameset the frameset
088:             */
089:            public void addFrame(PrintableFrameSet frameset) {
090:                book.append(frameset, pageFormat);
091:            }
092:
093:            /**
094:             * Show the page format dialog. The dialog allows the setup of the page margins
095:             * @return the updated page format
096:             */
097:            public PageFormat showPageFormat() {
098:                pageFormat = printerJob.pageDialog(pageFormat);
099:                return pageFormat;
100:            }
101:
102:            /**
103:             * Initiate the printing
104:             */
105:            public void print() {
106:                try {
107:                    int numPages = book.getNumberOfPages();
108:                    if (numPages > 0) {
109:                        PageAttributes pa = new PageAttributes();
110:                        pa
111:                                .setOrientationRequested(PageAttributes.OrientationRequestedType.LANDSCAPE);
112:                        pa.setOrigin(PageAttributes.OriginType.PRINTABLE);
113:                        pa
114:                                .setPrintQuality(PageAttributes.PrintQualityType.HIGH);
115:                        pa.setColor(PageAttributes.ColorType.COLOR);
116:
117:                        printerJob.setPageable(book);
118:
119:                        boolean result = printerJob.printDialog();
120:                        if (result)
121:                            printerJob.print();
122:                    }
123:                } catch (Exception e) {
124:                    e.printStackTrace();
125:                }
126:            }
127:
128:            /**
129:             * Set the name that will appear in the operating system task list
130:             * @param name the job name
131:             */
132:            public void setJobName(String name) {
133:                printerJob.setJobName(name);
134:            }
135:
136:            /**
137:             * Set the header
138:             * @param text the header text
139:             * @param position the text postition
140:             */
141:            public void setHeader(Object text, int position) {
142:                headers[position] = text;
143:            }
144:
145:            /**
146:             * Set the footer
147:             * @param text the header text
148:             * @param position the text postition
149:             */
150:            public void setFooter(Object text, int position) {
151:                footers[position] = text;
152:            }
153:
154:            /**
155:             * Set the name that will appear in the operating system task list
156:             * @param top true to add the page numbers to the header, false for the footer
157:             * @param position the text postition
158:             * @param format the page number format
159:             */
160:            public void setPageNumbers(boolean top, int position, String format) {
161:                if (top)
162:                    setHeader(new PageNumber(format), position);
163:                else
164:                    setFooter(new PageNumber(format), position);
165:            }
166:
167:            /**
168:             * Print the header or footer decorations for the document
169:             * @param g the graphics context
170:             * @param pf the page setup
171:             * @param top true for header objects
172:             * @param position the poition withing the header/footer
173:             * @param x the x starting position of the decoration
174:             * @param y the y starting position of the decoration
175:             * @param fm the font metriucs of the header/footer font
176:             * @return the decoration height
177:             */
178:            public int printDecoration(Graphics2D g, PageFormat pf,
179:                    boolean top, int position, double x, double y,
180:                    FontMetrics fm) {
181:                String text = null;
182:                if (top && (headers[position] != null))
183:                    text = headers[position].toString();
184:                else if (footers[position] != null)
185:                    text = footers[position].toString();
186:
187:                int fontHeight = fm.getHeight();
188:                int cellW = (int) (pf.getImageableWidth() / 3.0);
189:
190:                if (text != null) {
191:                    Rectangle2D rect = fm.getStringBounds(text, g);
192:                    int xOffset = (int) rect.getWidth();
193:                    if (position == CENTER)
194:                        xOffset = ((cellW - xOffset) / 2);
195:                    else if (position == RIGHT)
196:                        xOffset = (cellW - xOffset - 2);
197:                    else
198:                        xOffset = 0;
199:                    g.drawString(text, (int) (x + xOffset), (int) y
200:                            + fontHeight + fontHeight / 2);
201:                    return 2 * fontHeight;
202:                }
203:
204:                return 0;
205:            }
206:
207:            /**
208:             * Set the style used for the headers and footers
209:             * @param styleName the style name
210:             */
211:            public void setHeaderStyle(String styleName) {
212:                headerStyle = styleName;
213:            }
214:
215:            /**
216:             * Get the style used in the header and footer
217:             * @return the style name
218:             */
219:            public String getHeaderStyle() {
220:                return headerStyle;
221:            }
222:
223:            class PageNumber {
224:                String format;
225:
226:                public PageNumber(String baseStr) {
227:                    format = baseStr;
228:                }
229:
230:                public String toString() {
231:                    return format + (pageNumber + 1);
232:                }
233:            }
234:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.