001: /*
002: * PrintCommands.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: PrintCommands.java,v 1.1.1.1 2002/09/24 16:07:56 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.print.Book;
025: import java.awt.print.PrinterException;
026: import java.awt.print.PrinterJob;
027:
028: public final class PrintCommands {
029: public static void print() {
030: final Editor editor = Editor.currentEditor();
031: if (editor.getMark() != null
032: && editor.getMarkLine() != editor.getDotLine()
033: && editor.getDotOffset() == 0
034: && editor.getMarkOffset() == 0) {
035: printRegion();
036: } else {
037: printBuffer();
038: }
039: }
040:
041: public static void printRegion() {
042: final Editor editor = Editor.currentEditor();
043: if (editor.getMark() == null)
044: return;
045: if (editor.getMarkLine() == editor.getDotLine())
046: return;
047: if (editor.getDotOffset() != 0)
048: return;
049: if (editor.getMarkOffset() != 0)
050: return;
051: Region r = new Region(editor);
052: int lineCount = r.getEndLine().lineNumber()
053: - r.getBeginLine().lineNumber();
054: final String title = "Print Region";
055: FastStringBuffer sb = new FastStringBuffer(
056: "Print selected region (");
057: sb.append(lineCount);
058: sb.append(" line");
059: if (lineCount > 1)
060: sb.append('s');
061: sb.append(")?");
062: String prompt = sb.toString();
063: if (!editor.confirm(title, prompt))
064: return;
065: editor.setWaitCursor();
066: PrinterJob job = PrinterJob.getPrinterJob();
067: PageFormat pageFormat = new PageFormat(editor, r);
068: PrintPainter painter = new PrintPainter(editor, r);
069: Book book = new Book();
070: int pages = lineCount / pageFormat.getLinesPerPage();
071: if (lineCount % pageFormat.getLinesPerPage() != 0)
072: ++pages;
073: book.append(painter, pageFormat, pages); // All pages.
074: pageFormat.setPageCount(pages);
075: job.setPageable(book);
076: try {
077: job.print();
078: } catch (PrinterException e) {
079: Log.error(e);
080: }
081: editor.setDefaultCursor();
082: }
083:
084: public static void printBuffer() {
085: final Editor editor = Editor.currentEditor();
086: final Buffer buffer = editor.getBuffer();
087: final String title = "Print Buffer";
088: int lineCount = buffer.getLineCount();
089: FastStringBuffer sb = new FastStringBuffer(
090: "Print current buffer in its entirety (");
091: sb.append(lineCount);
092: sb.append(" line");
093: if (lineCount > 1)
094: sb.append('s');
095: sb.append(")?");
096: String prompt = sb.toString();
097: if (!editor.confirm(title, prompt))
098: return;
099: editor.setWaitCursor();
100: PrinterJob job = PrinterJob.getPrinterJob();
101: PageFormat pageFormat = new PageFormat(editor, null);
102: PrintPainter painter = new PrintPainter(editor);
103: Book book = new Book();
104: int pages = lineCount / pageFormat.getLinesPerPage();
105: if (lineCount % pageFormat.getLinesPerPage() != 0)
106: ++pages;
107: book.append(painter, pageFormat, pages); // All pages.
108: pageFormat.setPageCount(pages);
109: job.setPageable(book);
110: try {
111: job.print();
112: } catch (PrinterException e) {
113: Log.error(e);
114: }
115: editor.setDefaultCursor();
116: }
117: }
|