001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.contrib.tree.simple; 016 017 import java.io.Serializable; 018 import java.util.Iterator; 019 020 import javax.swing.tree.TreePath; 021 022 import org.apache.tapestry.contrib.tree.model.ITreeDataModel; 023 import org.apache.tapestry.contrib.tree.model.ITreeNode; 024 025 /** 026 * @author ceco 027 */ 028 public class SimpleTreeDataModel implements ITreeDataModel, Serializable 029 { 030 031 private static final long serialVersionUID = 9215832847660213349L; 032 033 protected ITreeNode m_objRootNode; 034 035 /** 036 * Constructor for SimpleTreeDataModel. 037 */ 038 public SimpleTreeDataModel(ITreeNode objRootNode) 039 { 040 super(); 041 m_objRootNode = objRootNode; 042 } 043 044 /** 045 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getRoot() 046 */ 047 public Object getRoot() 048 { 049 return m_objRootNode; 050 } 051 052 /** 053 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getChildCount(Object) 054 */ 055 public int getChildCount(Object objParent) 056 { 057 ITreeNode objParentNode = (ITreeNode) objParent; 058 059 return objParentNode.getChildCount(); 060 } 061 062 /** 063 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getChildren(Object) 064 */ 065 public Iterator getChildren(Object objParent) 066 { 067 ITreeNode objParentNode = (ITreeNode) objParent; 068 return objParentNode.getChildren().iterator(); 069 } 070 071 /** 072 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getObject(Object) 073 */ 074 public Object getObject(Object objUniqueKey) 075 { 076 if (objUniqueKey != null) 077 { 078 TreePath objPath = (TreePath) objUniqueKey; 079 return objPath.getLastPathComponent(); 080 } 081 return null; 082 } 083 084 /** 085 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getUniqueKey(Object, 086 * Object) 087 */ 088 public Object getUniqueKey(Object objTarget, Object objParentUniqueKey) 089 { 090 TreePath objPath = (TreePath) objParentUniqueKey; 091 Object objTargetUID = null; 092 if (objPath != null) 093 { 094 objTargetUID = objPath.pathByAddingChild(objTarget); 095 } 096 else 097 { 098 objTargetUID = new TreePath(objTarget); 099 } 100 return objTargetUID; 101 } 102 103 /** 104 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#isAncestorOf(Object, 105 * Object) 106 */ 107 public boolean isAncestorOf(Object objTargetUniqueKey, 108 Object objParentUniqueKey) 109 { 110 TreePath objParentPath = (TreePath) objParentUniqueKey; 111 TreePath objTargetPath = (TreePath) objTargetUniqueKey; 112 boolean bResult = objParentPath.isDescendant(objTargetPath); 113 return bResult; 114 } 115 116 /** 117 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getParentUniqueKey 118 */ 119 public Object getParentUniqueKey(Object objChildUniqueKey) 120 { 121 TreePath objChildPath = (TreePath) objChildUniqueKey; 122 TreePath objParentPath = objChildPath.getParentPath(); 123 if (objParentPath == null) return null; 124 return objParentPath.getLastPathComponent(); 125 } 126 127 }