KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > web > renderers > tree > WebTree


1 package com.genimen.djeneric.web.renderers.tree;
2
3 import javax.xml.parsers.DocumentBuilder JavaDoc;
4 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
5 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
6 import javax.xml.parsers.ParserConfigurationException JavaDoc;
7
8 import org.w3c.dom.DOMException JavaDoc;
9 import org.w3c.dom.Document JavaDoc;
10 import org.w3c.dom.Element JavaDoc;
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 JavaDoc _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 JavaDoc asXml(String JavaDoc path) throws ParserConfigurationException JavaDoc, DOMException JavaDoc, DjenericException
33   {
34     Document JavaDoc doc = createDocument();
35     Element JavaDoc 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 JavaDoc asXml(Document JavaDoc doc) throws ParserConfigurationException JavaDoc, DOMException JavaDoc, DjenericException
44   {
45     Element JavaDoc 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 JavaDoc createTree(Document JavaDoc doc)
55   {
56     Element JavaDoc 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 JavaDoc createDocument() throws FactoryConfigurationError JavaDoc, ParserConfigurationException JavaDoc
76   {
77     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
78     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
79     Document JavaDoc doc = builder.newDocument();
80     return doc;
81   }
82
83   public AbstractWebNode getNode(String JavaDoc path)
84   {
85     return _root.getNode(path);
86   }
87
88   public void toggleExpanded(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc path)
126   {
127     AbstractWebNode node = _root.getNode(path);
128     if (node != null)
129     {
130       _selectionPath = node.getPath();
131     }
132   }
133 }
134
Popular Tags