KickJava   Java API By Example, From Geeks To Geeks.

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


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.handlers;
25
26 import com.iplanet.jato.*;
27 import com.iplanet.jato.model.*;
28 import com.iplanet.jato.view.*;
29 import com.iplanet.jato.view.event.*;
30 import com.iplanet.jato.view.html.*;
31 import com.iplanet.jato.util.NonSyncStringBuffer;
32
33 import com.sun.web.ui.taglib.header.*;
34 import com.sun.web.ui.taglib.pagetitle.*;
35 import com.sun.web.ui.common.CCI18N;
36 import com.sun.enterprise.tools.guiframework.view.DescriptorContainerView;
37 import com.sun.enterprise.tools.guiframework.view.HandlerContext;
38 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor;
39 import com.sun.enterprise.tools.guiframework.view.descriptors.CCActionTableDescriptor;
40 import com.sun.enterprise.tools.admingui.util.Util;
41 import com.sun.enterprise.tools.admingui.tree.IndexTreeNode;
42
43 import java.util.EventObject JavaDoc;
44 import java.util.StringTokenizer JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47 import com.sun.enterprise.tools.admingui.ConfigProperties;
48
49 public class HtmlHeaderHandler {
50     
51     private boolean refreshForOpenNodes(RequestContext ctx) {
52         IndexTreeNode node = Util.getSelectedNode();
53         if (node == null)
54             return false;
55         
56         node = node.getParent();
57         if (node == null)
58             return false;
59         
60         // open the parent of the selection node, return true if
61
// anything changed.
62
return node.openNode(ctx);
63     }
64     
65     private void setHeaderDisplayValues(CCHtmlHeaderTag tag, HandlerContext handlerCtx) {
66         String JavaDoc onUnload = (String JavaDoc)handlerCtx.getInputValue("onUnload");
67         if (onUnload != null)
68             tag.setOnUnload(onUnload);
69         
70         String JavaDoc copyRight = (String JavaDoc)handlerCtx.getInputValue("copyRight");
71         if (copyRight != null)
72             tag.setCopyrightYear(copyRight);
73         
74         String JavaDoc title = (String JavaDoc)handlerCtx.getInputValue("pageTitle");
75         if (title != null)
76             tag.setPageTitle(title);
77     }
78     
79     public void beginHtmlHeaderDisplay(RequestContext ctx, HandlerContext handlerCtx) {
80         ViewDescriptor vd = handlerCtx.getViewDescriptor();
81         if (vd.getParent() != null)
82             vd = vd.getParent(); // look for descriptor at the view bean level.
83
Iterator JavaDoc it = vd.getChildDescriptors().iterator();
84         String JavaDoc tableButtonInitScript = "";
85         while (it.hasNext()) {
86             ViewDescriptor child = (ViewDescriptor)it.next();
87             if (child instanceof CCActionTableDescriptor) {
88                 tableButtonInitScript =
89                     (String JavaDoc)handlerCtx.getInputValue("tableButtonInitScript");
90                 // "javascript: if (toggleDisabledState) toggleDisabledState(); ";
91
break;
92             }
93         }
94     if (tableButtonInitScript == null) {
95         tableButtonInitScript = "";
96     }
97         
98         CCHtmlHeaderTag tag = (CCHtmlHeaderTag)handlerCtx.getEvent().getSource();
99         
100         boolean refresh = ctx.getRequest().getAttribute("refreshTree") != null;
101     String JavaDoc onLoad = null;
102         if (refreshForOpenNodes(ctx) || refresh) {
103             onLoad = (String JavaDoc)handlerCtx.getInputValue("onLoadRefreshTree");
104         } else {
105             onLoad = (String JavaDoc)handlerCtx.getInputValue("onLoad");
106         }
107     if (onLoad != null) {
108         tag.setOnLoad(tableButtonInitScript+onLoad);
109     }
110         setHeaderDisplayValues(tag, handlerCtx);
111     }
112     
113     public void beginTreeHtmlHeaderDisplay(RequestContext ctx, HandlerContext handlerCtx) {
114         CCHtmlHeaderTag tag = (CCHtmlHeaderTag)handlerCtx.getEvent().getSource();
115         
116         boolean refresh = ctx.getRequest().getAttribute("refreshRightSide") != null;
117         if (refresh) {
118             String JavaDoc onLoad = (String JavaDoc)handlerCtx.getInputValue("onLoadRefreshRight");
119             if (onLoad != null)
120                 tag.setOnLoad(onLoad);
121         } else {
122             String JavaDoc onLoad = (String JavaDoc)handlerCtx.getInputValue("onLoad");
123             if (onLoad != null)
124                 tag.setOnLoad(onLoad);
125         }
126         ctx.getRequest().removeAttribute("refreshRightSide"); // probaby not needed.
127
setHeaderDisplayValues(tag, handlerCtx);
128     }
129     
130
131     private static CCI18N peHelpIds = null;
132     private static CCI18N eeHelpIds = null;
133     
134     private static String JavaDoc getHelpHTMLName(String JavaDoc key) {
135         Boolean JavaDoc isEE = ConfigProperties.getInstance().getTargetSupported();
136         if (isEE.booleanValue() && eeHelpIds == null) {
137             eeHelpIds = new CCI18N(RequestManager.getRequestContext(),
138                 "com.sun.enterprise.ee.tools.admingui.resources.Helplinks");
139         }
140         if (peHelpIds == null) {
141             peHelpIds = new CCI18N(RequestManager.getRequestContext(),
142                 "com.sun.enterprise.tools.admingui.resources.Helplinks");
143         }
144         String JavaDoc htmlFileName = null;
145         if (eeHelpIds != null) {
146             htmlFileName = eeHelpIds.getMessage(key);
147             if (htmlFileName.equals(key))
148                 htmlFileName = null; // not really found.
149
}
150         if (htmlFileName == null) {
151             htmlFileName = peHelpIds.getMessage(key);
152         }
153         if (htmlFileName != null) {
154             htmlFileName = htmlFileName.trim();
155         }
156         if (htmlFileName == null || htmlFileName.length() == 0 || htmlFileName.equals(key)) {
157             htmlFileName = peHelpIds.getMessage("default");
158         }
159         if (htmlFileName != null) {
160             htmlFileName = htmlFileName.trim();
161         }
162         if (htmlFileName == null || htmlFileName.equals("")) {
163             htmlFileName = key;
164         }
165         if (htmlFileName.endsWith(".html") == false) {
166             htmlFileName += ".html";
167         }
168         return htmlFileName;
169     }
170     
171     private void addHiddenAttrValue(NonSyncStringBuffer buff, RequestContext ctx,
172             IndexTreeNode node, String JavaDoc attrName, boolean lookUpInTreeHierarchy) {
173         String JavaDoc attr = null;
174         Object JavaDoc obj = node.getAttribute(attrName, lookUpInTreeHierarchy);
175         if (obj == null) // values propagated if the user clicks the back button
176
obj = ctx.getRequest().getAttribute(attrName);
177         // no encod
178
if (obj != null)
179             //attr = Util.URLencode(ctx, obj.toString());
180
attr = obj.toString();
181         if (attr != null && attr.length() > 0) {
182             buff.append("\n<input name=\"")
183                 .append(attrName)
184                 .append("\" type=\"hidden\" value=\"")
185                 .append(attr).append("\">");
186         }
187     }
188     
189     private void setCurrentView(RequestContext ctx, View view) {
190         // When refreshing a page from switching tree views, the state
191
// information can be retrieved from the current node of tree, except
192
// for 'editKeyValue' which some pages need in order to draw.
193
String JavaDoc currentView = view.getName();
194         String JavaDoc editKeyValue = (String JavaDoc)ctx.getRequest().getAttribute("editKeyValue");
195         if (editKeyValue != null)
196             currentView += "?editKeyValue="+Util.URLencode(ctx, editKeyValue);
197         
198         Util.setCurrentViewURL(currentView);
199     }
200     
201     // add a hidden variable for the highlight ID and include javascript
202
public String JavaDoc endExtraDisplay(RequestContext ctx, HandlerContext handlerCtx) {
203         ChildContentDisplayEvent ev = (ChildContentDisplayEvent)handlerCtx.getEvent();
204     View bean = handlerCtx.getView();
205
206     while (!(bean instanceof DescriptorContainerView)) {
207         // Should only happen once, if at all.
208
bean = bean.getParent();
209     }
210         // remember the current view (page) so we can go back to it when switching trees.
211
setCurrentView(ctx, bean);
212
213         NonSyncStringBuffer buff = new NonSyncStringBuffer(ev.getContent(), 512);
214         
215         String JavaDoc script = (String JavaDoc)handlerCtx.getInputValue("script") ;
216         if (script != null) {
217             buff.append("\n").append(script);
218     }
219
220         
221     IndexTreeNode selectedNode = Util.getSelectedNode();
222         if (selectedNode != null) {
223             String JavaDoc id = selectedNode.getHighlightIDPath();
224             buff.append("\n<input name=\"highlightid\" type=\"hidden\" value=\"")
225                 .append(id).append("\">");
226             
227             addHiddenAttrValue(buff, ctx, selectedNode, IndexTreeNode.INSTANCE_NAME, true);
228             addHiddenAttrValue(buff, ctx, selectedNode, IndexTreeNode.CONFIG_NAME, true);
229             addHiddenAttrValue(buff, ctx, selectedNode, IndexTreeNode.CLUSTER_NAME, true);
230             addHiddenAttrValue(buff, ctx, selectedNode, IndexTreeNode.NODEAGENT_NAME, true);
231             addHiddenAttrValue(buff, ctx, selectedNode, IndexTreeNode.APPLICATION_TYPE, true);
232             addHiddenAttrValue(buff, ctx, selectedNode, IndexTreeNode.OBJECT_NAME, false);
233         }
234         
235         String JavaDoc helpID = (String JavaDoc)handlerCtx.getInputValue("helpID") ;
236         if (helpID == null)
237             helpID = bean.getName();
238         String JavaDoc helpHTMLName = getHelpHTMLName(helpID);
239         buff.append("\n<input name=\"helplink\" type=\"hidden\" value=\"")
240             .append(helpHTMLName).append("\">");
241         
242         return buff.toString();
243     }
244
245     public String JavaDoc endResourceStringsDisplay(RequestContext ctx, HandlerContext handlerCtx) {
246         ChildContentDisplayEvent ev = (ChildContentDisplayEvent)handlerCtx.getEvent();
247         String JavaDoc keys = ev.getContent();
248         if (keys == null | keys.length() == 0)
249             return "";
250         
251         String JavaDoc result = "";
252         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(keys);
253         while (st.hasMoreTokens()) {
254             String JavaDoc key = st.nextToken().trim();
255             if (key.length() == 0)
256                 continue;
257             result += "<input name=\"" + key + "\" type=\"hidden\" value=\"" +
258                         Util.getMessage(key) + "\">\n";
259         }
260         return result;
261     }
262        
263      public void beginLinksPageTitleDisplay(RequestContext ctx, HandlerContext handlerCtx) {
264          CCPageTitleTag tag = (CCPageTitleTag)handlerCtx.getEvent().getSource();
265          IndexTreeNode selectedNode = Util.getSelectedNode();
266          tag.setPageTitleText(selectedNode.getName());
267      }
268
269      public String JavaDoc endChildPageLinkDisplay(RequestContext ctx, HandlerContext handlerCtx) {
270         final String JavaDoc hrefExtras = "title=\"@@@@\" " +
271             "onmouseover=\"window.status='@@@@'; return true\" " +
272             "onmouseout=\"window.status=''; return true\" " +
273             "onblur=\"window.status=''; return true\" " +
274             "onfocus=\"window.status='@@@@'; return true\" ";
275
276         ChildContentDisplayEvent ev = (ChildContentDisplayEvent)handlerCtx.getEvent();
277         String JavaDoc html = ev.getContent();
278         String JavaDoc replace = (String JavaDoc)handlerCtx.getInputValue("replace");
279         if (Util.isEmpty(replace))
280             replace = "links";
281     IndexTreeNode selectedNode = Util.getSelectedNode();
282         if (selectedNode != null) {
283             // don't need the name.
284
html = Util.removeHtmlProp(html, "name");
285             // change the href location...
286
html = html.replaceAll(replace, "Index");
287             html = html.replaceAll("childPageLink", "TreeView1.showContainer");
288             
289             // get the child index
290
int i = html.indexOf("__");
291             int j = html.indexOf("__", i+2);
292             if (i < 0 || j < 0)
293                 return "";
294             int index = new Integer JavaDoc(html.substring(i+2, j)).intValue();
295
296             // put the child specific info into the tag
297
IndexTreeNode kid = (IndexTreeNode)selectedNode.getChildren().get(index);
298             if (kid == null)
299                 return "";
300             html = html.replaceAll(html.substring(i, j+2), kid.getName());
301             html = html.replaceAll("_VALUE_", kid.getPath());
302             
303             html = Util.addHtmlProp(html, hrefExtras.replaceAll("@@@@", kid.getName()));
304             return html;
305         }
306         return "";
307     }
308
309 }
310
Popular Tags