use of javax.print.PrintService in project jgnash by ccavanaugh.
the class ReportPrintFactory method getDefaultPage.
/**
* Returns the default paper size for a report
*
* @return page format
*/
private static PageFormat getDefaultPage() {
/* A4 is the assumed default */
MediaSize defaultMediaSize = MediaSize.ISO.A4;
/* US and Canada use letter size as the default */
Locale[] letterLocales = new Locale[] { Locale.US, Locale.CANADA, Locale.CANADA_FRENCH };
if (Arrays.asList(letterLocales).contains(Locale.getDefault())) {
defaultMediaSize = MediaSize.NA.LETTER;
}
/* Create the default paper size with a default margin */
Paper paper = new Paper();
int width = (int) (defaultMediaSize.getX(MediaSize.INCH) / (1f / 72f));
int height = (int) (defaultMediaSize.getY(MediaSize.INCH) / (1f / 72f));
paper.setSize(width, height);
paper.setImageableArea(DEFAULT_MARGIN, DEFAULT_MARGIN, width - (2 * DEFAULT_MARGIN), height - (2 * DEFAULT_MARGIN));
PageFormat format = new PageFormat();
format.setPaper(paper);
/* if a default printer is found, validate the page */
PrintService[] services = PrinterJob.lookupPrintServices();
if (services.length != 0) {
// no printers found on the system.
format = PrinterJob.getPrinterJob().validatePage(format);
}
return format;
}
use of javax.print.PrintService in project camel by apache.
the class PrinterPrintTest method moreThanOneLprEndpoint.
@Test
public void moreThanOneLprEndpoint() throws Exception {
if (isAwtHeadless()) {
return;
}
int numberOfPrintservicesBefore = PrintServiceLookup.lookupPrintServices(null, null).length;
// setup javax.print
PrintService ps1 = mock(PrintService.class);
when(ps1.getName()).thenReturn("printer1");
when(ps1.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE);
PrintService ps2 = mock(PrintService.class);
when(ps2.getName()).thenReturn("printer2");
boolean res1 = PrintServiceLookup.registerService(ps1);
assertTrue("PrintService #1 should be registered.", res1);
boolean res2 = PrintServiceLookup.registerService(ps2);
assertTrue("PrintService #2 should be registered.", res2);
PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, null);
assertEquals("lookup should report two PrintServices.", numberOfPrintservicesBefore + 2, pss.length);
DocPrintJob job1 = mock(DocPrintJob.class);
when(ps1.createPrintJob()).thenReturn(job1);
context.addRoutes(new RouteBuilder() {
public void configure() {
from("direct:start1").to("lpr://localhost/printer1?sendToPrinter=true");
from("direct:start2").to("lpr://localhost/printer2?sendToPrinter=false");
}
});
context.start();
// Are there two different PrintConfigurations?
Map<String, Endpoint> epm = context().getEndpointMap();
assertEquals("Four endpoints", 4, epm.size());
Endpoint lp1 = null;
Endpoint lp2 = null;
for (Map.Entry<String, Endpoint> ep : epm.entrySet()) {
if (ep.getKey().contains("printer1")) {
lp1 = ep.getValue();
}
if (ep.getKey().contains("printer2")) {
lp2 = ep.getValue();
}
}
assertNotNull(lp1);
assertNotNull(lp2);
assertEquals("printer1", ((PrinterEndpoint) lp1).getConfig().getPrintername());
assertEquals("printer2", ((PrinterEndpoint) lp2).getConfig().getPrintername());
template.sendBody("direct:start1", "Hello Printer 1");
context.stop();
verify(job1, times(1)).print(any(Doc.class), any(PrintRequestAttributeSet.class));
}
use of javax.print.PrintService in project camel by apache.
the class PrinterProducer method assignPrintService.
private PrintService assignPrintService() throws PrintException {
PrintService printService;
if ((config.getHostname().equalsIgnoreCase("localhost")) && (config.getPrintername().equalsIgnoreCase("default"))) {
printService = PrintServiceLookup.lookupDefaultPrintService();
} else {
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
String name;
if (config.getHostname().equalsIgnoreCase("localhost")) {
// no hostname for localhost printers
name = config.getPrintername();
} else {
name = config.getHostname() + "/" + config.getPrintername();
if (config.getPrinterPrefix() != null) {
name = config.getPrinterPrefix() + name;
}
}
log.debug("Using printer name: {}", name);
setPrinter(name);
int position = findPrinter(services, printer);
if (position < 0) {
throw new PrintException("No printer found with name: " + printer + ". Please verify that the host and printer are registered and reachable from this machine.");
}
printService = services[position];
}
return printService;
}
use of javax.print.PrintService in project camel by apache.
the class PrinterPrintTest method printerNameTest.
@Test
public void printerNameTest() throws Exception {
if (isAwtHeadless()) {
return;
}
// setup javax.print
PrintService ps1 = mock(PrintService.class);
when(ps1.getName()).thenReturn("MyPrinter\\\\remote\\printer1");
when(ps1.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE);
boolean res1 = PrintServiceLookup.registerService(ps1);
assertTrue("The Remote PrintService #1 should be registered.", res1);
DocPrintJob job1 = mock(DocPrintJob.class);
when(ps1.createPrintJob()).thenReturn(job1);
context.addRoutes(new RouteBuilder() {
public void configure() {
from("direct:start1").to("lpr://remote/printer1?sendToPrinter=true");
}
});
context.start();
template.sendBody("direct:start1", "Hello Printer 1");
context.stop();
verify(job1, times(1)).print(any(Doc.class), any(PrintRequestAttributeSet.class));
}
use of javax.print.PrintService in project jdk8u_jdk by JetBrains.
the class PrinterJob method getPageFormat.
/**
* Calculates a <code>PageFormat</code> with values consistent with those
* supported by the current <code>PrintService</code> for this job
* (ie the value returned by <code>getPrintService()</code>) and media,
* printable area and orientation contained in <code>attributes</code>.
* <p>
* Calling this method does not update the job.
* It is useful for clients that have a set of attributes obtained from
* <code>printDialog(PrintRequestAttributeSet attributes)</code>
* and need a PageFormat to print a Pageable object.
* @param attributes a set of printing attributes, for example obtained
* from calling printDialog. If <code>attributes</code> is null a default
* PageFormat is returned.
* @return a <code>PageFormat</code> whose settings conform with
* those of the current service and the specified attributes.
* @since 1.6
*/
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {
PrintService service = getPrintService();
PageFormat pf = defaultPage();
if (service == null || attributes == null) {
return pf;
}
Media media = (Media) attributes.get(Media.class);
MediaPrintableArea mpa = (MediaPrintableArea) attributes.get(MediaPrintableArea.class);
OrientationRequested orientReq = (OrientationRequested) attributes.get(OrientationRequested.class);
if (media == null && mpa == null && orientReq == null) {
return pf;
}
Paper paper = pf.getPaper();
/* If there's a media but no media printable area, we can try
* to retrieve the default value for mpa and use that.
*/
if (mpa == null && media != null && service.isAttributeCategorySupported(MediaPrintableArea.class)) {
Object mpaVals = service.getSupportedAttributeValues(MediaPrintableArea.class, null, attributes);
if (mpaVals instanceof MediaPrintableArea[] && ((MediaPrintableArea[]) mpaVals).length > 0) {
mpa = ((MediaPrintableArea[]) mpaVals)[0];
}
}
if (media != null && service.isAttributeValueSupported(media, null, attributes)) {
if (media instanceof MediaSizeName) {
MediaSizeName msn = (MediaSizeName) media;
MediaSize msz = MediaSize.getMediaSizeForName(msn);
if (msz != null) {
double inch = 72.0;
double paperWid = msz.getX(MediaSize.INCH) * inch;
double paperHgt = msz.getY(MediaSize.INCH) * inch;
paper.setSize(paperWid, paperHgt);
if (mpa == null) {
paper.setImageableArea(inch, inch, paperWid - 2 * inch, paperHgt - 2 * inch);
}
}
}
}
if (mpa != null && service.isAttributeValueSupported(mpa, null, attributes)) {
float[] printableArea = mpa.getPrintableArea(MediaPrintableArea.INCH);
for (int i = 0; i < printableArea.length; i++) {
printableArea[i] = printableArea[i] * 72.0f;
}
paper.setImageableArea(printableArea[0], printableArea[1], printableArea[2], printableArea[3]);
}
if (orientReq != null && service.isAttributeValueSupported(orientReq, null, attributes)) {
int orient;
if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
orient = PageFormat.REVERSE_LANDSCAPE;
} else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
orient = PageFormat.LANDSCAPE;
} else {
orient = PageFormat.PORTRAIT;
}
pf.setOrientation(orient);
}
pf.setPaper(paper);
pf = validatePage(pf);
return pf;
}
Aggregations