KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > tree > components > TreeDataView


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.tree.components;
16
17 import java.util.Iterator JavaDoc;
18
19 import org.apache.tapestry.BaseComponent;
20 import org.apache.tapestry.IMarkupWriter;
21 import org.apache.tapestry.IRequestCycle;
22 import org.apache.tapestry.contrib.tree.model.ITreeDataModel;
23 import org.apache.tapestry.contrib.tree.model.ITreeModel;
24 import org.apache.tapestry.contrib.tree.model.ITreeRowSource;
25 import org.apache.tapestry.contrib.tree.model.TreeRowObject;
26
27 /**
28  * @author tsveltin ?
29  */

30 public abstract class TreeDataView extends BaseComponent implements ITreeRowSource
31 {
32     private TreeRowObject m_objTreeRowObject = null;
33
34     private int m_nTreeDeep = -1;
35
36     public TreeDataView()
37     {
38         super();
39         initialize();
40     }
41
42     public void initialize()
43     {
44         m_objTreeRowObject = null;
45         m_nTreeDeep = -1;
46     }
47
48     public abstract TreeView getTreeView();
49
50     public void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
51     {
52         // render data
53
Object JavaDoc objExistedTreeModelSource = cycle
54                 .getAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE);
55         cycle.setAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE, this);
56
57         TreeView objView = getTreeView();
58         ITreeModel objTreeModel = objView.getTreeModel();
59         ITreeDataModel objTreeDataModel = objTreeModel.getTreeDataModel();
60
61         Object JavaDoc objRoot = objTreeDataModel.getRoot();
62         Object JavaDoc objRootUID = objTreeDataModel.getUniqueKey(objRoot, null);
63         if (getShowRootNode())
64         {
65             walkTree(
66                     objRoot,
67                     objRootUID,
68                     0,
69                     objTreeModel,
70                     writer,
71                     cycle,
72                     TreeRowObject.FIRST_LAST_ROW,
73                     new int[0],
74                     true);
75         }
76         else
77         {
78             boolean bFirst = true;
79             int nChildenCount = objTreeModel.getTreeDataModel().getChildCount(objRoot);
80             int nRowPossiotionType = nChildenCount == 1 ? TreeRowObject.FIRST_LAST_ROW
81                     : TreeRowObject.FIRST_ROW;
82             for (Iterator JavaDoc iter = objTreeModel.getTreeDataModel().getChildren(objRoot); iter
83                     .hasNext();)
84             {
85                 Object JavaDoc objChild = iter.next();
86                 Object JavaDoc objChildUID = objTreeModel.getTreeDataModel()
87                         .getUniqueKey(objChild, objRoot);
88                 boolean bChildLast = !iter.hasNext();
89                 if (!bFirst)
90                 {
91                     if (bChildLast)
92                         nRowPossiotionType = TreeRowObject.LAST_ROW;
93                     else
94                         nRowPossiotionType = TreeRowObject.MIDDLE_ROW;
95                 }
96
97                 walkTree(
98                         objChild,
99                         objChildUID,
100                         0,
101                         objTreeModel,
102                         writer,
103                         cycle,
104                         nRowPossiotionType,
105                         new int[0],
106                         bChildLast);
107
108                 bFirst = false;
109             }
110         }
111
112         cycle.setAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE, objExistedTreeModelSource);
113     }
114
115     public void walkTree(Object JavaDoc objParent, Object JavaDoc objParentUID, int nDepth,
116             ITreeModel objTreeModel, IMarkupWriter writer, IRequestCycle cycle,
117             int nRowPossiotionType, int[] arrConnectImages, boolean bLast)
118     {
119         m_nTreeDeep = nDepth;
120         int nNumberOfChildren = objTreeModel.getTreeDataModel().getChildCount(objParent);
121         boolean bLeaf = (nNumberOfChildren == 0) ? true : false;
122         m_objTreeRowObject = new TreeRowObject(objParent, objParentUID, nDepth, bLeaf,
123                 nRowPossiotionType, arrConnectImages);
124
125         super.renderComponent(writer, cycle);
126
127         boolean bContain = objTreeModel.getTreeStateModel().isUniqueKeyExpanded(objParentUID);
128         if (bContain)
129         {
130             int[] arrConnectImagesNew = new int[arrConnectImages.length + 1];
131             System.arraycopy(arrConnectImages, 0, arrConnectImagesNew, 0, arrConnectImages.length);
132             if (bLast)
133                 arrConnectImagesNew[arrConnectImagesNew.length - 1] = TreeRowObject.EMPTY_CONN_IMG;
134             else
135                 arrConnectImagesNew[arrConnectImagesNew.length - 1] = TreeRowObject.LINE_CONN_IMG;
136
137             for (Iterator JavaDoc iter = objTreeModel.getTreeDataModel().getChildren(objParent); iter
138                     .hasNext();)
139             {
140                 Object JavaDoc objChild = iter.next();
141                 Object JavaDoc objChildUID = objTreeModel.getTreeDataModel().getUniqueKey(
142                         objChild,
143                         objParentUID);
144                 boolean bChildLast = !iter.hasNext();
145                 if (bChildLast)
146                     nRowPossiotionType = TreeRowObject.LAST_ROW;
147                 else
148                     nRowPossiotionType = TreeRowObject.MIDDLE_ROW;
149                 walkTree(
150                         objChild,
151                         objChildUID,
152                         nDepth + 1,
153                         objTreeModel,
154                         writer,
155                         cycle,
156                         nRowPossiotionType,
157                         arrConnectImagesNew,
158                         bChildLast);
159             }
160         }
161     }
162
163     public int getTreeDeep()
164     {
165         return m_nTreeDeep;
166     }
167
168     /**
169      * @see org.apache.tapestry.contrib.tree.model.ITreeRowSource#getTreeRow()
170      */

171     public TreeRowObject getTreeRow()
172     {
173         return getTreeRowObject();
174     }
175
176     public TreeRowObject getTreeRowObject()
177     {
178         return m_objTreeRowObject;
179     }
180
181     public void setTreeRowObject(TreeRowObject object)
182     {
183         m_objTreeRowObject = object;
184     }
185
186     public abstract boolean getShowRootNode();
187
188 }
Popular Tags