PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/print/BufferPrinter1_4.java

#
Java | 194 lines | 131 code | 24 blank | 39 comment | 19 complexity | 20ab52f826ef01ef2528da3d02d79e9c MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit.print;
  24. //{{{ Imports
  25. import javax.print.attribute.*;
  26. import javax.print.attribute.standard.*;
  27. import java.awt.print.*;
  28. import java.awt.*;
  29. import java.io.*;
  30. import org.gjt.sp.jedit.*;
  31. import org.gjt.sp.util.Log;
  32. //}}}
  33. public class BufferPrinter1_4
  34. {
  35. //{{{ getPrintJob() method
  36. private static PrinterJob getPrintJob(String jobName)
  37. {
  38. job = PrinterJob.getPrinterJob();
  39. format = new HashPrintRequestAttributeSet();
  40. String settings = jEdit.getSettingsDirectory();
  41. if(settings != null)
  42. {
  43. String printSpecPath = MiscUtilities.constructPath(
  44. settings, "printspec");
  45. File filePrintSpec = new File(printSpecPath);
  46. if (filePrintSpec.exists())
  47. {
  48. try
  49. {
  50. FileInputStream fileIn = new FileInputStream(filePrintSpec);
  51. ObjectInputStream obIn = new ObjectInputStream(fileIn);
  52. format = (HashPrintRequestAttributeSet)obIn.readObject();
  53. }
  54. catch(Exception e)
  55. {
  56. Log.log(Log.ERROR,BufferPrinter1_4.class,e);
  57. }
  58. //for backwards compatibility, the color variable is stored also as a property
  59. if(jEdit.getBooleanProperty("print.color"))
  60. format.add(Chromaticity.COLOR);
  61. else
  62. format.add(Chromaticity.MONOCHROME);
  63. //no need to always keep the same job name for every printout.
  64. format.add(new JobName(jobName, null));
  65. }
  66. }
  67. return job;
  68. } //}}}
  69. //{{{ pageSetup() method
  70. public static void pageSetup(View view)
  71. {
  72. PrinterJob prnJob = getPrintJob("PageSetupOnly");
  73. if(prnJob.pageDialog(format)!=null)
  74. savePrintSpec();
  75. } //}}}
  76. //{{{ print() method
  77. public static void print(final View view, final Buffer buffer, boolean selection)
  78. {
  79. job = getPrintJob(buffer.getPath());
  80. boolean header = jEdit.getBooleanProperty("print.header");
  81. boolean footer = jEdit.getBooleanProperty("print.footer");
  82. boolean lineNumbers = jEdit.getBooleanProperty("print.lineNumbers");
  83. boolean color = jEdit.getBooleanProperty("print.color");
  84. Font font = jEdit.getFontProperty("print.font");
  85. BufferPrintable printable = new BufferPrintable(job,format,view,
  86. buffer,font,header,footer,lineNumbers,color);
  87. job.setPrintable(printable);
  88. if(!job.printDialog(format))
  89. return;
  90. savePrintSpec();
  91. printable.print();
  92. } //}}}
  93. //{{{ getPageFormat() method
  94. public static PageFormat getPageFormat()
  95. {
  96. //convert from PrintRequestAttributeSet to the pageFormat
  97. PrinterJob prnJob=getPrintJob(" ");
  98. PageFormat pf=prnJob.defaultPage();
  99. Paper pap=pf.getPaper();
  100. MediaSizeName media=(MediaSizeName)format.get(
  101. Media.class);
  102. MediaSize ms=MediaSize.getMediaSizeForName(media);
  103. MediaPrintableArea mediaarea=(MediaPrintableArea)format.get(
  104. MediaPrintableArea.class);
  105. if(mediaarea!=null)
  106. pap.setImageableArea((double)(mediaarea.getX(MediaPrintableArea.INCH)*72),
  107. (double)(mediaarea.getY(MediaPrintableArea.INCH)*72),
  108. (double)(mediaarea.getWidth(MediaPrintableArea.INCH)*72),
  109. (double)(mediaarea.getHeight(MediaPrintableArea.INCH)*72));
  110. if(ms!=null)
  111. pap.setSize((double)(ms.getX(MediaSize.INCH)*72),
  112. (double)(ms.getY(MediaSize.INCH)*72));
  113. pf.setPaper(pap);
  114. OrientationRequested orientation=(OrientationRequested)format.get(
  115. OrientationRequested.class);
  116. if(orientation!=null)
  117. {
  118. if(orientation.getValue()==OrientationRequested.LANDSCAPE.getValue())
  119. {
  120. pf.setOrientation(PageFormat.LANDSCAPE);
  121. }
  122. else if(orientation.getValue()==OrientationRequested.REVERSE_LANDSCAPE.getValue())
  123. {
  124. pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
  125. }
  126. else if(orientation.getValue()==OrientationRequested.PORTRAIT.getValue())
  127. {
  128. pf.setOrientation(PageFormat.PORTRAIT);
  129. }
  130. else if(orientation.getValue()==OrientationRequested.REVERSE_PORTRAIT.getValue())
  131. {
  132. //doesnt exist??
  133. //pf.setOrientation(PageFormat.REVERSE_PORTRAIT);
  134. //then just do the next best thing
  135. pf.setOrientation(PageFormat.PORTRAIT);
  136. }
  137. }
  138. return pf;
  139. } //}}}
  140. //{{{ savePrintSpec() method
  141. private static void savePrintSpec()
  142. {
  143. String settings = jEdit.getSettingsDirectory();
  144. if(settings == null)
  145. return;
  146. String printSpecPath = MiscUtilities.constructPath(
  147. settings, "printspec");
  148. File filePrintSpec = new File(printSpecPath);
  149. try
  150. {
  151. FileOutputStream fileOut=new FileOutputStream(filePrintSpec);
  152. ObjectOutputStream obOut=new ObjectOutputStream(fileOut);
  153. obOut.writeObject(format);
  154. //for backwards compatibility, the color variable is stored also as a property
  155. Chromaticity cc=(Chromaticity)format.get(Chromaticity.class);
  156. if (cc!=null)
  157. jEdit.setBooleanProperty("print.color",
  158. cc.getValue()==Chromaticity.COLOR.getValue());
  159. }
  160. catch(Exception e)
  161. {
  162. e.printStackTrace();
  163. }
  164. }
  165. //}}}
  166. //{{{ Private members
  167. private static PrintRequestAttributeSet format;
  168. private static PrinterJob job;
  169. //}}}
  170. }