KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > handlers > TreeHandlers


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 /*
25  * CommonHandlers.java
26  *
27  */

28
29 package com.sun.enterprise.tools.admingui.handlers;
30
31 import java.util.ArrayList JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import com.iplanet.jato.RequestContext;
34 import com.iplanet.jato.RequestContextImpl;
35 import com.iplanet.jato.RequestManager;
36 import com.iplanet.jato.view.ViewBean;
37
38 import com.sun.enterprise.tools.admingui.util.Util;
39 import com.sun.enterprise.tools.admingui.tree.IndexTreeNode;
40 import com.sun.enterprise.tools.admingui.tree.IndexTreeModelImpl;
41 import com.sun.enterprise.tools.admingui.tree.IndexTreeModel;
42
43 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
44 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
45
46 /**
47  *
48  */

49 public class TreeHandlers {
50
51     public void refreshTreeNode(RequestContext ctx, HandlerContext handlerCtx) {
52         ctx.getRequest().setAttribute("refreshTree", new Boolean JavaDoc("true"));
53         Util.getSelectedNode().setRefresh(true);
54     }
55
56     public void refreshTreeNodeById(RequestContext ctx, HandlerContext handlerCtx) {
57         ctx.getRequest().setAttribute("refreshTree", new Boolean JavaDoc("true"));
58         String JavaDoc nodeId = (String JavaDoc)handlerCtx.getInputValue("nodeId");
59         IndexTreeNode node =
60             Util.getCurrentTreeModel().getNodeByUniqueID(nodeId, null, null);
61         if (node != null)
62             node.setRefresh(true);
63     }
64
65     public void refreshParentNode(RequestContext ctx, HandlerContext handlerCtx) {
66         ctx.getRequest().setAttribute("refreshTree", new Boolean JavaDoc("true"));
67         Util.getSelectedNode().getParent().setRefresh(true);
68     }
69
70     public void openTreeNode(RequestContext ctx, HandlerContext handlerCtx) {
71         Util.getSelectedNode().openNode(ctx);
72     }
73     
74     public void getNumberOfChildNodes(RequestContext ctx, HandlerContext handlerCtx) {
75         int count = Util.getSelectedNode().getChildren().size();
76         handlerCtx.setOutputValue("count", new Integer JavaDoc(count));
77     }
78     
79     public void getAttributeFromTree(RequestContext ctx, HandlerContext handlerCtx) {
80         HttpServletRequest JavaDoc req = ctx.getRequest();
81         String JavaDoc attrName = (String JavaDoc)handlerCtx.getInputValue("attrName");
82         boolean lookInHierarchy = true;
83         Boolean JavaDoc look = (Boolean JavaDoc)handlerCtx.getInputValue("lookInHierarchy");
84         if (look != null)
85             lookInHierarchy = look.booleanValue();
86         Object JavaDoc attrValue = Util.getSelectedNode().getAttribute(attrName, lookInHierarchy);
87         handlerCtx.setOutputValue(VALUE, attrValue);
88     }
89
90     public void changeTreeModel(RequestContext ctx, HandlerContext handlerCtx) {
91         Util.setCurrentTreeModelType(ctx, (String JavaDoc)handlerCtx.getInputValue("modelName"));
92         // set a flag to make the tree view refresh the right side with a new page.
93
ctx.getRequest().setAttribute("refreshRightSide", new Boolean JavaDoc("true"));
94     }
95
96     public void getTreeModelType(RequestContext ctx, HandlerContext handlerCtx) {
97         handlerCtx.setOutputValue(VALUE, Util.getCurrentTreeModelType(ctx));
98     }
99     
100     // This method needs to be called when a child node is selected from the right
101
// hand frame. This is required to make the tree highlighting and the breadcrumb
102
// track properly.
103
public void moveSelectedNode(RequestContext ctx, HandlerContext handlerCtx) {
104         String JavaDoc childName = (String JavaDoc)handlerCtx.getInputValue("editKeyValue");
105         if (childName == null) {
106         throw new FrameworkException("\"editKeyValue\" not defined!",
107         null, handlerCtx.getView());
108         }
109         IndexTreeNode selectedNode = Util.getSelectedNode();
110         IndexTreeNode childNode = selectedNode.getChild(childName);
111         if (childNode == null) {
112             if (Util.isLoggableFINE()) {
113                 Util.logFINE("Child node ("+childName+") not found!");
114             }
115             handlerCtx.setOutputValue(OBJECT_NAME, null);
116             return;
117         //throw new FrameworkException("Child node ("+childName+
118
// ") not found!", null, handlerCtx.getView());
119
}
120         Util.setSelectedNode(childNode);
121         handlerCtx.setOutputValue(OBJECT_NAME, childNode.getAttribute(OBJECT_NAME, false));
122     }
123     
124     public void warpToNode(RequestContext ctx, HandlerContext handlerCtx) {
125         ArrayList JavaDoc parentIDs = (ArrayList JavaDoc)handlerCtx.getInputValue("parentID");
126         if (parentIDs == null) {
127         throw new FrameworkException("warpToNode: \"parentID\" not defined!",
128         null, handlerCtx.getView());
129         }
130         String JavaDoc childName = (String JavaDoc)handlerCtx.getInputValue("childName");
131         
132         IndexTreeNode childNode = null;
133         for (int i=0; i<parentIDs.size(); i++) {
134             String JavaDoc parentID = (String JavaDoc)parentIDs.get(i);
135             childNode =
136                 Util.getCurrentTreeModel().getNodeByUniqueID(parentID, childName, null);
137             if (childNode != null)
138                 break;
139         }
140         if (childNode == null)
141             return;
142         
143         String JavaDoc grandChildId = (String JavaDoc)handlerCtx.getInputValue("grandChildId");
144         if (grandChildId != null) {
145             IndexTreeNode node = Util.getCurrentTreeModel().
146                 getNodeByUniqueID(grandChildId, null, childNode);
147             if (node != null)
148         childNode = node;
149         }
150         
151         IndexTreeNode parentNode = childNode.getParent();
152         if (parentNode != null) { // parentNode is null if childNode is root
153
parentNode.setRefresh(true);
154             parentNode.openNode(ctx);
155         }
156         Util.setSelectedNode(childNode);
157         handlerCtx.setOutputValue(OBJECT_NAME, childNode.getAttribute(OBJECT_NAME, false));
158     }
159
160     // Typically called for a back button on a page.
161
public void moveSelectedNodeToParent(RequestContext ctx, HandlerContext handlerCtx) {
162         IndexTreeNode selectedNode = Util.getSelectedNode();
163         IndexTreeNode parent = selectedNode.getParent();
164         Util.setSelectedNode(parent);
165         if (parent != null)
166             handlerCtx.setOutputValue(OBJECT_NAME, parent.getAttribute(OBJECT_NAME, false));
167         else
168             // should never happen
169
handlerCtx.setOutputValue(OBJECT_NAME, null);
170     }
171     
172     // This method is used to set the current node of the tree to the first node
173
// under the root. This is required for the tree highlighting and bread
174
// crumb to work properly. It needs to be called only once at begin display
175
// of the TopFrameSet
176
public void initializeCurrentNode(RequestContext ctx, HandlerContext handlerCtx) {
177         IndexTreeModel model = Util.getCurrentTreeModel();
178         IndexTreeNode root = model.getRoot();
179         if (root != null) {
180             model.setCurrentNode(root);
181             Util.setSelectedNode(root);
182         }
183     }
184
185     /**
186      * This procedure provides a quick check to make sure the tree view is
187      * showing the correct number of nodes. This method should be called
188      * when filling the table. An attribute will have the objects which are
189      * about to be loaded in the table. A check of the correct counts is made.
190      * If there is a mismatch in the counts, the tree will be refreashed.
191      */

192     public void syncTreeView(RequestContext ctx, HandlerContext handlerCtx) {
193         Object JavaDoc[] keys = (Object JavaDoc[])handlerCtx.getInputValue("keys");
194         if (keys == null)
195             return;
196         
197         IndexTreeNode node = Util.getSelectedNode();
198         if (node == null)
199             return;
200         
201         if (node.getChildren().size() != keys.length) {
202             ctx.getRequest().setAttribute("refreshTree", new Boolean JavaDoc("true"));
203             node.setRefresh(true);
204         }
205     }
206     
207     public static final String JavaDoc VALUE = "value";
208     public static final String JavaDoc OBJECT_NAME = "objectName";
209 }
210
Popular Tags