001 /* PrinterJob.java -- This job is the printer control class
002 Copyright (C) 1999, 2004, 2005, 2006 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.awt.print;
040
041 import gnu.java.awt.print.JavaPrinterJob;
042
043 import java.awt.HeadlessException;
044 import javax.print.PrintService;
045 import javax.print.PrintServiceLookup;
046 import javax.print.DocFlavor;
047 import javax.print.StreamPrintServiceFactory;
048 import javax.print.attribute.PrintRequestAttributeSet;
049
050 /**
051 * This class controls printing.
052 *
053 * @author Aaron M. Renn (arenn@urbanophile.com)
054 */
055 public abstract class PrinterJob
056 {
057 // The print service associated with this job
058 private PrintService printer = null;
059
060 /**
061 * Creates a new print job.
062 *
063 * @return A <code>PrinterJob</code> object for the newly created print job.
064 */
065 public static PrinterJob getPrinterJob()
066 {
067 return new JavaPrinterJob();
068 }
069
070 /**
071 * Initializes a new instance of <code>PrinterJob</code>.
072 */
073 public PrinterJob()
074 {
075 }
076
077 /**
078 * Returns the number of copies to be printed.
079 *
080 * @return The number of copies to be printed.
081 */
082 public abstract int getCopies();
083
084 /**
085 * Sets the number of copies to be printed.
086 *
087 * @param copies The number of copies to be printed.
088 */
089 public abstract void setCopies(int copies);
090
091 /**
092 * Returns the name of the print job.
093 *
094 * @return The name of the print job.
095 */
096 public abstract String getJobName();
097
098 /**
099 * Sets the name of the print job.
100 *
101 * @param job_name The name of the print job.
102 */
103 public abstract void setJobName(String job_name);
104
105 /**
106 * Returns the printing user name.
107 *
108 * @return The printing username.
109 */
110 public abstract String getUserName();
111
112 /**
113 * Cancels an in progress print job.
114 */
115 public abstract void cancel();
116
117 /**
118 * Tests whether or not this job has been cancelled.
119 *
120 * @return <code>true</code> if this job has been cancelled, <code>false</code>
121 * otherwise.
122 */
123 public abstract boolean isCancelled();
124
125 /**
126 * Returns an instance of the default page which will have the default
127 * paper and orientation.
128 *
129 * @return A default instance of <code>PageFormat</code>.
130 */
131 public PageFormat defaultPage()
132 {
133 return new PageFormat();
134 }
135
136 /**
137 * Clones the specified <code>PageFormat</code> object then alters the
138 * clone so that it represents the default page format.
139 *
140 * @param page_format The <code>PageFormat</code> to clone.
141 *
142 * @return A new default page format.
143 */
144 public abstract PageFormat defaultPage(PageFormat page_format);
145
146 /**
147 * Displays a dialog box to the user which allows the page format
148 * attributes to be modified.
149 *
150 * @param page_format The <code>PageFormat</code> object to modify.
151 *
152 * @return The modified <code>PageFormat</code>.
153 */
154 public abstract PageFormat pageDialog(PageFormat page_format)
155 throws HeadlessException;
156
157 /**
158 * @since 1.4
159 */
160 public PageFormat pageDialog(PrintRequestAttributeSet attributes)
161 throws HeadlessException
162 {
163 // FIXME: Implement this for real.
164 return pageDialog((PageFormat) null);
165 }
166
167 /**
168 * Prints the pages.
169 */
170 public abstract void print () throws PrinterException;
171
172 /**
173 * Prints the page with given attributes.
174 */
175 public void print (PrintRequestAttributeSet attributes)
176 throws PrinterException
177 {
178 print ();
179 }
180
181 /**
182 * Displays a dialog box to the user which allows the print job
183 * attributes to be modified.
184 *
185 * @return <code>false</code> if the user cancels the dialog box,
186 * <code>true</code> otherwise.
187 */
188 public abstract boolean printDialog()
189 throws HeadlessException;
190
191 /**
192 * Displays a dialog box to the user which allows the print job
193 * attributes to be modified.
194 *
195 * @return <code>false</code> if the user cancels the dialog box,
196 * <code>true</code> otherwise.
197 */
198 public boolean printDialog(PrintRequestAttributeSet attributes)
199 throws HeadlessException
200 {
201 // FIXME: Implement this for real.
202 return printDialog();
203 }
204
205 /**
206 * This sets the pages that are to be printed.
207 *
208 * @param pageable The pages to be printed, which may not be <code>null</code>.
209 */
210 public abstract void setPageable(Pageable pageable);
211
212 /**
213 * Sets this specified <code>Printable</code> as the one to use for
214 * rendering the pages on the print device.
215 *
216 * @param printable The <code>Printable</code> for the print job.
217 */
218 public abstract void setPrintable(Printable printable);
219
220 /**
221 * Sets the <code>Printable</code> and the page format for the pages
222 * to be printed.
223 *
224 * @param printable The <code>Printable</code> for the print job.
225 * @param page_format The <code>PageFormat</code> for the print job.
226 */
227 public abstract void setPrintable(Printable printable, PageFormat page_format);
228
229 /**
230 * Makes any alterations to the specified <code>PageFormat</code>
231 * necessary to make it work with the current printer. The alterations
232 * are made to a clone of the input object, which is then returned.
233 *
234 * @param page_format The <code>PageFormat</code> to validate.
235 *
236 * @return The validated <code>PageFormat</code>.
237 */
238 public abstract PageFormat validatePage(PageFormat page_format);
239
240 /**
241 * Find and return 2D image print services.
242 *
243 * This is the same as calling PrintServiceLookup.lookupPrintServices()
244 * with Pageable service-specified DocFlavor.
245 * @return Array of PrintService objects, could be empty.
246 * @since 1.4
247 */
248 public static PrintService[] lookupPrintServices()
249 {
250 return PrintServiceLookup.lookupPrintServices
251 (
252 new DocFlavor("application/x-java-jvm-local-objectref",
253 "java.awt.print.Pageable"),
254 null);
255 }
256
257 /**
258 * Find and return 2D image stream print services.
259 *
260 * This is the same as calling
261 * StreamPrintServiceFactory.lookupStreamPrintServices()
262 * with Pageable service-specified DocFlavor.
263 * @param mimeType The output format mime type, or null for any type.
264 * @return Array of stream print services, could be empty.
265 * @since 1.4
266 */
267 public static StreamPrintServiceFactory[]
268 lookupStreamPrintServices(String mimeType)
269 {
270 return StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
271 DocFlavor.SERVICE_FORMATTED.PAGEABLE, mimeType);
272 }
273
274 /**
275 * Return the printer for this job. If print services aren't supported by
276 * the subclass, returns null.
277 *
278 * @return The associated PrintService.
279 * @since 1.4
280 */
281 public PrintService getPrintService()
282 {
283 return printer;
284 }
285
286 /**
287 * Change the printer for this print job to service. Subclasses that
288 * support setting the print service override this method. Throws
289 * PrinterException when the class doesn't support setting the printer,
290 * the service doesn't support Pageable or Printable interfaces for 2D
291 * print output.
292 * @param service The new printer to use.
293 * @throws PrinterException if service is not valid.
294 */
295 public void setPrintService(PrintService service)
296 throws PrinterException
297 {
298 printer = service;
299 }
300 }