KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > workbench > tree > examples > fsmodel > FileSystemDataModel


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.workbench.tree.examples.fsmodel;
16
17 import java.io.Serializable JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.tapestry.contrib.tree.model.ITreeNode;
21 import org.apache.tapestry.contrib.tree.simple.SimpleTreeDataModel;
22
23 /**
24  * @author ceco
25  */

26 public class FileSystemDataModel extends SimpleTreeDataModel
27     implements Serializable JavaDoc {
28     /**
29      * Constructor for FileSystemDataModel.
30      * @param objRootNode
31      */

32     public FileSystemDataModel(ITreeNode objRootNode) {
33         super(objRootNode);
34     }
35
36     /**
37      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getObject(Object)
38      */

39     public Object JavaDoc getObject(Object JavaDoc objUniqueKey) {
40         return findNode(objUniqueKey, (IFileSystemTreeNode)getRoot());
41     }
42
43     private IFileSystemTreeNode findNode(Object JavaDoc objUniqueKey,
44                                          IFileSystemTreeNode objParentNode) {
45         String JavaDoc strUniqueKey = (String JavaDoc) objUniqueKey;
46         String JavaDoc 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 JavaDoc 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     /**
69      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getUniqueKey(Object, Object)
70      */

71     public Object JavaDoc getUniqueKey(Object JavaDoc objTarget, Object JavaDoc objParentUniqueKey) {
72         IFileSystemTreeNode objNode = (IFileSystemTreeNode) objTarget;
73         return objNode.getAbsolutePath();
74     }
75
76     /**
77      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#isAncestorOf(Object, Object)
78      */

79     public boolean isAncestorOf(Object JavaDoc objChildUniqueKey,
80                                 Object JavaDoc objParentUniqueKey) {
81         String JavaDoc strChildAbsolutePath = (String JavaDoc)objChildUniqueKey;
82         String JavaDoc strParentAbsolutePath = (String JavaDoc)objParentUniqueKey;
83
84         if("".equals(strParentAbsolutePath)) {
85             return true;
86         }
87
88         return strChildAbsolutePath.lastIndexOf(strParentAbsolutePath) > -1;
89     }
90
91     /**
92      * @see org.apache.tapestry.contrib.tree.model.ITreeDataModel#getParentUniqueKey(Object)
93      */

94     public Object JavaDoc getParentUniqueKey(Object JavaDoc objChildUniqueKey) {
95         IFileSystemTreeNode objNode =
96             (IFileSystemTreeNode) getObject(objChildUniqueKey);
97         return objNode.getParent();
98     }
99
100 }
101
Popular Tags