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: package org.apache.harmony.x.print;
018:
019: import javax.print.attribute.Attribute;
020: import javax.print.attribute.AttributeSet;
021: import javax.print.attribute.ResolutionSyntax;
022: import javax.print.attribute.Size2DSyntax;
023: import javax.print.attribute.standard.Chromaticity;
024: import javax.print.attribute.standard.Copies;
025: import javax.print.attribute.standard.Media;
026: import javax.print.attribute.standard.MediaSize;
027: import javax.print.attribute.standard.MediaSizeName;
028: import javax.print.attribute.standard.OrientationRequested;
029: import javax.print.attribute.standard.PrintQuality;
030: import javax.print.attribute.standard.PrinterResolution;
031: import javax.print.attribute.standard.SheetCollate;
032: import javax.print.attribute.standard.Sides;
033:
034: /**
035: * Wrapper for the DEVMODE native structure.
036: */
037: public class DevmodeStructWrapper {
038:
039: // DmField flags
040: public static final int DM_ORIENTATION = 0x00000001;
041: public static final int DM_PAPERSIZE = 0x00000002;
042: public static final int DM_PAPERLENGTH = 0x00000004;
043: public static final int DM_PAPERWIDTH = 0x00000008;
044: public static final int DM_SCALE = 0x00000010;
045: public static final int DM_COPIES = 0x00000100;
046: public static final int DM_DEFAULTSOURCE = 0x00000200;
047: public static final int DM_PRINTQUALITY = 0x00000400;
048: public static final int DM_COLOR = 0x00000800;
049: public static final int DM_DUPLEX = 0x00001000;
050: public static final int DM_YRESOLUTION = 0x00002000;
051: public static final int DM_TTOPTION = 0x00004000;
052: public static final int DM_COLLATE = 0x00008000;
053:
054: // Orientation fields
055: public static final short DMORIENT_PORTRAIT = 1;
056: public static final short DMORIENT_LANDSCAPE = 2;
057:
058: // Print quality predefined values
059: public static final short DMRES_HIGH = -4;
060: public static final short DMRES_MEDIUM = -3;
061: public static final short DMRES_DRAFT = -1;
062:
063: // Sides
064: public static final short DMDUP_SIMPLEX = 1;
065: public static final short DMDUP_VERTICAL = 2;
066: public static final short DMDUP_HORIZONTAL = 3;
067:
068: // Collate
069: public static final short DMCOLLATE_FALSE = 0;
070: public static final short DMCOLLATE_TRUE = 1;
071:
072: // Chromaticity
073: public static final short DMCOLOR_MONOCHROME = 1;
074: public static final short DMCOLOR_COLOR = 2;
075:
076: public long structPtr;
077:
078: public DevmodeStructWrapper(final long structPtr) {
079: this .structPtr = structPtr;
080: }
081:
082: public long getStructPtr() {
083: return structPtr;
084: }
085:
086: public String getDmDeviceName() {
087: return getDmDeviceName(structPtr);
088: }
089:
090: public long getDmFields() {
091: return getDmFields(structPtr);
092: }
093:
094: public OrientationRequested getOrientation() {
095: return getDmOrientation(structPtr) == DMORIENT_LANDSCAPE ? OrientationRequested.LANDSCAPE
096: : OrientationRequested.PORTRAIT;
097: }
098:
099: public void setOrientation(final OrientationRequested orientation) {
100: if (OrientationRequested.PORTRAIT.equals(orientation)) {
101: setDmOrientation(structPtr, DMORIENT_PORTRAIT);
102: } else if (OrientationRequested.LANDSCAPE.equals(orientation)
103: || OrientationRequested.REVERSE_LANDSCAPE
104: .equals(orientation)) {
105: setDmOrientation(structPtr, DMORIENT_LANDSCAPE);
106: }
107: }
108:
109: public Paper getPaper() {
110: final short size = getDmPaperSize(structPtr);
111: Paper p = StdPaper.getPaper(size);
112:
113: if (p == null) {
114: final long fields = getDmFields();
115:
116: if (((fields & DM_PAPERLENGTH) != 0)
117: && ((fields & DM_PAPERWIDTH) != 0)) {
118: p = new CustomPaper(size, new MediaSize(
119: getDmPaperWidth(structPtr) / 10,
120: getDmPaperLength(structPtr) / 10,
121: Size2DSyntax.MM));
122: }
123: }
124:
125: return p;
126: }
127:
128: public void setPaper(final Paper paper) {
129: if (paper != null) {
130: if (paper.getDmPaperSize() > 0) {
131: setDmPaperSize(structPtr, paper.getDmPaperSize());
132: } else {
133: setDmPaperWidth(structPtr, (short) (paper.getSize()
134: .getX(Size2DSyntax.MM) * 10));
135: setDmPaperLength(structPtr, (short) (paper.getSize()
136: .getY(Size2DSyntax.MM) * 10));
137: }
138: }
139: }
140:
141: public void setPaper(final MediaSize size) {
142: final Paper p = StdPaper.getPaper(size);
143: setPaper((p != null) ? p : new CustomPaper(0, size));
144: }
145:
146: public void setPaper(final MediaSizeName name) {
147: final Paper p = StdPaper.getPaper(name);
148: setPaper((p != null) ? p : new CustomPaper(0, MediaSize
149: .getMediaSizeForName(name)));
150: }
151:
152: public Copies getCopies() {
153: final short copies = getDmCopies(structPtr);
154: return copies > 0 ? new Copies(copies) : new Copies(1);
155: }
156:
157: public void setCopies(final Copies c) {
158: setDmCopies(structPtr, (short) c.getValue());
159: }
160:
161: public PrintQuality getPrintQuality() {
162: switch (getDmPrintQuality(structPtr)) {
163: case DMRES_HIGH:
164: return PrintQuality.HIGH;
165: case DMRES_DRAFT:
166: return PrintQuality.DRAFT;
167: default:
168: return PrintQuality.NORMAL;
169: }
170: }
171:
172: public void setPrintQuality(final PrintQuality quality) {
173: if (PrintQuality.NORMAL.equals(quality)) {
174: setDmPrintQuality(structPtr, DMRES_MEDIUM);
175: } else if (PrintQuality.HIGH.equals(quality)) {
176: setDmPrintQuality(structPtr, DMRES_HIGH);
177: } else if (PrintQuality.DRAFT.equals(quality)) {
178: setDmPrintQuality(structPtr, DMRES_DRAFT);
179: }
180: }
181:
182: public Sides getSides() {
183: switch (getDmDuplex(structPtr)) {
184: case DMDUP_VERTICAL:
185: return Sides.TWO_SIDED_LONG_EDGE;
186: case DMDUP_HORIZONTAL:
187: return Sides.TWO_SIDED_SHORT_EDGE;
188: default:
189: return Sides.ONE_SIDED;
190: }
191: }
192:
193: public void setSides(final Sides sides) {
194: if (Sides.ONE_SIDED.equals(sides)) {
195: setDmDuplex(structPtr, DMDUP_SIMPLEX);
196: } else if (Sides.TWO_SIDED_LONG_EDGE.equals(sides)) {
197: setDmDuplex(structPtr, DMDUP_VERTICAL);
198: } else if (Sides.TWO_SIDED_SHORT_EDGE.equals(sides)) {
199: setDmDuplex(structPtr, DMDUP_HORIZONTAL);
200: }
201: }
202:
203: public SheetCollate getCollate() {
204: return getDmCollate(structPtr) == DMCOLLATE_TRUE ? SheetCollate.COLLATED
205: : SheetCollate.UNCOLLATED;
206: }
207:
208: public void setCollate(final SheetCollate collate) {
209: if (SheetCollate.UNCOLLATED.equals(collate)) {
210: setDmCollate(structPtr, DMCOLLATE_FALSE);
211: } else if (SheetCollate.COLLATED.equals(collate)) {
212: setDmCollate(structPtr, DMCOLLATE_TRUE);
213: }
214: }
215:
216: public PrinterResolution getPrinterResolution() {
217: final int x = getDmPrintQuality(structPtr);
218: final int y = getDmYResolution(structPtr);
219:
220: if (y > 0) {
221: return new PrinterResolution(x > 0 ? x : y, y,
222: ResolutionSyntax.DPI);
223: }
224:
225: return null;
226: }
227:
228: public void setPrinterResolution(final PrinterResolution res) {
229: setDmPrintQuality(structPtr, (short) res
230: .getCrossFeedResolution(ResolutionSyntax.DPI));
231: setDmYResolution(structPtr, (short) res
232: .getFeedResolution(ResolutionSyntax.DPI));
233: }
234:
235: public Chromaticity getChromaticity() {
236: return getDmColor(structPtr) == DMCOLOR_COLOR ? Chromaticity.COLOR
237: : Chromaticity.MONOCHROME;
238: }
239:
240: public void setChromaticity(final Chromaticity chromaticity) {
241: if (Chromaticity.COLOR.equals(chromaticity)) {
242: setDmColor(structPtr, DMCOLOR_COLOR);
243: } else if (Chromaticity.MONOCHROME.equals(chromaticity)) {
244: setDmColor(structPtr, DMCOLOR_MONOCHROME);
245: }
246: }
247:
248: public void setAttribute(final Attribute attr) {
249: final Class<? extends Attribute> category = attr.getCategory();
250:
251: if (OrientationRequested.class.equals(category)) {
252: setOrientation((OrientationRequested) attr);
253: } else if (MediaSize.class.equals(category)) {
254: setPaper((MediaSize) attr);
255: } else if (Media.class.equals(category)) {
256: setPaper((MediaSizeName) attr);
257: } else if (Paper.class.equals(category)) {
258: setPaper((Paper) attr);
259: } else if (Copies.class.equals(category)) {
260: setCopies((Copies) attr);
261: } else if (PrintQuality.class.equals(category)) {
262: setPrintQuality((PrintQuality) attr);
263: } else if (Sides.class.equals(category)) {
264: setSides((Sides) attr);
265: } else if (SheetCollate.class.equals(category)) {
266: setCollate((SheetCollate) attr);
267: } else if (PrinterResolution.class.equals(category)) {
268: setPrinterResolution((PrinterResolution) attr);
269: } else if (Chromaticity.class.equals(category)) {
270: setChromaticity((Chromaticity) attr);
271: }
272: }
273:
274: public void setAttributes(final AttributeSet attrs) {
275: if (attrs != null) {
276: for (Attribute attr : attrs.toArray()) {
277: setAttribute(attr);
278: }
279: }
280: }
281:
282: public <T extends AttributeSet> T getAttributes(final T attrs) {
283: final long flags = getDmFields();
284: final Paper p = getPaper();
285: final PrinterResolution res = getPrinterResolution();
286:
287: if (p != null) {
288: attrs.add(p.getSize());
289: attrs.add(p.getSize().getMediaSizeName());
290: }
291: if (res != null) {
292: attrs.add(res);
293: }
294: if ((flags & DM_ORIENTATION) != 0) {
295: attrs.add(getOrientation());
296: }
297: if ((flags & DM_COPIES) != 0) {
298: attrs.add(getCopies());
299: }
300: if ((flags & DM_PRINTQUALITY) != 0) {
301: attrs.add(getPrintQuality());
302: }
303: if ((flags & DM_DUPLEX) != 0) {
304: attrs.add(getSides());
305: }
306: if ((flags & DM_COLLATE) != 0) {
307: attrs.add(getCollate());
308: }
309: if ((flags & DM_COLOR) != 0) {
310: attrs.add(getChromaticity());
311: }
312:
313: return attrs;
314: }
315:
316: // --------------------- Native functions --------------------------- //
317: public static native String getDmDeviceName(final long structPtr);
318:
319: public static native long getDmFields(final long structPtr);
320:
321: public static native short getDmOrientation(final long structPtr);
322:
323: public static native void setDmOrientation(final long structPtr,
324: final short orientation);
325:
326: public static native short getDmPaperSize(final long structPtr);
327:
328: public static native void setDmPaperSize(final long structPtr,
329: final short paperSize);
330:
331: public static native short getDmPaperLength(final long structPtr);
332:
333: public static native void setDmPaperLength(final long structPtr,
334: final short paperLength);
335:
336: public static native short getDmPaperWidth(final long structPtr);
337:
338: public static native void setDmPaperWidth(final long structPtr,
339: final short paperWidth);
340:
341: public static native short getDmScale(final long structPtr);
342:
343: public static native void setDmScale(final long structPtr,
344: final short scale);
345:
346: public static native short getDmCopies(final long structPtr);
347:
348: public static native void setDmCopies(final long structPtr,
349: final short copies);
350:
351: public static native short getDmDefaultSource(final long structPtr);
352:
353: public static native void setDmDefaultSource(final long structPtr,
354: final short defaultSource);
355:
356: public static native short getDmPrintQuality(final long structPtr);
357:
358: public static native void setDmPrintQuality(final long structPtr,
359: final short printQuality);
360:
361: public static native short getDmColor(final long structPtr);
362:
363: public static native void setDmColor(final long structPtr,
364: final short color);
365:
366: public static native short getDmDuplex(final long structPtr);
367:
368: public static native void setDmDuplex(final long structPtr,
369: final short duplex);
370:
371: public static native short getDmYResolution(final long structPtr);
372:
373: public static native void setDmYResolution(final long structPtr,
374: final short yResolution);
375:
376: public static native short getDmTTOption(final long structPtr);
377:
378: public static native void setDmTTOption(final long structPtr,
379: final short option);
380:
381: public static native short getDmCollate(final long structPtr);
382:
383: public static native void setDmCollate(final long structPtr,
384: final short collate);
385:
386: public static native void releaseStruct(final long structPtr);
387:
388: @Override
389: protected synchronized void finalize() throws Throwable {
390: if (structPtr > 0) {
391: releaseStruct(structPtr);
392: structPtr = 0;
393: }
394: }
395:
396: public static interface Paper extends Attribute {
397: public short getDmPaperSize();
398:
399: public MediaSize getSize();
400: }
401:
402: public static class CustomPaper implements Paper {
403: private static final long serialVersionUID = 3265772990664792005L;
404: final short dmPaperSize;
405: final MediaSize size;
406:
407: public CustomPaper(final int dmPaperSize, final MediaSize size) {
408: this .dmPaperSize = (short) dmPaperSize;
409: this .size = size;
410: }
411:
412: public short getDmPaperSize() {
413: return dmPaperSize;
414: }
415:
416: public MediaSize getSize() {
417: return size;
418: }
419:
420: public Class<? extends Attribute> getCategory() {
421: return Paper.class;
422: }
423:
424: public String getName() {
425: return size.getName();
426: }
427: }
428:
429: public static enum StdPaper implements Paper {
430: ISO_A2(66, MediaSize.ISO.A2), // DMPAPER_A2
431: ISO_A3(8, MediaSize.ISO.A3), // DMPAPER_A3
432: ISO_A4(9, MediaSize.ISO.A4), // DMPAPER_A4
433: ISO_A5(11, MediaSize.ISO.A5), // DMPAPER_A5
434: ISO_A6(70, MediaSize.ISO.A6), // DMPAPER_A6
435: NA_LETTER(1, MediaSize.NA.LETTER), // DMPAPER_LETTER
436: NA_LEGAL(5, MediaSize.NA.LEGAL), // DMPAPER_LEGAL
437: TABLOID(3, MediaSize.Other.TABLOID), // DMPAPER_TABLOID
438: NA_10x14(16, MediaSize.NA.NA_10x14_ENVELOPE), // DMPAPER_10X14
439: ISO_B4(12, MediaSize.ISO.B4), // DMPAPER_B4
440: JIS_B5(13, MediaSize.JIS.B5), // DMPAPER_B5
441: JIS_B6(88, MediaSize.JIS.B5), // DMPAPER_B6_JIS
442: JPC(43, MediaSize.Other.JAPANESE_POSTCARD), // DMPAPER_JAPANESE_POSTCARD
443: JPC_D(69, MediaSize.Other.JAPANESE_DOUBLE_POSTCARD); // DMPAPER_DBL_JAPANESE_POSTCARD
444:
445: final short dmPaperSize;
446: final MediaSize size;
447:
448: StdPaper(final int dmPaperSize, final MediaSize size) {
449: this .dmPaperSize = (short) dmPaperSize;
450: this .size = size;
451: }
452:
453: public static Paper getPaper(final short dmPaperSize) {
454: for (StdPaper p : values()) {
455: if (p.dmPaperSize == dmPaperSize) {
456: return p;
457: }
458: }
459:
460: return null;
461: }
462:
463: public static Paper getPaper(final MediaSize size) {
464: for (StdPaper p : values()) {
465: if (p.size.equals(size)) {
466: return p;
467: }
468: }
469:
470: return null;
471: }
472:
473: public static Paper getPaper(final MediaSizeName name) {
474: for (StdPaper p : values()) {
475: if (p.size.getMediaSizeName().equals(name)) {
476: return p;
477: }
478: }
479:
480: return null;
481: }
482:
483: public static MediaSize[] getSizes() {
484: final StdPaper[] paper = values();
485: final MediaSize[] names = new MediaSize[paper.length];
486:
487: for (int i = 0; i < paper.length; i++) {
488: names[i] = paper[i].size;
489: }
490:
491: return names;
492: }
493:
494: public static MediaSizeName[] getNames() {
495: final StdPaper[] paper = values();
496: final MediaSizeName[] names = new MediaSizeName[paper.length];
497:
498: for (int i = 0; i < paper.length; i++) {
499: names[i] = paper[i].size.getMediaSizeName();
500: }
501:
502: return names;
503: }
504:
505: public short getDmPaperSize() {
506: return dmPaperSize;
507: }
508:
509: public MediaSize getSize() {
510: return size;
511: }
512:
513: public Class<? extends Attribute> getCategory() {
514: return Paper.class;
515: }
516:
517: public String getName() {
518: return toString() + ": " + size; //$NON-NLS-1$
519: }
520: }
521: }
|