KickJava   Java API By Example, From Geeks To Geeks.

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


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.view.ContainerView;
29 import com.iplanet.jato.view.ContainerViewBase;
30 import com.iplanet.jato.view.View;
31
32 import com.sun.web.ui.model.CCTabsModelInterface;
33 import com.sun.web.ui.model.CCNavNode;
34 import com.sun.web.ui.model.CCNavNodeInterface;
35
36 import com.sun.enterprise.tools.guiframework.view.DescriptorCCTabs;
37
38 import java.util.HashMap JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41
42 import javax.servlet.ServletRequest JavaDoc;
43
44
45 /**
46  *
47  *
48  * @author Garrett Short, garrett.short@sun.com
49  * @author Ken Paulsen, ken.paulsen@sun.com
50  */

51 public class CCTabsDescriptor extends ViewDescriptor {
52
53     /**
54      * Constructor
55      */

56     public CCTabsDescriptor(String JavaDoc name) {
57     super(name);
58     }
59
60
61     /**
62      * Make sure the model is initialized.
63      */

64     public void registerChildren(ContainerViewBase instance) {
65     getNodeTabsModel();
66     super.registerChildren(instance);
67     }
68
69
70     /**
71      * This is a factory method for CCTabs instances.
72      *
73      * @param ctx The RequestContext
74      * @param container The container for the newly created
75      * @param name The name of the component, must be the same name as in the JSP file
76      */

77     public View getInstance(RequestContext ctx, ContainerView container, String JavaDoc name) {
78     return new DescriptorCCTabs(ctx, container, name, this, getNodeTabsModel(ctx));
79     }
80
81
82     /**
83      * Builds the Tab model
84      */

85     public CCTabsModelInterface getNodeTabsModel() {
86     return getNodeTabsModel(RequestManager.getRequestContext());
87     }
88
89
90     /**
91      *
92      */

93     public CCTabsModelInterface getNodeTabsModel(RequestContext ctx) {
94     // Get or create the model
95
String JavaDoc pageName = getParent().getName();
96     CCTabsModelInterface model = (CCTabsModelInterface)
97         ctx.getModelManager().getModel(
98         CCTabsModelInterface.class, pageName+'.'+getName(), false, false);
99     if (model.getNodes().size() > 0) {
100         // If there are some tabs already, this must be a cached model
101
return model;
102     }
103     // We have a new Model, let's set it up
104
model.setIsLocal(true);
105     ServletRequest JavaDoc request = ctx.getRequest();
106         
107         // There may be more than one tabs set registered. The ViewBeanMap
108
// needs to contain the map of all tabs. So add the enttries to any
109
// existing ViewBeanMap. However, only do this if the tabs set is for
110
// the same page. Store the page name in the hash map so as to check.
111
Map JavaDoc viewBeanMap = ((Map JavaDoc)request.getAttribute(VIEW_BEAN_MAP));
112         int startCount = 1;
113         if (viewBeanMap == null ||
114                 pageName.equals((String JavaDoc)viewBeanMap.get("pageName")) == false) {
115             viewBeanMap = new HashMap JavaDoc();
116             viewBeanMap.put("pageName", pageName);
117             request.setAttribute(VIEW_BEAN_MAP, viewBeanMap);
118         } else {
119             startCount += viewBeanMap.size();
120         }
121     buildTabs(ctx, model, getChildDescriptors(), null, startCount);
122     model.setSelectedNode(
123         ((Integer JavaDoc)request.getAttribute(CURR_TAB_ID)).intValue());
124     return model;
125     }
126
127     
128     /**
129      *
130      */

131     protected int buildTabs(RequestContext ctx, CCTabsModelInterface model, List JavaDoc tabs, CCNavNodeInterface parent, int id) {
132         if ((tabs == null) || (tabs.size() == 0)) {
133             return id;
134         }
135
136         String JavaDoc currViewName = getParent().getName();
137
138     // Add each Tab, and each tab's children
139
for (int index=0; index<tabs.size(); index++) {
140             ViewDescriptor tab = (ViewDescriptor)tabs.get(index);
141             if (tab.getName().equals("TabHref")) {
142                 // "TabHref" is a Lockhart hard-code name. It is the name of the
143
// hrefs used to construct the tabs component. A view Descriptor
144
// may be defined for this href for processing events on the
145
// href itself. If this view descriptor is present, ignore it
146
// in this tab processing.
147
continue;
148             }
149             String JavaDoc label = (String JavaDoc)tab.getParameter("label");
150             String JavaDoc tooltip = (String JavaDoc)tab.getParameter("tooltip");
151             String JavaDoc status = (String JavaDoc)tab.getParameter("status");
152             String JavaDoc tabPage = (String JavaDoc)tab.getParameter("nextPage");
153             String JavaDoc target = (String JavaDoc)tab.getParameter("target");
154             if (label == null || tabPage == null) {
155                 throw new RuntimeException JavaDoc("Missing required parameters (label, nextPage) in tab declaration: "+
156                     tab.getName()+", in view: "+currViewName, null);
157             }
158             CCNavNode node = new CCNavNode(id, parent, label, tooltip, status);
159         if ((target != null) && !(target.trim().equals(""))) {
160         node.setTarget(target);
161         }
162             if (parent == null) {
163                 model.addNode(node);
164         }
165             if ((tabPage != null) && !(tabPage.equals(""))) {
166         ServletRequest JavaDoc request = ctx.getRequest();
167         ((Map JavaDoc)request.getAttribute(VIEW_BEAN_MAP)).put(""+id, tabPage);
168                 if (tabPage.equals(currViewName)) {
169             request.setAttribute(CURR_TAB_ID, new Integer JavaDoc(id));
170         }
171             }
172             id++;
173             id = buildTabs(ctx, model, tab.getChildDescriptors(), node, id);
174         }
175         return id;
176     }
177
178     
179     /**
180      * Returns the page name associated with the given tab id.
181      */

182     public String JavaDoc getViewBean(RequestContext ctx, int id) {
183     Map JavaDoc vbMap = (Map JavaDoc)ctx.getRequest().getAttribute(VIEW_BEAN_MAP);
184     if (vbMap == null) {
185         getNodeTabsModel(ctx);
186         vbMap = (Map JavaDoc)ctx.getRequest().getAttribute(VIEW_BEAN_MAP);
187     }
188         // Remove the ViewBeanMap once its referenced. It is only looked at once
189
// per web event during the submit cycle. Then during the display cycle,
190
// the map needs to be recreated from scratch.
191
ctx.getRequest().setAttribute(VIEW_BEAN_MAP, null);
192     return vbMap.get(""+id).toString();
193     }
194
195
196     protected static final String JavaDoc VIEW_BEAN_MAP = "__CCTabsDesc_VBs";
197     protected static final String JavaDoc CURR_TAB_ID = "__CCTabsDesc_parentID";
198 }
199
Popular Tags