1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: ViewContext.java,v 1.12 2002/09/11 19:15:55 ludovicc Exp $
37 */
38 package org.scopemvc.controller.basic;
39
40
41 import java.util.HashMap;
42 import java.util.Map;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45 import org.scopemvc.core.View;
46 import org.scopemvc.util.Debug;
47
48 /***
49 * <P>
50 *
51 * ViewContext handles show/hide of views, errors and has some concept of the
52 * application's context. eg a Swing implementation would show views inside
53 * JFrames or JDialogs and show errors using JOptionPanes. A servlet
54 * implementation would know about the HTTP response and push views into its
55 * stream. </P> <P>
56 *
57 * There's a place in here to store properties per context. This is used in the
58 * servlet implementation to maintain state over a single request. </P> <P>
59 *
60 * Several static methods are used to set the context for an application either
61 * on a global basis (eg Swing) or per-thread (eg servlet): see {@link
62 * #getViewContext} etc. </P>
63 *
64 * @author <a href="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</a>
65 * @created 05 August 2002
66 * @version $Revision: 1.12 $ $Date: 2002/09/11 19:15:55 $
67 */
68 public abstract class ViewContext {
69
70 private static final Log LOG = LogFactory.getLog(ViewContext.class);
71
72 // ----------------------- Global context management
73
74 private static ViewContext globalContext;
75
76 private static ThreadLocal localContext = new ThreadLocal();
77
78 /***
79 * The properties associated with the ViewContext
80 */
81 private Map properties = new HashMap();
82
83
84 /***
85 * Return the current ViewContext. First try the per-thread context and if
86 * none, returns the global context. May return null if no context is set.
87 *
88 * @return The viewContext value
89 */
90 public static ViewContext getViewContext() {
91
92 // Try to find a "per Thread" context
93 ViewContext context = (ViewContext) localContext.get();
94 if (context != null) {
95 return context;
96 }
97
98 return globalContext;
99 // may be null...
100 }
101
102
103 /***
104 * Set the global ViewContext.
105 *
106 * @param inContext The new globalContext value
107 */
108 public static void setGlobalContext(ViewContext inContext) {
109 globalContext = inContext;
110 }
111
112
113 /***
114 * Set a ViewContext for the current thread.
115 *
116 * @param inContext The new threadContext value
117 */
118 public static void setThreadContext(ViewContext inContext) {
119 if (inContext == null) {
120 throw new IllegalArgumentException("can't set a null ViewContext for a Thread -- see clearThreadContext()");
121 }
122
123 localContext.set(inContext);
124 }
125
126
127 /***
128 * Clear the ViewContext for the current thread.
129 */
130 public static void clearThreadContext() {
131 localContext.set(null);
132 }
133
134
135 /***
136 * Fetch an arbitrary object under a known key.
137 *
138 * @param inKey The property key
139 * @return The property value
140 */
141 public Object getProperty(String inKey) {
142 if (LOG.isDebugEnabled()) {
143 LOG.debug("getProperty: " + inKey);
144 }
145 return properties.get(inKey);
146 }
147
148
149 /***
150 * Show the view
151 *
152 * @param inView The view to show
153 */
154 public abstract void showView(View inView);
155
156
157 /***
158 * Hide the view
159 *
160 * @param inView The view to hide
161 */
162 public abstract void hideView(View inView);
163
164
165 /***
166 * Show an error message.
167 *
168 * @param inErrorTitle The title for the error message
169 * @param inErrorMessage The error message
170 */
171 public abstract void showError(String inErrorTitle, String inErrorMessage);
172
173
174 /***
175 * Exit the application
176 */
177 public abstract void exit();
178
179
180 /***
181 * Start a progress indicator for long operations.
182 */
183 public abstract void startProgress();
184
185
186 /***
187 * Stop the progress indicator.
188 */
189 public abstract void stopProgress();
190
191
192 // --------------------- Properties ----------------------------
193
194 /***
195 * Store an arbitrary object under a known key.
196 *
197 * @param inKey The element to be added to the Property attribute
198 * @param inObject The element to be added to the Property attribute
199 */
200 public void addProperty(String inKey, Object inObject) {
201 if (LOG.isDebugEnabled()) {
202 LOG.debug("addProperty: " + inKey + ", " + inObject);
203 }
204 if (inKey == null) {
205 throw new IllegalArgumentException("Can't use a null key.");
206 }
207 properties.put(inKey, inObject);
208 }
209
210
211 /***
212 * Fetch and remove an arbitrary object under a known key.
213 *
214 * @param inKey The property key
215 * @return The removed value, or null if the key was not defined
216 */
217 public Object removeProperty(String inKey) {
218 if (LOG.isDebugEnabled()) {
219 LOG.debug("removeProperty: " + inKey);
220 }
221 return properties.remove(inKey);
222 }
223
224
225 /***
226 * Clear all properties from this context.
227 */
228 public void clearProperties() {
229 if (LOG.isDebugEnabled()) {
230 LOG.debug("clearProperties");
231 }
232 properties.clear();
233 }
234 }
This page was automatically generated by Maven