KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tapestry.IMarkupWriter;
18 import org.apache.tapestry.IRender;
19 import org.apache.tapestry.IRequestCycle;
20 import org.apache.tapestry.asset.PrivateAsset;
21 import org.apache.tapestry.contrib.tree.components.INodeRenderFactory;
22 import org.apache.tapestry.contrib.tree.model.ITreeDataModel;
23 import org.apache.tapestry.contrib.tree.model.ITreeModelSource;
24 import org.apache.tapestry.contrib.tree.model.ITreeStateModel;
25
26 /**
27  * @author ceco
28  */

29 public class NodeRenderFactory implements INodeRenderFactory {
30
31     /**
32      * Constructor for NodeRenderFactory.
33      */

34     public NodeRenderFactory() {
35         super();
36     }
37
38     public IRender getRenderByID(Object JavaDoc objUniqueKey,
39                                  ITreeModelSource objTreeModelSource,
40                                  IRequestCycle objCycle) {
41         Object JavaDoc objValue = objTreeModelSource.getTreeModel().getTreeDataModel().getObject(objUniqueKey);
42         return getRender(objValue, objTreeModelSource, objCycle);
43     }
44
45     public IRender getRender(Object JavaDoc objValue,
46                              ITreeModelSource objTreeModelSource,
47                              IRequestCycle objCycle) {
48         return new CFileSystemRender(objValue, objTreeModelSource);
49     }
50
51
52     public class CFileSystemRender implements IRender{
53         private Object JavaDoc m_objNode;
54         private ITreeModelSource m_objTreeModelSource;
55
56         public CFileSystemRender(Object JavaDoc objNode, ITreeModelSource objTreeModelSource) {
57             super();
58             m_objNode = objNode;
59             m_objTreeModelSource = objTreeModelSource;
60         }
61
62         public boolean isOpen() {
63             ITreeDataModel objDataModel =
64                 m_objTreeModelSource.getTreeModel().getTreeDataModel();
65             ITreeStateModel objStateModel =
66                 m_objTreeModelSource.getTreeModel().getTreeStateModel();
67             Object JavaDoc objUniqueKey = objDataModel.getUniqueKey(m_objNode, null);
68             return objStateModel.isUniqueKeyExpanded(objUniqueKey);
69         }
70
71         public boolean isSelected(){
72             ITreeDataModel objDataModel =
73                 m_objTreeModelSource.getTreeModel().getTreeDataModel();
74             ITreeStateModel objStateModel =
75                 m_objTreeModelSource.getTreeModel().getTreeStateModel();
76             Object JavaDoc objUniqueKey = objDataModel.getUniqueKey(m_objNode, null);
77             return objUniqueKey.equals(objStateModel.getSelectedNode());
78         }
79
80         public void render(IMarkupWriter objWriter, IRequestCycle objCycle) {
81             PrivateAsset objAsset = getAsset();
82             objWriter.begin("img");
83             objWriter.attribute("border", "0");
84             objWriter.attribute("src", objAsset.buildURL(objCycle));
85             objWriter.attribute("valign", "middle");
86             objWriter.end();
87             objWriter.print(" ");
88             objWriter.begin("span");
89             String JavaDoc strClassName = "fsNodeValue";
90             objWriter.attribute("class", strClassName);
91             objWriter.closeTag();
92             objWriter.print(getNode().toString().trim());
93             objWriter.end();
94         }
95
96         public IFileSystemTreeNode getNode(){
97             return (IFileSystemTreeNode) m_objNode;
98         }
99
100         private PrivateAsset getAsset(){
101             PrivateAsset objAsset;
102
103             if (!isOpen()) {
104                 objAsset = getNode().getAssets().getAssetForCloseNode();
105             } else {
106                 objAsset = getNode().getAssets().getAssetForOpenNode();
107             }
108
109             return objAsset;
110         }
111     }
112 }
113
Popular Tags