KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > print > BufferPrinter1_4


1 /*
2  * BufferPrinter1_4.java - Main class that controls printing
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2001 Slava Pestov
7  * Portions copyright (C) 2002 Thomas Dilts
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */

23
24 package org.gjt.sp.jedit.print;
25
26 //{{{ Imports
27
import javax.print.attribute.*;
28 import javax.print.attribute.standard.*;
29 import java.awt.print.*;
30 import java.awt.*;
31 import java.io.*;
32 import org.gjt.sp.jedit.*;
33 import org.gjt.sp.util.Log;
34 //}}}
35

36 public class BufferPrinter1_4
37 {
38     //{{{ getPrintJob() method
39
private static PrinterJob getPrintJob(String JavaDoc jobName)
40     {
41         job = PrinterJob.getPrinterJob();
42
43         format = new HashPrintRequestAttributeSet();
44
45         String JavaDoc settings = jEdit.getSettingsDirectory();
46         if(settings != null)
47         {
48             String JavaDoc printSpecPath = MiscUtilities.constructPath(
49                 settings, "printspec");
50             File filePrintSpec = new File(printSpecPath);
51
52             if (filePrintSpec.exists())
53             {
54                 try
55                 {
56                     FileInputStream fileIn = new FileInputStream(filePrintSpec);
57                     ObjectInputStream obIn = new ObjectInputStream(fileIn);
58                     format = (HashPrintRequestAttributeSet)obIn.readObject();
59                 }
60                 catch(Exception JavaDoc e)
61                 {
62                     Log.log(Log.ERROR,BufferPrinter1_4.class,e);
63                 }
64                 //for backwards compatibility, the color variable is stored also as a property
65
if(jEdit.getBooleanProperty("print.color"))
66                     format.add(Chromaticity.COLOR);
67                 else
68                     format.add(Chromaticity.MONOCHROME);
69
70                 //no need to always keep the same job name for every printout.
71
format.add(new JobName(jobName, null));
72             }
73         }
74
75         return job;
76     } //}}}
77

78     //{{{ pageSetup() method
79
public static void pageSetup(View view)
80     {
81         PrinterJob prnJob = getPrintJob("PageSetupOnly");
82         if(prnJob.pageDialog(format)!=null)
83             savePrintSpec();
84     } //}}}
85

86     //{{{ print() method
87
public static void print(final View view, final Buffer buffer, boolean selection)
88     {
89         job = getPrintJob(buffer.getPath());
90
91         boolean header = jEdit.getBooleanProperty("print.header");
92         boolean footer = jEdit.getBooleanProperty("print.footer");
93         boolean lineNumbers = jEdit.getBooleanProperty("print.lineNumbers");
94         boolean color = jEdit.getBooleanProperty("print.color");
95         Font font = jEdit.getFontProperty("print.font");
96
97         BufferPrintable printable = new BufferPrintable(job,format,view,
98             buffer,font,header,footer,lineNumbers,color);
99         job.setPrintable(printable);
100
101         if(!job.printDialog(format))
102             return;
103
104         savePrintSpec();
105
106         printable.print();
107     } //}}}
108

109     //{{{ getPageFormat() method
110
public static PageFormat getPageFormat()
111     {
112         //convert from PrintRequestAttributeSet to the pageFormat
113
PrinterJob prnJob=getPrintJob(" ");
114         PageFormat pf=prnJob.defaultPage();
115         Paper pap=pf.getPaper();
116
117         MediaSizeName media=(MediaSizeName)format.get(
118                                     Media.class);
119         MediaSize ms=MediaSize.getMediaSizeForName(media);
120
121         MediaPrintableArea mediaarea=(MediaPrintableArea)format.get(
122                                              MediaPrintableArea.class);
123         if(mediaarea!=null)
124             pap.setImageableArea((mediaarea.getX(MediaPrintableArea.INCH)*72),
125                                  (mediaarea.getY(MediaPrintableArea.INCH)*72),
126                                  (mediaarea.getWidth(MediaPrintableArea.INCH)*72),
127                                  (mediaarea.getHeight(MediaPrintableArea.INCH)*72));
128         if(ms!=null)
129             pap.setSize((ms.getX(MediaSize.INCH)*72),
130                         (ms.getY(MediaSize.INCH)*72));
131         pf.setPaper(pap);
132
133         OrientationRequested orientation=(OrientationRequested)format.get(
134                                                  OrientationRequested.class);
135         if(orientation!=null)
136         {
137             if(orientation.getValue()==OrientationRequested.LANDSCAPE.getValue())
138             {
139                 pf.setOrientation(PageFormat.LANDSCAPE);
140             }
141             else if(orientation.getValue()==OrientationRequested.REVERSE_LANDSCAPE.getValue())
142             {
143                 pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
144             }
145             else if(orientation.getValue()==OrientationRequested.PORTRAIT.getValue())
146             {
147                 pf.setOrientation(PageFormat.PORTRAIT);
148             }
149             else if(orientation.getValue()==OrientationRequested.REVERSE_PORTRAIT.getValue())
150             {
151                 //doesnt exist??
152
//pf.setOrientation(PageFormat.REVERSE_PORTRAIT);
153
//then just do the next best thing
154
pf.setOrientation(PageFormat.PORTRAIT);
155             }
156         }
157         return pf;
158     } //}}}
159

160     //{{{ savePrintSpec() method
161
private static void savePrintSpec()
162     {
163         String JavaDoc settings = jEdit.getSettingsDirectory();
164         if(settings == null)
165             return;
166
167         String JavaDoc printSpecPath = MiscUtilities.constructPath(
168             settings, "printspec");
169         File filePrintSpec = new File(printSpecPath);
170
171         try
172         {
173             FileOutputStream fileOut=new FileOutputStream(filePrintSpec);
174             ObjectOutputStream obOut=new ObjectOutputStream(fileOut);
175             obOut.writeObject(format);
176             //for backwards compatibility, the color variable is stored also as a property
177
Chromaticity cc=(Chromaticity)format.get(Chromaticity.class);
178             if (cc!=null)
179                 jEdit.setBooleanProperty("print.color",
180                     cc.getValue()==Chromaticity.COLOR.getValue());
181         }
182         catch(Exception JavaDoc e)
183         {
184             e.printStackTrace();
185         }
186     }
187     //}}}
188

189     //{{{ Private members
190
private static PrintRequestAttributeSet format;
191     private static PrinterJob job;
192     //}}}
193
}
194
195
Popular Tags