1 package com.genimen.djeneric.web.renderers.tree; 2 3 import javax.xml.parsers.DocumentBuilder ; 4 import javax.xml.parsers.DocumentBuilderFactory ; 5 import javax.xml.parsers.FactoryConfigurationError ; 6 import javax.xml.parsers.ParserConfigurationException ; 7 8 import org.w3c.dom.DOMException ; 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.Element ; 11 12 import com.genimen.djeneric.repository.DjSession; 13 import com.genimen.djeneric.repository.exceptions.DjenericException; 14 import com.genimen.djeneric.structure.ExtentUsage; 15 16 public class WebTree 17 { 18 WebNode _root; 19 String _selectionPath = null; 20 21 public WebTree(DjSession session, ExtentUsage[] usages) 22 { 23 _root = new WebNode(session); 24 for (int i = 0; i < usages.length; i++) 25 { 26 WebFolder folder = new WebFolder(session); 27 folder.setExtentUsage(usages[i]); 28 _root.insertAsChild(folder); 29 } 30 } 31 32 public Document asXml(String path) throws ParserConfigurationException , DOMException , DjenericException 33 { 34 Document doc = createDocument(); 35 Element tree = createTree(doc); 36 doc.appendChild(tree); 37 38 AbstractWebNode node = _root.getNode(path); 39 if (node != null) tree.appendChild(node.asXml(doc)); 40 return doc; 41 } 42 43 public Element asXml(Document doc) throws ParserConfigurationException , DOMException , DjenericException 44 { 45 Element tree = createTree(doc); 46 47 for (int i = 0; i < _root.getChildCount(); i++) 48 { 49 tree.appendChild(_root.getChildAt(i).asXml(doc)); 50 } 51 return tree; 52 } 53 54 private Element createTree(Document doc) 55 { 56 Element tree = doc.createElement("tree"); 57 58 boolean isSelected = false; 59 if (_selectionPath != null) 60 { 61 AbstractWebNode node = _root.getNode(_selectionPath); 62 if (node != null) 63 { 64 tree.setAttribute("selectednode", node.getPath()); 65 isSelected = true; 66 } 67 } 68 if (!isSelected && _root.getChildCount() > 0) 69 { 70 tree.setAttribute("selectednode", _root.getChildAt(0).getPath()); 71 } 72 return tree; 73 } 74 75 private Document createDocument() throws FactoryConfigurationError , ParserConfigurationException 76 { 77 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 78 DocumentBuilder builder = factory.newDocumentBuilder(); 79 Document doc = builder.newDocument(); 80 return doc; 81 } 82 83 public AbstractWebNode getNode(String path) 84 { 85 return _root.getNode(path); 86 } 87 88 public void toggleExpanded(String path) throws DjenericException 89 { 90 AbstractWebNode node = _root.getNode(path); 91 if (node != null) 92 { 93 node.setExpanded(!node.isExpanded()); 94 } 95 } 96 97 public void expandAll() throws DjenericException 98 { 99 _root.expandAll(); 100 } 101 102 public void collapseAll() throws DjenericException 103 { 104 _root.collapseAll(); 105 } 106 107 public void expandAll(String path) throws DjenericException 108 { 109 AbstractWebNode node = _root.getNode(path); 110 if (node != null) 111 { 112 node.expandAll(); 113 } 114 } 115 116 public void collapseAll(String path) throws DjenericException 117 { 118 AbstractWebNode node = _root.getNode(path); 119 if (node != null) 120 { 121 node.collapseAll(); 122 } 123 } 124 125 public void setSelectedNode(String path) 126 { 127 AbstractWebNode node = _root.getNode(path); 128 if (node != null) 129 { 130 _selectionPath = node.getPath(); 131 } 132 } 133 } 134 | Popular Tags |