KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > view > DescriptorViewBeanBase


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.guiframework.view;
25
26 import com.iplanet.jato.RequestContext;
27 import com.iplanet.jato.SecurityCheckException;
28 import com.iplanet.jato.command.Command;
29 import com.iplanet.jato.command.CommandEvent;
30 import com.iplanet.jato.command.CommandException;
31 import com.iplanet.jato.model.ModelControlException;
32 import com.iplanet.jato.view.View;
33 import com.iplanet.jato.view.ViewBean;
34 import com.iplanet.jato.view.ViewBeanBase;
35 import com.iplanet.jato.view.event.ChildContentDisplayEvent;
36 import com.iplanet.jato.view.event.ChildDisplayEvent;
37 import com.iplanet.jato.view.event.DisplayEvent;
38
39 import java.util.EventObject JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Locale JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 import com.sun.enterprise.tools.guiframework.event.descriptors.EventDescriptor;
48 import com.sun.enterprise.tools.guiframework.exception.FrameworkError;
49 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
50 import com.sun.enterprise.tools.guiframework.util.LogUtil;
51 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
52
53
54 /**
55  * This is the base ViewBean class that provides Descriptor support. This
56  * class may often be used directly, or extended. This class changes the
57  * default event handling mechanism in JATO to one where the events (such as
58  * beginDisplay or begin<child>Display) are handled by declared handlers.
59  */

60 public class DescriptorViewBeanBase extends ViewBeanBase implements Command, DescriptorContainerView {
61
62     /**
63      * Constructor for creating a new ViewBean. The desired ViewBean should
64      * be referenced by name. This name is used to look up the configuration
65      * information for the ViewBean and construct it.
66      *
67      * @param context The RequestContext
68      * @param pageName The ViewBean name to create
69      */

70     public DescriptorViewBeanBase(RequestContext context, String JavaDoc pageName, ViewDescriptor desc) {
71     super(pageName);
72
73     // Invoke these two methods first b/c other methods depend on these
74
setRequestContext(context);
75     setLocale(context.getRequest().getLocale());
76
77     setDescriptor(desc);
78     registerViewDescriptorChildren();
79     }
80
81
82     /**
83      * This method retrieves this ViewBean's ViewDescriptor.
84      *
85      * @return This ViewBean's ViewDescriptor.
86      */

87     public ViewDescriptor getViewDescriptor() {
88     return _viewDesc;
89     }
90
91
92     /**
93      * This method sets the ViewDescriptor for this ViewBean.
94      */

95     public void setDescriptor(ViewDescriptor desc) {
96     if (desc == null) {
97         throw new FrameworkException("Cannot set null ViewDescriptor!",
98         desc, this);
99     }
100
101     // Set the Default Display URL
102
String JavaDoc url = desc.getDisplayURL();
103
104     // Tracing message...
105
if (LogUtil.isLoggable(LogUtil.FINER)) {
106         LogUtil.log(LogUtil.FINER, "trace.setDisplayURL", url);
107     }
108
109     if (url == null) {
110         throw new FrameworkException(
111         "DisplayURL in ViewDescriptor cannot be NULL for ViewBeans!",
112         desc, this);
113     }
114     setDefaultDisplayURL(getLocalizedURL(url));
115
116     // Save the Descriptor for use later
117
_viewDesc = desc;
118     }
119
120
121
122     //////////////////////////////////////////////////////////////////////
123
// Child Registration / Creation Methods //
124
//////////////////////////////////////////////////////////////////////
125

126     /**
127      * <P>This invokes getViewDescriptor().registerChildren(this) to provide a
128      * place to register children. The reason the Descriptors are involved is
129      * because some children may be Views which in turn have CommandField's.
130      * However, these Views may not be RequestHandling Views. Because of
131      * this, they do not get a chance to register their own children and thus
132      * get a chance to handle the request.</P>
133      */

134     public void registerViewDescriptorChildren() {
135     DescriptorViewHelper.registerViewDescriptorChildren(getViewDescriptor(), this);
136     }
137
138
139     /**
140      *
141      */

142     public View createChild(String JavaDoc name) {
143     return DescriptorViewHelper.createChild(this, name);
144     }
145
146
147
148     //////////////////////////////////////////////////////////////////////
149
// Event Methods //
150
//////////////////////////////////////////////////////////////////////
151

152     /**
153      * This method dispatches BeginDisplay events to each registered
154      * BeginDisplay event handler according the the ViewDescriptor.
155      *
156      * @param event The DisplayEvent, created internally by JATO
157      */

158     public void beginDisplay(DisplayEvent event) throws ModelControlException {
159     DescriptorViewHelper.beginDisplay(this, event);
160     super.beginDisplay(event);
161     }
162
163
164     /**
165      *
166      */

167     public boolean beginChildDisplay(ChildDisplayEvent event) throws ModelControlException {
168         return DescriptorViewHelper.beginChildDisplay(this, event);
169     }
170
171
172     /**
173      *
174      */

175     public String JavaDoc endChildDisplay(ChildContentDisplayEvent event) throws ModelControlException {
176     return DescriptorViewHelper.endChildDisplay(this, event);
177     }
178
179
180     /**
181      * We should check to make sure the user is authenticated here.
182      */

183     public void securityCheck() throws SecurityCheckException {
184     // Do nothing for now
185
super.securityCheck();
186     }
187
188
189     public void endDisplay(DisplayEvent event) {
190     DescriptorViewHelper.endDisplay(this, event);
191     super.endDisplay(event);
192     }
193
194
195     //////////////////////////////////////////////////////////////////////
196
// Command Methods //
197
//////////////////////////////////////////////////////////////////////
198

199     /**
200      * This method is invoked to dispatch all requests to the appropriate
201      * place.
202      *
203      * @param event The command event, contains information pertinent to
204      * to the invocation of this command
205      *
206      * @throws CommandException Thrown if an error occurs executing
207      * the command
208      */

209     public void execute(CommandEvent event) throws CommandException {
210     DescriptorViewHelper.execute(
211         getRequestContext(), (View)event.getSource(), event);
212     }
213
214
215     /**
216      * This is a convenience method to obtain a ViewBean by name. The
217      * ViewBeanManager will be used.
218      *
219      * @param viewBeanName The ViewBean name to use.
220      */

221     public ViewBean getViewBean(String JavaDoc viewBeanName) {
222     ViewBean viewBean = null;
223     try {
224         viewBean = getRequestContext().getViewBeanManager().
225         getViewBean(viewBeanName);
226     } catch (ClassNotFoundException JavaDoc ex) {
227         throw new RuntimeException JavaDoc("Unable to forward to: '"+viewBeanName+
228         "', cannot find that ViewBean!", ex);
229     }
230     return viewBean;
231     }
232
233
234
235     //////////////////////////////////////////////////////////////////////
236
// IDescriptorViewBean Methods //
237
//////////////////////////////////////////////////////////////////////
238

239     /**
240      * This method is needed from IndexTreeView.
241      */

242     public void setAttributes(Map JavaDoc attributes) {
243     _attributes = attributes;
244     }
245     
246
247     /**
248      * This method is needed from IndexTreeView.
249      */

250     protected Object JavaDoc getAttribute(String JavaDoc name) {
251     return (_attributes == null) ? null : _attributes.get(name);
252     }
253
254
255     /**
256      * This method returns the user's perferred locale.
257      *
258      * @return The user's locale (if available, default otherwise)
259      */

260     public Locale JavaDoc getLocale() {
261     return _locale;
262     }
263
264
265     /**
266      * This method allows you to overwrite the user's locale, which by
267      * default, is obtained via the ServletRequest.getLocale().
268      *
269      * @param locale The Locale to use
270      */

271     public void setLocale(Locale JavaDoc locale) {
272     _locale = locale;
273     }
274
275
276     //////////////////////////////////////////////////////////////////////
277
// Utility Methods //
278
//////////////////////////////////////////////////////////////////////
279

280     public String JavaDoc getLocalizedURL(String JavaDoc fileName) {
281     if (getLocale().equals(Locale.ENGLISH) || getLocale().getLanguage().equals("en")) {
282         return fileName;
283     }
284
285     int index = fileName.indexOf('?');
286     String JavaDoc newName = null;
287
288     if(index != -1) {
289         newName = fileName.substring(0, index);
290     } else {
291         newName = fileName;
292     }
293     int lastdot = newName.lastIndexOf(".jsp");
294     newName = newName.substring(0,lastdot) + "_" + getLocale().getLanguage() + ".jsp";
295
296     try {
297         DescriptorViewHelper.verifyClassExists(getRequestContext().getServletContext(), newName);
298         if(index >= 0) {
299         newName += fileName.substring(fileName.indexOf('?'));
300         }
301     } catch (ClassNotFoundException JavaDoc ex) {
302         // don't return a localized version, use the given version
303
return fileName;
304     }
305     return newName;
306     }
307
308     private Locale JavaDoc _locale = null;
309
310     private ViewDescriptor _viewDesc = null;
311     private Map JavaDoc _attributes = null;
312 }
313
Popular Tags