KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > guiframework > view > descriptors > ViewDescriptor


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.descriptors;
25
26 import com.iplanet.jato.RequestContext;
27 import com.iplanet.jato.RequestManager;
28 import com.iplanet.jato.RequestParticipant;
29 import com.iplanet.jato.view.ContainerView;
30 import com.iplanet.jato.view.ContainerViewBase;
31 import com.iplanet.jato.view.View;
32 import com.iplanet.jato.view.ViewBean;
33 import com.iplanet.jato.view.event.DisplayEvent;
34
35 import java.io.InputStream JavaDoc;
36 import java.lang.reflect.Constructor JavaDoc;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39 import java.util.*;
40
41 import com.sun.enterprise.tools.guiframework.FrameworkDescriptor;
42 import com.sun.enterprise.tools.guiframework.event.descriptors.EventDescriptor;
43 import com.sun.enterprise.tools.guiframework.exception.FrameworkError;
44 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
45 import com.sun.enterprise.tools.guiframework.util.LogUtil;
46 import com.sun.enterprise.tools.guiframework.util.Util;
47 import com.sun.enterprise.tools.guiframework.view.ChildNullException;
48 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
49 import com.sun.enterprise.tools.guiframework.view.DescriptorViewManager;
50
51
52 /**
53  *
54  */

55 public class ViewDescriptor implements FrameworkDescriptor {
56
57     /**
58      * Name / Class Name Constructor
59      *
60      * @param name The name (or id) of the descriptor
61      */

62     public ViewDescriptor(String JavaDoc name) {
63     setName(name);
64     addChildDescriptors();
65     }
66
67
68     /**
69      * This method sets the instance name for the Described View.
70      *
71      * @return The instance name for the Described View.
72      */

73     public String JavaDoc getName() {
74     return _name;
75     }
76
77
78     /**
79      * This method returns the instance name for the Described View.
80      *
81      * @param name The name for the described View.
82      */

83     public void setName(String JavaDoc name) {
84     if (name == null) {
85         throw new IllegalArgumentException JavaDoc("'name' cannot be null!");
86     }
87     _name = name;
88     }
89
90
91     /**
92      * For future tool support
93      */

94     public String JavaDoc getDescription() {
95     return _description;
96     }
97
98
99     /**
100      * For future tool support
101      */

102     public void setDescription(String JavaDoc desc) {
103     _description = desc;
104     }
105
106
107     /**
108      * This method throws a FrameworkException if it is called. It is
109      * expected that other descriptors will override this method and provide
110      * the correct way to instantiate the requested View.
111      *
112      * <BLOCKQUOTE>
113      * NOTE: This method should not be invoked directly if you want the
114      * ViewManager to manage the instance created. This method does not
115      * register the newly created instance with the View Manager.
116      * </BLOCKQUOTE>
117      *
118      * @param ctx The RequestContext
119      * @param container The parent container (if any)
120      * @param name The name of the View to create
121      *
122      * @return A newly created View of getViewClass() type.
123      */

124     public View getInstance(RequestContext ctx, ContainerView container, String JavaDoc name) {
125     throw new FrameworkException("Descriptor type for '"+name+
126         "' is ViewDescriptor, this is not valid.", this, container);
127     }
128
129
130     /**
131      * <P>This method retrieves the corresponding View object for this
132      * ViewDescriptor. This is useful for obtaining nested Views when you do
133      * not have a reference to the parent container. Remember you cannot
134      * simply call "getInstance()" on a ViewDescriptor as this will create a
135      * NEW instance. Instead the top level ViewDescriptor must obtain its
136      * peer via the ViewManager (unless a sub-view registers itself with the
137      * ViewManager). This method does this for you... it obtains the TopLevel
138      * ViewDescriptor from the given ViewDescriptor, obtains the View peer and
139      * walks the tree to the child that matches the given ViewDescriptor.</P>
140      *
141      * <BLOCKQUOTE>
142      * WARNING: Do not invoke this method in a beforeCreate. Since this
143      * method walks the View tree, it will attempt to create the View that is
144      * the subject of the beforeCreate. When this happens the beforeCreate
145      * will be fired, which will try to create the View again... which will
146      * fire the before create... etc., etc., etc., etc. You get the idea,
147      * don't do it. In general you should always be careful with the tasks
148      * you perform in the beforeCreate events.
149      * </BLOCKQUOTE>
150      *
151      * @param ctx The RequestContext
152      *
153      * @return View corresponding to this ViewDescriptor
154      */

155     public View getView(RequestContext ctx) {
156     Stack descStack = new Stack();
157
158     // Find the top
159
ViewDescriptor topDesc = this;
160     while (topDesc.getParent() != null) {
161         descStack.push(topDesc.getName());
162         topDesc = topDesc.getParent();
163     }
164
165     // Get the top's peer. We use the ViewManager in order to get a cached
166
// instance. This should usually work b/c the ViewBean (the top should
167
// be a ViewBean) gets created early during the submit cycle. Although
168
// it may be possible this Descriptor is part of a different ViewBean
169
// that hasn't been referenced yet.
170
View view = ((DescriptorViewManager)ctx.getViewBeanManager()).
171         getView(null, topDesc.getName(), topDesc);
172
173     // Use the ViewDescriptors to get to the correct View child
174
while (!descStack.empty()) {
175         view = ((ContainerView)view).getChild((String JavaDoc)descStack.pop());
176     }
177
178     // Return the requested View
179
return view;
180     }
181
182
183     /**
184      *
185      */

186     public void registerChildren(ContainerViewBase instance) {
187     // Do all the children
188
Iterator it = getChildDescriptors().iterator();
189     ViewDescriptor desc = null;
190     View child = null;
191
192     // Trace message...
193
if (LogUtil.isLoggable(LogUtil.FINER) && it.hasNext()) {
194         LogUtil.log(LogUtil.FINER, "trace.registerChildren", getName());
195     }
196
197     while (it.hasNext()) {
198         desc = (ViewDescriptor)it.next();
199         // NOTE: calling getChild() will often do the child registration,
200
// NOTE: however -- unless the constructor is aware of descriptors
201
// NOTE: -- children declared via descriptors won't get registered
202
// NOTE: unless we do it here
203
try {
204         // Trace message...
205
if (LogUtil.isLoggable(LogUtil.FINEST) && it.hasNext()) {
206             LogUtil.log(LogUtil.FINEST, "trace.registerChild",
207             instance.getName()+"."+desc.getName());
208         }
209
210         child = instance.getChild(desc.getName());
211         instance.registerChild(desc.getName(), child.getClass());
212         if (child instanceof DescriptorContainerView) {
213             // All DescriptorContainerView's are required to register
214
// their own Descriptor children. Doing so again here
215
// would just waste time.
216
continue;
217         } else if (child instanceof ContainerViewBase) {
218             desc.registerChildren((ContainerViewBase)child);
219         } else if (desc instanceof FakeContainerDescriptor) {
220             // Fake Containers have children, but aren't necessarily
221
// ContainerView's :( So we will register their children
222
// in the parent container so acceptRequest() will work
223
desc.registerChildren(instance);
224         }
225         } catch (ChildNullException ex) {
226         // Ignore b/c some components can't be created (no View
227
// corresponding to JSP tag)
228
} catch (Exception JavaDoc ex) {
229         throw new FrameworkError(
230             ex, desc, (child == null) ? instance : child);
231         }
232     }
233     }
234
235
236     /**
237      * Accessor for the DisplayURL.
238      *
239      * @return The DisplayURL for this descriptor.
240      */

241     public String JavaDoc getDisplayURL() {
242     return _displayURL;
243     }
244
245
246     /**
247      * Allows the DisplayURL to be set.
248      *
249      * @param url The DisplayURL to use.
250      */

251     public void setDisplayURL(String JavaDoc url) {
252     _displayURL = url;
253     }
254
255
256     /**
257      * This method returns a EventDescriptor Object for the given type. If
258      * there is not a registered EventDescriptor Object for the requested
259      * type, then null will be returned.
260      *
261      * @param type The EventDescriptor.TYPES type
262      *
263      * @return EventDescriptor for the requested type.
264      */

265     public EventDescriptor getEventDescriptor(String JavaDoc type) {
266     return (EventDescriptor)_eventDescriptors.get(type);
267     }
268
269
270     /**
271      * This method sets an EventDescriptor. Only 1 EventDescriptor can be
272      * set for a given type. If an EventDescriptor has been previously set,
273      * this will replace the previous EventDescriptor.
274      *
275      * @param desc The EventDescriptor
276      */

277     public void setEventDescriptor(EventDescriptor desc) {
278     _eventDescriptors.put(desc.getType(), desc);
279     }
280
281
282     /**
283      * This method allows you to add a child ViewDescriptor.
284      *
285      * @param fieldDesc The ViewDescriptor to add
286      */

287     public void addChildDescriptor(ViewDescriptor fieldDesc) {
288         fieldDesc.setParent(this);
289     _childDescriptors.add(fieldDesc);
290     _childDescriptorMap = null;
291     }
292
293
294     /**
295      * This method allows you to retrieve the child descriptors as a
296      * List.
297      *
298      * @return List of ViewDescriptors
299      */

300     public List getChildDescriptors() {
301     return _childDescriptors;
302     }
303
304
305     /**
306      * This method allows you to set the complete list of child descriptors
307      * to be used.
308      *
309      * @param fields The List of ViewDescriptors to be set
310      */

311     public void setChildDescriptors(List fields) {
312     _childDescriptors = fields;
313     _childDescriptorMap = null;
314     }
315
316
317     /**
318      * This method retrieves the requested child ViewDescriptor. You may use
319      * .'s in the name to represent a path of child ViewDescriptors to
320      * traverse to obtain the desired ViewDescriptor. If the ViewDescriptor
321      * is not found, null will be returned.
322      *
323      * @param name The name of the desired child descriptor
324      *
325      * @return The requested ViewDescriptor or null if not found.
326      */

327     public ViewDescriptor getChildDescriptor(String JavaDoc name) {
328     if (_childDescriptorMap == null) {
329         // If we haven't created the Map yet, create it.
330
Iterator it = getChildDescriptors().iterator();
331         ViewDescriptor desc = null;
332         Map newMap = new HashMap();
333         while (it.hasNext()) {
334         desc = (ViewDescriptor)it.next();
335         newMap.put(desc.getName(), desc);
336         }
337         if (_childDescriptorMap == null) {
338         _childDescriptorMap = newMap;
339         }
340     }
341     ViewDescriptor child = (ViewDescriptor )_childDescriptorMap.get(name);
342     if (child == null) {
343         // Perhaps this name is a hierarchical .'d name...
344
int pos = name.indexOf('.');
345         if (pos != -1) {
346         // Check first name
347
child = getChildDescriptor(name.substring(0, pos));
348         if (child != null) {
349             // Recurse to take care of the rest of the .'s
350
return child.getChildDescriptor(name.substring(pos+1));
351         }
352         }
353     }
354     return child;
355     }
356
357
358     /**
359      * Returns the Parent ViewDescriptor
360      *
361      * @return The Parent ViewDescriptor
362      */

363     public ViewDescriptor getParent() {
364         return _parent;
365     }
366
367     
368     /**
369      * Sets the Parent ViewDescriptor
370      *
371      * @param viewDesc The Parent ViewDescriptor
372      */

373     public void setParent(ViewDescriptor viewDesc) {
374         _parent = viewDesc;
375     }
376     
377
378     /**
379      * This method allows all the parameters to be set at once by passing in
380      * a Map object. The Map object should contain parameter-name to
381      * parameter-value pairs. Parameters may be used to provide additional
382      * instance-specific information.
383      *
384      * @param parameters The Map of parameters.
385      */

386     public void setParameters(Map parameters) {
387     if (parameters == null) {
388         throw new IllegalArgumentException JavaDoc(
389         "The parameter map cannot be null!");
390     }
391     _parameters = parameters;
392     }
393
394     public Set getParameterKeys() {
395     return _parameters.keySet();
396     }
397
398     /**
399      * <P>This method retrieves the Map of parameters.</P>
400      *
401      * @return The Map of parameters.
402      */

403     public Map getParameters() {
404     Map newMap = new HashMap(_parameters.size());
405     Iterator it = _parameters.keySet().iterator();
406     Object JavaDoc key;
407     Object JavaDoc value;
408     // Iterate through the keys, and make the new Map w/ substitutions
409
while (it.hasNext()) {
410         key = it.next();
411         value = Util.replaceVariablesWithAttributes(
412         _parameters.get(key), this);
413         newMap.put(key, value);
414     }
415     return newMap;
416     }
417
418
419     /**
420      * This method addes a parameter.
421      *
422      * @param name The Parameter name
423      * @param value The Parameter value
424      */

425     public void addParameter(String JavaDoc name, Object JavaDoc value) {
426     if (name == null) {
427         throw new IllegalArgumentException JavaDoc("'name' cannot be null!");
428     }
429     _parameters.put(name, value);
430     }
431
432
433     /**
434      * This method retrieves the requested parameter. If the parameter is not
435      * found, this method will return null.
436      *
437      * @param name The name of the desired parameter.
438      *
439      * @return The parameter value, or nulll if not found.
440      */

441     public Object JavaDoc getParameter(String JavaDoc name) {
442     return Util.replaceVariablesWithAttributes(_parameters.get(name), this);
443     }
444     
445
446     /**
447      * The full name of the Model Class. This used the MODEL_CLASS_NAME
448      * parameter or DEFAULT_MODEL if MODEL_CLASS_NAME is not supplied.
449      *
450      * @return The full name of the Model Class.
451      */

452     public String JavaDoc getModelClassName() {
453     String JavaDoc clsName = (String JavaDoc)getParameter(MODEL_CLASS_NAME);
454     if (clsName == null) {
455         clsName = DEFAULT_MODEL;
456     }
457     return clsName;
458     }
459
460
461     /**
462      * This method provides the default Model instance name, which is simply
463      * the name of this ViewDescriptor. Some ViewDescriptors may override
464      * this method to provide a more unique name if desired.
465      *
466      * @return The default model instance name (this.getName() for this
467      * implementation).
468      */

469     public String JavaDoc getDefaultModelInstanceName() {
470     return getName();
471     }
472
473
474     /**
475      * <P>The model instance name is an arbitrary name to refer to the
476      * instance of the Model. In most cases the model class name will work
477      * for the instance name, or some other semi-unique value.</P>
478      *
479      * <P>This method will use the MODEL_INSTANCE_NAME parameter. If this is
480      * null, this.getName() will be used which is more unique than the model
481      * class name.</P>
482      *
483      * @return The model instance name.
484      */

485     public String JavaDoc getModelInstanceName() {
486     String JavaDoc instName = (String JavaDoc)getParameter(MODEL_INSTANCE_NAME);
487     if (instName == null) {
488         instName = getDefaultModelInstanceName();
489     }
490     return instName;
491     }
492
493
494     /**
495      * Returns true if the model should be retrieved from session. This
496      * method uses the GET_MODEL_FROM_SESSION parameter to determine if
497      * session should be used. The default is "false", "true" should be set
498      * as a the parameter value to use session.
499      *
500      * @return true if the model should be retrieved from session.
501      */

502     public boolean shouldGetModelFromSession() {
503     return new Boolean JavaDoc(""+getParameter(GET_MODEL_FROM_SESSION)).booleanValue();
504     }
505
506
507     /**
508      * Returns true if the model should be stored in session. This method
509      * uses the PUT_MODEL_TO_SESSION parameter to determine if session should
510      * be used. The default is "false", "true" should be set as a the
511      * parameter value to use session.
512      *
513      * @return true if the model should be stored in session.
514      */

515     public boolean shouldPutModelToSession() {
516     return new Boolean JavaDoc(""+getParameter(PUT_MODEL_TO_SESSION)).booleanValue();
517     }
518
519
520     /**
521      * This method returns the resource bundle, it must be specified as a
522      * parameter of the ViewDescriptor (or a parameter of one its parents).
523      * If not declared, null will be returned.
524      *
525      * @return The resourceBundle
526      */

527     public String JavaDoc getResourceBundle() {
528     String JavaDoc bundle = (String JavaDoc)getParameter(RESOURCE_BUNDLE);
529
530     // Look at parent ViewDescriptors if we need to
531
for (ViewDescriptor vd = getParent();
532         ((bundle == null) && (vd != null)); vd = vd.getParent()) {
533         bundle = (String JavaDoc)vd.getParameter(RESOURCE_BUNDLE);
534     }
535
536     // Return the resource bundle or null
537
return bundle;
538     }
539
540
541     /**
542      * The XML File name backing the described component (i.e.
543      * propertySheet.xml or table.xml).
544      *
545      * @return The XML File Name or null
546      */

547     public String JavaDoc getXMLFileName() {
548     return (String JavaDoc)getParameter(XML_FILE);
549     }
550
551
552     /**
553      * This method attempts to open a stream to the XML file.
554      *
555      * @return An InputStream to the XML file.
556      */

557     public InputStream JavaDoc getXMLFileAsStream() {
558     String JavaDoc fileName = getXMLFileName();
559     if ((fileName == null) || (fileName.equals(""))) {
560         throw new FrameworkException(
561         "Parameter '"+XML_FILE+"' is missing for '"+
562         this.getClass().getName()+":"+getName()+"'.");
563     }
564     try {
565             // try loading from the classpath with the call loader. This is cached.
566
InputStream JavaDoc inStream = getClass().getClassLoader().getResourceAsStream(fileName);
567             if (inStream == null) {
568                 // try getting the file from the context root. This is not cached.
569
inStream = RequestManager.getRequestContext().getServletContext().
570             getResourceAsStream(fileName);
571                 if (inStream == null) {
572                     throw new FrameworkException("InputStream (null) for '"+fileName+"'.");
573                 }
574         }
575         return inStream;
576     } catch (Exception JavaDoc ex) {
577         throw new FrameworkException("Unable to open '"+fileName+"' for '"+
578         this.getClass().getName()+":"+getName()+"'.", ex);
579     }
580     }
581     
582
583     /**
584      * The toString() returns getName()
585      */

586     public String JavaDoc toString() {
587     return getName();
588     }
589
590
591     /**
592      * <P>This method gets invoked by the constructor, however it does nothing
593      * by default. The purpose of this method is to provide a designated
594      * place for programatically adding child descriptors when extending this
595      * class. To add a child descriptor, invoke the method:</P>
596      *
597      * <BLOCKQUOTE>
598      * addChildDescriptor(ViewDescriptor fieldDesc)
599      * </BLOCKQUOTE>
600      *
601      * @see #addChildDescriptor(ViewDescriptor)
602      */

603     protected void addChildDescriptors() {
604     }
605
606
607     /**
608      * This is the Model class. If a parameter of this name is supplied, its
609      * value will be used; otherwise the value of DEFAULT_MODEL will be used.
610      * ("modelClassName")
611      */

612     public static final String JavaDoc MODEL_CLASS_NAME = "modelClassName";
613
614     /**
615      * The default model name. ("com.iplanet.jato.model.DefaultModel")
616      */

617     public static final String JavaDoc DEFAULT_MODEL =
618         "com.iplanet.jato.model.DefaultModel";
619
620     /**
621      * This is the ModelManager key the model will be stored as. If a
622      * parameter of this name is supplied, its value will be used; otherwise
623      * this.getName() will be used as the instance name. ("modelInstanceName")
624      */

625     public static final String JavaDoc MODEL_INSTANCE_NAME = "modelInstanceName";
626
627     /**
628      * This parameter should be true or false depending of if you would like
629      * to retrieve the model from session.
630      * "modelFromSession"
631      */

632     public static final String JavaDoc GET_MODEL_FROM_SESSION = "modelFromSession";
633
634     /**
635      * This parameter should be true or false depending of if you would like
636      * to store the model in session.
637      * "modelToSession"
638      */

639     public static final String JavaDoc PUT_MODEL_TO_SESSION = "modelToSession";
640
641
642     /**
643      * This is the parameter to specify the XML file backing this component.
644      */

645     public static final String JavaDoc XML_FILE = "xmlFile";
646
647     /**
648      * This parameter specifies the resourceBundle to used for I18N.
649      * "resourceBundle"
650      */

651     public static final String JavaDoc RESOURCE_BUNDLE = "resourceBundle";
652
653
654     private Map _eventDescriptors = new HashMap();
655     private List _childDescriptors = new ArrayList();
656     private Map _childDescriptorMap = null;
657     private Map _parameters = new HashMap();
658     private String JavaDoc _name = "";
659     private String JavaDoc _description = "";
660     private String JavaDoc _displayURL = null;
661     private ViewDescriptor _parent = null;
662 }
663
Popular Tags