001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.awt.print;
019:
020: import java.awt.Dialog;
021: import java.awt.Frame;
022: import java.awt.GraphicsEnvironment;
023: import java.awt.HeadlessException;
024: import java.awt.KeyboardFocusManager;
025: import java.awt.Rectangle;
026: import java.awt.Window;
027:
028: import javax.print.CancelablePrintJob;
029: import javax.print.Doc;
030: import javax.print.DocFlavor;
031: import javax.print.DocPrintJob;
032: import javax.print.PrintException;
033: import javax.print.PrintService;
034: import javax.print.PrintServiceLookup;
035: import javax.print.ServiceUI;
036: import javax.print.SimpleDoc;
037: import javax.print.attribute.Attribute;
038: import javax.print.attribute.HashPrintRequestAttributeSet;
039: import javax.print.attribute.PrintRequestAttributeSet;
040: import javax.print.attribute.Size2DSyntax;
041: import javax.print.attribute.standard.Copies;
042: import javax.print.attribute.standard.JobName;
043: import javax.print.attribute.standard.Media;
044: import javax.print.attribute.standard.MediaPrintableArea;
045: import javax.print.attribute.standard.MediaSize;
046: import javax.print.attribute.standard.MediaSizeName;
047: import javax.print.attribute.standard.OrientationRequested;
048: import javax.print.attribute.standard.RequestingUserName;
049:
050: import org.apache.harmony.awt.internal.nls.Messages;
051: import org.apache.harmony.x.print.ServiceUIDialog;
052:
053: class PrinterJobImpl extends PrinterJob {
054:
055: private final PrintRequestAttributeSet attributes;
056:
057: private Doc doc;
058: private PageFormat format;
059: private DocPrintJob printJob;
060: private PrintService printService;
061: private boolean isCanceled;
062:
063: PrinterJobImpl() {
064: attributes = new HashPrintRequestAttributeSet();
065: }
066:
067: @Override
068: public PrintService getPrintService() {
069: if (printService == null) {
070: printService = PrintServiceLookup
071: .lookupDefaultPrintService();
072: printService = isServiceSupported(printService) ? printService
073: : null;
074: }
075: return printService;
076: }
077:
078: @Override
079: public void setPrintService(final PrintService printservice)
080: throws PrinterException {
081: if (!isServiceSupported(printservice)) {
082: // DocFlavor.SERVICE_FORMATTED is not supported
083: throw new PrinterException(Messages.getString(
084: "awt.5D", "DocFlavor.SERVICE_FORMATTED")); //$NON-NLS-1$ //$NON-NLS-2$
085: }
086:
087: this .printService = printservice;
088: }
089:
090: @Override
091: public void cancel() {
092: if (printJob instanceof CancelablePrintJob) {
093: try {
094: ((CancelablePrintJob) printJob).cancel();
095: isCanceled = true;
096: } catch (PrintException e) {
097: // ignore
098: }
099: }
100: }
101:
102: @Override
103: public PageFormat defaultPage(final PageFormat page) {
104: if (page != null) {
105: format = (PageFormat) page.clone();
106: attributes.addAll(formatToAttrs(format));
107: }
108:
109: return format;
110: }
111:
112: @Override
113: public int getCopies() {
114: final Copies c = (Copies) attributes.get(Copies.class);
115: return c != null ? c.getValue() : 1;
116: }
117:
118: @Override
119: public String getJobName() {
120: final JobName name = (JobName) attributes.get(JobName.class);
121: return name != null ? name.getValue() : null;
122: }
123:
124: @Override
125: public String getUserName() {
126: final RequestingUserName name = (RequestingUserName) attributes
127: .get(RequestingUserName.class);
128: return name != null ? name.getValue() : null;
129: }
130:
131: @Override
132: public boolean isCancelled() {
133: return isCanceled;
134: }
135:
136: @Override
137: public PageFormat pageDialog(
138: final PrintRequestAttributeSet attributes)
139: throws HeadlessException {
140: checkHeadless();
141:
142: final Window wnd = KeyboardFocusManager
143: .getCurrentKeyboardFocusManager().getActiveWindow();
144: final Window owner = (((wnd instanceof Dialog) || (wnd instanceof Frame)) ? wnd
145: : new Frame());
146: final Rectangle screen = GraphicsEnvironment
147: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
148: .getDefaultConfiguration().getBounds();
149: final ServiceUIDialog dialog = new ServiceUIDialog(null,
150: screen.width / 3, screen.height / 3, getPrintService(),
151: attributes, owner);
152:
153: dialog.show();
154:
155: if (owner != wnd) {
156: owner.dispose();
157: }
158:
159: if (attributes != dialog.getAttributes()) {
160: attributes.addAll(dialog.getAttributes());
161: }
162:
163: return (dialog.getResult() == ServiceUIDialog.APPROVE_PRINT) ? attrsToFormat(attributes)
164: : null;
165: }
166:
167: @Override
168: public PageFormat pageDialog(final PageFormat page)
169: throws HeadlessException {
170: final PageFormat format = pageDialog(formatToAttrs(page));
171: return format != null ? format : page;
172: }
173:
174: @Override
175: public void print() throws PrinterException {
176: print(attributes);
177: }
178:
179: @Override
180: public void print(final PrintRequestAttributeSet attributes)
181: throws PrinterException {
182: if (doc == null) {
183: // Neither Printable nor Pageable specified
184: throw new PrinterException(Messages.getString("awt.29A")); //$NON-NLS-1$
185: }
186:
187: final PrintRequestAttributeSet attrs = mergeAttrs(attributes);
188: final PrintService service = getPrintService();
189:
190: if (service == null) {
191: // Printer not found
192: throw new PrinterException(Messages.getString("awt.29A")); //$NON-NLS-1$
193: }
194:
195: try {
196: printJob = service.createPrintJob();
197: printJob.print(doc, attrs);
198: } catch (final PrintException ex) {
199: final PrinterException pe = new PrinterException();
200: pe.initCause(ex);
201: throw pe;
202: }
203: }
204:
205: @Override
206: public boolean printDialog() throws HeadlessException {
207: return printDialog(attributes);
208: }
209:
210: @Override
211: public boolean printDialog(final PrintRequestAttributeSet attributes)
212: throws HeadlessException {
213: checkHeadless();
214:
215: if (attributes == null) {
216: throw new NullPointerException();
217: }
218:
219: final PrintRequestAttributeSet attrs = mergeAttrs(attributes);
220: final Rectangle screen = GraphicsEnvironment
221: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
222: .getDefaultConfiguration().getBounds();
223: final PrintService newService = ServiceUI.printDialog(null,
224: screen.width / 3, screen.height / 3,
225: lookupPrintServices(), getPrintService(),
226: DocFlavor.SERVICE_FORMATTED.PRINTABLE, attrs);
227:
228: if (newService != null) {
229: printService = newService;
230: }
231:
232: return newService != null;
233: }
234:
235: @Override
236: public void setCopies(final int copies) {
237: attributes.add(new Copies(copies));
238: }
239:
240: @Override
241: public void setJobName(final String jobName) {
242: attributes.add(new JobName(jobName, null));
243: }
244:
245: @Override
246: public void setPageable(final Pageable document)
247: throws NullPointerException {
248: doc = new SimpleDoc(document,
249: DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
250: }
251:
252: @Override
253: public void setPrintable(final Printable painter) {
254: setPrintable(painter, null);
255: }
256:
257: @Override
258: public void setPrintable(final Printable painter,
259: final PageFormat format) {
260: if (format != null) {
261: doc = new SimpleDoc(new PrintableWrapper(painter, format),
262: DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
263: } else {
264: doc = new SimpleDoc(new PrintableWrapper(painter,
265: this .format), DocFlavor.SERVICE_FORMATTED.PAGEABLE,
266: null);
267: }
268: }
269:
270: @Override
271: public PageFormat validatePage(final PageFormat page) {
272: return page;
273: }
274:
275: private static void checkHeadless() throws HeadlessException {
276: if (GraphicsEnvironment.isHeadless()) {
277: throw new HeadlessException();
278: }
279: }
280:
281: private static boolean isServiceSupported(final PrintService service) {
282: return (service != null)
283: && service
284: .isDocFlavorSupported(DocFlavor.SERVICE_FORMATTED.PAGEABLE);
285: }
286:
287: private static PrintRequestAttributeSet formatToAttrs(
288: final PageFormat format) {
289: final PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
290:
291: if (format != null) {
292: attributes.add(new MediaPrintableArea((float) (format
293: .getImageableX() / 72.0), (float) (format
294: .getImageableY() / 72.0), (float) (format
295: .getWidth() / 72.0),
296: (float) (format.getHeight() / 72.0),
297: Size2DSyntax.INCH));
298:
299: switch (format.getOrientation()) {
300: case PageFormat.PORTRAIT:
301: attributes.add(OrientationRequested.PORTRAIT);
302: break;
303: case PageFormat.LANDSCAPE:
304: attributes.add(OrientationRequested.LANDSCAPE);
305: break;
306: case PageFormat.REVERSE_LANDSCAPE:
307: attributes.add(OrientationRequested.REVERSE_LANDSCAPE);
308: break;
309: }
310: }
311:
312: return attributes;
313: }
314:
315: private static PageFormat attrsToFormat(
316: final PrintRequestAttributeSet attributes) {
317: if (attributes == null) {
318: return new PageFormat();
319: }
320:
321: final PageFormat format = new PageFormat();
322: final Paper paper = new Paper();
323: final OrientationRequested orient = (OrientationRequested) attributes
324: .get(OrientationRequested.class);
325: final MediaSize size = attributes.containsKey(Media.class) ? MediaSize
326: .getMediaSizeForName((MediaSizeName) attributes
327: .get(Media.class))
328: : (MediaSize) attributes.get(MediaSize.class);
329: final MediaPrintableArea area = (MediaPrintableArea) attributes
330: .get(MediaPrintableArea.class);
331:
332: if (orient != null) {
333: if (orient.equals(OrientationRequested.LANDSCAPE)) {
334: format.setOrientation(PageFormat.LANDSCAPE);
335: } else if (orient
336: .equals(OrientationRequested.REVERSE_LANDSCAPE)) {
337: format.setOrientation(PageFormat.REVERSE_LANDSCAPE);
338: }
339: }
340:
341: if (size != null) {
342: paper.setSize(size.getX(Size2DSyntax.INCH) * 72.0, size
343: .getY(Size2DSyntax.INCH) * 72.0);
344: }
345:
346: if (area != null) {
347: paper.setImageableArea(area.getX(Size2DSyntax.INCH) * 72.0,
348: area.getY(Size2DSyntax.INCH) * 72.0, area
349: .getWidth(Size2DSyntax.INCH) * 72.0, area
350: .getHeight(Size2DSyntax.INCH) * 72.0);
351: }
352:
353: format.setPaper(paper);
354:
355: return format;
356: }
357:
358: private PrintRequestAttributeSet mergeAttrs(
359: final PrintRequestAttributeSet dest) {
360: if (dest == attributes) {
361: return attributes;
362: }
363:
364: final PrintRequestAttributeSet attrs = (dest != null) ? dest
365: : new HashPrintRequestAttributeSet();
366:
367: for (Attribute attr : attributes.toArray()) {
368: if ((attr != null)
369: && !attrs.containsKey(attr.getCategory())) {
370: attrs.add(attr);
371: }
372: }
373: return attrs;
374: }
375:
376: private static class PrintableWrapper implements Pageable {
377: private final Printable p;
378: private final PageFormat format;
379:
380: PrintableWrapper(final Printable p, final PageFormat format) {
381: this .p = p;
382: this .format = (format != null) ? format : new PageFormat();
383: }
384:
385: public int getNumberOfPages() {
386: return Pageable.UNKNOWN_NUMBER_OF_PAGES;
387: }
388:
389: public PageFormat getPageFormat(int pageIndex)
390: throws IndexOutOfBoundsException {
391: return format;
392: }
393:
394: public Printable getPrintable(int pageIndex)
395: throws IndexOutOfBoundsException {
396: return p;
397: }
398: }
399: }
|