001: /*
002: * BufferPrinter1_4.java - Main class that controls printing
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2001 Slava Pestov
007: * Portions copyright (C) 2002 Thomas Dilts
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: */
023:
024: package org.gjt.sp.jedit.print;
025:
026: //{{{ Imports
027: import javax.print.attribute.*;
028: import javax.print.attribute.standard.*;
029: import java.awt.print.*;
030: import java.awt.*;
031: import java.io.*;
032: import org.gjt.sp.jedit.*;
033: import org.gjt.sp.util.Log;
034:
035: //}}}
036:
037: public class BufferPrinter1_4 {
038: //{{{ getPrintJob() method
039: private static PrinterJob getPrintJob(String jobName) {
040: job = PrinterJob.getPrinterJob();
041:
042: format = new HashPrintRequestAttributeSet();
043:
044: String settings = jEdit.getSettingsDirectory();
045: if (settings != null) {
046: String printSpecPath = MiscUtilities.constructPath(
047: settings, "printspec");
048: File filePrintSpec = new File(printSpecPath);
049:
050: if (filePrintSpec.exists()) {
051: try {
052: FileInputStream fileIn = new FileInputStream(
053: filePrintSpec);
054: ObjectInputStream obIn = new ObjectInputStream(
055: fileIn);
056: format = (HashPrintRequestAttributeSet) obIn
057: .readObject();
058: } catch (Exception e) {
059: Log.log(Log.ERROR, BufferPrinter1_4.class, e);
060: }
061: //for backwards compatibility, the color variable is stored also as a property
062: if (jEdit.getBooleanProperty("print.color"))
063: format.add(Chromaticity.COLOR);
064: else
065: format.add(Chromaticity.MONOCHROME);
066:
067: //no need to always keep the same job name for every printout.
068: format.add(new JobName(jobName, null));
069: }
070: }
071:
072: return job;
073: } //}}}
074:
075: //{{{ pageSetup() method
076: public static void pageSetup(View view) {
077: PrinterJob prnJob = getPrintJob("PageSetupOnly");
078: if (prnJob.pageDialog(format) != null)
079: savePrintSpec();
080: } //}}}
081:
082: //{{{ print() method
083: public static void print(final View view, final Buffer buffer,
084: boolean selection) {
085: job = getPrintJob(buffer.getPath());
086:
087: boolean header = jEdit.getBooleanProperty("print.header");
088: boolean footer = jEdit.getBooleanProperty("print.footer");
089: boolean lineNumbers = jEdit
090: .getBooleanProperty("print.lineNumbers");
091: boolean color = jEdit.getBooleanProperty("print.color");
092: Font font = jEdit.getFontProperty("print.font");
093:
094: BufferPrintable printable = new BufferPrintable(job, format,
095: view, buffer, font, header, footer, lineNumbers, color);
096: job.setPrintable(printable);
097:
098: if (!job.printDialog(format))
099: return;
100:
101: savePrintSpec();
102:
103: printable.print();
104: } //}}}
105:
106: //{{{ getPageFormat() method
107: public static PageFormat getPageFormat() {
108: //convert from PrintRequestAttributeSet to the pageFormat
109: PrinterJob prnJob = getPrintJob(" ");
110: PageFormat pf = prnJob.defaultPage();
111: Paper pap = pf.getPaper();
112:
113: MediaSizeName media = (MediaSizeName) format.get(Media.class);
114: MediaSize ms = MediaSize.getMediaSizeForName(media);
115:
116: MediaPrintableArea mediaarea = (MediaPrintableArea) format
117: .get(MediaPrintableArea.class);
118: if (mediaarea != null)
119: pap
120: .setImageableArea(
121: (mediaarea.getX(MediaPrintableArea.INCH) * 72),
122: (mediaarea.getY(MediaPrintableArea.INCH) * 72),
123: (mediaarea
124: .getWidth(MediaPrintableArea.INCH) * 72),
125: (mediaarea
126: .getHeight(MediaPrintableArea.INCH) * 72));
127: if (ms != null)
128: pap.setSize((ms.getX(MediaSize.INCH) * 72), (ms
129: .getY(MediaSize.INCH) * 72));
130: pf.setPaper(pap);
131:
132: OrientationRequested orientation = (OrientationRequested) format
133: .get(OrientationRequested.class);
134: if (orientation != null) {
135: if (orientation.getValue() == OrientationRequested.LANDSCAPE
136: .getValue()) {
137: pf.setOrientation(PageFormat.LANDSCAPE);
138: } else if (orientation.getValue() == OrientationRequested.REVERSE_LANDSCAPE
139: .getValue()) {
140: pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
141: } else if (orientation.getValue() == OrientationRequested.PORTRAIT
142: .getValue()) {
143: pf.setOrientation(PageFormat.PORTRAIT);
144: } else if (orientation.getValue() == OrientationRequested.REVERSE_PORTRAIT
145: .getValue()) {
146: //doesnt exist??
147: //pf.setOrientation(PageFormat.REVERSE_PORTRAIT);
148: //then just do the next best thing
149: pf.setOrientation(PageFormat.PORTRAIT);
150: }
151: }
152: return pf;
153: } //}}}
154:
155: //{{{ savePrintSpec() method
156: private static void savePrintSpec() {
157: String settings = jEdit.getSettingsDirectory();
158: if (settings == null)
159: return;
160:
161: String printSpecPath = MiscUtilities.constructPath(settings,
162: "printspec");
163: File filePrintSpec = new File(printSpecPath);
164:
165: try {
166: FileOutputStream fileOut = new FileOutputStream(
167: filePrintSpec);
168: ObjectOutputStream obOut = new ObjectOutputStream(fileOut);
169: obOut.writeObject(format);
170: //for backwards compatibility, the color variable is stored also as a property
171: Chromaticity cc = (Chromaticity) format
172: .get(Chromaticity.class);
173: if (cc != null)
174: jEdit.setBooleanProperty("print.color",
175: cc.getValue() == Chromaticity.COLOR.getValue());
176: } catch (Exception e) {
177: e.printStackTrace();
178: }
179: }
180:
181: //}}}
182:
183: //{{{ Private members
184: private static PrintRequestAttributeSet format;
185: private static PrinterJob job;
186: //}}}
187: }
|