KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > print > PrinterRegistryContext


1 /**
2  * $Id: PrinterRegistryContext.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.print;
30
31 import com.idaremedia.antx.AntX;
32 import com.idaremedia.antx.FixtureAdministrator;
33 import com.idaremedia.antx.FixtureOverlays;
34 import com.idaremedia.antx.FixtureIds;
35 import com.idaremedia.antx.Iteration;
36 import com.idaremedia.antx.apis.ProblemHandler;
37
38 /**
39  * Manages <nobr>{@linkplain FixtureOverlays FixtureOverlays}-based</nobr> PrinterRegistrys
40  * for the active thread. PrintTasks and friends should use this class instead of directly
41  * accessing FixtureOverlays stacks. Unless {@linkplain ManagePrintersTask explicitly
42  * initialized}, the default printer registry is an empty (readonly) registry.
43  *
44  * @since JWare/AntX 0.2
45  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
46  * @version 0.5
47  * @.safety guarded
48  * @.group impl,infra
49  * @see ManagePrintersTask
50  **/

51
52 public final class PrinterRegistryContext implements FixtureAdministrator
53 {
54     /**
55      * The iteration category for all PrinterRegistryContext's information.
56      **/

57     public static final String JavaDoc FXID= FixtureIds.PRINTER_REGISTRY;
58
59
60
61     /**
62      * Returns the current thread's PrinterRegistry instance if one exists.
63      * Returns <i>null</i> if no registry installed for executing thread.
64      **/

65     public static PrinterRegistry getPrinterRegistry()
66     {
67         return (PrinterRegistry)FixtureOverlays.getContextInstance().nearest(FXID);
68     }
69
70
71
72     /**
73      * Helper to {@linkplain #getPrinterRegistry getPrinterRegistry} that returns
74      * the {@linkplain #getDefaultPrinterRegistry default printer registry} if the
75      * current thread has no explicit configuration. Never returns <i>null</i>.
76      **/

77     public static PrinterRegistry getPrinterRegistryNoNull()
78     {
79         PrinterRegistry pr = getPrinterRegistry();
80         return (pr!=null) ? pr : getDefaultPrinterRegistry();
81     }
82
83
84
85     /**
86      * Installs a new PrinterRegistry instance for the current thread. This
87      * printer registry becomes the active (and only visible) printer registry
88      * until it is unwound or another printer registry is installed.
89      * @param pr the new printer registry (non-null)
90      * @param noInstallHandler [optional] callback if cannot install registry
91      * @return previous string manager if any (can be null)
92      * @throws BuildException if incoming configuration already on iteration stack
93      **/

94     public static PrinterRegistry installPrinterRegistry(PrinterRegistry pr,
95                                                          ProblemHandler noInstallHandler)
96     {
97         if (pr==null) {
98             throw new IllegalArgumentException JavaDoc
99                 ("PrinterRegistryContext- Cannot install NULL");
100         }
101
102         String JavaDoc whoami = AntX.uistrs().dget("task.print.whoami","PrinterRegistry");
103         return (PrinterRegistry)FixtureOverlays.installIfNot
104                                     (FXID, pr, noInstallHandler, whoami);
105     }
106
107
108
109     /**
110      * Removes the most recently installed PrinterRegistry for the current
111      * thread. The previous installation is reactivated, or if this was
112      * the only printer registry, the current thread's printer registry is
113      * undefined (should use default if necessary).
114      * @param noUninstallHandler [optional] handler used to notify in event of
115      * already unwound stack
116      **/

117     public static void unwindPrinterRegistry(ProblemHandler noUninstallHandler)
118     {
119         String JavaDoc whoami = AntX.uistrs().dget("task.print.whoami","PrinterRegistry");
120         FixtureOverlays.uninstallIfIs(FXID,noUninstallHandler,whoami);
121     }
122
123
124
125     /**
126      * Returns the default PrinterRegistry used by this context. Never returns
127      * <i>null</i>.
128      * @see #setDefaultPrinterRegistry
129      * @see #getEmptyPrinterRegistry
130      **/

131     public static PrinterRegistry getDefaultPrinterRegistry()
132     {
133         PrinterRegistry pr = (PrinterRegistry)Iteration.getProperty(FXID);
134         return (pr==null) ? sm_EmptyRegistry : pr;
135     }
136
137
138
139     /**
140      * Initializes the default PrinterRegistry returned by this context. Note
141      * that your application does have to call this method to setup an empty
142      * default registry. Contexts will automatically us an empty registry if
143      * no default registry was installed. The new registry should not
144      * be set to a printer registry that already exists on a thread's stack.
145      * This method <em>replaces</em> any pre-existing default registry-- the
146      * current setting is lost unless the caller maintains it.
147      * @param pr new default registry
148      * @return existing default registry or <i>null</i> if none set
149      * @.safety guarded
150      **/

151     public static PrinterRegistry setDefaultPrinterRegistry(PrinterRegistry pr)
152     {
153         if (pr==null) {
154             throw new IllegalArgumentException JavaDoc
155                 (AntX.uistrs().get("print.null.registry"));
156         }
157         return (PrinterRegistry)Iteration.setProperty(FXID,pr);
158     }
159
160
161
162     /**
163      * Resets the default PrinterRegistry to the empty placeholder.
164      * @.safety guarded
165      * @since JWare/AntX 0.5
166      **/

167     public static final PrinterRegistry unsetDefaultPrinterRegistry()
168     {
169         return (PrinterRegistry)Iteration.removeProperty(FXID);
170     }
171
172
173
174     /**
175      * Returns this context's immutable empty printer registry. Never
176      * returns <i>null</i>.
177      **/

178     public static PrinterRegistry getEmptyPrinterRegistry()
179     {
180         return sm_EmptyRegistry;
181     }
182
183
184
185     /** Disallow; only static helpers. **/
186     private PrinterRegistryContext()
187     { }
188
189
190     /** Controlling the default printer registry. **/
191     private static final PrinterRegistry sm_EmptyRegistry = new PrinterRegistry(true);
192 }
193
194 /* end-of-PrinterRegistryContext.java */
195
Popular Tags