KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.InputStream JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31
32 import com.iplanet.jato.ModelManager;
33 import com.iplanet.jato.RequestManager;
34 import com.iplanet.jato.RequestContext;
35 import com.iplanet.jato.view.ContainerView;
36 import com.iplanet.jato.view.ContainerViewBase;
37 import com.iplanet.jato.view.View;
38 import com.iplanet.jato.util.NonSyncStringBuffer;
39
40 import com.sun.web.ui.model.CCPageTitleModel;
41 import com.sun.web.ui.model.CCPageTitleModelInterface;
42 import com.sun.web.ui.view.pagetitle.CCPageTitle;
43 import com.sun.web.ui.view.html.CCButton;
44 import com.sun.web.ui.view.html.CCDropDownMenu;
45 import com.sun.web.ui.taglib.html.CCButtonTag;
46 import com.sun.web.ui.taglib.html.CCDropDownMenuTag;
47 import com.sun.web.ui.common.CCTagClass;
48 import com.sun.web.ui.taglib.common.CCDisplayFieldTagBase;
49 import com.sun.web.ui.model.CCPageTitleModelInterface;
50
51 import com.sun.enterprise.tools.guiframework.util.LogUtil;
52 import com.sun.enterprise.tools.guiframework.view.DescriptorCCPageTitle;
53
54 /**
55  *
56  */

57 public class CCPageTitleDescriptor extends ViewDescriptor {
58     
59     // XML string parts for the PageTitle model creation.
60
private static final String JavaDoc xmlStart =
61         CCPageTitleModelInterface.XML_DELCARATION + "\n" +
62         CCPageTitleModelInterface.DOCTYPE + "\n" +
63         "<" + CCPageTitleModelInterface.PAGE_TITLE_ELEMENT + ">\n";
64     
65     private static final String JavaDoc xmlEnd =
66         "</" + CCPageTitleModelInterface.PAGE_TITLE_ELEMENT + ">\n";
67     
68
69     
70     /**
71      * Constructor
72      */

73     public CCPageTitleDescriptor(String JavaDoc name) {
74     super(name);
75     }
76
77
78     public void registerChildren(ContainerViewBase instance) {
79     // Invoke the super registerChild
80
super.registerChildren(instance);
81     getModel().registerChildren(instance);
82     }
83     
84     /**
85      * This method creates empty descriptors on the fly for fields that are
86      * defined by the underlying model.
87      */

88     public ViewDescriptor getChildDescriptor(String JavaDoc name) {
89         // Do the normal stuff if we can...
90
ViewDescriptor desc = super.getChildDescriptor(name);
91     if (desc != null) {
92         return desc;
93     }
94
95     // Do we need to create a descriptor on the fly?
96
CCPageTitleModel model = getModel();
97         if (model != null && model.isChildSupported(name)) {
98         // YES
99
desc = new CCPageTitleChildDescriptor(name);
100         // NOTE: This call is safe (mostly), because although we are
101
// NOTE: modifying a static structure... this change is applicable
102
// NOTE: to all users. However, there is a slight possibility of
103
// NOTE: of a sync. problem... not worth worrying about now. If
104
// NOTE: this does become a problem, just remove the
105
// NOTE: addChildDescriptor() line below.
106
addChildDescriptor(desc);
107         return desc;
108     }
109     return null;
110     }
111     
112     private boolean isLegalTagAttribute(String JavaDoc name, CCDisplayFieldTagBase tag) {
113         // checks if the attribute value is legal by reflection.
114
char c = Character.toUpperCase(name.charAt(0));
115     // Create method name for setting the attribute value.
116
String JavaDoc methodName = "set" + c + name.substring(1);
117     try {
118         // test for existence by try to retrieve the method.
119
Method JavaDoc method = tag.getClass().getMethod(
120         methodName, new Class JavaDoc[] {String JavaDoc.class});
121             if (method == null)
122                 return false;
123     } catch (Exception JavaDoc e) {
124         return false;
125     }
126         return true;
127     }
128     
129     private void addParameters(NonSyncStringBuffer buff,
130             CCDisplayFieldTagBase tag, ViewDescriptor desc) {
131         Iterator JavaDoc it = desc.getParameterKeys().iterator();
132         while (it.hasNext()) {
133             String JavaDoc key = (String JavaDoc) it.next();
134             if (isLegalTagAttribute(key, tag) == false) {
135                  // parameters that are not illegal attributes of the given tag
136
// should not pass to the page title XML file.
137
continue;
138             }
139             Object JavaDoc obj = desc.getParameter(key);
140             String JavaDoc value = null;
141             if (obj != null){
142                 value = obj.toString();
143             }
144             if (value != null) {
145                 buff.append(" <attribute name=\"")
146                     .append(key)
147                     .append("\" value=\"")
148                     .append(value)
149                     .append("\"/>\n");
150             }
151         }
152     }
153      
154     private void appendPageActionButtons(NonSyncStringBuffer buff) {
155     Iterator JavaDoc it = getChildDescriptors().iterator();
156     ViewDescriptor desc = null;
157         boolean first = true;
158         
159         while (it.hasNext()) {
160             desc = (ViewDescriptor)it.next();
161             String JavaDoc buttonType = (String JavaDoc) desc.getParameter("type");
162             if (buttonType != null &&
163                 (buttonType.equals(CCButton.TYPE_PRIMARY) ||
164                  buttonType.equals(CCButton.TYPE_SECONDARY) ||
165                  buttonType.equals(CCDropDownMenu.TYPE_JUMP) ||
166                  buttonType.equals(CCDropDownMenu.TYPE_STANDARD))) {
167                 // Consider all primary and secondary buttons as page buttons,
168
// all others are page action buttons. The difference is where
169
// they are placed on the page.
170
continue;
171             }
172             Object JavaDoc show = desc.getParameter("showButton");
173             if (show != null && show.toString().equals("false"))
174                 continue;
175             String JavaDoc buttonName = desc.getName();
176             if (buttonName == null)
177                 continue; // probably an error wo/ a name?
178

179             if (first)
180                 buff.append(" <" + CCPageTitleModelInterface.PAGE_ACTIONS_ELEMENT + ">\n");
181
182             buff.append(" <cc name=\"")
183                 .append(buttonName)
184                 .append("\" tagclass=\"" + CCTagClass.BUTTON + "\">\n");
185             
186             addParameters(buff, new CCButtonTag(), desc);
187             
188             buff.append(" </cc>\n");
189             first = false;
190         }
191         if (! first)
192             buff.append(" </" + CCPageTitleModelInterface.PAGE_ACTIONS_ELEMENT + ">\n");
193     }
194     
195     private void appendPageButtons(NonSyncStringBuffer buff) {
196     Iterator JavaDoc it = getChildDescriptors().iterator();
197     ViewDescriptor desc = null;
198         boolean first = true;
199         
200         while (it.hasNext()) {
201             desc = (ViewDescriptor)it.next();
202             String JavaDoc buttonType = (String JavaDoc) desc.getParameter("type");
203             if (buttonType == null ||
204                 (buttonType.equals(CCButton.TYPE_PRIMARY) == false &&
205                  buttonType.equals(CCButton.TYPE_SECONDARY) == false)) {
206                 // Consider all primary and secondary buttons as page buttons,
207
// all others are page action buttons. The difference is where
208
// they are placed on the page.
209
continue;
210             }
211             String JavaDoc show = (String JavaDoc) desc.getParameter("showButton");
212             if (show != null && show.equals("false"))
213                 continue;
214             String JavaDoc buttonName = desc.getName();
215             if (buttonName == null)
216                 continue; // probably an error without a name?
217

218             if (first)
219                 buff.append(" <" + CCPageTitleModelInterface.PAGE_BUTTONS_ELEMENT + ">\n");
220
221             buff.append(" <cc name=\"")
222                 .append(buttonName)
223                 .append("\" tagclass=\"" + CCTagClass.BUTTON + "\">\n");
224                 
225             addParameters(buff, new CCButtonTag(), desc);
226                 
227             buff.append(" </cc>\n");
228             first = false;
229         }
230         if (! first)
231             buff.append(" </" + CCPageTitleModelInterface.PAGE_BUTTONS_ELEMENT + ">\n");
232     }
233     
234     private void addOptionList(NonSyncStringBuffer buff, ViewDescriptor desc) {
235     Object JavaDoc options = desc.getParameter("labels");
236     if (options != null) {
237         if (options instanceof String JavaDoc) {
238         List JavaDoc tmp = new ArrayList JavaDoc();
239         tmp.add(options);
240         options = tmp;
241         }
242         List JavaDoc optionList = (List JavaDoc)options;
243
244         Object JavaDoc values = desc.getParameter("values");
245         List JavaDoc valueList = optionList;
246         if (values != null) {
247         if (values instanceof String JavaDoc) {
248             List JavaDoc tmp = new ArrayList JavaDoc();
249             tmp.add(values);
250             values = tmp;
251         }
252         valueList = (List JavaDoc)values;
253         if (valueList.size() != optionList.size()) {
254             throw new RuntimeException JavaDoc(
255             "Unequal number of option names / values! " +
256                         "(CCPageTitleDescriptor.addOptionList)");
257         }
258         }
259             for (int i=0; i<valueList.size(); i++) {
260                 buff.append(" <option label=\"").append(optionList.get(i))
261                     .append("\" value=\"").append(valueList.get(i))
262                     .append("\"/>\n");
263             }
264         }
265     }
266     
267     private void appendPageViewDropDown(NonSyncStringBuffer buff) {
268     Iterator JavaDoc it = getChildDescriptors().iterator();
269     ViewDescriptor desc = null;
270         boolean first = true;
271         
272         while (it.hasNext()) {
273             desc = (ViewDescriptor)it.next();
274             String JavaDoc type = (String JavaDoc) desc.getParameter("type");
275             if (type == null ||
276                 (type.equals(CCDropDownMenu.TYPE_JUMP) == false &&
277                  type.equals(CCDropDownMenu.TYPE_STANDARD) == false)) {
278                     continue;
279             }
280             String JavaDoc name = desc.getName();
281             if (name == null)
282                 continue; // probably an error without a name?
283

284             if (first)
285                 buff.append(" <" + CCPageTitleModelInterface.PAGE_VIEWS_ELEMENT + ">\n");
286
287             buff.append(" <cc name=\"").append(name)
288                 .append("\" tagclass=\"" + CCTagClass.DROPDOWNMENU + "\">\n");
289                 
290             addParameters(buff, new CCDropDownMenuTag(), desc);
291             addOptionList(buff, desc);
292                 
293             buff.append(" </cc>\n");
294             first = false;
295         }
296         if (! first)
297             buff.append(" </" + CCPageTitleModelInterface.PAGE_VIEWS_ELEMENT + ">\n");
298     }
299     
300     private String JavaDoc getPageTitleXML() {
301         NonSyncStringBuffer buff = new NonSyncStringBuffer(2048);
302         buff.append(xmlStart);
303         
304         appendPageButtons(buff);
305         appendPageActionButtons(buff);
306         appendPageViewDropDown(buff);
307         
308         buff.append(xmlEnd);
309
310     // Log Trace Message
311
if (LogUtil.isLoggable(LogUtil.FINEST)) {
312         LogUtil.log(LogUtil.FINEST, "trace.pageTitleXML", buff.toString());
313     }
314         return buff.toString();
315     }
316
317     private InputStream JavaDoc getPageTitleXMLAsStream() {
318     ViewDescriptor desc = this;
319     InputStream JavaDoc is = null;
320     is = (InputStream JavaDoc)desc.getParameter("fileAsStream");
321     return is;
322     }
323
324
325     /**
326      * This method overrides the super class method to provide a more unique
327      * instance name. Page titles from one ViewBean to the next may often
328      * share the same name, so their name is not unique enough. Instead,
329      * this method uses the top ViewDescriptor's name (i.e. the ViewBean
330      * name).
331      *
332      * @return The default model instance name (the top ViewDescriptor for
333      * this implementation).
334      */

335     public String JavaDoc getDefaultModelInstanceName() {
336     // Find the top ViewDescriptor
337
ViewDescriptor top = this;
338     while (top.getParent() != null) {
339         top = top.getParent();
340     }
341
342     // Return the top ViewDescriptor's name + the current VD name
343
return top.getName()+"."+getName();
344     }
345
346
347     /**
348      *
349      */

350     public CCPageTitleModel getModel() {
351         // Determine if the session should be used w/ the model manager.
352
boolean fromSession = shouldGetModelFromSession();
353     boolean toSession = shouldPutModelToSession();
354     String JavaDoc instanceName = getModelInstanceName();
355
356     // Use the ModelManager to create/get the model
357
ModelManager mgr = RequestManager.getRequestContext().getModelManager();
358     CCPageTitleModel model = (CCPageTitleModel)mgr.getModel(
359             CCPageTitleModel.class, instanceName, fromSession, toSession);
360         // Get the XML document
361
InputStream JavaDoc is = null;
362     //Make sure document is not in the model, look CCActionTable for a
363
//similar call
364
if(model.getDocument() == null) {
365         if((is = getPageTitleXMLAsStream()) != null) {
366                 model.setDocument(is);
367             try {
368                     is.close();
369             } catch (java.io.IOException JavaDoc ex) {
370                 //Ignore
371
}
372         }
373         else {
374                 model.setDocument(getPageTitleXML());
375         }
376     }
377         return model;
378     }
379
380
381     /**
382      * This is a factory method for CCActionTable instances.
383      *
384      * @param ctx The RequestContext
385      * @param container The container for the newly created
386      */

387     public View getInstance(RequestContext ctx, ContainerView container, String JavaDoc name) {
388     return new DescriptorCCPageTitle(ctx, container, name, this, getModel());
389     }
390 }
391
Popular Tags