1 15 package org.apache.tapestry.contrib.tree.simple; 16 17 import java.io.Serializable ; 18 import java.util.Iterator ; 19 20 import javax.swing.tree.TreePath ; 21 22 import org.apache.tapestry.contrib.tree.model.ITreeDataModel; 23 import org.apache.tapestry.contrib.tree.model.ITreeNode; 24 25 28 public class SimpleTreeDataModel implements ITreeDataModel, Serializable { 29 30 protected ITreeNode m_objRootNode; 31 34 public SimpleTreeDataModel(ITreeNode objRootNode) { 35 super(); 36 m_objRootNode = objRootNode; 37 } 38 39 42 public Object getRoot() { 43 return m_objRootNode; 44 } 45 46 49 public int getChildCount(Object objParent) { 50 ITreeNode objParentNode = (ITreeNode)objParent; 51 52 return objParentNode.getChildCount(); 53 } 54 55 58 public Iterator getChildren(Object objParent) { 59 ITreeNode objParentNode = (ITreeNode)objParent; 60 return objParentNode.getChildren().iterator(); 61 } 62 63 66 public Object getObject(Object objUniqueKey) { 67 if(objUniqueKey != null) { 68 TreePath objPath = (TreePath )objUniqueKey; 69 return objPath.getLastPathComponent(); 70 } 71 return null; 72 } 73 74 77 public Object getUniqueKey(Object objTarget, Object objParentUniqueKey) { 78 TreePath objPath = (TreePath )objParentUniqueKey; 79 Object objTargetUID = null; 80 if(objPath != null){ 81 objTargetUID = objPath.pathByAddingChild(objTarget); 82 }else{ 83 objTargetUID = new TreePath (objTarget); 84 } 85 return objTargetUID; 86 } 87 88 91 public boolean isAncestorOf(Object objTargetUniqueKey, Object objParentUniqueKey) { 92 TreePath objParentPath = (TreePath )objParentUniqueKey; 93 TreePath objTargetPath = (TreePath )objTargetUniqueKey; 94 boolean bResult = objParentPath.isDescendant(objTargetPath); 95 return bResult; 96 } 97 98 101 public Object getParentUniqueKey(Object objChildUniqueKey) { 102 TreePath objChildPath = (TreePath )objChildUniqueKey; 103 TreePath objParentPath = objChildPath.getParentPath(); 104 if(objParentPath == null) 105 return null; 106 return objParentPath.getLastPathComponent(); 107 } 108 109 } 110 | Popular Tags |