public MediaTray findMediaTray(int dmBin) { if (dmBin >= 1 && dmBin <= dmPaperBinToPrintService.length) { return dmPaperBinToPrintService[dmBin-1]; } MediaTray[] trays = getMediaTrays(); if (trays != null) { for (int i=0;i<trays.length;i++) { if(trays[i] instanceof Win32MediaTray) { Win32MediaTray win32Tray = (Win32MediaTray)trays[i]; if (win32Tray.winID == dmBin) { return win32Tray; } } } } return Win32MediaTray.AUTO; }
private void setMediaTrayAttrib(Attribute attr) { if (attr == MediaTray.BOTTOM) { mAttMediaTray = 2; // DMBIN_LOWER } else if (attr == MediaTray.ENVELOPE) { mAttMediaTray = 5; // DMBIN_ENVELOPE } else if (attr == MediaTray.LARGE_CAPACITY) { mAttMediaTray = 11; // DMBIN_LARGECAPACITY } else if (attr == MediaTray.MAIN) { mAttMediaTray =1; // DMBIN_UPPER } else if (attr == MediaTray.MANUAL) { mAttMediaTray = 4; // DMBIN_MANUAL } else if (attr == MediaTray.MIDDLE) { mAttMediaTray = 3; // DMBIN_MIDDLE } else if (attr == MediaTray.SIDE) { // no equivalent predefined value mAttMediaTray = 7; // DMBIN_AUTO } else if (attr == MediaTray.TOP) { mAttMediaTray = 1; // DMBIN_UPPER } else { if (attr instanceof Win32MediaTray) { mAttMediaTray = ((Win32MediaTray)attr).winID; } else { mAttMediaTray = 1; // default } } }
private MediaTray[] getMediaTrays(){ if( mediaTrays != null ){ // the print service is a singleton per printer so we only do this once return mediaTrays; } PaperSourceCollection trays = settings.get_PaperSources(); int count = trays.get_Count(); List<MediaTray> trayList = new ArrayList<MediaTray>(); for( int i=0; i < count; i++ ){ PaperSource tray = trays.get_Item(i); MediaTray javaTray = getDefaultTray(tray); if( javaTray != null ){ trayList.add( javaTray ); } else { trayList.add( new NetMediaTray( tray ) ); } } mediaTrays = trayList.toArray( new MediaTray[trayList.size()]); return mediaTrays; }
/** * Returns the Java-default {@link MediaTray} for a paper source. This is required since these default * trays are public constants which can be used without checking for the actually present media trays * @param source the .NET paper source to get the predefined source for * @return the media tray or null, in case there is no mapping for the paper source */ private MediaTray getDefaultTray( PaperSource source ){ // convert from .NET kind to java's pre defined MediaTrays switch( source.get_RawKind() ){ case 1 : return MediaTray.TOP; case 2 : return MediaTray.BOTTOM; case 3 : return MediaTray.MIDDLE; case 4 : return MediaTray.MANUAL; case 5 : return MediaTray.ENVELOPE; case 6 : return Win32MediaTray.ENVELOPE_MANUAL; case 7 : return Win32MediaTray.AUTO; case 8 : return Win32MediaTray.TRACTOR; case 9 : return Win32MediaTray.SMALL_FORMAT; case 10 : return Win32MediaTray.LARGE_FORMAT; case 11 : return MediaTray.LARGE_CAPACITY; case 14 : return MediaTray.MAIN; case 15 : return Win32MediaTray.FORMSOURCE; // FIXME which PaperSourceKind is MediaTray.SIDE ??? } return null; }
/** * Returns the .NET {@link PaperSource} for a media tray. This will be done either by mapping or * directly in case the tray is a {@link NetMediaTray} * @param tray the tray to get the paper source for, must not be null * @return the selected {@link PaperSource} or null, in case there is no matching {@link PaperSource} */ public PaperSource getPaperSourceForTray( MediaTray tray ){ if( tray instanceof NetMediaTray ){ return ((NetMediaTray)tray).getPaperSource( this ); } // try to find the appropriate paper source for the Java-Defined tray PaperSourceCollection trays = settings.get_PaperSources(); int count = trays.get_Count(); for( int i=0; i < count; i++ ){ PaperSource paperSource = trays.get_Item(i); if( getDefaultTray( paperSource ) == tray ){ return paperSource; } } return null; }
private static void printTest() { MediaTray tray = null; //tray = getMediaTray( prservices, "Bypass Tray" ); tray = getMediaTray( prservices, "Tray 4" ); PrintRequestAttributeSet atrset = new HashPrintRequestAttributeSet(); //atrset.add( MediaSizeName.ISO_A4 ); atrset.add(tray); PrinterJob pjob = PrinterJob.getPrinterJob(); pjob.setPrintable(new TestMediaTraySelection()); try { pjob.print(atrset); } catch (PrinterException e) { e.printStackTrace(); fail(); } }
private PrintRequestAttributeSet assignPrintAttributes() throws PrintException { PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); if (config.getCopies() >= 1) { printRequestAttributeSet.add(new Copies(config.getCopies())); } else { throw new PrintException("Number of print copies should be greater than zero"); } printRequestAttributeSet.add(config.getMediaSizeName()); printRequestAttributeSet.add(config.getInternalSides()); printRequestAttributeSet.add(config.getInternalOrientation()); if (config.getMediaTray() != null) { MediaTray mediaTray = resolveMediaTray(config.getMediaTray()); if (mediaTray == null) { throw new PrintException("mediatray not found " + config.getMediaTray()); } printRequestAttributeSet.add(mediaTray); } return printRequestAttributeSet; }
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); }
private void setMediaTrayAttrib(Attribute attr) { if (attr == MediaTray.BOTTOM) { mAttMediaTray = 2; // DMBIN_LOWER } else if (attr == MediaTray.ENVELOPE) { mAttMediaTray = 5; // DMBIN_ENVELOPE } else if (attr == MediaTray.LARGE_CAPACITY) { mAttMediaTray = 11; // DMBIN_LARGECAPACITY } else if (attr == MediaTray.MAIN) { mAttMediaTray =1; // DMBIN_UPPER } else if (attr == MediaTray.MANUAL) { mAttMediaTray = 4; // DMBIN_MANUAL } else if (attr == MediaTray.MIDDLE) { mAttMediaTray = 3; // DMBIN_MIDDLE } else if (attr == MediaTray.SIDE) { // no equivalent predefined value mAttMediaTray = 7; // DMBIN_AUTO } else if (attr == MediaTray.TOP) { mAttMediaTray =1; // DMBIN_UPPER } else { if (attr instanceof Win32MediaTray) { mAttMediaTray = ((Win32MediaTray)attr).winID; } else { mAttMediaTray = 1; // default } } }
@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); } } }
@Before public void setup() { PrintService psDefault = Mockito.mock(PrintService.class); Mockito.when(psDefault.getName()).thenReturn("DefaultPrinter"); Mockito.when(psDefault.isDocFlavorSupported(Mockito.any(DocFlavor.class))).thenReturn(Boolean.TRUE); PrintServiceLookup psLookup = Mockito.mock(PrintServiceLookup.class); Mockito.when(psLookup.getPrintServices()).thenReturn(new PrintService[]{psDefault}); Mockito.when(psLookup.getDefaultPrintService()).thenReturn(psDefault); DocPrintJob docPrintJob = Mockito.mock(DocPrintJob.class); Mockito.when(psDefault.createPrintJob()).thenReturn(docPrintJob); MediaTray[] trays = new MediaTray[]{ MediaTray.TOP, MediaTray.MIDDLE, MediaTray.BOTTOM }; Mockito.when(psDefault.getSupportedAttributeValues(Media.class, null, null)).thenReturn(trays); PrintServiceLookup.registerServiceProvider(psLookup); }
private void initSelection(){ if (selPrinter == null) { PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService(); if (defaultPrintService != null && printServices.contains(defaultPrintService)) { cvPrinters.setSelection(new StructuredSelection(defaultPrintService)); } } else { for (PrintService ps : printServices) { if (ps.getName().equals(selPrinter)) { cvPrinters.setSelection(new StructuredSelection(ps)); } } } if (!mediaTrays.isEmpty()) { if (selTray == null) { cvTrays.setSelection(new StructuredSelection(mediaTrays.get(0))); } else { for (MediaTray mt : mediaTrays) { if (mt.toString().equals(selTray)) { cvTrays.setSelection(new StructuredSelection(mt)); } } } } }
@Override protected void okPressed(){ IStructuredSelection selPrintService = (IStructuredSelection) cvPrinters.getSelection(); if (selPrintService != null && !selPrintService.isEmpty()) { selPrinter = ((PrintService) selPrintService.getFirstElement()).getName(); } IStructuredSelection selMediaTray = (IStructuredSelection) cvTrays.getSelection(); if (selMediaTray != null) { if (selMediaTray.isEmpty()) { selTray = ""; } else { selTray = ((MediaTray) selMediaTray.getFirstElement()).toString(); } } super.okPressed(); }
public int findTrayID(MediaTray tray) { getMediaTrays(); // make sure they are initialised. if (tray instanceof Win32MediaTray) { Win32MediaTray winTray = (Win32MediaTray)tray; return winTray.getDMBinID(); } for (int id=0; id<dmPaperBinToPrintService.length; id++) { if (tray.equals(dmPaperBinToPrintService[id])) { return id+1; // DMBIN_FIRST = 1; } } return 0; // didn't find the tray }
private boolean isSupportedMediaTray(MediaTray msn) { MediaTray[] trays = getMediaTrays(); if (trays != null) { for (int i=0; i<trays.length; i++) { if (msn.equals(trays[i])) { return true; } } } return false; }