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 java.security.AccessController;
020: import java.security.PrivilegedAction;
021: import java.util.Vector;
022:
023: import javax.print.PrintException;
024: import javax.print.attribute.ResolutionSyntax;
025: import javax.print.attribute.Size2DSyntax;
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.PrinterResolution;
030: import javax.print.attribute.standard.PrinterState;
031: import javax.print.attribute.standard.QueuedJobCount;
032:
033: public class WinPrinterFactory {
034:
035: public static final int PRINTER_STATUS_PAUSED = 1;
036: public static final int PRINTER_STATUS_ERROR = 2;
037: public static final int PRINTER_STATUS_PENDING_DELETION = 4;
038: public static final int PRINTER_STATUS_PAPER_JAM = 8;
039: public static final int PRINTER_STATUS_PAPER_OUT = 0x10;
040: public static final int PRINTER_STATUS_MANUAL_FEED = 0x20;
041: public static final int PRINTER_STATUS_PAPER_PROBLEM = 0x40;
042: public static final int PRINTER_STATUS_OFFLINE = 0x80;
043: public static final int PRINTER_STATUS_IO_ACTIVE = 0x100;
044: public static final int PRINTER_STATUS_BUSY = 0x200;
045: public static final int PRINTER_STATUS_PRINTING = 0x400;
046: public static final int PRINTER_STATUS_OUTPUT_BIN_FULL = 0x800;
047: public static final int PRINTER_STATUS_NOT_AVAILABLE = 0x1000;
048: public static final int PRINTER_STATUS_WAITING = 0x2000;
049: public static final int PRINTER_STATUS_PROCESSING = 0x4000;
050: public static final int PRINTER_STATUS_INITIALIZING = 0x8000;
051: public static final int PRINTER_STATUS_WARMING_UP = 0x10000;
052: public static final int PRINTER_STATUS_TONER_LOW = 0x20000;
053: public static final int PRINTER_STATUS_NO_TONER = 0x40000;
054: public static final int PRINTER_STATUS_PAGE_PUNT = 0x80000;
055: public static final int PRINTER_STATUS_USER_INTERVENTION = 0x100000;
056: public static final int PRINTER_STATUS_OUT_OF_MEMORY = 0x200000;
057: public static final int PRINTER_STATUS_DOOR_OPEN = 0x400000;
058: public static final int PRINTER_STATUS_SERVER_UNKNOWN = 0x800000;
059: public static final int PRINTER_STATUS_POWER_SAVE = 0x1000000;
060:
061: static {
062: checkPrintJobAccess();
063: AccessController.doPrivileged(new PrivilegedAction<Object>() {
064: public Object run() {
065: System.loadLibrary("print"); //$NON-NLS-1$
066: return null;
067: }
068: });
069: }
070:
071: public static PrinterState getPrinterState(final long handle)
072: throws PrintException {
073: final long status = getPrinterStatus(handle);
074:
075: if ((status & (PRINTER_STATUS_PRINTING | PRINTER_STATUS_PROCESSING)) != 0) {
076: return PrinterState.PROCESSING;
077: } else if ((status & (PRINTER_STATUS_DOOR_OPEN
078: | PRINTER_STATUS_ERROR | PRINTER_STATUS_NO_TONER
079: | PRINTER_STATUS_NOT_AVAILABLE | PRINTER_STATUS_OFFLINE
080: | PRINTER_STATUS_OUT_OF_MEMORY
081: | PRINTER_STATUS_OUTPUT_BIN_FULL
082: | PRINTER_STATUS_PAPER_JAM | PRINTER_STATUS_PAPER_OUT
083: | PRINTER_STATUS_PAPER_PROBLEM | PRINTER_STATUS_USER_INTERVENTION)) != 0) {
084: return PrinterState.STOPPED;
085: } else if ((status & PRINTER_STATUS_SERVER_UNKNOWN) != 0) {
086: return PrinterState.UNKNOWN;
087: } else {
088: return PrinterState.IDLE;
089: }
090: }
091:
092: public static QueuedJobCount getQueuedJobCount(final long handle)
093: throws PrintException {
094: return new QueuedJobCount(getQueuedJobs(handle));
095: }
096:
097: public static MediaSizeName[] getSupportedMediaSizeNames(
098: final long handle) throws PrintException {
099: final MediaSizeName[] names;
100: final int[] sizes = getSupportedPaperSizes(handle);
101: final Vector<MediaSizeName> v = new Vector<MediaSizeName>(
102: sizes.length / 2);
103:
104: for (int i = 0; i < sizes.length; i += 2) {
105: if ((sizes[i] > 0) && (sizes[i + 1] > 0)) {
106: final MediaSizeName name = MediaSize.findMedia(
107: sizes[i] / 10, sizes[i + 1] / 10,
108: Size2DSyntax.MM);
109:
110: if ((name != null) && !v.contains(name)) {
111: v.add(name);
112: }
113: }
114: }
115:
116: names = new MediaSizeName[v.size()];
117: return v.toArray(names);
118: }
119:
120: public static MediaSize[] getSupportedMediaSizes(final long handle)
121: throws PrintException {
122: final MediaSizeName[] names = getSupportedMediaSizeNames(handle);
123: final MediaSize[] sizes = new MediaSize[names.length];
124:
125: for (int i = 0; i < names.length; i++) {
126: sizes[i] = MediaSize.getMediaSizeForName(names[i]);
127: }
128:
129: return sizes;
130: }
131:
132: public static OrientationRequested[] getSupportedOrientations(
133: final long handle) throws PrintException {
134: if (getLandscapeOrientationDegree(handle) == 270) {
135: return new OrientationRequested[] {
136: OrientationRequested.PORTRAIT,
137: OrientationRequested.REVERSE_LANDSCAPE };
138: }
139: return new OrientationRequested[] {
140: OrientationRequested.PORTRAIT,
141: OrientationRequested.LANDSCAPE };
142: }
143:
144: public static PrinterResolution[] getSupportedPrinterResolutions(
145: final long handle) throws PrintException {
146: final int[] res = getSupportedResolutions(handle);
147: final PrinterResolution[] resolutions = new PrinterResolution[res.length / 2];
148:
149: for (int i = 0; i < res.length; i += 2) {
150: resolutions[i / 2] = new PrinterResolution(res[i],
151: res[i + 1], ResolutionSyntax.DPI);
152: }
153: return resolutions;
154: }
155:
156: public static void checkPrintJobAccess() {
157: final SecurityManager mgr = System.getSecurityManager();
158:
159: if (mgr != null) {
160: mgr.checkPrintJobAccess();
161: }
162: }
163:
164: public static native String getDefaultPrinterName()
165: throws PrintException;
166:
167: public static native String[] getConnectedPrinterNames()
168: throws PrintException;
169:
170: public static native long getPrinterHandle(final String printerName)
171: throws PrintException;
172:
173: public static native void releasePrinterHandle(final long handle)
174: throws PrintException;
175:
176: /**
177: * Returns pointer to DEVMODEW structure
178: */
179: public static native long getPrinterProps(final String printerName,
180: final long handle) throws PrintException;
181:
182: public static native long getPrinterDC(final String printerName,
183: final long pDevMode) throws PrintException;
184:
185: public static native void releasePrinterDC(final long pdc)
186: throws PrintException;
187:
188: public static native int startDoc(final long pdc,
189: final String docName, final String filePath)
190: throws PrintException;
191:
192: public static native void endDoc(final long pdc)
193: throws PrintException;
194:
195: public static native void startPage(final long pdc)
196: throws PrintException;
197:
198: public static native void endPage(final long pdc)
199: throws PrintException;
200:
201: public static native int getQueuedJobs(final long handle)
202: throws PrintException;
203:
204: public static native int getPixelsPerInchX(final long pdc)
205: throws PrintException;
206:
207: public static native int getPixelsPerInchY(final long pdc)
208: throws PrintException;
209:
210: public static native int getPaperPhysicalWidth(final long pdc)
211: throws PrintException;
212:
213: public static native int getPaperPhysicalHeight(final long pdc)
214: throws PrintException;
215:
216: public static native long getPrinterStatus(final long handle)
217: throws PrintException;
218:
219: public static native boolean isColorPrintingSupported(
220: final long handle) throws PrintException;
221:
222: public static native boolean isCollatingSupported(final long handle)
223: throws PrintException;
224:
225: public static native boolean isDuplexSupported(final long handle)
226: throws PrintException;
227:
228: public static native int[] getSupportedPaperSizes(final long handle)
229: throws PrintException;
230:
231: public static native int[] getSupportedResolutions(final long handle)
232: throws PrintException;
233:
234: public static native int getLandscapeOrientationDegree(
235: final long handle) throws PrintException;
236:
237: public static native int getMaxNumberOfCopies(final long handle)
238: throws PrintException;
239:
240: public static native void cancelPrinterJob(final long handle,
241: final int jobId) throws PrintException;
242: }
|