KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > PrintCommands


1 /*
2  * PrintCommands.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: PrintCommands.java,v 1.1.1.1 2002/09/24 16:07:56 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.print.Book JavaDoc;
25 import java.awt.print.PrinterException JavaDoc;
26 import java.awt.print.PrinterJob JavaDoc;
27
28 public final class PrintCommands
29 {
30     public static void print()
31     {
32         final Editor editor = Editor.currentEditor();
33         if (editor.getMark() != null &&
34             editor.getMarkLine() != editor.getDotLine() &&
35             editor.getDotOffset() == 0 &&
36             editor.getMarkOffset() == 0) {
37             printRegion();
38         } else {
39             printBuffer();
40         }
41     }
42
43     public static void printRegion()
44     {
45         final Editor editor = Editor.currentEditor();
46         if (editor.getMark() == null)
47             return;
48         if (editor.getMarkLine() == editor.getDotLine())
49             return;
50         if (editor.getDotOffset() != 0)
51             return;
52         if (editor.getMarkOffset() != 0)
53             return;
54         Region r = new Region(editor);
55         int lineCount = r.getEndLine().lineNumber() - r.getBeginLine().lineNumber();
56         final String JavaDoc title = "Print Region";
57         FastStringBuffer sb = new FastStringBuffer("Print selected region (");
58         sb.append(lineCount);
59         sb.append(" line");
60         if (lineCount > 1)
61             sb.append('s');
62         sb.append(")?");
63         String JavaDoc prompt = sb.toString();
64         if (!editor.confirm(title, prompt))
65             return;
66         editor.setWaitCursor();
67         PrinterJob JavaDoc job = PrinterJob.getPrinterJob();
68         PageFormat pageFormat = new PageFormat(editor, r);
69         PrintPainter painter = new PrintPainter(editor, r);
70         Book JavaDoc book = new Book JavaDoc();
71         int pages = lineCount / pageFormat.getLinesPerPage();
72         if (lineCount % pageFormat.getLinesPerPage() != 0)
73             ++pages;
74         book.append(painter, pageFormat, pages); // All pages.
75
pageFormat.setPageCount(pages);
76         job.setPageable(book);
77         try {
78             job.print();
79         }
80         catch (PrinterException JavaDoc e) {
81             Log.error(e);
82         }
83         editor.setDefaultCursor();
84     }
85
86     public static void printBuffer()
87     {
88         final Editor editor = Editor.currentEditor();
89         final Buffer buffer = editor.getBuffer();
90         final String JavaDoc title = "Print Buffer";
91         int lineCount = buffer.getLineCount();
92         FastStringBuffer sb = new FastStringBuffer("Print current buffer in its entirety (");
93         sb.append(lineCount);
94         sb.append(" line");
95         if (lineCount > 1)
96             sb.append('s');
97         sb.append(")?");
98         String JavaDoc prompt = sb.toString();
99         if (!editor.confirm(title, prompt))
100             return;
101         editor.setWaitCursor();
102         PrinterJob JavaDoc job = PrinterJob.getPrinterJob();
103         PageFormat pageFormat = new PageFormat(editor, null);
104         PrintPainter painter = new PrintPainter(editor);
105         Book JavaDoc book = new Book JavaDoc();
106         int pages = lineCount / pageFormat.getLinesPerPage();
107         if (lineCount % pageFormat.getLinesPerPage() != 0)
108             ++pages;
109         book.append(painter, pageFormat, pages); // All pages.
110
pageFormat.setPageCount(pages);
111         job.setPageable(book);
112         try {
113             job.print();
114         }
115         catch (PrinterException JavaDoc e) {
116             Log.error(e);
117         }
118         editor.setDefaultCursor();
119     }
120 }
121
Popular Tags