/** MediaSizeName / dmPaper */ private final int[] getWin32MediaAttrib() { int wid_ht[] = {0, 0}; if (attributes != null) { Media media = (Media)attributes.get(Media.class); if (media instanceof MediaSizeName) { MediaSizeName msn = (MediaSizeName)media; MediaSize ms = MediaSize.getMediaSizeForName(msn); if (ms != null) { wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0); wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0); } } } return wid_ht; }
private MediaPrintableArea[] getAllPrintableAreas() { if (mpas == null) { Media[] media = (Media[])getSupportedAttributeValues(Media.class, null, null); mpas = new MediaPrintableArea[media.length]; for (int i=0; i< mpas.length; i++) { if (media[i] instanceof MediaSizeName) { MediaSizeName msn = (MediaSizeName)media[i]; MediaSize mediaSize = MediaSize.getMediaSizeForName(msn); if (mediaSize == null) { mpas[i] = (MediaPrintableArea) getDefaultAttributeValue(MediaPrintableArea.class); } else { mpas[i] = new MediaPrintableArea(0.25f, 0.25f, mediaSize.getX(MediaSize.INCH)-0.5f, mediaSize.getY(MediaSize.INCH)-0.5f, MediaSize.INCH); } } } } MediaPrintableArea[] mpasCopy = new MediaPrintableArea[mpas.length]; System.arraycopy(mpas, 0, mpasCopy, 0, mpas.length); return mpasCopy; }
public PrintServiceStub(String name) { _name = name; _flavors = new HashSet<DocFlavor>(); _flavors.add(DocFlavor.SERVICE_FORMATTED.PAGEABLE); _flavors.add(DocFlavor.SERVICE_FORMATTED.PRINTABLE); _attributes = new HashMap<>(); _attributes.put(PrinterName.class, new PrinterName(name, null)); _attributes.put(PrinterState.class, PrinterState.IDLE); _attributes.put(PrinterInfo.class, new PrinterInfo("Custom location", null)); _attributes.put(PrinterIsAcceptingJobs.class, PrinterIsAcceptingJobs.ACCEPTING_JOBS); _attributes.put(PrinterMakeAndModel.class, new PrinterMakeAndModel( "Custom printer", null)); _attributes.put(Media.class, new Media[] { MediaSizeName.ISO_A4 }); }
@Override protected void setAttributes(PrintRequestAttributeSet attributes) throws PrinterException { super.setAttributes(attributes); if (attributes == null) { return; // now always use attributes, so this shouldn't happen. } Attribute attr = attributes.get(Media.class); if (attr instanceof CustomMediaTray) { CustomMediaTray customTray = (CustomMediaTray)attr; String choice = customTray.getChoiceName(); if (choice != null) { mOptions = " InputSlot="+ choice; } } }
/** MediaSizeName / dmPaper */ private int[] getWin32MediaAttrib() { int wid_ht[] = {0, 0}; if (attributes != null) { Media media = (Media)attributes.get(Media.class); if (media instanceof MediaSizeName) { MediaSizeName msn = (MediaSizeName)media; MediaSize ms = MediaSize.getMediaSizeForName(msn); if (ms != null) { wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0); wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0); } } } return wid_ht; }
@Override public void actionPerformed(ActionEvent e) { try { if (selectedDriver == null) return; PrintService ps = (PrintService)printers.getSelectedItem(); PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet(); attr.add(new Copies(1)); attr.add((Media)ps.getDefaultAttributeValue(Media.class)); // set to default paper from printer attr.add(OrientationRequested.LANDSCAPE); SimpleDoc doc = new SimpleDoc(activeLabel, DocFlavor.SERVICE_FORMATTED.PRINTABLE, null); ps.createPrintJob().print(doc, attr); } catch (PrintException ex) { log.log(Level.SEVERE, "\bBarcode print failed: " + ex.getMessage(), ex); } }
private MediaTray resolveMediaTray(String tray) { Media medias[] = (Media[]) getPrintService().getSupportedAttributeValues(Media.class, null, null); if (medias == null) { return null; } else { for (Media media : medias) { if (media instanceof MediaTray) { MediaTray mediaTray = (MediaTray) media; String trayName = mediaTray.toString().trim(); if (trayName.contains(" ")) { trayName = trayName.replace(' ', '_'); } if (trayName.equals(tray)) { return mediaTray; } } } return null; } }
@Test public void printToMiddleTray() throws Exception { PrinterEndpoint endpoint = new PrinterEndpoint(); PrinterConfiguration configuration = new PrinterConfiguration(); configuration.setHostname("localhost"); configuration.setPort(631); configuration.setPrintername("DefaultPrinter"); configuration.setMediaSizeName(MediaSizeName.ISO_A4); configuration.setInternalSides(Sides.ONE_SIDED); configuration.setInternalOrientation(OrientationRequested.PORTRAIT); configuration.setMediaTray("middle"); PrinterProducer producer = new PrinterProducer(endpoint, configuration); producer.start(); PrinterOperations printerOperations = producer.getPrinterOperations(); PrintRequestAttributeSet attributeSet = printerOperations.getPrintRequestAttributeSet(); Attribute attribute = attributeSet.get(javax.print.attribute.standard.Media.class); assertNotNull(attribute); assertTrue(attribute instanceof MediaTray); MediaTray mediaTray = (MediaTray) attribute; assertEquals("middle", mediaTray.toString()); }
protected void setupJavaPrint() { // "install" another default printer PrintService psDefault = mock(PrintService.class); when(psDefault.getName()).thenReturn("DefaultPrinter"); when(psDefault.isDocFlavorSupported(any(DocFlavor.class))).thenReturn(Boolean.TRUE); PrintServiceLookup psLookup = mock(PrintServiceLookup.class); when(psLookup.getPrintServices()).thenReturn(new PrintService[]{psDefault}); when(psLookup.getDefaultPrintService()).thenReturn(psDefault); DocPrintJob docPrintJob = mock(DocPrintJob.class); when(psDefault.createPrintJob()).thenReturn(docPrintJob); MediaTray[] trays = new MediaTray[]{ MediaTray.TOP, MediaTray.MIDDLE, MediaTray.BOTTOM }; when(psDefault.getSupportedAttributeValues(Media.class, null, null)).thenReturn(trays); PrintServiceLookup.registerServiceProvider(psLookup); }
@Test public void testListPrinterTrays () { final PrintService [] aAllServices = PrintServiceLookup.lookupPrintServices (null, null); for (final PrintService aService : aAllServices) { s_aLogger.info (aService.toString ()); final Object aAttrs = aService.getSupportedAttributeValues (Media.class, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null); if (ArrayHelper.isArray (aAttrs)) { for (final Media aElement : (Media []) aAttrs) if (aElement instanceof MediaTray) s_aLogger.info (" " + aElement); } } }
public void setAttribute(final Attribute attr) { final Class<? extends Attribute> category = attr.getCategory(); if (OrientationRequested.class.equals(category)) { setOrientation((OrientationRequested) attr); } else if (MediaSize.class.equals(category)) { setPaper((MediaSize) attr); } else if (Media.class.equals(category)) { setPaper((MediaSizeName) attr); } else if (Paper.class.equals(category)) { setPaper((Paper) attr); } else if (Copies.class.equals(category)) { setCopies((Copies) attr); } else if (PrintQuality.class.equals(category)) { setPrintQuality((PrintQuality) attr); } else if (Sides.class.equals(category)) { setSides((Sides) attr); } else if (SheetCollate.class.equals(category)) { setCollate((SheetCollate) attr); } else if (PrinterResolution.class.equals(category)) { setPrinterResolution((PrinterResolution) attr); } else if (Chromaticity.class.equals(category)) { setChromaticity((Chromaticity) attr); } }
void fillMediaFields() { if (myService.isAttributeCategorySupported(Media.class)) { Media [] mediaList = (Media []) myService .getSupportedAttributeValues(Media.class, flavor, attrs); Media oldMedia = (sizeBox.getItemCount() <= 0) ? null : (Media)sizeBox.getSelectedItem(); sizeBox.removeAllItems(); if ((mediaList != null) && (mediaList.length > 0)) { for(int i = 0; i < mediaList.length; i++) { sizeBox.addItem(mediaList[i]); } selectMedia(oldMedia); } sizeBox.setEnabled((mediaList != null) && (mediaList.length > 0)); sizeLabel.setEnabled((mediaList != null) && (mediaList.length > 0)); } else { sizeBox.setEnabled(false); sizeLabel.setEnabled(false); } sizeBox.updateUI(); }