KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > workbench > tree > examples > FileSystemTree


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;
16
17 import java.io.File JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Date JavaDoc;
20
21 import org.apache.tapestry.contrib.tree.components.INodeRenderFactory;
22 import org.apache.tapestry.contrib.tree.components.TreeView;
23 import org.apache.tapestry.contrib.tree.model.ITreeDataModel;
24 import org.apache.tapestry.contrib.tree.model.ITreeModel;
25 import org.apache.tapestry.contrib.tree.model.ITreeNode;
26 import org.apache.tapestry.contrib.tree.model.ITreeSessionStateManager;
27 import org.apache.tapestry.contrib.tree.model.ITreeStateListener;
28 import org.apache.tapestry.contrib.tree.model.ITreeStateModel;
29 import org.apache.tapestry.contrib.tree.model.TreeStateEvent;
30 import org.apache.tapestry.contrib.tree.simple.SimpleTreeModel;
31 import org.apache.tapestry.html.BasePage;
32 import org.apache.tapestry.workbench.tree.examples.fsmodel.FileSystem;
33 import org.apache.tapestry.workbench.tree.examples.fsmodel.FileSystemDataModel;
34 import org.apache.tapestry.workbench.tree.examples.fsmodel.FileSystemStateManager;
35 import org.apache.tapestry.workbench.tree.examples.fsmodel.FolderObject;
36 import org.apache.tapestry.workbench.tree.examples.fsmodel.NodeRenderFactory;
37
38 public abstract class FileSystemTree extends BasePage implements ISelectedFolderSource,
39         ITreeStateListener
40 {
41     private ITreeSessionStateManager m_objTreeSessionStateManager = null;
42
43     private ITreeDataModel m_objDataModel;
44
45     private ITreeModel m_objModel;
46
47     private Object JavaDoc m_objValue;
48
49     /**
50      * Injected
51      *
52      * @since 4.0
53      */

54     public abstract String JavaDoc getTreeRootDir();
55
56     public FileSystemTree()
57     {
58         super();
59     }
60
61     protected void initialize()
62     {
63         super.initialize();
64         m_objDataModel = null;
65         m_objValue = null;
66         m_objTreeSessionStateManager = null;
67     }
68
69     public void initTableModel()
70     {
71         ITreeNode objParent;
72         
73         String JavaDoc strRootDir = getTreeRootDir();
74
75         System.out.println("strRootDir = " + strRootDir);
76
77         if (strRootDir == null || "".equals(strRootDir))
78         {
79             objParent = new FileSystem();
80         }
81         else
82         {
83             FolderObject objFolder = new FolderObject(null, new File JavaDoc(strRootDir), true);
84             objFolder.reload();
85             objParent = objFolder;
86         }
87
88         m_objDataModel = new FileSystemDataModel(objParent);
89         m_objModel = new SimpleTreeModel(m_objDataModel);
90     }
91
92     public Date JavaDoc getCurrentTime()
93     {
94         return new Date JavaDoc();
95     }
96
97     public ITreeModel getTreeModel()
98     {
99         if (m_objDataModel == null)
100         {
101             initTableModel();
102         }
103         return m_objModel;
104     }
105
106     /**
107      * Returns the value.
108      *
109      * @return Object
110      */

111     public Object JavaDoc getValue()
112     {
113         return m_objValue;
114     }
115
116     /**
117      * Sets the value.
118      *
119      * @param value
120      * The value to set
121      */

122     public void setValue(Object JavaDoc value)
123     {
124         m_objValue = value;
125     }
126
127     public INodeRenderFactory getRenderFactory()
128     {
129         return new NodeRenderFactory();
130     }
131
132     public ITreeSessionStateManager getSessionStateManager()
133     {
134         //IPage objPage = getRequestCycle().getPage("contrib:TreeNodeViewPage");
135
//System.out.println("TreeNodeViewPage NamespaceId :
136
// "+objPage.getNamespace().getNamespaceId());
137

138         if (m_objTreeSessionStateManager == null)
139         {
140             String JavaDoc strRootDir = getTreeRootDir();
141             //System.out.println("strRootDir = " + strRootDir);
142

143             m_objTreeSessionStateManager = new FileSystemStateManager(strRootDir);
144         }
145         return m_objTreeSessionStateManager;
146     }
147
148     /**
149      * @see org.apache.tapestry.workbench.tree.examples.ISelectedFolderSource#getSelectedFolder()
150      */

151     public Collection JavaDoc getSelectedFolderChildren()
152     {
153         TreeView objTreeView = (TreeView) getComponent("treeView");
154         ITreeStateModel objTreeStateModel = objTreeView.getTreeModel().getTreeStateModel();
155         Object JavaDoc objSelectedNodeUID = objTreeStateModel.getSelectedNode();
156         ITreeNode objSelectedNode = null;
157         if (objSelectedNodeUID != null)
158             objSelectedNode = (ITreeNode) getTreeModel().getTreeDataModel().getObject(
159                     objSelectedNodeUID);
160         else
161         {
162             objSelectedNode = (ITreeNode) getTreeModel().getTreeDataModel().getRoot();
163         }
164         return objSelectedNode.getChildren();
165     }
166
167     /**
168      * @see org.apache.tapestry.contrib.tree.model.ITreeStateListener#treeStateChanged(org.apache.tapestry.contrib.tree.model.TreeStateEvent)
169      */

170     public void treeStateChanged(TreeStateEvent objEvent)
171     {
172         DirectoryTableView objDirectoryTableView = (DirectoryTableView) getComponent("directoryTableView");
173         objDirectoryTableView.resetState();
174     }
175
176     public ITreeStateListener getTreeStateListener()
177     {
178         return this;
179     }
180
181     public ISelectedFolderSource getSelectedFolderSource()
182     {
183         return this;
184     }
185
186     /**
187      * @see org.apache.tapestry.workbench.tree.examples.ISelectedFolderSource#getSelectedNodeName()
188      */

189     public String JavaDoc getSelectedNodeName()
190     {
191         TreeView objTreeView = (TreeView) getComponent("treeView");
192         ITreeStateModel objTreeStateModel = objTreeView.getTreeModel().getTreeStateModel();
193         Object JavaDoc objSelectedNodeUID = objTreeStateModel.getSelectedNode();
194         ITreeNode objSelectedNode = null;
195         if (objSelectedNodeUID != null)
196             objSelectedNode = (ITreeNode) getTreeModel().getTreeDataModel().getObject(
197                     objSelectedNodeUID);
198         else
199         {
200             objSelectedNode = (ITreeNode) getTreeModel().getTreeDataModel().getRoot();
201         }
202         return objSelectedNode.toString();
203     }
204
205 }
Popular Tags