Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Win32PrintJob.java « print « sun « openjdk - github.com/mono/ikvm-fork.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 654f742b5888d09a2c618d12c53aa732a444bd2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
/*
  Copyright (C) 2009, 2012 Volker Berlin (i-net software)
  Copyright (C) 2010, 2011 Karsten Heinrich (i-net software)

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
 */
package sun.print;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import javax.print.CancelablePrintJob;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSetUtilities;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashPrintJobAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintJobAttribute;
import javax.print.attribute.PrintJobAttributeSet;
import javax.print.attribute.PrintRequestAttribute;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Chromaticity;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Destination;
import javax.print.attribute.standard.DocumentName;
import javax.print.attribute.standard.Fidelity;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.JobOriginatingUserName;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.PageRanges;
import javax.print.attribute.standard.PrinterIsAcceptingJobs;
import javax.print.attribute.standard.PrinterState;
import javax.print.attribute.standard.PrinterStateReason;
import javax.print.attribute.standard.PrinterStateReasons;
import javax.print.attribute.standard.RequestingUserName;
import javax.print.attribute.standard.SheetCollate;
import javax.print.event.PrintJobAttributeListener;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;

import sun.awt.windows.WPrinterJob;
import sun.print.Win32PrintService.NetMediaTray;

import cli.System.Drawing.Printing.*;

/**
 * @author Volker Berlin
 */
public class Win32PrintJob implements CancelablePrintJob{

    private ArrayList<PrintJobListener> jobListeners;

    private ArrayList<PrintJobAttributeListener> attrListeners;

    private ArrayList<PrintJobAttributeSet> listenedAttributeSets;

    private final Win32PrintService service;

    
    private boolean printing;
    private boolean printReturned;

    private PrintRequestAttributeSet reqAttrSet;

    private PrintJobAttributeSet jobAttrSet;

    private PrinterJob job = new WPrinterJob();
    private Doc doc;
    private String mDestination;

    /* these variables used globally to store reference to the print
     * data retrieved as a stream. On completion these are always closed
     * if non-null.
     */
    private InputStream instream;

    /* default values overridden by those extracted from the attributes */
    private boolean fidelity;
    private boolean printColor;
    private String jobName = "Java Printing";
    private int copies;
    private MediaSizeName mediaName;
    private MediaSize     mediaSize;
    private OrientationRequested orient;
    
    private final PrintPeer peer;
    private PrinterException printerException;

	private PageNumberConverter pageRanges;

	private MediaTray mediaTray;

    
    Win32PrintJob(Win32PrintService service, PrintPeer peer){
        this.service = service;
        this.peer = peer;
    }


    @Override
    public PrintService getPrintService(){
        return service;
    }


    @Override
    public PrintJobAttributeSet getAttributes(){
        synchronized(this){
            if(jobAttrSet == null){
                /* just return an empty set until the job is submitted */
                PrintJobAttributeSet jobSet = new HashPrintJobAttributeSet();
                return AttributeSetUtilities.unmodifiableView(jobSet);
            }else{
                return jobAttrSet;
            }
        }
    }


    @Override
    public void addPrintJobListener(PrintJobListener listener){
        synchronized(this){
            if(listener == null){
                return;
            }
            if(jobListeners == null){
                jobListeners = new ArrayList<PrintJobListener>();
            }
            jobListeners.add(listener);
        }
    }


    @Override
    public void removePrintJobListener(PrintJobListener listener){
        synchronized(this){
            if(listener == null || jobListeners == null){
                return;
            }
            jobListeners.remove(listener);
            if(jobListeners.isEmpty()){
                jobListeners = null;
            }
        }
    }


    /* Closes any stream already retrieved for the data.
     * We want to avoid unnecessarily asking the Doc to create a stream only
     * to get a reference in order to close it because the job failed.
     * If the representation class is itself a "stream", this
     * closes that stream too.
     */
    private void closeDataStreams() {
        // TODO
    }

    private void notifyEvent(int reason) {

        /* since this method should always get called, here's where
         * we will perform the clean up of any data stream supplied.
         */
        switch (reason) {
            case PrintJobEvent.DATA_TRANSFER_COMPLETE:
            case PrintJobEvent.JOB_CANCELED :
            case PrintJobEvent.JOB_FAILED :
            case PrintJobEvent.NO_MORE_EVENTS :
            case PrintJobEvent.JOB_COMPLETE :
                closeDataStreams();
        }

        synchronized (this) {
            if (jobListeners != null) {
                PrintJobListener listener;
                PrintJobEvent event = new PrintJobEvent(this, reason);
                for (int i = 0; i < jobListeners.size(); i++) {
                    listener = jobListeners.get(i);
                    switch (reason) {

                        case PrintJobEvent.JOB_COMPLETE :
                            listener.printJobCompleted(event);
                            break;

                        case PrintJobEvent.JOB_CANCELED :
                            listener.printJobCanceled(event);
                            break;

                        case PrintJobEvent.JOB_FAILED :
                            listener.printJobFailed(event);
                            break;

                        case PrintJobEvent.DATA_TRANSFER_COMPLETE :
                            listener.printDataTransferCompleted(event);
                            break;

                        case PrintJobEvent.NO_MORE_EVENTS :
                            listener.printJobNoMoreEvents(event);
                            break;

                        default:
                            break;
                    }
                }
            }
       }
    }

    @Override
    public void addPrintJobAttributeListener(PrintJobAttributeListener listener, PrintJobAttributeSet attributes){
        synchronized(this){
            if(listener == null){
                return;
            }
            if(attrListeners == null){
                attrListeners = new ArrayList<PrintJobAttributeListener>();
                listenedAttributeSets = new ArrayList<PrintJobAttributeSet>();
            }
            attrListeners.add(listener);
            if(attributes == null){
                attributes = new HashPrintJobAttributeSet();
            }
            listenedAttributeSets.add(attributes);
        }
    }


    @Override
    public void removePrintJobAttributeListener(PrintJobAttributeListener listener){
        synchronized(this){
            if(listener == null || attrListeners == null){
                return;
            }
            int index = attrListeners.indexOf(listener);
            if(index == -1){
                return;
            }else{
                attrListeners.remove(index);
                listenedAttributeSets.remove(index);
                if(attrListeners.isEmpty()){
                    attrListeners = null;
                    listenedAttributeSets = null;
                }
            }
        }
    }


    @Override
    public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException{

        synchronized(this){
            if(printing){
                throw new PrintException("already printing");
            }else{
                printing = true;
            }
        }

        PrinterState prnState = (PrinterState)service.getAttribute(PrinterState.class);
        if(prnState == PrinterState.STOPPED){
            PrinterStateReasons prnStateReasons = (PrinterStateReasons)service.getAttribute(PrinterStateReasons.class);
            if((prnStateReasons != null) && (prnStateReasons.containsKey(PrinterStateReason.SHUTDOWN))){
                throw new PrintException("PrintService is no longer available.");
            }
        }

        if((PrinterIsAcceptingJobs)(service.getAttribute(PrinterIsAcceptingJobs.class)) == PrinterIsAcceptingJobs.NOT_ACCEPTING_JOBS){
            throw new PrintException("Printer is not accepting job.");
        }

        this.doc = doc;
        /* check if the parameters are valid before doing much processing */
        DocFlavor flavor = doc.getDocFlavor();
        Object data;

        try{
            data = doc.getPrintData();
        }catch(IOException e){
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("can't get print data: " + e.toString());
        }

        if(flavor == null || (!service.isDocFlavorSupported(flavor))){
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintJobFlavorException("invalid flavor", flavor);
        }

        initializeAttributeSets(doc, attributes);

        getAttributeValues(flavor);

        String repClassName = flavor.getRepresentationClassName();

        if(flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG)
                || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF)
                || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)){
            try{
                instream = doc.getStreamForBytes();
                if(instream == null){
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException("No stream for data");
                }
                printableJob(new ImagePrinter(instream));
                service.wakeNotifier();
                return;
            }catch(ClassCastException cce){
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(cce);
            }catch(IOException ioe){
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe);
            }
        }else if(flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG)
                || flavor.equals(DocFlavor.URL.PNG)){
            try{
                printableJob(new ImagePrinter((URL)data));
                service.wakeNotifier();
                return;
            }catch(ClassCastException cce){
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(cce);
            }
        }else if(repClassName.equals("java.awt.print.Pageable")){
            try{
                pageableJob((Pageable)doc.getPrintData());
                service.wakeNotifier();
                return;
            }catch(ClassCastException cce){
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(cce);
            }catch(IOException ioe){
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe);
            }
        }else if(repClassName.equals("java.awt.print.Printable")){
            try{
                printableJob((Printable)doc.getPrintData());
                service.wakeNotifier();
                return;
            }catch(ClassCastException cce){
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(cce);
            }catch(IOException ioe){
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException(ioe);
            }
        }else if(repClassName.equals("[B") || repClassName.equals("java.io.InputStream")
                || repClassName.equals("java.net.URL")){

            if(repClassName.equals("java.net.URL")){
                URL url = (URL)data;
                try{
                    instream = url.openStream();
                }catch(IOException e){
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException(e.toString());
                }
            }else{
                try{
                    instream = doc.getStreamForBytes();
                }catch(IOException ioe){
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException(ioe.toString());
                }
            }

            if(instream == null){
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("No stream for data");
            }

            if(mDestination != null){ // if destination attribute is set
                try{
                    FileOutputStream fos = new FileOutputStream(mDestination);
                    byte[] buffer = new byte[1024];
                    int cread;

                    while((cread = instream.read(buffer, 0, buffer.length)) >= 0){
                        fos.write(buffer, 0, cread);
                    }
                    fos.flush();
                    fos.close();
                }catch(FileNotFoundException fnfe){
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException(fnfe.toString());
                }catch(IOException ioe){
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException(ioe.toString());
                }
                notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
                notifyEvent(PrintJobEvent.JOB_COMPLETE);
                service.wakeNotifier();
                return;
            }

            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("Print job failed. IKVM does not support raw data currently.");
        }else{
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException("unrecognized class: " + repClassName);
        }
    }
    
    
    public void printableJob(Printable printable) throws PrintException {
        throw new PrintException("Win32PrintJob.printableJob() not implemented:");
    }

    public void pageableJob(Pageable pageable) throws PrintException {
    	// a general hint for this code here: The PrintJob implementation (WPrinterJob) in the Oracle
    	// JRE implements SunPrinterJobService - which we don't here. Unfortunately this is used as a hint
    	// to distinguish between the build-in printer service and custom printer services. So for the RasterPrinterJob
    	// this implementation of the Win32PrintJob is a custom print job! That's why several methods of
    	// RasterPrintJob(which only apply to the SunPrinterJobService otherwise) had to be moved here.
    	// This includes the attribute resolving and the media patch for instance.
        try {
            PrintDocument printDocument = createPrintDocument( );
            pageable = patchMedia( pageable );
    
            printDocument.add_QueryPageSettings(new QueryPageSettingsEventHandler(new QueryPage( pageable, printColor, mediaTray ) ) );
            printDocument.add_PrintPage(new PrintPageEventHandler(new PrintPage(pageable)));
            printDocument.Print();
            if(printerException != null){
                throw printerException;
            }
            //TODO throw exception on Cancel
            notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
            return;
        } catch (PrinterException pe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(pe);
        } finally {
            printReturned = true;
            notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
        }
    }
    
    
    private PrintDocument createPrintDocument() throws PrintException{
        PrintDocument printDocument = new PrintDocument();
        PrinterSettings settings = printDocument.get_PrinterSettings();
        settings.set_PrinterName( service.getName() );
        if( !settings.get_IsValid() ){
        	throw new PrintException("Printer name ''" + service.getName() + "' is invalid.");
        }
        
        if( jobName != null ){
        	printDocument.set_DocumentName( jobName );
        }
        printDocument.get_DefaultPageSettings().set_Color(printColor);
        
        Attribute destination = reqAttrSet.get(Destination.class);
        if(destination instanceof Destination){
        	File destFile = new File(((Destination)destination).getURI());
            settings.set_PrintFileName(destFile.getAbsolutePath());
            settings.set_PrintToFile(true);
        }
        
        settings.set_Copies((short)copies);
        boolean collated = false;
        if(copies > 1){
            Object collate = reqAttrSet.get(SheetCollate.class);
            if( collate == null ){
            	collate = service.getDefaultAttributeValue(SheetCollate.class);
            }
            collated = collate == SheetCollate.COLLATED;
            settings.set_Collate( collated );
        }
        Attribute pageRangeObj = reqAttrSet.get(PageRanges.class);
        if( pageRangeObj != null ){
        	int[][] ranges = ((PageRanges)pageRangeObj).getMembers();
        	if( ranges.length > 1 ){
    			settings.set_PrintRange( PrintRange.wrap( PrintRange.Selection ) );
        	} else {
        		if( ranges.length > 0 ){
        			settings.set_FromPage(ranges[0][0]);
        			settings.set_ToPage(ranges[0][1]);
        			settings.set_PrintRange( PrintRange.wrap( PrintRange.SomePages ) );
        		} // else allPages???
        	}
        } else {
        	settings.set_PrintRange( PrintRange.wrap( PrintRange.AllPages ) );
        }
        pageRanges = new PageNumberConverter( (PageRanges)pageRangeObj, copies, collated );
        return printDocument;
    }
    
    // copied from RasterPrintJob 
    // Since we don't implement SunPrinterJobService the hack in RasterPrintService which applies
    // the media format to the auto-generated OpenBook doesn't work here. So we have to modify
    // the page format of the OpenBook here - equal to the code in RasterPrintJob
    private Pageable patchMedia( Pageable pageable ){
    	/* OpenBook is used internally only when app uses Printable.
         * This is the case when we use the values from the attribute set.
         */
        Media media = (Media)reqAttrSet.get(Media.class);
        OrientationRequested orientReq = (OrientationRequested)reqAttrSet.get(OrientationRequested.class);
        MediaPrintableArea mpa = (MediaPrintableArea)reqAttrSet.get(MediaPrintableArea.class);

        if ((orientReq != null || media != null || mpa != null) && pageable instanceof OpenBook) {

            /* We could almost(!) use PrinterJob.getPageFormat() except
             * here we need to start with the PageFormat from the OpenBook :
             */
            Printable printable = pageable.getPrintable(0);
            PageFormat pf = (PageFormat)pageable.getPageFormat(0).clone();
            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, reqAttrSet);
                if (mpaVals instanceof MediaPrintableArea[] && ((MediaPrintableArea[])mpaVals).length > 0) {
                    mpa = ((MediaPrintableArea[])mpaVals)[0];
                }
            }

            if (isSupportedValue(orientReq, reqAttrSet) || (!fidelity && orientReq != null)) {
                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);
            }

            if (isSupportedValue(media, reqAttrSet) || (!fidelity && media != null)) {
                if (media instanceof MediaSizeName) {
                    MediaSizeName msn = (MediaSizeName)media;
                    MediaSize msz = MediaSize.getMediaSizeForName(msn);
                    if (msz != null) {
                        float paperWid =  msz.getX(MediaSize.INCH) * 72.0f;
                        float paperHgt =  msz.getY(MediaSize.INCH) * 72.0f;
                        paper.setSize(paperWid, paperHgt);
                        if (mpa == null) {
                            paper.setImageableArea(72.0, 72.0, paperWid-144.0, paperHgt-144.0);
                        }
                    }
                }
            }

            if (isSupportedValue(mpa, reqAttrSet) || (!fidelity && mpa != null)) {
                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]);
            }

            pf.setPaper(paper);
            pf = validatePage(pf);
            return new OpenBook(pf, printable);
        }
        return pageable;
    }
    
    // copied from RasterPrintJob to since we don't implement SunPrinterJobService
    /**
     * The passed in PageFormat is cloned and altered to be usable on
     * the PrinterJob's current printer.
     */
    private PageFormat validatePage(PageFormat page) {
        PageFormat newPage = (PageFormat)page.clone();
        Paper newPaper = new Paper();
        validatePaper(newPage.getPaper(), newPaper);
        newPage.setPaper(newPaper);

        return newPage;
    }
    
    // copied from RasterPrintJob to since we don't implement SunPrinterJobService
    /**
     * updates a Paper object to reflect the current printer's selected
     * paper size and imageable area for that paper size.
     * Default implementation copies settings from the original, applies
     * applies some validity checks, changes them only if they are
     * clearly unreasonable, then sets them into the new Paper.
     * Subclasses are expected to override this method to make more
     * informed decisons.
     */
    protected void validatePaper(Paper origPaper, Paper newPaper) {
        if (origPaper == null || newPaper == null) {
            return;
        } else {
            double wid = origPaper.getWidth();
            double hgt = origPaper.getHeight();
            double ix = origPaper.getImageableX();
            double iy = origPaper.getImageableY();
            double iw = origPaper.getImageableWidth();
            double ih = origPaper.getImageableHeight();

            /* Assume any +ve values are legal. Overall paper dimensions
             * take precedence. Make sure imageable area fits on the paper.
             */
            Paper defaultPaper = new Paper();
            wid = ((wid > 0.0) ? wid : defaultPaper.getWidth());
            hgt = ((hgt > 0.0) ? hgt : defaultPaper.getHeight());
            ix = ((ix > 0.0) ? ix : defaultPaper.getImageableX());
            iy = ((iy > 0.0) ? iy : defaultPaper.getImageableY());
            iw = ((iw > 0.0) ? iw : defaultPaper.getImageableWidth());
            ih = ((ih > 0.0) ? ih : defaultPaper.getImageableHeight());
            /* full width/height is not likely to be imageable, but since we
             * don't know the limits we have to allow it
             */
            if (iw > wid) {
                iw = wid;
            }
            if (ih > hgt) {
                ih = hgt;
            }
            if ((ix + iw) > wid) {
                ix = wid - iw;
            }
            if ((iy + ih) > hgt) {
                iy = hgt - ih;
            }
            newPaper.setSize(wid, hgt);
            newPaper.setImageableArea(ix, iy, iw, ih);
        }
    }
    
    // copied from RasterPrintJob
    /**
     * Checks whether a certain attribute value is valid for the current print service
     * @param attrval the attribute value 
     * @param attrset Set of printing attributes for a supposed job (both job-level attributes and document-level attributes), or null.
     * @return true if valid
     */
    protected boolean isSupportedValue(Attribute attrval, PrintRequestAttributeSet attrset) {
    	return (attrval != null && service != null && service.isAttributeValueSupported(attrval, DocFlavor.SERVICE_FORMATTED.PAGEABLE, attrset));
    }
    

    /*
     * There's some inefficiency here as the job set is created even though it may never be requested.
     */
    private synchronized void initializeAttributeSets(Doc doc, PrintRequestAttributeSet reqSet){

        reqAttrSet = new HashPrintRequestAttributeSet();
        jobAttrSet = new HashPrintJobAttributeSet();

        Attribute[] attrs;
        if(reqSet != null){
            reqAttrSet.addAll(reqSet);
            attrs = reqSet.toArray();
            for(int i = 0; i < attrs.length; i++){
                if(attrs[i] instanceof PrintJobAttribute){
                    jobAttrSet.add(attrs[i]);
                }
            }
        }

        DocAttributeSet docSet = doc.getAttributes();
        if(docSet != null){
            attrs = docSet.toArray();
            for(int i = 0; i < attrs.length; i++){
                if(attrs[i] instanceof PrintRequestAttribute){
                    reqAttrSet.add(attrs[i]);
                }
                if(attrs[i] instanceof PrintJobAttribute){
                    jobAttrSet.add(attrs[i]);
                }
            }
        }

        /* add the user name to the job */
        String userName = "";
        try{
            userName = System.getProperty("user.name");
        }catch(SecurityException se){
        }

        if(userName == null || userName.equals("")){
            RequestingUserName ruName = (RequestingUserName)reqSet.get(RequestingUserName.class);
            if(ruName != null){
                jobAttrSet.add(new JobOriginatingUserName(ruName.getValue(), ruName.getLocale()));
            }else{
                jobAttrSet.add(new JobOriginatingUserName("", null));
            }
        }else{
            jobAttrSet.add(new JobOriginatingUserName(userName, null));
        }

        /*
         * if no job name supplied use doc name (if supplied), if none and its a URL use that, else finally anything ..
         */
        if(jobAttrSet.get(JobName.class) == null){
            JobName jobName;
            if(docSet != null && docSet.get(DocumentName.class) != null){
                DocumentName docName = (DocumentName)docSet.get(DocumentName.class);
                jobName = new JobName(docName.getValue(), docName.getLocale());
                jobAttrSet.add(jobName);
            }else{
                String str = "JPS Job:" + doc;
                try{
                    Object printData = doc.getPrintData();
                    if(printData instanceof URL){
                        str = ((URL)(doc.getPrintData())).toString();
                    }
                }catch(IOException e){
                }
                jobName = new JobName(str, null);
                jobAttrSet.add(jobName);
            }
        }

        jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
    }

    private void getAttributeValues(DocFlavor flavor) throws PrintException {

        if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
            fidelity = true;
        } else {
            fidelity = false;
        }
        
        Attribute chroma = reqAttrSet.get( Chromaticity.class );
        // TODO check whether supported by the print service
        printColor = chroma == Chromaticity.COLOR;

        Attribute newTray = reqAttrSet.get( Media.class );
        if( newTray instanceof MediaTray ){
        	mediaTray = (MediaTray)newTray;
        }
        // TODO check whether supported by the print service
        
        Class category;
        Attribute [] attrs = reqAttrSet.toArray();
        for (int i=0; i<attrs.length; i++) {
            Attribute attr = attrs[i];
            category = attr.getCategory();
            if (fidelity == true) {
                if (!service.isAttributeCategorySupported(category)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported category: " + category, category, null);
                } else if
                    (!service.isAttributeValueSupported(attr, flavor, null)) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintJobAttributeException(
                        "unsupported attribute: " + attr, null, attr);
                }
            }
            if (category == Destination.class) {
              URI uri = ((Destination)attr).getURI();
              if (!"file".equals(uri.getScheme())) {
                notifyEvent(PrintJobEvent.JOB_FAILED);
                throw new PrintException("Not a file: URI");
              } else {
                try {
                  mDestination = (new File(uri)).getPath();
                } catch (Exception e) {
                  throw new PrintException(e);
                }
                // check write access
                SecurityManager security = System.getSecurityManager();
                if (security != null) {
                  try {
                    security.checkWrite(mDestination);
                  } catch (SecurityException se) {
                    notifyEvent(PrintJobEvent.JOB_FAILED);
                    throw new PrintException(se);
                  }
                }
              }
            } else if (category == JobName.class) {
                jobName = ((JobName)attr).getValue();
            } else if (category == Copies.class) {
                copies = ((Copies)attr).getValue();
            } else if (category == Media.class) {
              if (attr instanceof MediaSizeName) {
                    mediaName = (MediaSizeName)attr;
                    // If requested MediaSizeName is not supported,
                    // get the corresponding media size - this will
                    // be used to create a new PageFormat.
                    if (!service.isAttributeValueSupported(attr, flavor, null)) {
                        mediaSize = MediaSize.getMediaSizeForName(mediaName);
                    }
                }
            } else if (category == OrientationRequested.class) {
                orient = (OrientationRequested)attr;
            }
        }
    }


    @Override
    public void cancel() throws PrintException{
        synchronized(this){
            if(!printing){
                throw new PrintException("Job is not yet submitted.");
            }else if(job != null && !printReturned){
                job.cancel();
                notifyEvent(PrintJobEvent.JOB_CANCELED);
                return;
            }else{
                throw new PrintException("Job could not be cancelled.");
            }
        }
    }
    
    /**
     * Converts the Java 1/72 inch to .NET 1/100 inch
     * @param javaLength the java length in 1/72 inch
     * @return the .NET length in 1/100 inch
     */
    private static int java2netLength( int javaLength ){
    	return (int) Math.round( (double)(javaLength * 100) / 72d );
    }
    
    /**
     * Converts the Java 1/72 inch to .NET 1/100 inch
     * @param javaLength the java length in 1/72 inch
     * @return the .NET length in 1/100 inch
     */
    private static int java2netLength( double javaLength ){
    	return (int) Math.round( (javaLength * 100) / 72d );
    }

    
    private class PrintPage implements PrintPageEventHandler.Method{

        private final Pageable pageable;
        private int pageIndex;


        PrintPage(Pageable pageable){
            this.pageable = pageable;
        }


        @Override
        public void Invoke(Object paramObject, PrintPageEventArgs ev){

            try{
            	int realPage = pageRanges.getPageForIndex(pageIndex);
            	
                Printable printable = pageable.getPrintable(realPage);
                PageFormat pageFormat = pageable.getPageFormat(realPage);

                
                BufferedImage pBand = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
                Graphics2D imageGraphics = pBand.createGraphics();
                ((RasterPrinterJob)job).setGraphicsConfigInfo(imageGraphics.getTransform(), pageFormat.getImageableWidth(), pageFormat.getImageableHeight());
				PeekGraphics peekGraphics = new PeekGraphics(imageGraphics, job );

                /* 
                 * Because Sun is calling first with a PeekGraphics that we do it else for compatibility
                 */
                if(realPage == 0){
                    int pageResult = printable.print(peekGraphics, pageFormat, realPage);
                    if(pageResult != Printable.PAGE_EXISTS){
                        ev.set_HasMorePages(false);
                        ev.set_Cancel(true);
                        return;
                    }
                }
                Graphics2D printGraphics = peer.createGraphics(ev.get_Graphics());
            	Graphics2D g2d = ((Graphics2D)printGraphics);
            	int tX = (int) pageFormat.getWidth(); 
            	int tY = (int) pageFormat.getHeight();
            	// apply Java to .NET scaling (1/72 inch to 1/100 inch)
            	g2d.scale(100d/72d, 100d/72d);
            	// NOTE on Landscape printing:
            	// Setting landscape to true on the printer settings
            	// of a page already rotates the page! The orig. java code rotates the page itself,
            	// for .NET this is not required.
            	if( orient == OrientationRequested.REVERSE_LANDSCAPE){
            		g2d.translate( tX, tY );
            		g2d.rotate( Math.PI );
            	}

                printable.print(printGraphics, pageFormat, realPage);
                
                realPage = pageRanges.getPageForIndex(++pageIndex);
                if( realPage >= 0 ){
	                printable = pageable.getPrintable(realPage);
	                pageFormat = pageable.getPageFormat(realPage);
	                int pageResult = printable.print(peekGraphics, pageFormat, realPage);
                	ev.set_HasMorePages( pageResult == Printable.PAGE_EXISTS );
                } else {
                	ev.set_HasMorePages( false );
                }
            }catch(PrinterException ex){
                printerException = ex;
                ex.printStackTrace();
                ev.set_HasMorePages(false);
            }

        }

    }
    
    private class QueryPage implements QueryPageSettingsEventHandler.Method{

    	private final Pageable pageable;
        private int pageIndex;
		private final boolean printColor;
		private final MediaTray tray;


        QueryPage(Pageable pageable, boolean printColor, MediaTray tray  ){
            this.printColor = printColor;
			this.pageable = pageable;
			this.tray = tray;
        }
        
		@Override
		public void Invoke(Object source, QueryPageSettingsEventArgs e) {
			int realPage = pageRanges.getPageForIndex(pageIndex);
			// apply page settings to the current page
			PageFormat format = pageable.getPageFormat(realPage);
			PageSettings pageSettings = e.get_PageSettings();
			pageSettings.set_Color( printColor );
			PaperSource paperSource = service.getPaperSourceForTray( tray );
			if( paperSource != null ){
				pageSettings.set_PaperSource( paperSource );
			}
			
			PaperSize ps = new PaperSize();
			ps.set_Height( java2netLength( format.getHeight() ) );
			ps.set_Width( java2netLength( format.getWidth() ) );
			pageSettings.set_PaperSize( ps );
			
			Margins margins = new Margins();
			margins.set_Left( java2netLength( format.getImageableX() ) );
			margins.set_Top( java2netLength( format.getImageableY() ) );
			margins.set_Right( java2netLength(format.getWidth() - format.getImageableX() - format.getImageableWidth() ) );
			margins.set_Bottom( java2netLength(format.getHeight() - format.getImageableY() - format.getImageableHeight() ) );			
			pageSettings.set_Margins( margins );			
			pageIndex++;
		}
    }

    /**
     * Determines which logical page to print for a certain physical page 
     */
    public static class PageNumberConverter{        
        
        private List<Range> ranges;
        private int totalPages = -1;
        private final int copies;
        
        public PageNumberConverter( PageRanges pages, int copies, boolean collated ) {
            // NOTE: uncollated is handled by the printer driver! If we would handle that here, 
            // we would get copies^2 copies! 
            this.copies = collated ? copies : 1;
            if( pages != null && pages.getMembers() != null && pages.getMembers().length > 0 ){
                TreeSet<Range> rangesSort = new TreeSet<Range>();
                OUTER:
                for( int[] range : pages.getMembers() ){
                    Range r = new Range( range[0], range[1] + 1 ); // +1 to inlucde the uppre end
                    for( Range recent : rangesSort ){
                        if( recent.canMerge(r) ){
                            recent.merge(r);
                            continue OUTER;
                        }
                    }
                    rangesSort.add( r );
                }
                // finally merge
                Range recent = null;
                ranges = new ArrayList<Range>();
                for( Range r : rangesSort ){
                    if( recent != null && recent.canMerge( r ) ){
                        recent.merge( r );
                    } else {
                        ranges.add( r );
                        recent = r;
                    }
                }
                // calculate total pages, required for collated copies
                totalPages = 0;
                for( Range r : rangesSort ){
                    int diff = r.end - r.start;
                    totalPages += diff;
                }
            }
        }
        
        /**
         * Must be called in case the printable returns no-more-pages. Will return whether the print job 
         * will continue due to pending copies
         * @param index the current page index
         * @return if false, the print job will continue with copies, if true terminate the job
         */
        public boolean checkJobComplete( int index ){
            if( ranges != null ){
                return true;
            }
            if( totalPages < 0 ){
                // this is the first time, this was called for 'all-pages' so it's the total number
                totalPages = index;
            }
            return index > copies * totalPages;
        }
        
        /**
         * Returns which page to be printed for a certain page index
         * @param index the inex to be printed
         * @return the page number or -1, if there is no page for this index
         */
        public int getPageForIndex( int index ){
            if( index < 0 || ( totalPages >=0 && index >= copies * totalPages ) ){
                return -1;
            }
            if( ranges == null ){
                return totalPages >=0 ? index % totalPages : index;
            }
            int counter = 0;
            if( copies > 1 ){
                counter += (index / totalPages) * totalPages;
            }
            for( Range r : ranges ){
                int upper = counter + (r.end - r.start);
                if( index < upper ){
                    // so we're in the correct range
                    return r.start + ( index - counter ) - 1;
                } else {
                    counter = upper;
                }
            }
            return -1;
        }
        
        /**
         * A singular page range
         */
        private static class Range implements Comparable<Range> {

            public int start;
            public int end;
            
            public Range(int start, int end) {
                this.start = start;
                this.end = end;
            }
            
            /**
             * Checks whether the ranges intersect of have no gap in between
             * @param otherRange the range to be checked
             * @return true, if the ranges can be merged
             */
            public boolean canMerge( Range otherRange ){
                if( otherRange.end >= start && otherRange.end <= end ){
                    return true;
                }
                if( otherRange.start >= start && otherRange.start <= end ){
                    return true;
                }
                return false;
            }
            
            /**
             * Merges the other range into this range. Ignores the gap between the ranges if there is any
             * @param otherRange the range to be merged
             */
            public void merge( Range otherRange ){
                start = Math.min(start, otherRange.start);
                end = Math.max(end, otherRange.end);
            }
            
            public int compareTo(Range o) {             
                return start - o.start;
            }
        }
    }
}