KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > Tree


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui.web;
20
21 import org.objectweb.jac.aspects.gui.*;
22 import org.objectweb.jac.core.Wrappee;
23 import org.objectweb.jac.core.rtti.CollectionItem;
24 import java.io.PrintWriter JavaDoc;
25 import java.util.HashSet JavaDoc;
26
27 /**
28  * This class defines a Swing component tree view for objects that are
29  * related to a root object through relations or collections.
30  *
31  * @see GuiAC */

32
33 public class Tree extends AbstractView
34     implements View, HTMLViewer, TreeListener
35 {
36     TreeModel model;
37     boolean showRelations = true;
38     RootNode rootNode = null;
39     HashSet JavaDoc expandedNodes = new HashSet JavaDoc();
40
41     /**
42      * Constructs a new tree view.
43      * @param pathDef designate root objects of the tree
44      * @param showRelations wether to build a node for relation items
45      */

46     public Tree(ViewFactory factory, DisplayContext context,
47                 String JavaDoc pathDef, boolean showRelations) {
48         super(factory,context);
49         this.showRelations = showRelations;
50
51         rootNode = new RootNode();
52         model = new TreeModel( rootNode, pathDef, showRelations );
53     }
54     // interface TreeView
55

56     public void close(boolean validate) {
57         model.unregisterEvents();
58     }
59
60     public void genHTML(PrintWriter JavaDoc out) {
61         genNode(out,(AbstractNode)model.getRoot(),"");
62     }
63
64     protected boolean isExpanded(AbstractNode node) {
65         return expandedNodes.contains(node);
66     }
67
68     protected void genNode(PrintWriter JavaDoc out, AbstractNode node, String JavaDoc curPath) {
69         String JavaDoc nodePath = node instanceof RootNode ? "/" : curPath;
70         String JavaDoc nodeClass = node.isLeaf() ? "leafNode" : "treeNode";
71         boolean isRoot = node == model.getRoot();
72
73         if (!isRoot) {
74             out.println("<div class=\""+nodeClass+"\">");
75             if (!node.isLeaf()) {
76                 if (isExpanded(node))
77                     out.print(" <a HREF=\""+eventURL("onCollapseNode")+
78                               "&amp;nodePath="+nodePath+"\" class=\"fixed\">[-]</a>");
79                 else
80                     out.print(" <a HREF=\""+eventURL("onExpandNode")+
81                               "&amp;nodePath="+nodePath+"\" class=\"fixed\">[+]</a>");
82             }
83             out.println(iconElement(node.getIcon(),"")+
84                         " <a HREF=\""+eventURL("onSelectNode")+"&amp;nodePath="+
85                         nodePath+"\">"+node.getText()+"</a>");
86         }
87
88         if (isExpanded(node) || isRoot) {
89             out.println(" <div class=\""+(isRoot?"rootNodes":"nodes")+"\">");
90             for (int i=0; i<node.getChildCount();i++) {
91                 genNode(out,(AbstractNode)node.getChildAt(i),curPath+"/"+i);
92             }
93             out.println(" </div>");
94         }
95         if (!isRoot) {
96             out.println("</div>");
97         }
98     }
99
100     // TreeListener interface
101

102     public void onSelectNode(String JavaDoc nodePath) {
103         try {
104             AbstractNode node = pathToNode(nodePath);
105             logger.debug("onSelectNode path="+nodePath+" -> "+node);
106             if (node == null)
107                 return;
108             logger.debug(" node="+node);
109             Object JavaDoc selected = node.getUserObject();
110             logger.debug(" selected="+selected);
111             if (selected instanceof CollectionItem) {
112                 context.getDisplay().refresh();
113             } else {
114                 EventHandler.get().onNodeSelection(context,node,true);
115             }
116         } catch (Exception JavaDoc e) {
117             context.getDisplay().showError("Error",e.toString());
118         }
119     }
120
121     public void onExpandNode(String JavaDoc nodePath) {
122         try {
123             AbstractNode node = pathToNode(nodePath);
124             // ensure that the children of this node are uptodate
125
if (node instanceof ObjectNode)
126                 ((ObjectNode)node).updateChildren();
127             expandedNodes.add(node);
128         } finally {
129             context.getDisplay().refresh();
130         }
131     }
132
133     public void onCollapseNode(String JavaDoc nodePath) {
134         try {
135             AbstractNode node = pathToNode(nodePath);
136             expandedNodes.remove(node);
137         } finally {
138             context.getDisplay().refresh();
139         }
140     }
141
142     /*
143      * Get a Node from its path.
144      */

145     protected AbstractNode pathToNode(String JavaDoc nodePath) {
146         AbstractNode node = (AbstractNode)model.getRoot();
147         if (nodePath.startsWith("/")) {
148             nodePath = nodePath.substring(1);
149             int index = nodePath.indexOf("/");
150             while (index!=-1) {
151                 node = (AbstractNode)node.getChildAt(
152                     Integer.parseInt(nodePath.substring(0,index)));
153                 nodePath = nodePath.substring(index+1);
154                 index = nodePath.indexOf("/");
155             }
156             if (nodePath.length()>0)
157                 node = (AbstractNode)node.getChildAt(Integer.parseInt(nodePath));
158         }
159         return node;
160     }
161 }
162
Popular Tags