1 23 24 package org.gjt.sp.jedit.print; 25 26 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 36 public class BufferPrinter1_4 37 { 38 private static PrinterJob getPrintJob(String jobName) 40 { 41 job = PrinterJob.getPrinterJob(); 42 43 format = new HashPrintRequestAttributeSet(); 44 45 String settings = jEdit.getSettingsDirectory(); 46 if(settings != null) 47 { 48 String 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 e) 61 { 62 Log.log(Log.ERROR,BufferPrinter1_4.class,e); 63 } 64 if(jEdit.getBooleanProperty("print.color")) 66 format.add(Chromaticity.COLOR); 67 else 68 format.add(Chromaticity.MONOCHROME); 69 70 format.add(new JobName(jobName, null)); 72 } 73 } 74 75 return job; 76 } 78 public static void pageSetup(View view) 80 { 81 PrinterJob prnJob = getPrintJob("PageSetupOnly"); 82 if(prnJob.pageDialog(format)!=null) 83 savePrintSpec(); 84 } 86 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 } 109 public static PageFormat getPageFormat() 111 { 112 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 pf.setOrientation(PageFormat.PORTRAIT); 155 } 156 } 157 return pf; 158 } 160 private static void savePrintSpec() 162 { 163 String settings = jEdit.getSettingsDirectory(); 164 if(settings == null) 165 return; 166 167 String 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 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 e) 183 { 184 e.printStackTrace(); 185 } 186 } 187 189 private static PrintRequestAttributeSet format; 191 private static PrinterJob job; 192 } 194 195 | Popular Tags |