KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

118     public Object JavaDoc getValue()
119     {
120         return m_objValue;
121     }
122
123     /**
124      * Sets the value.
125      *
126      * @param value
127      * The value to set
128      */

129     public void setValue(Object JavaDoc value)
130     {
131         m_objValue = value;
132     }
133
134     public INodeRenderFactory getRenderFactory()
135     {
136         return new NodeRenderFactory();
137     }
138
139     public ITreeSessionStateManager getSessionStateManager()
140     {
141         if (m_objTreeSessionStateManager == null)
142         {
143             String JavaDoc strRootDir = getTreeRootDir();
144             m_objTreeSessionStateManager = new FileSystemStateManager(strRootDir);
145         }
146         return m_objTreeSessionStateManager;
147     }
148
149     /**
150      * @return
151      */

152     public ArrayList JavaDoc getTableColumns()
153     {
154         if (m_arrTableColumns == null)
155         {
156             m_arrTableColumns = new ArrayList JavaDoc();
157             m_arrTableColumns.add(new SimpleTableColumn("Date", true)
158             {
159                 public Object JavaDoc getColumnValue(Object JavaDoc objValue)
160                 {
161                     TreeRowObject objRowObject = (TreeRowObject) objValue;
162                     //SFObject objSFObject = (SFObject)objRowObject.getTreeNode();
163
IFileSystemTreeNode objFileSystemTreeNode = (IFileSystemTreeNode) objRowObject
164                             .getTreeNode();
165                     return objFileSystemTreeNode.getDate();
166                 }
167             });
168         }
169         return m_arrTableColumns;
170     }
171
172     /**
173      * @see org.apache.tapestry.workbench.tree.examples.ISelectedFolderSource#getSelectedFolder()
174      */

175     public Collection JavaDoc getSelectedFolderChildren()
176     {
177         TreeTable objTreeTable = (TreeTable) getComponent("tree");
178         ITreeModelSource objTreeModelSource = objTreeTable.getTreeModelSource();
179         ITreeStateModel objTreeStateModel = objTreeModelSource.getTreeModel().getTreeStateModel();
180         Object JavaDoc objSelectedNodeUID = objTreeStateModel.getSelectedNode();
181         ITreeNode objSelectedNode = null;
182         if (objSelectedNodeUID != null)
183             objSelectedNode = (ITreeNode) getTreeModel().getTreeDataModel().getObject(
184                     objSelectedNodeUID);
185         else
186         {
187             objSelectedNode = (ITreeNode) getTreeModel().getTreeDataModel().getRoot();
188         }
189         return objSelectedNode.getChildren();
190     }
191
192     /**
193      * @see org.apache.tapestry.contrib.tree.model.ITreeStateListener#treeStateChanged(org.apache.tapestry.contrib.tree.model.TreeStateEvent)
194      */

195     public void treeStateChanged(TreeStateEvent objEvent)
196     {
197         DirectoryTableView objDirectoryTableView = (DirectoryTableView) getComponent("directoryTableView");
198         objDirectoryTableView.resetState();
199     }
200
201     public ITreeStateListener getTreeStateListener()
202     {
203         return this;
204     }
205
206     public ISelectedFolderSource getSelectedFolderSource()
207     {
208         return this;
209     }
210
211     /**
212      * @see org.apache.tapestry.workbench.tree.examples.ISelectedFolderSource#getSelectedNodeName()
213      */

214     public String JavaDoc getSelectedNodeName()
215     {
216         TreeTable objTreeTable = (TreeTable) getComponent("tree");
217         ITreeModelSource objTreeModelSource = objTreeTable.getTreeModelSource();
218         ITreeStateModel objTreeStateModel = objTreeModelSource.getTreeModel().getTreeStateModel();
219         Object JavaDoc objSelectedNodeUID = objTreeStateModel.getSelectedNode();
220         ITreeNode objSelectedNode = null;
221         if (objSelectedNodeUID != null)
222             objSelectedNode = (ITreeNode) getTreeModel().getTreeDataModel().getObject(
223                     objSelectedNodeUID);
224         else
225         {
226             objSelectedNode = (ITreeNode) getTreeModel().getTreeDataModel().getRoot();
227         }
228         return objSelectedNode.toString();
229     }
230 }
Popular Tags