1 15 package org.apache.tapestry.workbench.tree.examples.fsmodel; 16 17 import java.io.Serializable ; 18 import java.util.Iterator ; 19 20 import org.apache.tapestry.contrib.tree.model.ITreeNode; 21 import org.apache.tapestry.contrib.tree.simple.SimpleTreeDataModel; 22 23 26 public class FileSystemDataModel extends SimpleTreeDataModel 27 implements Serializable { 28 32 public FileSystemDataModel(ITreeNode objRootNode) { 33 super(objRootNode); 34 } 35 36 39 public Object getObject(Object objUniqueKey) { 40 return findNode(objUniqueKey, (IFileSystemTreeNode)getRoot()); 41 } 42 43 private IFileSystemTreeNode findNode(Object objUniqueKey, 44 IFileSystemTreeNode objParentNode) { 45 String strUniqueKey = (String ) objUniqueKey; 46 String strParentUniqueKey = objParentNode.getAbsolutePath(); 47 48 if (strUniqueKey.equals(strParentUniqueKey)) { 49 return objParentNode; 50 } 51 52 IFileSystemTreeNode obj = null; 53 54 if(strUniqueKey.startsWith(strParentUniqueKey)) 55 { 56 for (Iterator iter = objParentNode.getChildren().iterator(); iter.hasNext();) { 57 IFileSystemTreeNode element = (IFileSystemTreeNode) iter.next(); 58 obj = findNode(objUniqueKey, element); 59 if (obj != null) { 60 break; 61 } 62 } 63 } 64 65 return obj; 66 } 67 68 71 public Object getUniqueKey(Object objTarget, Object objParentUniqueKey) { 72 IFileSystemTreeNode objNode = (IFileSystemTreeNode) objTarget; 73 return objNode.getAbsolutePath(); 74 } 75 76 79 public boolean isAncestorOf(Object objChildUniqueKey, 80 Object objParentUniqueKey) { 81 String strChildAbsolutePath = (String )objChildUniqueKey; 82 String strParentAbsolutePath = (String )objParentUniqueKey; 83 84 if("".equals(strParentAbsolutePath)) { 85 return true; 86 } 87 88 return strChildAbsolutePath.lastIndexOf(strParentAbsolutePath) > -1; 89 } 90 91 94 public Object getParentUniqueKey(Object objChildUniqueKey) { 95 IFileSystemTreeNode objNode = 96 (IFileSystemTreeNode) getObject(objChildUniqueKey); 97 return objNode.getParent(); 98 } 99 100 } 101 | Popular Tags |