1 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 ; 25 import java.util.HashSet ; 26 27 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 expandedNodes = new HashSet (); 40 41 46 public Tree(ViewFactory factory, DisplayContext context, 47 String 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 56 public void close(boolean validate) { 57 model.unregisterEvents(); 58 } 59 60 public void genHTML(PrintWriter 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 out, AbstractNode node, String curPath) { 69 String nodePath = node instanceof RootNode ? "/" : curPath; 70 String 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 "&nodePath="+nodePath+"\" class=\"fixed\">[-]</a>"); 79 else 80 out.print(" <a HREF=\""+eventURL("onExpandNode")+ 81 "&nodePath="+nodePath+"\" class=\"fixed\">[+]</a>"); 82 } 83 out.println(iconElement(node.getIcon(),"")+ 84 " <a HREF=\""+eventURL("onSelectNode")+"&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 102 public void onSelectNode(String 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 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 e) { 117 context.getDisplay().showError("Error",e.toString()); 118 } 119 } 120 121 public void onExpandNode(String nodePath) { 122 try { 123 AbstractNode node = pathToNode(nodePath); 124 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 nodePath) { 134 try { 135 AbstractNode node = pathToNode(nodePath); 136 expandedNodes.remove(node); 137 } finally { 138 context.getDisplay().refresh(); 139 } 140 } 141 142 145 protected AbstractNode pathToNode(String 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 |