1 15 package org.apache.tapestry.contrib.tree.components.table; 16 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.List ; 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 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 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_objTreeRowObject = null; 61 m_arrAllExpandedNodes = null; 62 } 63 64 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 generateNodeList() 83 { 84 if (m_arrAllExpandedNodes == null) 85 { 86 ArrayList arrAllExpandedNodes = new ArrayList (); 87 88 ITreeModelSource objTreeModelSource = getTreeModelSource(); 90 ITreeModel objTreeModel = objTreeModelSource.getTreeModel(); 91 ITreeDataModel objTreeDataModel = objTreeModel.getTreeDataModel(); 92 93 Object objRoot = objTreeDataModel.getRoot(); 94 Object 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 iter = objTreeModel.getTreeDataModel().getChildren(objRoot); iter 114 .hasNext();) 115 { 116 Object objChild = iter.next(); 117 Object 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 arrAllExpandedNodes, Object objParent, Object 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 colChildren = objTreeModel.getTreeDataModel().getChildren(objParent); 169 for (Iterator iter = colChildren; iter.hasNext();) 170 { 171 Object objChild = iter.next(); 172 Object 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 198 public int getTreeDeep() 199 { 200 return m_nTreeDeep; 201 } 202 203 207 public ITableModel getTableModel() 208 { 209 return createTableModel(); 210 } 211 212 private ITableModel createTableModel() 213 { 214 ArrayList arrAllNodes = generateNodeList(); 215 Object [] arrAllExpandedNodes = new Object [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 arrColumnsList = new ArrayList (); 228 arrColumnsList.add(new TreeTableColumn("Name", false, null)); 229 230 List 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 getTableColumnsParameter(); 241 242 public abstract int getEntriesPerTablePage(); 243 244 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 } 261 262 266 protected void renderComponent(IMarkupWriter arg0, IRequestCycle cycle) 267 { 268 Object 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 281 public void renderBody(IMarkupWriter writer, IRequestCycle cycle) 282 { 283 Object 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 |