KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > tree > IndexTreeView


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.tree;
25
26 import java.io.*;
27 import java.lang.reflect.*;
28 import java.util.*;
29 import javax.servlet.*;
30 import javax.servlet.http.*;
31 import javax.management.ObjectName JavaDoc;
32
33 import com.iplanet.jato.*;
34 import com.iplanet.jato.model.*;
35 import com.iplanet.jato.util.*;
36 import com.iplanet.jato.view.*;
37 import com.iplanet.jato.view.event.*;
38 import com.iplanet.jato.view.html.*;
39
40 import com.sun.enterprise.tools.admingui.util.Util;
41 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
42 import com.sun.enterprise.tools.admingui.handlers.TargetHandlers;
43 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
44
45
46 public class IndexTreeView extends RequestHandlingTreeViewBase {
47     public static final String JavaDoc CHILD_NAME = "name";
48     public static final String JavaDoc CHILD_SHOW_ROOT = "root";
49     public static final String JavaDoc CHILD_SHOW_LEAF = "showLeaf";
50     public static final String JavaDoc CHILD_SHOW_CONTAINER = "showContainer";
51     
52     public static final String JavaDoc CHILD_SHOW_ROOT_I = "Iroot";
53     public static final String JavaDoc CHILD_SHOW_LEAF_I = "IshowLeaf";
54     public static final String JavaDoc CHILD_SHOW_CONTANER_I = "IshowContainer";
55     
56     public static final String JavaDoc CHILD_HANDLE_IMAGE = "Handle";
57
58     private NewTagsText newTags = null;
59
60     public IndexTreeView(View parent, String JavaDoc name) {
61     super(parent, name, //"IndexTreeStateData");
62
Util.getCurrentTreeModel().getStateDataName());
63     
64     setPrimaryModel(Util.getCurrentTreeModel());
65     registerChildren();
66     }
67     
68     protected IndexTreeModel getIndexTreeModel() {
69     return (IndexTreeModel)getPrimaryModel();
70     }
71     
72     protected void registerChildren() {
73     registerChild(CHILD_NAME, StaticTextField.class);
74     registerChild(CHILD_SHOW_ROOT, HREF.class);
75     registerChild(CHILD_SHOW_LEAF, HREF.class);
76     registerChild(CHILD_SHOW_CONTAINER, HREF.class);
77     
78     registerChild(CHILD_SHOW_ROOT_I, ImageField.class);
79     registerChild(CHILD_SHOW_LEAF_I, ImageField.class);
80     registerChild(CHILD_SHOW_CONTANER_I,ImageField.class);
81     registerChild(CHILD_HANDLE_IMAGE, ImageField.class);
82     }
83     
84     protected View createChild(String JavaDoc name) {
85     if (name.equals(CHILD_NAME)) {
86         StaticTextField child = new StaticTextField(this, getIndexTreeModel(), CHILD_NAME,
87         IndexTreeModel.FIELD_NAME, "", null);
88         return child;
89     }
90     else if (name.equals(CHILD_SHOW_ROOT) ||
91                 name.equals(CHILD_SHOW_LEAF) ||
92                 name.equals(CHILD_SHOW_CONTAINER)) {
93         return new HREF(this, name, null);
94     }
95     else if (name.equals(CHILD_SHOW_ROOT_I) ||
96                 name.equals(CHILD_HANDLE_IMAGE) ||
97                 name.equals(CHILD_SHOW_LEAF_I) ||
98                 name.equals(CHILD_SHOW_CONTANER_I)) {
99         return new ImageField(this, name, null);
100     }
101     else
102         return null;
103     }
104     
105     private static Object JavaDoc osync = new Object JavaDoc(); // Synchronizer object
106

107     // an inner class to handle tag reformatting to support node highlighting in the tree.
108
class NewTagsText {
109         public final String JavaDoc beginTD1 = "<td align=\"left\" valign=\"center\" width=\"24\" nowrap=\"nowrap\">";
110         public final String JavaDoc beginTD2 = "<td align=\"left\" valign=\"center\" nowrap=\"nowrap\">";
111         public final String JavaDoc endTD = "</td>\n";
112         public final String JavaDoc restartImg = "<img SRC=\"../images/restartServer.gif\" alt=\"restart\" border=\"0\"/>";
113         public final String JavaDoc hrefExtras = "title=\"@@@@\" " +
114             "onmouseover=\"window.status='@@@@'; return true\" " +
115             "onmouseout=\"window.status=''; return true\" " +
116             "onkeypress=\"javascript: this.onClick();\" " +
117             "onblur=\"window.status=''; return true\" " +
118             "onfocus=\"window.status='@@@@'; return true\" ";
119
120     private String JavaDoc imgTag;
121     private String JavaDoc hrefTag;
122         private Stack sibs;
123         public boolean rootNode;
124     
125     public NewTagsText() {
126         reset();
127     }
128     private void reset() {
129         imgTag = null;
130         hrefTag = null;
131             sibs = null;
132             rootNode = false;
133     }
134         public boolean hasNextSibling() {
135             if (sibs == null) {
136                 synchronized (osync) {
137                     // add a synchronize here to protect the value of the current
138
// node tin the model. getNextSibling() changes the current
139
// node.
140
sibs = new Stack();
141                     IndexTreeModel model = (IndexTreeModel)getPrimaryModel();
142                     IndexTreeNode node = (IndexTreeNode)(model.getCurrentNode());
143                     int level = ((IndexTreeModel)getPrimaryModel()).getNodeLevel();
144                     //System.out.println("NEW STACK: level="+level);
145
while (node != null) {
146                         //System.out.print(" Parent Node: "+node.getName()+" "+node);
147
if (level > 1 && node.getParent() == null) {
148                             // jato is doing weird stuff, don't do the root node
149
// unless it's a child of the root.
150
break;
151                         }
152                         Boolean JavaDoc bool = new Boolean JavaDoc(node.getNextSibling() != null);
153                         //System.out.print(" Pushing: "+bool.booleanValue());
154

155                         // Push twice because the JSP calls twice, the posittive and
156
// negative versions of the content tags.
157
sibs.push(bool);
158                         sibs.push(bool);
159                         node = node.getParent();
160                     }
161                 }
162             }
163             if (sibs.empty()) {
164                 //throw new FrameworkException(
165
if (Util.isLoggableFINE()) {
166                     Util.logFINE("IndexTreeView.hasNextSibling: Next Sibling Stack is Empty!", null);
167                 }
168                 return false;
169             }
170             Boolean JavaDoc answer = (Boolean JavaDoc)sibs.pop();
171             return answer.booleanValue();
172         }
173     public void setImgTag(String JavaDoc s) {
174         imgTag = s;
175     }
176     public void setHrefTag(String JavaDoc s) {
177         hrefTag = s;
178     }
179     private String JavaDoc addHref(String JavaDoc text, String JavaDoc hrefTag) {
180         if (hrefTag == null)
181         return text;
182         int i = hrefTag.indexOf('>');
183         if (i<0)
184         return text;
185         return hrefTag.substring(0, i+1)+"\n"+text+"</a>";
186     }
187  
188     public String JavaDoc toString() {
189         // get the Highlight ID for the current node being processed.
190
String JavaDoc id = ((IndexTreeModel)getPrimaryModel()).getNodeHID();
191         boolean expanded = getStateData().isNodeExpanded(getPrimaryModel().getNodeID());
192         String JavaDoc icon = ((IndexTreeModel)getPrimaryModel()).getIconName(expanded);
193         
194         IndexTreeNode node = ((IndexTreeNode)((IndexTreeModel)getPrimaryModel()).getCurrentNode());
195             
196         // sanity checks.... shouldn't return from here
197
if (imgTag == null && hrefTag == null)
198         return "";
199         if (imgTag != null && hrefTag == null)
200         return imgTag;
201         if (imgTag == null && hrefTag != null)
202         return hrefTag;
203         
204             // may want to show a status icon?
205
String JavaDoc status = "";
206             String JavaDoc showStatus = (String JavaDoc) node.getAttribute("showStatus", false);
207             try {
208             if (showStatus != null && showStatus.equalsIgnoreCase("true")) {
209                 String JavaDoc objName = (String JavaDoc) node.getAttribute("objectName", false);
210                 status = "&nbsp;"+TargetHandlers.getStatusIconHtml(new ObjectName JavaDoc(objName));
211             }
212             } catch (Exception JavaDoc ex) {
213                 // ignore
214
}
215
216             // modify the JavaScript by replacing the "this" parameter with the highlight node ID
217
hrefTag = hrefTag.replaceAll("this", id);
218             
219             // check for a special display name for the node.
220
String JavaDoc displayName = (String JavaDoc)
221                 node.getAttribute(IndexTreeModel.FIELD_DISPLAYNAME, false);
222             if (displayName == null) {
223                 displayName = node.getName();
224             }
225             
226             // special case labeling of the admin server instance & config
227
String JavaDoc adminServerName = (String JavaDoc) node.getAttribute("adminServerName", false);
228             if (adminServerName != null && displayName.equalsIgnoreCase(adminServerName)) {
229                 displayName += " (" + Util.getMessage("tree.adminServer") + ")";
230                 String JavaDoc asIcon = Util.getMessage("tree.adminServerImage"); //"adminserver.gif";
231
if (asIcon != null && asIcon.length() > 0)
232                     icon = asIcon;
233             }
234             String JavaDoc adminServerConfigName = (String JavaDoc) node.getAttribute("adminServerConfigName", false);
235             if (adminServerConfigName != null && displayName.equalsIgnoreCase("server-config")) {
236                 displayName += " (" + Util.getMessage("tree.adminServerConfig") + ")";
237                 String JavaDoc asIcon = Util.getMessage("tree.adminServerConfigImage");
238                 if (asIcon != null && asIcon.length() > 0)
239                     icon = asIcon;
240             }
241             if (displayName.equals(node.getName()) == false) { // replace the name in the a-tag
242
hrefTag = addHref(displayName, hrefTag);
243             }
244             
245             hrefTag = Util.addHtmlProp(hrefTag, hrefExtras.replaceAll("@@@@", displayName));
246             String JavaDoc hrefImageTag = hrefTag;
247         
248         // change the name attribute of the img tag
249
String JavaDoc newImgTag = Util.addHtmlProp(Util.removeHtmlProp(imgTag, "name"), ("name=\"i" + id + "\"")); // for ns4
250
if (rootNode) {
251                 Object JavaDoc obj = RequestManager.getRequestContext()
252                             .getRequest().getAttribute("restartRequired");
253                 boolean replaceIcon = false;
254                 if (obj != null) {
255                     if (obj instanceof Boolean JavaDoc) {
256                         replaceIcon = ((Boolean JavaDoc)obj).booleanValue();
257                     } else if (obj instanceof String JavaDoc) {
258                         replaceIcon = "true".equalsIgnoreCase((String JavaDoc)obj);
259                     }
260                 }
261                 if (replaceIcon) {
262                     String JavaDoc rrIcon = Util.getMessage("icon.InstanceRestartRequired");
263                     if (rrIcon != null && rrIcon.equals("")==false)
264                         icon = rrIcon;
265
266                     String JavaDoc rrText = Util.getMessage("masthead.restartRequired");
267                     hrefImageTag = Util.addHtmlProp(Util.removeHtmlProp(hrefTag, "title"), ("title=\"" + rrText + "\""));
268                     hrefImageTag = Util.addHtmlProp(Util.removeHtmlProp(hrefImageTag, "href"), ("href=\"../admingui/restart\""));
269                 }
270             }
271             newImgTag = Util.addHtmlProp(Util.removeHtmlProp(newImgTag, "src"), "src=\"../images/"+icon+"\"");
272             
273         // build the new tags, first surround the img tab in an a-tag.
274
String JavaDoc newTag = beginTD1 + addHref(newImgTag, hrefImageTag) + endTD;
275
276             // add an ID attribute to the TD-tag and add the href tag
277
newTag += beginTD2 + Util.addHtmlProp(hrefTag, "id=\"a"+id+"\"") + status + endTD;
278         
279         reset(); // be ready to start again.
280
return newTag.trim();
281     }
282     }
283     
284     public TreeViewStateData getStateData() {
285         // override JATO function to make sure the correct state data is being used.
286
String JavaDoc dataName = Util.getCurrentTreeModel().getStateDataName();
287         if (getStateDataLookupName().equals(dataName) == false) {
288             setStateDataLookupName(dataName);
289             TreeViewStateData data = (TreeViewStateData)
290                 getRequestContext().getRequest().getSession().getAttribute(dataName);
291
292             if (data == null)
293                 data = new SimpleTreeViewStateData(new HashMap());
294
295             super.setStateData(data);
296         }
297         return super.getStateData();
298     }
299     
300     public void beginDisplay(DisplayEvent event) throws ModelControlException {
301         IndexTreeModel model = Util.getCurrentTreeModel();
302     setPrimaryModel(model);
303
304     // Ensure the primary model is non-null; if null, it would cause
305
// havoc with the various methods dependent on the primary model
306
if (getPrimaryModel()==null)
307         throw new ModelControlException("Primary model is null");
308     
309     super.beginDisplay(event);
310     
311     model.beforeRoot();
312     // move the root node and open it.
313
model.nextNode();
314     getStateData().setNodeExpanded(model.getNodeID(), true);
315         
316         IndexTreeNode root = model.getRoot();
317         String JavaDoc count = (String JavaDoc)root.getAttribute("kidsToOpen");
318         if (count != null) {
319             int max = new Integer JavaDoc(count).intValue();
320             root.setAttribute("kidsToOpen", null);
321             Iterator it = root.getChildren().iterator();
322             int i = 0;
323             // open the first two top level nodes for PE only.
324
while (it.hasNext()) {
325                 if (i >= max) break;
326                 getStateData().setNodeExpanded(((IndexTreeNode)it.next()).getPath(), true);
327                 i++;
328             }
329         }
330     newTags = new NewTagsText();
331     }
332     
333     // The next set of methods come in pairs. The image tag is first and is
334
// saved. It is then written with the a-tag, which should immediately follow.
335
public String JavaDoc endIrootDisplay(ChildContentDisplayEvent event) {
336     String JavaDoc content = event.getContent();
337     newTags.setImgTag(content);
338     return "";
339     }
340     public String JavaDoc endRootDisplay(ChildContentDisplayEvent event) {
341     String JavaDoc content = event.getContent();
342     newTags.setHrefTag(content);
343         newTags.rootNode = true;
344     String JavaDoc newContent = newTags.toString();
345     return newContent;
346     }
347     public String JavaDoc endIshowLeafDisplay(ChildContentDisplayEvent event) {
348     String JavaDoc content = event.getContent();
349     newTags.setImgTag(content);
350     return "";
351     }
352     public String JavaDoc endShowLeafDisplay(ChildContentDisplayEvent event) {
353     String JavaDoc content = event.getContent();
354     newTags.setHrefTag(content);
355     String JavaDoc newContent = newTags.toString();
356     return newContent;
357     }
358     public String JavaDoc endIshowContainerDisplay(ChildContentDisplayEvent event) {
359     String JavaDoc content = event.getContent();
360     newTags.setImgTag(content);
361     return "";
362     }
363     public String JavaDoc endShowContainerDisplay(ChildContentDisplayEvent event) {
364     String JavaDoc content = event.getContent();
365     newTags.setHrefTag(content);
366     String JavaDoc newContent = newTags.toString();
367     return newContent;
368     }
369     
370     // Only used for debugging; an easy way to show the ID on the page.
371
public String JavaDoc endGetIdDisplay(ChildContentDisplayEvent event) {
372     String JavaDoc id = ((IndexTreeModel)getPrimaryModel()).getNodeHID();
373     return id;
374     }
375     
376     public String JavaDoc endTableRowDisplay(ChildContentDisplayEvent event) {
377         String JavaDoc tr = "<tr ";
378     IndexTreeNode node = ((IndexTreeNode)((IndexTreeModel)getPrimaryModel()).getCurrentNode());
379         
380         if (node.getType().equalsIgnoreCase(IndexTreeModel.ROOT)) {
381             String JavaDoc rootClass = (String JavaDoc) node.getAttribute("rootClass");
382             if (rootClass == null)
383                 rootClass = "TreRotRow"; // or "treTbl"
384
// The root nodes need a different bg color and hieght must be 30px
385
// The letter before the id telling javascript how to highlight.
386
tr += "height=\"30\" class=\""+rootClass+"\" id=\"r" + node.getHighlightID() + "\">";
387         } else {
388             tr += "id=\"c" + node.getHighlightID() + "\">";
389         }
390         return tr;
391     }
392     
393     // This wraps the content between the <td> </td> tags
394
// This is needed because the must not be a newline before the </td>
395
// or the table cell will be the wrong size.
396
public String JavaDoc endAddTdDisplay(ChildContentDisplayEvent event) {
397     String JavaDoc content = event.getContent();
398         return newTags.beginTD2 + content.trim() + newTags.endTD;
399     }
400     
401     
402     public boolean beginHasNextSiblingDisplay(ChildDisplayEvent event) {
403         return newTags.hasNextSibling();
404     }
405     public boolean beginHasNoNextSiblingDisplay(ChildDisplayEvent event) {
406     return (newTags.hasNextSibling() == false);
407     }
408
409     public boolean beginHasContainerKidsDisplay(ChildDisplayEvent event) {
410     IndexTreeNode node = getIndexTreeModel().getNode(getPrimaryModel().getNodeID());
411     return node.getChildren().size() > 0;
412     }
413     public boolean beginHasNoContainerKidsDisplay(ChildDisplayEvent event) {
414     return ! beginHasContainerKidsDisplay(event);
415     }
416     
417     public boolean beginShowContainerDisplay(ChildDisplayEvent event) {
418     HREF HREF=(HREF)getDisplayField(CHILD_SHOW_CONTAINER);
419     href.setValue(getIndexTreeModel().getValue(IndexTreeModel.FIELD_ID));
420     
421     IndexTreeNode node = getIndexTreeModel().getNode(getPrimaryModel().getNodeID());
422     node.setIsExpanded(getStateData().isNodeExpanded(getPrimaryModel().getNodeID()));
423     return true;
424     }
425     
426     public boolean beginRootDisplay(ChildDisplayEvent event) {
427     HREF HREF=(HREF)getDisplayField(CHILD_SHOW_ROOT);
428     href.setValue(getIndexTreeModel().getValue(IndexTreeModel.FIELD_ID));
429     
430     IndexTreeNode node = getIndexTreeModel().getNode(getPrimaryModel().getNodeID());
431     node.setIsExpanded(getStateData().isNodeExpanded(getPrimaryModel().getNodeID()));
432     return true;
433     }
434     
435     public boolean beginShowLeafDisplay(ChildDisplayEvent event) {
436     // We do this here so that the field can remain bound to the default model
437
HREF HREF=(HREF)getDisplayField(CHILD_SHOW_LEAF);
438     href.setValue(getIndexTreeModel().getValue(IndexTreeModel.FIELD_ID));
439     
440     IndexTreeNode node = getIndexTreeModel().getNode(getPrimaryModel().getNodeID());
441     node.setIsExpanded(getStateData().isNodeExpanded(getPrimaryModel().getNodeID()));
442     return true;
443     }
444     
445     private void handleNode(String JavaDoc nodeID, RequestInvocationEvent event)
446     throws ModelControlException, ClassNotFoundException JavaDoc, ServletException, IOException, NodeNotFoundException {
447     IndexTreeNode selectedNode = getIndexTreeModel().getNode(nodeID);
448     selectedNode.handleSelection(getRequestContext());
449     }
450     
451     public void handleShowLeafRequest(RequestInvocationEvent event)
452     throws ModelControlException, ClassNotFoundException JavaDoc, ServletException, IOException, NodeNotFoundException {
453     handleNode((String JavaDoc)getDisplayFieldValue(CHILD_SHOW_LEAF), event);
454     }
455     
456     public void handleRootRequest(RequestInvocationEvent event)
457     throws ModelControlException, ClassNotFoundException JavaDoc, ServletException, IOException, NodeNotFoundException {
458     handleNode((String JavaDoc)getDisplayFieldValue(CHILD_SHOW_ROOT), event);
459     }
460     
461     public void handleShowContainerRequest(RequestInvocationEvent event)
462     throws ModelControlException, ClassNotFoundException JavaDoc, ServletException, IOException, NodeNotFoundException {
463     handleNode((String JavaDoc)getDisplayFieldValue(CHILD_SHOW_CONTAINER), event);
464     }
465 }
466
Popular Tags