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 015package org.apache.tapestry.contrib.tree.simple; 016 017import java.io.Serializable; 018import java.util.Iterator; 019 020import javax.swing.tree.TreePath; 021 022import org.apache.tapestry.contrib.tree.model.ITreeDataModel; 023import org.apache.tapestry.contrib.tree.model.ITreeNode; 024 025/** 026 * @author ceco 027 */ 028public class SimpleTreeDataModel implements ITreeDataModel, Serializable 029{ 030 private static final long serialVersionUID = 9215832847660213349L; 031 032 protected ITreeNode m_objRootNode; 033 /** 034 * Constructor for SimpleTreeDataModel. 035 */ 036 public SimpleTreeDataModel(ITreeNode objRootNode) { 037 super(); 038 m_objRootNode = objRootNode; 039 } 040 041 /** 042 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getRoot() 043 */ 044 public Object getRoot() { 045 return m_objRootNode; 046 } 047 048 /** 049 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getChildCount(Object) 050 */ 051 public int getChildCount(Object objParent) { 052 ITreeNode objParentNode = (ITreeNode)objParent; 053 054 return objParentNode.getChildCount(); 055 } 056 057 /** 058 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getChildren(Object) 059 */ 060 public Iterator getChildren(Object objParent) { 061 ITreeNode objParentNode = (ITreeNode)objParent; 062 return objParentNode.getChildren().iterator(); 063 } 064 065 /** 066 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getObject(Object) 067 */ 068 public Object getObject(Object objUniqueKey) { 069 if(objUniqueKey != null) { 070 TreePath objPath = (TreePath)objUniqueKey; 071 return objPath.getLastPathComponent(); 072 } 073 return null; 074 } 075 076 /** 077 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getUniqueKey(Object, Object) 078 */ 079 public Object getUniqueKey(Object objTarget, Object objParentUniqueKey) { 080 TreePath objPath = (TreePath)objParentUniqueKey; 081 Object objTargetUID = null; 082 if(objPath != null){ 083 objTargetUID = objPath.pathByAddingChild(objTarget); 084 }else{ 085 objTargetUID = new TreePath(objTarget); 086 } 087 return objTargetUID; 088 } 089 090 /** 091 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#isAncestorOf(Object, Object) 092 */ 093 public boolean isAncestorOf(Object objTargetUniqueKey, Object objParentUniqueKey) { 094 TreePath objParentPath = (TreePath)objParentUniqueKey; 095 TreePath objTargetPath = (TreePath)objTargetUniqueKey; 096 boolean bResult = objParentPath.isDescendant(objTargetPath); 097 return bResult; 098 } 099 100 /** 101 * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getParentUniqueKey 102 */ 103 public Object getParentUniqueKey(Object objChildUniqueKey) { 104 TreePath objChildPath = (TreePath)objChildUniqueKey; 105 TreePath objParentPath = objChildPath.getParentPath(); 106 if(objParentPath == null) 107 return null; 108 return objParentPath.getLastPathComponent(); 109 } 110 111}