1 16 17 package org.pentaho.plugin.print; 18 19 import java.awt.print.PrinterException ; 20 import java.awt.print.PrinterJob ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.ArrayList ; 27 28 import javax.print.PrintService ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.fop.apps.Driver; 33 import org.apache.fop.render.awt.AWTRenderer; 34 import org.pentaho.core.repository.ISolutionRepository; 35 import org.pentaho.core.solution.IActionResource; 36 import org.pentaho.core.system.PentahoSystem; 37 import org.pentaho.messages.Messages; 38 import org.pentaho.plugin.ComponentBase; 39 import org.pentaho.plugin.core.StandardSettings; 40 import org.xml.sax.InputSource ; 41 42 47 public class PrintComponent extends ComponentBase { 48 51 private static final long serialVersionUID = 7377566797214172734L; 52 53 private static final String LAST_PRINTER = "default-printer"; 55 private static final String DEFAULT_PRINTER = "PENTAHO_DEFAULT_PRINTER"; 57 private static final String COPIES = "copies"; 59 private static final String PRINT_FILE = "printFile"; 61 private static final String REPORT_OUTPUT = "report-output"; 63 public Log getLogger() { 64 return LogFactory.getLog(PrintComponent.class); 65 } 66 67 public boolean init() { 68 return true; 70 } 71 72 protected boolean validateSystemSettings() { 73 return true; 75 } 76 77 protected boolean validateAction() { 78 79 if (!isDefinedInput(PRINT_FILE) && !isDefinedResource(PRINT_FILE) && !isDefinedInput("report-output") && !isDefinedOutput(StandardSettings.PRINTER_NAME)) { error(Messages.getErrorString("PrintComponent.ERROR_0001_NO_PRINT_FILE_DEFINED") + getActionName()); return false; 82 } 83 return true; 84 } 85 86 protected boolean executeAction() { 87 String printFileName = null; 88 IActionResource printFileResource = null; 89 90 if (isDefinedInput(PRINT_FILE)) { 91 printFileName = getInputStringValue(PRINT_FILE); 92 } else if (isDefinedResource(PRINT_FILE)) { 93 printFileResource = getResource(PRINT_FILE); 94 } 95 96 InputStream inStream = null; 97 String printerName = DEFAULT_PRINTER; 98 String lastPrinter = null; 99 100 if (isDefinedInput(StandardSettings.PRINTER_NAME)) { 102 printerName = getInputStringValue(StandardSettings.PRINTER_NAME); 103 } 104 105 if (isDefinedInput(LAST_PRINTER)) { 106 lastPrinter = getInputStringValue(LAST_PRINTER); 107 } else { 108 lastPrinter = null; 109 } 110 111 if (isDefinedOutput(StandardSettings.PRINTER_NAME) && !printerName.equals("")) { setOutputValue(StandardSettings.PRINTER_NAME, printerName); 114 if (isDefinedOutput(LAST_PRINTER)) { 116 setOutputValue(LAST_PRINTER, printerName); 117 } 118 return true; 119 } 120 121 PrintService printer = getPrinterInternal(printerName, lastPrinter); 122 if (printer == null) { 123 if (!feedbackAllowed()) { 124 error(Messages.getErrorString("PrintComponent.ERROR_0002_NO_SUITABLE_PRINTER")); return false; 126 } 127 return true; 129 } 130 if (isDefinedOutput(LAST_PRINTER)) { 132 setOutputValue(LAST_PRINTER, printerName); 133 } 134 135 int copies = 1; 137 if (isDefinedInput(COPIES)) { 138 copies = Integer.valueOf(getInputStringValue(COPIES)).intValue(); 139 } 140 141 if (printFileName != null) { 143 ISolutionRepository repository = PentahoSystem.getSolutionRepository(getSession()); 144 inStream = repository.getResourceInputStream(printFileName); 145 146 if (inStream == null) { return false; 150 } 151 } else if (printFileResource != null) { 152 inStream = getResourceInputStream(printFileResource); 153 } else if (isDefinedInput(REPORT_OUTPUT)) { 154 inStream = getInputStream(REPORT_OUTPUT); 155 } else { return false; 157 } 158 try { 159 InputSource source = new InputSource (inStream); 161 try { 162 Driver driver = new Driver(source, null); 163 PrinterJob pj = PrinterJob.getPrinterJob(); 164 if (!(DEFAULT_PRINTER.equals(printerName))) { 167 pj.setPrintService(getPrinterInternal(printerName, lastPrinter)); 168 } 169 PrintRenderer renderer = new PrintRenderer(pj, copies); 170 driver.setRenderer(renderer); 171 driver.run(); 172 } catch (Exception ex) { 173 return false; 174 } 175 } finally { 176 try { 177 inStream.close(); 178 } catch (IOException ex) { 179 ex.printStackTrace(); 181 } 182 } 183 return true; 184 } 185 186 public void done() { 187 189 } 190 191 199 public PrintService getPrinterInternal(String printerName, String lastPrinterName) { 200 203 PrintService [] services = PrinterJob.lookupPrintServices(); 204 for (int i = 0; i < services.length; i++) { 207 if (services[i].getName().equals(printerName)) { 208 return services[i]; 209 } 210 } 211 if (feedbackAllowed()) { 212 ArrayList values = new ArrayList (); 214 for (int i = 0; i < services.length; i++) { 215 String value = services[i].getName(); 216 values.add(value); 217 } 218 createFeedbackParameter(StandardSettings.PRINTER_NAME, Messages.getString("PrintComponent.USER_PRINTER_NAME"), "", lastPrinterName, values, null, "select"); promptNeeded(); 220 return null; 221 } 222 return services[0]; 223 } 224 225 233 public static PrintService getPrinter(String printerName) { 234 237 PrintService [] services = PrinterJob.lookupPrintServices(); 238 for (int i = 0; i < services.length; i++) { 241 if (services[i].getName().equals(printerName)) { 242 return services[i]; 243 } 244 } 245 return services[0]; 246 } 247 248 public static PrintService getDefaultPrinter() { 249 252 PrintService [] services = PrinterJob.lookupPrintServices(); 253 if (services == null || services.length == 0) { 254 return null; 255 } 256 return services[0]; 257 } 258 259 265 class PrintRenderer extends AWTRenderer { 266 267 private static final int EVEN_AND_ALL = 0; 268 269 private static final int EVEN = 1; 270 271 private static final int ODD = 2; 272 273 private int startNumber; 274 275 private int endNumber; 276 277 private int mode = EVEN_AND_ALL; 278 279 private int copies = 1; 280 281 private PrinterJob printerJob; 282 283 PrintRenderer(PrinterJob printerJob, int copies) { 284 super(null); 285 286 this.printerJob = printerJob; 287 this.copies = copies; 288 startNumber = 0; 289 endNumber = -1; 290 291 printerJob.setPageable(this); 292 printerJob.setCopies(this.copies); 293 mode = EVEN_AND_ALL; 294 String str = null; 295 if (str != null) { 296 try { 297 mode = Boolean.valueOf(str).booleanValue() ? EVEN : ODD; 298 } catch (Exception e) { 299 } 300 301 } 302 303 } 304 305 public void stopRenderer(OutputStream outputStream) throws IOException { 306 super.stopRenderer(outputStream); 307 308 if (endNumber == -1) 309 endNumber = getPageCount(); 310 311 ArrayList numbers = getInvalidPageNumbers(); 312 for (int i = numbers.size() - 1; i > -1; i--) 313 removePage(Integer.parseInt((String ) numbers.get(i))); 314 315 try { 316 printerJob.print(); 317 } catch (PrinterException e) { 318 e.printStackTrace(); 319 throw new IOException (Messages.getString("PrintComponent.ERROR_0003_UNABLE_TO_PRINT", e.getClass().getName(), e.getMessage())); } 321 } 322 323 329 private ArrayList getInvalidPageNumbers() { 330 ArrayList vec = new ArrayList (); 331 int max = getPageCount(); 332 boolean isValid; 333 for (int i = 0; i < max; i++) { 334 isValid = true; 335 if (i < startNumber || i > endNumber) { 336 isValid = false; 337 } else if (mode != EVEN_AND_ALL) { 338 if (mode == EVEN && ((i + 1) % 2 != 0)) 339 isValid = false; 340 else if (mode == ODD && ((i + 1) % 2 != 1)) 341 isValid = false; 342 } 343 if (!isValid) 344 vec.add(i + ""); } 346 return vec; 347 } 348 } } 350 | Popular Tags |