KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > tree > simple > SimpleTreeDataModel


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.tree.simple;
16
17 import java.io.Serializable JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import javax.swing.tree.TreePath JavaDoc;
21
22 import org.apache.tapestry.contrib.tree.model.ITreeDataModel;
23 import org.apache.tapestry.contrib.tree.model.ITreeNode;
24
25 /**
26  * @author ceco
27  */

28 public class SimpleTreeDataModel implements ITreeDataModel, Serializable JavaDoc {
29
30     protected ITreeNode m_objRootNode;
31     /**
32      * Constructor for SimpleTreeDataModel.
33      */

34     public SimpleTreeDataModel(ITreeNode objRootNode) {
35         super();
36         m_objRootNode = objRootNode;
37     }
38
39     /**
40      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getRoot()
41      */

42     public Object JavaDoc getRoot() {
43         return m_objRootNode;
44     }
45
46     /**
47      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getChildCount(Object)
48      */

49     public int getChildCount(Object JavaDoc objParent) {
50         ITreeNode objParentNode = (ITreeNode)objParent;
51         
52         return objParentNode.getChildCount();
53     }
54
55     /**
56      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getChildren(Object)
57      */

58     public Iterator JavaDoc getChildren(Object JavaDoc objParent) {
59         ITreeNode objParentNode = (ITreeNode)objParent;
60         return objParentNode.getChildren().iterator();
61     }
62
63     /**
64      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getObject(Object)
65      */

66     public Object JavaDoc getObject(Object JavaDoc objUniqueKey) {
67         if(objUniqueKey != null) {
68             TreePath JavaDoc objPath = (TreePath JavaDoc)objUniqueKey;
69             return objPath.getLastPathComponent();
70         }
71         return null;
72     }
73
74     /**
75      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getUniqueKey(Object, Object)
76      */

77     public Object JavaDoc getUniqueKey(Object JavaDoc objTarget, Object JavaDoc objParentUniqueKey) {
78         TreePath JavaDoc objPath = (TreePath JavaDoc)objParentUniqueKey;
79         Object JavaDoc objTargetUID = null;
80         if(objPath != null){
81             objTargetUID = objPath.pathByAddingChild(objTarget);
82         }else{
83             objTargetUID = new TreePath JavaDoc(objTarget);
84         }
85         return objTargetUID;
86     }
87
88     /**
89      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#isAncestorOf(Object, Object)
90      */

91     public boolean isAncestorOf(Object JavaDoc objTargetUniqueKey, Object JavaDoc objParentUniqueKey) {
92         TreePath JavaDoc objParentPath = (TreePath JavaDoc)objParentUniqueKey;
93         TreePath JavaDoc objTargetPath = (TreePath JavaDoc)objTargetUniqueKey;
94         boolean bResult = objParentPath.isDescendant(objTargetPath);
95         return bResult;
96     }
97
98     /**
99      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getParentUniqueKey
100      */

101     public Object JavaDoc getParentUniqueKey(Object JavaDoc objChildUniqueKey) {
102         TreePath JavaDoc objChildPath = (TreePath JavaDoc)objChildUniqueKey;
103         TreePath JavaDoc objParentPath = objChildPath.getParentPath();
104         if(objParentPath == null)
105             return null;
106         return objParentPath.getLastPathComponent();
107     }
108
109 }
110
Popular Tags