KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > UISMContext


1 /**
2  * $Id: UISMContext.java 186 2007-03-16 13:42:35Z ssmc $
3  * Copyright 2002-2004 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;
30
31 import com.idaremedia.apis.EmptyStringManager;
32 import com.idaremedia.apis.UIStringManager;
33
34 import com.idaremedia.antx.apis.ProblemHandler;
35
36 /**
37  * Fixture administrator of iteration-based UIStringManagers for the active thread.
38  * The {@linkplain AssertableTask} base task uses a UISMContext to implements its
39  * resource bundle-based message support instead of directly accessing the
40  * FixtureOverlays stacks.
41  *
42  * @since JWare/AntX 0.2
43  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
44  * @version 0.5
45  * @.safety guarded
46  * @.group impl,infra
47  **/

48
49 public final class UISMContext implements FixtureCore, FixtureAdministrator
50 {
51     /**
52      * The fixture component id for all strings manager information.
53      **/

54     public static final String JavaDoc FXID= FixtureIds.MSGS_STRINGMANAGER;
55
56
57
58     /**
59      * Returns the current thread's nearest UIStringManager instance if
60      * one exists. Returns <i>null</i> if no string manager installed for
61      * executing thread.
62      **/

63     public static UIStringManager getStringManager()
64     {
65         return (UIStringManager)FixtureOverlays.getContextInstance().nearest(FXID);
66     }
67
68
69
70     /**
71      * Helper to {@linkplain #getStringManager getStringManager} that returns
72      * the {@linkplain #getDefaultStringManager default string manager} if the
73      * current thread has no explicit configuration. Never returns <i>null</i>.
74      **/

75     public static UIStringManager getStringManagerNoNull()
76     {
77         UIStringManager sm = getStringManager();
78         return (sm!=null) ? sm : getDefaultStringManager();
79     }
80
81
82
83     /**
84      * Installs a new UIStringManager instance for the current thread. This
85      * string manager becomes the active (and only visible) string manager
86      * until it is unwound or another string manager is installed.
87      * @param sm the new string manager (non-null)
88      * @param noInstallHandler [optional] used to notify if cannot install
89      * @return previous string manager if any (can be <i>null</i>)
90      * @throws BuildException if incoming manager already on iteration stack
91      **/

92     public static UIStringManager installStringManager(UIStringManager sm,
93                                                   ProblemHandler noInstallHandler)
94     {
95         if (sm==null) {
96             throw new IllegalArgumentException JavaDoc
97                 (AntX.uistrs().get("task.uism.null.sm"));
98         }
99
100         String JavaDoc whoami = AntX.uistrs().dget("task.uism.whoami", "UIStringManager");
101         return (UIStringManager)FixtureOverlays.installIfNot
102                         (FXID, sm, noInstallHandler, whoami);
103     }
104
105
106
107     /**
108      * Removes the most recently installed UIStringManager for the current
109      * thread. The previous installation is reactivated, or if this was
110      * the only string manager, the current thread's string manager is
111      * undefined (should use default if necessary).
112      * @param noUninstallHandler [optional] used if unable to uninstall
113      **/

114     public static void unwindStringManager(ProblemHandler noUninstallHandler)
115     {
116         String JavaDoc whoami = AntX.uistrs().dget("task.uism.whoami","UIStringManager");
117         FixtureOverlays.uninstallIfIs(FXID, noUninstallHandler, whoami);
118     }
119
120
121
122     /**
123      * Returns the default UIStringManager used by this context. Never
124      * returns <i>null</i>.
125      * @see #setDefaultStringManager
126      **/

127     public static final UIStringManager getDefaultStringManager()
128     {
129         UIStringManager sm = (UIStringManager)Iteration.getProperty(FXID);
130         return (sm==null) ? EmptyStringManager.INSTANCE : sm;
131     }
132
133
134
135     /**
136      * Initializes the default UIStringManager returned by this context. Not
137      * necessary since by default, is set to an EmptyStringManager. Should not
138      * be set to a string manager that exists on a thread's UISM stack. This
139      * method <em>replaces</em> any pre-existing default string manager-- the
140      * current setting is lost unless the caller maintains it.
141      * @param sm the new default string manager (non-null)
142      * @.safety guarded
143      **/

144     public static final UIStringManager setDefaultStringManager(UIStringManager sm)
145     {
146         if (sm==null) {
147             throw new IllegalArgumentException JavaDoc
148                 (AntX.uistrs().get("task.uism.null.sm"));
149         }
150         return (UIStringManager)Iteration.setProperty(FXID,sm);
151     }
152
153
154
155     /**
156      * Returns <i>true</i> if the default UIStringManager is defined as
157      * the empty placeholder.
158      * @see #setDefaultStringManager setDefaultStringManager()
159      * @since JWare/AntX 0.4
160      **/

161     public static final boolean isDefaultUndefined()
162     {
163         return getDefaultStringManager()==EmptyStringManager.INSTANCE;
164     }
165
166
167
168     /**
169      * Resets the default UIStringManager to the empty placeholder. Your
170      * scripts and application should never need to do this. This method is
171      * provided for test scripts and harnesses.
172      * @.safety guarded
173      * @since JWare/AntX 0.4
174      **/

175     static final void unsetDefaultStringManager()
176     {
177         Iteration.removeProperty(FXID);
178     }
179
180
181
182
183     /**
184      * Installs a test-harness helper that clears up the various UISMContext
185      * fixture components. Your application should never use this helper
186      * directly; it is provided so that test harnesses can reset the environment
187      * to a known state.
188      * @since JWare/AntX 0.4
189      **/

190     static {
191         AntXFixture.setKillMethod
192             (FXID,
193              new String JavaDoc[]{"messagebundles","stringmanagers"},
194              new KillMethodSkeleton() {
195                      protected boolean killDefault(ProblemHandler from) {
196                          UISMContext.unsetDefaultStringManager();
197                          return true;
198                      }
199                      protected String JavaDoc getComponentId() {
200                          return UISMContext.FXID;
201                      }
202                      protected String JavaDoc getComponentName() {
203                          return AntX.uistrs().dget
204                              ("task.uism.whoami", "UIStringManager");
205                      }
206                  }
207              );
208     }
209
210
211
212     /** Disallow; only public static utility methods. **/
213     private UISMContext()
214     { }
215 }
216
217 /* end-of-UISMContext.java */
218
Popular Tags