use of com.servoy.j2db.printing.FormPreviewPanel in project servoy-client by Servoy.
the class WebForm method print.
// public void print(boolean showDialogs, boolean printCurrentRecordOnly, boolean showPrinterSelectDialog, int zoomFactor, PrinterJob printerJob)
// {
// print(showDialogs, printCurrentRecordOnly, showPrinterSelectDialog, printerJob);
// }
/**
* @see com.servoy.j2db.IFormUIInternal#print(boolean, boolean, boolean, java.awt.print.PrinterJob)
*/
public void print(boolean showDialogs, boolean printCurrentRecordOnly, boolean showPrinterSelectDialog, PrinterJob printerJob) {
IFoundSetInternal fs = formController.getFoundSet();
try {
if (printCurrentRecordOnly) {
fs = fs.copyCurrentRecordFoundSet();
}
} catch (ServoyException e1) {
Debug.error(e1);
}
IApplication application = formController.getApplication();
ByteArrayOutputStream baos = null;
MainPage page = null;
if (printerJob == null) {
page = (MainPage) findPage();
if (page == null) {
IMainContainer tmp = ((FormManager) application.getFormManager()).getCurrentContainer();
if (tmp instanceof MainPage)
page = (MainPage) tmp;
}
// if "page" is still null then there is no wicket front-end for this client - so printing is not intended to reach the client; print on the server instead
// (can happen for batch processors for example)
}
if (page != null) {
baos = new ByteArrayOutputStream();
StreamPrintServiceFactory[] factories = null;
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(((PluginManager) application.getPluginManager()).getClassLoader());
// $NON-NLS-1$
factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, "application/pdf");
if (factories == null || factories.length == 0) {
// $NON-NLS-1$
Debug.error("No suitable pdf printer found");
return;
}
} finally {
Thread.currentThread().setContextClassLoader(savedClassLoader);
}
try {
FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
// AWT stuff happens here, so execute it in the AWT Event Thread - else exceptions can occur
// for example in JEditorPane while getting the preferred size & stuff
processFppInAWTEventQueue(fpp, application);
StreamPrintService sps = factories[0].getPrintService(baos);
Doc doc = new SimpleDoc(fpp.getPageable(), flavor, null);
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
sps.createPrintJob().print(doc, pras);
fpp.destroy();
} catch (Exception ex) {
// $NON-NLS-1$
application.reportError(application.getI18NMessage("servoy.formPanel.error.printDocument"), ex);
}
// $NON-NLS-1$
String contentType = "application/pdf";
// BTW, "application/octet-stream" works for all browsers, but it is not that accurate
if (// if it's not batch processor/jsp, because if it is, then getClientInfo() gives NullPointerException
application.getApplicationType() != IApplication.HEADLESS_CLIENT) {
ClientInfo info = Session.get().getClientInfo();
if (info instanceof WebClientInfo) {
String userAgent = ((WebClientInfo) info).getProperties().getNavigatorUserAgent();
if (// $NON-NLS-1$
userAgent != null && userAgent.toLowerCase().contains("safari")) {
// $NON-NLS-1$
contentType = "application/octet-stream";
}
}
}
// $NON-NLS-1$ //$NON-NLS-2$
String url = page.serveResource(formController.getName() + ".pdf", baos.toByteArray(), contentType, "attachment");
// $NON-NLS-1$
page.setShowURLCMD(url, "_self", null, 0, false);
} else {
try {
FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
// AWT stuff happens here, so execute it in the AWT Event Thread - else exceptions can occur
// for example in JEditorPane while getting the preferred size & stuff
processFppInAWTEventQueue(fpp, application);
PrintPreview.startPrinting(application, fpp.getPageable(), printerJob, formController.getPreferredPrinterName(), false, true);
fpp.destroy();
} catch (Exception ex) {
// $NON-NLS-1$
application.reportError(application.getI18NMessage("servoy.formPanel.error.printDocument"), ex);
}
}
}
use of com.servoy.j2db.printing.FormPreviewPanel in project servoy-client by Servoy.
the class SwingForm method print.
/**
* @see com.servoy.j2db.IFormUIInternal#print(boolean, boolean, boolean, java.awt.print.PrinterJob)
*/
public void print(boolean showDialogs, boolean printCurrentRecordOnly, boolean showPrinterSelectDialog, PrinterJob printerJob) {
IFoundSetInternal fs = formController.getFoundSet();
if (!printCurrentRecordOnly) {
if (showDialogs) {
int option = willingToPrint(fs);
if (option == 2) {
printCurrentRecordOnly = true;
} else if (option == 1) {
// cancel
return;
}
}
}
IApplication application = formController.getApplication();
try {
// $NON-NLS-1$
application.blockGUI(Messages.getString("servoy.formPanel.status.printProgress"));
if (printCurrentRecordOnly) {
fs = fs.copyCurrentRecordFoundSet();
}
FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
fpp.process();
PrintPreview.startPrinting(application, fpp.getPageable(), printerJob, formController.getPreferredPrinterName(), showPrinterSelectDialog, false);
fpp.destroy();
} catch (Exception ex) {
// $NON-NLS-1$
application.reportError(Messages.getString("servoy.formPanel.error.printDocument"), ex);
} finally {
application.releaseGUI();
}
}
use of com.servoy.j2db.printing.FormPreviewPanel in project servoy-client by Servoy.
the class SwingForm method printXML.
/**
* @see com.servoy.j2db.IFormUIInternal#printXML(boolean)
*/
public String printXML(boolean printCurrentRecordOnly) {
IApplication application = formController.getApplication();
IFoundSetInternal fs = formController.getFoundSet();
try {
// $NON-NLS-1$
application.blockGUI(Messages.getString("servoy.formPanel.status.xmlPrinting"));
if (printCurrentRecordOnly) {
fs = fs.copyCurrentRecordFoundSet();
}
FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
fpp.process();
StringWriter w = new StringWriter();
((PageList) fpp.getPageable()).toXML(w);
fpp.destroy();
return w.toString();
} catch (Throwable ex) {
// $NON-NLS-1$
application.reportError(Messages.getString("servoy.formPanel.error.printDocument"), ex);
} finally {
application.releaseGUI();
}
return null;
}
use of com.servoy.j2db.printing.FormPreviewPanel in project servoy-client by Servoy.
the class WebForm method printXML.
/**
* @see com.servoy.j2db.IFormUIInternal#printXML(boolean)
*/
public String printXML(boolean printCurrentRecordOnly) {
IApplication application = formController.getApplication();
IFoundSetInternal fs = formController.getFoundSet();
try {
if (printCurrentRecordOnly) {
fs = fs.copyCurrentRecordFoundSet();
}
FormPreviewPanel fpp = new FormPreviewPanel(application, formController, fs);
// AWT stuff happens here, so execute it in the AWT Event Thread - else exceptions can occur
// for example in JEditorPane while getting the preferred size & stuff
processFppInAWTEventQueue(fpp, application);
StringWriter w = new StringWriter();
((PageList) fpp.getPageable()).toXML(w);
fpp.destroy();
return w.toString();
} catch (Throwable ex) {
// $NON-NLS-1$
application.reportError(application.getI18NMessage("servoy.formPanel.error.printDocument"), ex);
}
return null;
}
Aggregations