49 import javax.print.attribute.HashPrintRequestAttributeSet;
50 import javax.print.attribute.PrintJobAttribute;
51 import javax.print.attribute.PrintJobAttributeSet;
52 import javax.print.attribute.PrintRequestAttribute;
53 import javax.print.attribute.PrintRequestAttributeSet;
54 import javax.print.attribute.standard.Copies;
55 import javax.print.attribute.standard.DocumentName;
56 import javax.print.attribute.standard.Fidelity;
57 import javax.print.attribute.standard.JobName;
58 import javax.print.attribute.standard.JobOriginatingUserName;
59 import javax.print.attribute.standard.Media;
60 import javax.print.attribute.standard.MediaSize;
61 import javax.print.attribute.standard.MediaSizeName;
62 import javax.print.attribute.standard.OrientationRequested;
63 import javax.print.attribute.standard.RequestingUserName;
64
65 import java.awt.print.*;
66
67 public class PSStreamPrintJob implements CancelablePrintJob {
68
69 transient private Vector jobListeners;
70 transient private Vector attrListeners;
71 transient private Vector listenedAttributeSets;
72
73 private PSStreamPrintService service;
74 private boolean fidelity;
75 private boolean printing = false;
76 private boolean printReturned = false;
77 private PrintRequestAttributeSet reqAttrSet = null;
78 private PrintJobAttributeSet jobAttrSet = null;
79 private PrinterJob job;
80 private Doc doc;
81 /* these variables used globally to store reference to the print
82 * data retrieved as a stream. On completion these are always closed
83 * if non-null.
84 */
85 private InputStream instream = null;
86 private Reader reader = null;
87
88 /* default values overridden by those extracted from the attributes */
89 private String jobName = "Java Printing";
90 private int copies = 1;
91 private MediaSize mediaSize = MediaSize.NA.LETTER;
100 }
101
102 public PrintJobAttributeSet getAttributes() {
103 synchronized (this) {
104 if (jobAttrSet == null) {
105 /* just return an empty set until the job is submitted */
106 PrintJobAttributeSet jobSet = new HashPrintJobAttributeSet();
107 return AttributeSetUtilities.unmodifiableView(jobSet);
108 } else {
109 return jobAttrSet;
110 }
111 }
112 }
113
114 public void addPrintJobListener(PrintJobListener listener) {
115 synchronized (this) {
116 if (listener == null) {
117 return;
118 }
119 if (jobListeners == null) {
120 jobListeners = new Vector();
121 }
122 jobListeners.add(listener);
123 }
124 }
125
126 public void removePrintJobListener(PrintJobListener listener) {
127 synchronized (this) {
128 if (listener == null || jobListeners == null ) {
129 return;
130 }
131 jobListeners.remove(listener);
132 if (jobListeners.isEmpty()) {
133 jobListeners = null;
134 }
135 }
136 }
137
138 /* Closes any stream already retrieved for the data.
139 * We want to avoid unnecessarily asking the Doc to create a stream only
140 * to get a reference in order to close it because the job failed.
174 else if (data instanceof InputStream) {
175 try {
176 ((InputStream)data).close();
177 } catch (IOException e) {
178 }
179 }
180 else if (data instanceof Reader) {
181 try {
182 ((Reader)data).close();
183 } catch (IOException e) {
184 }
185 }
186 }
187
188 private void notifyEvent(int reason) {
189 synchronized (this) {
190 if (jobListeners != null) {
191 PrintJobListener listener;
192 PrintJobEvent event = new PrintJobEvent(this, reason);
193 for (int i = 0; i < jobListeners.size(); i++) {
194 listener = (PrintJobListener)(jobListeners.elementAt(i));
195 switch (reason) {
196
197 case PrintJobEvent.JOB_CANCELED :
198 listener.printJobCanceled(event);
199 break;
200
201 case PrintJobEvent.JOB_FAILED :
202 listener.printJobFailed(event);
203 break;
204
205 case PrintJobEvent.DATA_TRANSFER_COMPLETE :
206 listener.printDataTransferCompleted(event);
207 break;
208
209 case PrintJobEvent.NO_MORE_EVENTS :
210 listener.printJobNoMoreEvents(event);
211 break;
212
213 case PrintJobEvent.JOB_COMPLETE :
214 listener.printJobCompleted(event);
215 break;
216
217 default:
218 break;
219 }
220 }
221 }
222 }
223 }
224
225 public void addPrintJobAttributeListener(
226 PrintJobAttributeListener listener,
227 PrintJobAttributeSet attributes) {
228 synchronized (this) {
229 if (listener == null) {
230 return;
231 }
232 if (attrListeners == null) {
233 attrListeners = new Vector();
234 listenedAttributeSets = new Vector();
235 }
236 attrListeners.add(listener);
237 if (attributes == null) {
238 attributes = new HashPrintJobAttributeSet();
239 }
240 listenedAttributeSets.add(attributes);
241 }
242 }
243
244 public void removePrintJobAttributeListener(
245 PrintJobAttributeListener listener) {
246 synchronized (this) {
247 if (listener == null || attrListeners == null ) {
248 return;
249 }
250 int index = attrListeners.indexOf(listener);
251 if (index == -1) {
252 return;
253 } else {
254 attrListeners.remove(index);
477 } else {
478 String str = "JPS Job:" + doc;
479 try {
480 Object printData = doc.getPrintData();
481 if (printData instanceof URL) {
482 str = ((URL)(doc.getPrintData())).toString();
483 }
484 } catch (IOException e) {
485 }
486 jobName = new JobName(str, null);
487 jobAttrSet.add(jobName);
488 }
489 }
490
491 jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
492 }
493
494 private void getAttributeValues(DocFlavor flavor) throws PrintException {
495
496 Attribute attr;
497 Class category;
498
499 if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
500 fidelity = true;
501 } else {
502 fidelity = false;
503 }
504
505 Attribute []attrs = reqAttrSet.toArray();
506 for (int i=0; i<attrs.length; i++) {
507 attr = attrs[i];
508 category = attr.getCategory();
509 if (fidelity == true) {
510 if (!service.isAttributeCategorySupported(category)) {
511 notifyEvent(PrintJobEvent.JOB_FAILED);
512 throw new PrintJobAttributeException(
513 "unsupported category: " + category, category, null);
514 } else if
515 (!service.isAttributeValueSupported(attr, flavor, null)) {
516 notifyEvent(PrintJobEvent.JOB_FAILED);
517 throw new PrintJobAttributeException(
|
49 import javax.print.attribute.HashPrintRequestAttributeSet;
50 import javax.print.attribute.PrintJobAttribute;
51 import javax.print.attribute.PrintJobAttributeSet;
52 import javax.print.attribute.PrintRequestAttribute;
53 import javax.print.attribute.PrintRequestAttributeSet;
54 import javax.print.attribute.standard.Copies;
55 import javax.print.attribute.standard.DocumentName;
56 import javax.print.attribute.standard.Fidelity;
57 import javax.print.attribute.standard.JobName;
58 import javax.print.attribute.standard.JobOriginatingUserName;
59 import javax.print.attribute.standard.Media;
60 import javax.print.attribute.standard.MediaSize;
61 import javax.print.attribute.standard.MediaSizeName;
62 import javax.print.attribute.standard.OrientationRequested;
63 import javax.print.attribute.standard.RequestingUserName;
64
65 import java.awt.print.*;
66
67 public class PSStreamPrintJob implements CancelablePrintJob {
68
69 transient private Vector<PrintJobListener> jobListeners;
70 transient private Vector<PrintJobAttributeListener> attrListeners;
71 transient private Vector<PrintJobAttributeSet> listenedAttributeSets;
72
73 private PSStreamPrintService service;
74 private boolean fidelity;
75 private boolean printing = false;
76 private boolean printReturned = false;
77 private PrintRequestAttributeSet reqAttrSet = null;
78 private PrintJobAttributeSet jobAttrSet = null;
79 private PrinterJob job;
80 private Doc doc;
81 /* these variables used globally to store reference to the print
82 * data retrieved as a stream. On completion these are always closed
83 * if non-null.
84 */
85 private InputStream instream = null;
86 private Reader reader = null;
87
88 /* default values overridden by those extracted from the attributes */
89 private String jobName = "Java Printing";
90 private int copies = 1;
91 private MediaSize mediaSize = MediaSize.NA.LETTER;
100 }
101
102 public PrintJobAttributeSet getAttributes() {
103 synchronized (this) {
104 if (jobAttrSet == null) {
105 /* just return an empty set until the job is submitted */
106 PrintJobAttributeSet jobSet = new HashPrintJobAttributeSet();
107 return AttributeSetUtilities.unmodifiableView(jobSet);
108 } else {
109 return jobAttrSet;
110 }
111 }
112 }
113
114 public void addPrintJobListener(PrintJobListener listener) {
115 synchronized (this) {
116 if (listener == null) {
117 return;
118 }
119 if (jobListeners == null) {
120 jobListeners = new Vector<>();
121 }
122 jobListeners.add(listener);
123 }
124 }
125
126 public void removePrintJobListener(PrintJobListener listener) {
127 synchronized (this) {
128 if (listener == null || jobListeners == null ) {
129 return;
130 }
131 jobListeners.remove(listener);
132 if (jobListeners.isEmpty()) {
133 jobListeners = null;
134 }
135 }
136 }
137
138 /* Closes any stream already retrieved for the data.
139 * We want to avoid unnecessarily asking the Doc to create a stream only
140 * to get a reference in order to close it because the job failed.
174 else if (data instanceof InputStream) {
175 try {
176 ((InputStream)data).close();
177 } catch (IOException e) {
178 }
179 }
180 else if (data instanceof Reader) {
181 try {
182 ((Reader)data).close();
183 } catch (IOException e) {
184 }
185 }
186 }
187
188 private void notifyEvent(int reason) {
189 synchronized (this) {
190 if (jobListeners != null) {
191 PrintJobListener listener;
192 PrintJobEvent event = new PrintJobEvent(this, reason);
193 for (int i = 0; i < jobListeners.size(); i++) {
194 listener = jobListeners.elementAt(i);
195 switch (reason) {
196
197 case PrintJobEvent.JOB_CANCELED :
198 listener.printJobCanceled(event);
199 break;
200
201 case PrintJobEvent.JOB_FAILED :
202 listener.printJobFailed(event);
203 break;
204
205 case PrintJobEvent.DATA_TRANSFER_COMPLETE :
206 listener.printDataTransferCompleted(event);
207 break;
208
209 case PrintJobEvent.NO_MORE_EVENTS :
210 listener.printJobNoMoreEvents(event);
211 break;
212
213 case PrintJobEvent.JOB_COMPLETE :
214 listener.printJobCompleted(event);
215 break;
216
217 default:
218 break;
219 }
220 }
221 }
222 }
223 }
224
225 public void addPrintJobAttributeListener(
226 PrintJobAttributeListener listener,
227 PrintJobAttributeSet attributes) {
228 synchronized (this) {
229 if (listener == null) {
230 return;
231 }
232 if (attrListeners == null) {
233 attrListeners = new Vector<>();
234 listenedAttributeSets = new Vector<>();
235 }
236 attrListeners.add(listener);
237 if (attributes == null) {
238 attributes = new HashPrintJobAttributeSet();
239 }
240 listenedAttributeSets.add(attributes);
241 }
242 }
243
244 public void removePrintJobAttributeListener(
245 PrintJobAttributeListener listener) {
246 synchronized (this) {
247 if (listener == null || attrListeners == null ) {
248 return;
249 }
250 int index = attrListeners.indexOf(listener);
251 if (index == -1) {
252 return;
253 } else {
254 attrListeners.remove(index);
477 } else {
478 String str = "JPS Job:" + doc;
479 try {
480 Object printData = doc.getPrintData();
481 if (printData instanceof URL) {
482 str = ((URL)(doc.getPrintData())).toString();
483 }
484 } catch (IOException e) {
485 }
486 jobName = new JobName(str, null);
487 jobAttrSet.add(jobName);
488 }
489 }
490
491 jobAttrSet = AttributeSetUtilities.unmodifiableView(jobAttrSet);
492 }
493
494 private void getAttributeValues(DocFlavor flavor) throws PrintException {
495
496 Attribute attr;
497 Class<? extends Attribute> category;
498
499 if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
500 fidelity = true;
501 } else {
502 fidelity = false;
503 }
504
505 Attribute []attrs = reqAttrSet.toArray();
506 for (int i=0; i<attrs.length; i++) {
507 attr = attrs[i];
508 category = attr.getCategory();
509 if (fidelity == true) {
510 if (!service.isAttributeCategorySupported(category)) {
511 notifyEvent(PrintJobEvent.JOB_FAILED);
512 throw new PrintJobAttributeException(
513 "unsupported category: " + category, category, null);
514 } else if
515 (!service.isAttributeValueSupported(attr, flavor, null)) {
516 notifyEvent(PrintJobEvent.JOB_FAILED);
517 throw new PrintJobAttributeException(
|