KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > descriptors > CCBreadCrumbsDescriptor


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.admingui.descriptors;
25
26 import com.iplanet.jato.RequestContext;
27 import com.iplanet.jato.RequestManager;
28 import com.iplanet.jato.model.DefaultModel;
29 import com.iplanet.jato.view.BasicCommandField;
30 import com.iplanet.jato.view.ContainerView;
31 import com.iplanet.jato.view.ContainerViewBase;
32 import com.iplanet.jato.view.View;
33
34 import com.sun.web.ui.model.CCBreadCrumbsModel;
35 import com.sun.web.ui.model.CCBreadCrumbsModelInterface;
36 import com.sun.web.ui.view.breadcrumb.CCBreadCrumbs;
37
38 import com.sun.enterprise.tools.admingui.tree.IndexTreeNode;
39 import com.sun.enterprise.tools.admingui.util.Util;
40 import com.sun.enterprise.tools.admingui.ConfigProperties;
41 import com.sun.enterprise.tools.guiframework.event.descriptors.EventDescriptor;
42 import com.sun.enterprise.tools.guiframework.event.descriptors.HandlerDescriptor;
43 import com.sun.enterprise.tools.guiframework.event.descriptors.UseHandlerDescriptor;
44 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
45 import com.sun.enterprise.tools.guiframework.view.descriptors.BasicCommandFieldDescriptor;
46 import com.sun.enterprise.tools.guiframework.view.descriptors.FakeContainerDescriptor;
47 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
48
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Stack JavaDoc;
53
54
55 /**
56  *
57  * @author Garrett Short, garrett.short@sun.com
58  * @author Ken Paulsen, ken.paulsen@sun.com
59  */

60 public class CCBreadCrumbsDescriptor extends ViewDescriptor implements FakeContainerDescriptor {
61
62     /**
63      * Default constructor
64      */

65     public CCBreadCrumbsDescriptor() {
66     this(BREADCRUMB);
67     }
68
69
70     /**
71      * Constructor w/ name
72      */

73     public CCBreadCrumbsDescriptor(String JavaDoc name) {
74     super(name);
75     }
76
77
78     /**
79      * This is a factory method for CCBreadCrumbs instances.
80      *
81      * @param ctx The RequestContext
82      * @param container The container for the newly created
83      */

84     public View getInstance(RequestContext ctx, ContainerView container, String JavaDoc name) {
85     return new CCBreadCrumbs(container, getModel(), name);
86     }
87
88
89     /**
90      *
91      */

92     public void registerChildren(ContainerViewBase instance) {
93     // Invoke the super registerChild
94
// NOTE: Not a real container, but may have child descriptors
95
super.registerChildren(instance);
96
97     // create the model
98
int numberOfCrumbs = getNumberOfBreadCrumbs(
99         instance.getParentViewBean().getRequestContext());
100     for (int i=0; i<numberOfCrumbs; i++) {
101         instance.registerChild(CHILD_BREADCRUMB_LINK+i, BasicCommandField.class);
102     }
103     }
104
105
106     /**
107      * This method returns the number of links in the bread crumb.
108      */

109     public int getNumberOfBreadCrumbs(RequestContext ctx) {
110     CCBreadCrumbsModelInterface model = getModel(ctx);
111     if (model == null) {
112         return 0;
113     }
114
115     // getNumRows() is not defined in any interface
116
return ((DefaultModel)model).getNumRows();
117     }
118
119
120     /**
121      * This method provides access to the Model using the ModelManager. It
122      * also populates the model with all the links. Note: the Model Manager
123      * key is equal to node.getPath() ensuring that Models that have different
124      * node path will be different Models.
125      *
126      * @return The CCBreadCrumbsModelInterface
127      */

128     public CCBreadCrumbsModelInterface getModel() {
129     RequestContext rc = RequestManager.getRequestContext();
130     return getModel(rc);
131     }
132
133
134     /**
135      * This method provides access to the Model using the ModelManager. It
136      * also populates the model with all the links. Note: the Model Manager
137      * key is equal to node.getPath() ensuring that Models that have different
138      * node path will be different Models.
139      *
140      * @param rc The RequestContext
141      *
142      * @return The CCBreadCrumbsModelInterface
143      */

144     protected CCBreadCrumbsModelInterface getModel(RequestContext rc) {
145     // Get the current/selected tree node
146
IndexTreeNode node = Util.getSelectedNode();
147
148     // Use the ModelManager to get/create the CCBreadCrumbsModel
149
// NOTE: This will not set the current node
150
CCBreadCrumbsModelInterface model = (CCBreadCrumbsModelInterface)
151         rc.getModelManager().getModel(CCBreadCrumbsModelInterface.class,
152         node.getPath(), false, false);
153
154     // If the current page label is set, then we've already initialized it
155
// (we know we have the right one because the Model Manager uses the
156
// "name" to register it).
157
if (model.getCurrentPageLabel() != null) {
158         // current model is still valid, return it, else init it
159
return model;
160     }
161         
162     // Initialize the model
163
model.setCurrentPageLabel(node.getDisplayName());
164
165     // Fill the stack w/ the node parents (skip the bottom and top node)
166
Stack JavaDoc stack = getNodeStack(node);
167
168     // Cast to DefaultModel b/c appendRow is not part of any interface
169
DefaultModel defModel = (DefaultModel)model;
170         
171     int crumbCount = 0;
172     while (!stack.empty()) {
173         node = (IndexTreeNode)stack.pop();
174         defModel.appendRow();
175         defModel.setValue(
176         CCBreadCrumbsModel.COMMANDFIELD,
177         CHILD_BREADCRUMB_LINK+crumbCount++);
178         defModel.setValue(CCBreadCrumbsModel.LABEL, node.getDisplayName());
179         defModel.setValue(CCBreadCrumbsModel.MOUSEOVER, node.getDisplayName());
180     }
181
182     // Return the model
183
return model;
184     }
185
186
187     /**
188      * This method is overriden because there are pre-request children that
189      * cannot be cached.
190      */

191     public List JavaDoc getChildDescriptors() {
192         // Get the "real" children (probably none)
193
List JavaDoc descs = new ArrayList JavaDoc(super.getChildDescriptors());
194
195     // Add the virtual children
196
Stack JavaDoc stack = getNodeStack(Util.getSelectedNode());
197     for (int count=0; !stack.empty(); count++) {
198         descs.add(createChildBreadCrumbDescriptor(
199         CHILD_BREADCRUMB_LINK+count, (IndexTreeNode)stack.pop()));
200     }
201
202     return descs;
203     }
204
205
206     /**
207      * Creates a Stack full of IndexTreeNode. The top of the stack is the top
208      * most -1 of the tree. The bottom of the stack is the parent of the
209      * current node.
210      *
211      * @param node The IndexTreeNode to start with (won't be in Stack)
212      *
213      * @return Stack of IndexTreeNode
214      */

215     protected Stack JavaDoc getNodeStack(IndexTreeNode node) {
216     // Fill the stack w/ the node parents (skip the bottom and top node)
217
Stack JavaDoc stack = new Stack JavaDoc();
218     for (node=node.getParent(); (node != null); node=node.getParent()) {
219             String JavaDoc include = (String JavaDoc)node.getAttribute("includeInBreadCrumb", false);
220             if (include != null && include.equals("false"))
221                 continue;
222         stack.push(node);
223     }
224
225     return stack;
226     }
227
228
229     /**
230      * This method is overriden because there are pre-request children that
231      * cannot be cached.
232      */

233     public ViewDescriptor getChildDescriptor(String JavaDoc name) {
234     // Can't call super b/c it will call getChildDescriptors and get the
235
// ones we don't want to be stored in a Map.
236
// super.getChildDescriptors() is ok to call
237

238     // First check to see if its a virtual descriptor
239
if (name.startsWith(CHILD_BREADCRUMB_LINK)) {
240         // Get the link #
241
int linkNum = 0;
242         try {
243         linkNum = Integer.parseInt(name.substring(CHILD_BREADCRUMB_LINK.length()));
244         } catch (NumberFormatException JavaDoc ex) {
245         throw new FrameworkException(
246             "Could not determine link number of: "+name,
247             ex, this, null);
248         }
249
250         // Get the IndexTreeNode Stack
251
Stack JavaDoc stack = getNodeStack(Util.getSelectedNode());
252
253         // Get the node associated with this link. List order is reverse,
254
// so use size-index-1 to get the List index.
255
// This may throw array bound exception, that's ok
256
IndexTreeNode node = (IndexTreeNode)
257         stack.get(stack.size()-linkNum-1);
258
259         // If the last line didn't throw an exception, then they are
260
// requesting a valid bread crumb -- create it.
261
return createChildBreadCrumbDescriptor(name, node);
262     }
263
264     // Not a virutal descriptor... then maybe a different child (unlikely)
265
// Must use super.getChildDescriptors() vs.
266
// super.getChildDescriptor(String) in order to avoid adding virtual
267
// descriptors to super's Map.
268
ViewDescriptor desc = null;
269     Iterator JavaDoc iter = super.getChildDescriptors().iterator();
270     while (iter.hasNext()) {
271         desc = (ViewDescriptor)iter.next();
272         if (desc.getName().equals(name)) {
273         return desc;
274         }
275     }
276
277     // Not found. :(
278
return null;
279     }
280
281
282     /**
283      * This method dynamically adds a child descriptor to accomodate a dynamic
284      * number of links.
285      */

286     protected ViewDescriptor createChildBreadCrumbDescriptor(String JavaDoc name, IndexTreeNode node) {
287     ViewDescriptor desc = new BasicCommandFieldDescriptor(name);
288
289     // set the tree node for simulating a click...
290
desc.addParameter("treeNode", node.getPath());
291
292     // Create an Event Descriptor for handling the "click"
293
EventDescriptor event = new EventDescriptor(desc, EventDescriptor.TYPES.COMMAND);
294     event.addEventHandler(new UseHandlerDescriptor(desc, CLICK_HANDLER));
295     desc.setEventDescriptor(event);
296         desc.setParent(this);
297
298     // Return the new ViewDescriptor
299
return desc;
300     }
301
302
303     // this name must be used in the JSP files
304
public static final String JavaDoc BREADCRUMB = "BreadCrumb";
305     public static final String JavaDoc CHILD_BREADCRUMB_LINK = "BreadCrumbLink";
306
307     public static HandlerDescriptor CLICK_HANDLER;
308     static {
309     CLICK_HANDLER = new HandlerDescriptor("BreadCrumbHandler");
310     CLICK_HANDLER.setHandlerMethod(
311         "com.sun.enterprise.tools.admingui.handlers.BreadCrumbHandler",
312         "handleClick");
313     }
314
315     private CCBreadCrumbsModel _model = null;
316 }
317
Popular Tags