KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > tree > components > table > TreeTableDataView


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.table;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.tapestry.BaseComponent;
22 import org.apache.tapestry.IMarkupWriter;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.contrib.table.model.ITableColumn;
25 import org.apache.tapestry.contrib.table.model.ITableModel;
26 import org.apache.tapestry.contrib.table.model.ITableSessionStateManager;
27 import org.apache.tapestry.contrib.table.model.simple.SimpleListTableDataModel;
28 import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumnModel;
29 import org.apache.tapestry.contrib.table.model.simple.SimpleTableModel;
30 import org.apache.tapestry.contrib.table.model.simple.SimpleTableSessionStateManager;
31 import org.apache.tapestry.contrib.tree.model.ITreeDataModel;
32 import org.apache.tapestry.contrib.tree.model.ITreeModel;
33 import org.apache.tapestry.contrib.tree.model.ITreeModelSource;
34 import org.apache.tapestry.contrib.tree.model.ITreeRowSource;
35 import org.apache.tapestry.contrib.tree.model.TreeRowObject;
36 import org.apache.tapestry.event.PageDetachListener;
37 import org.apache.tapestry.event.PageEvent;
38
39 /**
40  */

41 public abstract class TreeTableDataView extends BaseComponent implements ITreeRowSource,
42         PageDetachListener
43 {
44     private int m_nTreeDeep = -1;
45
46     private TreeRowObject m_objTreeRowObject = null;
47
48     private ArrayList JavaDoc m_arrAllExpandedNodes = null;
49
50     public TreeTableDataView()
51     {
52         super();
53         initialize();
54     }
55
56     public void initialize()
57     {
58         m_nTreeDeep = -1;
59         // m_objTableModel = null;
60
m_objTreeRowObject = null;
61         m_arrAllExpandedNodes = null;
62     }
63
64     /**
65      * @see org.apache.tapestry.event.PageDetachListener#pageDetached(org.apache.tapestry.event.PageEvent)
66      */

67     public void pageDetached(PageEvent event)
68     {
69         initialize();
70     }
71
72     public abstract ITreeModelSource getTreeViewParameter();
73
74     public ITreeModelSource getTreeModelSource()
75     {
76         ITreeModelSource objSource = (ITreeModelSource) getPage().getRequestCycle().getAttribute(
77                 ITreeModelSource.TREE_MODEL_SOURCE_ATTRIBUTE);
78
79         return objSource == null ? getTreeViewParameter() : objSource;
80     }
81
82     public ArrayList JavaDoc generateNodeList()
83     {
84         if (m_arrAllExpandedNodes == null)
85         {
86             ArrayList JavaDoc arrAllExpandedNodes = new ArrayList JavaDoc();
87
88             // render data
89
ITreeModelSource objTreeModelSource = getTreeModelSource();
90             ITreeModel objTreeModel = objTreeModelSource.getTreeModel();
91             ITreeDataModel objTreeDataModel = objTreeModel.getTreeDataModel();
92
93             Object JavaDoc objRoot = objTreeDataModel.getRoot();
94             Object JavaDoc objRootUID = objTreeDataModel.getUniqueKey(objRoot, null);
95             if (getShowRootNode())
96             {
97                 walkTree(
98                         arrAllExpandedNodes,
99                         objRoot,
100                         objRootUID,
101                         0,
102                         objTreeModel,
103                         TreeRowObject.FIRST_LAST_ROW,
104                         new int[0],
105                         true);
106             }
107             else
108             {
109                 int nChildenCount = objTreeModel.getTreeDataModel().getChildCount(objRoot);
110                 int nRowPossiotionType = nChildenCount == 1 ? TreeRowObject.FIRST_LAST_ROW
111                         : TreeRowObject.FIRST_ROW;
112                 boolean bFirst = true;
113                 for (Iterator JavaDoc iter = objTreeModel.getTreeDataModel().getChildren(objRoot); iter
114                         .hasNext();)
115                 {
116                     Object JavaDoc objChild = iter.next();
117                     Object JavaDoc objChildUID = objTreeModel.getTreeDataModel().getUniqueKey(
118                             objChild,
119                             objRoot);
120                     boolean bChildLast = !iter.hasNext();
121                     if (!bFirst)
122                     {
123                         if (bChildLast)
124                             nRowPossiotionType = TreeRowObject.LAST_ROW;
125                         else
126                             nRowPossiotionType = TreeRowObject.MIDDLE_ROW;
127                     }
128                     walkTree(
129                             arrAllExpandedNodes,
130                             objChild,
131                             objChildUID,
132                             0,
133                             objTreeModel,
134                             nRowPossiotionType,
135                             new int[0],
136                             bChildLast);
137                     bFirst = false;
138                 }
139             }
140
141             m_arrAllExpandedNodes = arrAllExpandedNodes;
142         }
143
144         return m_arrAllExpandedNodes;
145     }
146
147     public void walkTree(ArrayList JavaDoc arrAllExpandedNodes, Object JavaDoc objParent, Object JavaDoc objParentUID,
148             int nDepth, ITreeModel objTreeModel, int nRowPossiotionType, int[] arrConnectImages,
149             boolean bLast)
150     {
151         m_nTreeDeep = nDepth;
152
153         int nNumberOfChildren = objTreeModel.getTreeDataModel().getChildCount(objParent);
154         boolean bLeaf = (nNumberOfChildren == 0) ? true : false;
155         TreeRowObject objTreeRowObject = new TreeRowObject(objParent, objParentUID, nDepth, bLeaf,
156                 nRowPossiotionType, arrConnectImages);
157         arrAllExpandedNodes.add(objTreeRowObject);
158
159         boolean bContain = objTreeModel.getTreeStateModel().isUniqueKeyExpanded(objParentUID);
160         if (bContain)
161         {
162             int[] arrConnectImagesNew = new int[arrConnectImages.length + 1];
163             System.arraycopy(arrConnectImages, 0, arrConnectImagesNew, 0, arrConnectImages.length);
164             if (bLast)
165                 arrConnectImagesNew[arrConnectImagesNew.length - 1] = TreeRowObject.EMPTY_CONN_IMG;
166             else
167                 arrConnectImagesNew[arrConnectImagesNew.length - 1] = TreeRowObject.LINE_CONN_IMG;
168             Iterator JavaDoc colChildren = objTreeModel.getTreeDataModel().getChildren(objParent);
169             for (Iterator JavaDoc iter = colChildren; iter.hasNext();)
170             {
171                 Object JavaDoc objChild = iter.next();
172                 Object JavaDoc objChildUID = objTreeModel.getTreeDataModel().getUniqueKey(
173                         objChild,
174                         objParentUID);
175                 boolean bChildLast = !iter.hasNext();
176                 if (bChildLast)
177                     nRowPossiotionType = TreeRowObject.LAST_ROW;
178                 else
179                     nRowPossiotionType = TreeRowObject.MIDDLE_ROW;
180                 walkTree(
181                         arrAllExpandedNodes,
182                         objChild,
183                         objChildUID,
184                         nDepth + 1,
185                         objTreeModel,
186                         nRowPossiotionType,
187                         arrConnectImagesNew,
188                         bChildLast);
189             }
190         }
191     }
192
193     /**
194      * Returns the treeDeep.
195      *
196      * @return int
197      */

198     public int getTreeDeep()
199     {
200         return m_nTreeDeep;
201     }
202
203     /*
204      * public ITableModel getTableModel() { if(m_objTableModel == null){ m_objTableModel =
205      * createTableModel(); } return m_objTableModel; }
206      */

207     public ITableModel getTableModel()
208     {
209         return createTableModel();
210     }
211
212     private ITableModel createTableModel()
213     {
214         ArrayList JavaDoc arrAllNodes = generateNodeList();
215         Object JavaDoc[] arrAllExpandedNodes = new Object JavaDoc[arrAllNodes.size()];
216         arrAllNodes.toArray(arrAllExpandedNodes);
217
218         SimpleTableModel objTableModel = new SimpleTableModel(arrAllExpandedNodes,
219                 getTableColunms());
220         objTableModel.getPagingState().setPageSize(getEntriesPerTablePage());
221
222         return objTableModel;
223     }
224
225     public ITableColumn[] getTableColunms()
226     {
227         ArrayList JavaDoc arrColumnsList = new ArrayList JavaDoc();
228         arrColumnsList.add(new TreeTableColumn("Name", false, null));
229
230         List JavaDoc arrTableColunms = getTableColumnsParameter();
231         if (arrTableColunms != null)
232             arrColumnsList.addAll(arrTableColunms);
233
234         ITableColumn[] arrColumns = new ITableColumn[arrColumnsList.size()];
235         arrColumnsList.toArray(arrColumns);
236
237         return arrColumns;
238     }
239
240     public abstract List JavaDoc getTableColumnsParameter();
241
242     public abstract int getEntriesPerTablePage();
243
244     /**
245      * @see org.apache.tapestry.contrib.tree.model.ITreeRowSource#getTreeRow()
246      */

247     public TreeRowObject getTreeRow()
248     {
249         return getTreeRowObject();
250     }
251
252     public ITableSessionStateManager getTableSessionStateManager()
253     {
254         SimpleListTableDataModel objDataModel = new SimpleListTableDataModel(generateNodeList());
255         SimpleTableColumnModel objColumnModel = new SimpleTableColumnModel(getTableColunms());
256         SimpleTableSessionStateManager objStateManager = new SimpleTableSessionStateManager(
257                 objDataModel, objColumnModel);
258         return objStateManager;
259         //return NullTableSessionStateManager.NULL_STATE_MANAGER;
260
}
261
262     /**
263      * @see org.apache.tapestry.BaseComponent#renderComponent(org.apache.tapestry.IMarkupWriter,
264      * org.apache.tapestry.IRequestCycle)
265      */

266     protected void renderComponent(IMarkupWriter arg0, IRequestCycle cycle)
267     {
268         Object JavaDoc objExistedTreeModelSource = cycle
269                 .getAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE);
270         cycle.setAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE, this);
271
272         super.renderComponent(arg0, cycle);
273
274         cycle.setAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE, objExistedTreeModelSource);
275     }
276
277     /**
278      * @see org.apache.tapestry.AbstractComponent#renderBody(org.apache.tapestry.IMarkupWriter,
279      * org.apache.tapestry.IRequestCycle)
280      */

281     public void renderBody(IMarkupWriter writer, IRequestCycle cycle)
282     {
283         Object JavaDoc objExistedTreeModelSource = cycle
284                 .getAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE);
285         cycle.setAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE, this);
286
287         super.renderBody(writer, cycle);
288
289         cycle.setAttribute(ITreeRowSource.TREE_ROW_SOURCE_ATTRIBUTE, objExistedTreeModelSource);
290     }
291
292     public TreeRowObject getTreeRowObject()
293     {
294         return m_objTreeRowObject;
295     }
296
297     public void setTreeRowObject(TreeRowObject object)
298     {
299         m_objTreeRowObject = object;
300     }
301
302     public abstract boolean getShowRootNode();
303 }
Popular Tags