KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > tree > components > TreeView


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.components;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.tapestry.BaseComponent;
20 import org.apache.tapestry.IBinding;
21 import org.apache.tapestry.IMarkupWriter;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.Tapestry;
24 import org.apache.tapestry.contrib.tree.model.ISessionStoreManager;
25 import org.apache.tapestry.contrib.tree.model.ITreeModel;
26 import org.apache.tapestry.contrib.tree.model.ITreeModelSource;
27 import org.apache.tapestry.contrib.tree.model.ITreeSessionStateManager;
28 import org.apache.tapestry.contrib.tree.model.ITreeStateListener;
29 import org.apache.tapestry.contrib.tree.simple.FullTreeSessionStateManager;
30 import org.apache.tapestry.event.PageBeginRenderListener;
31 import org.apache.tapestry.event.PageDetachListener;
32 import org.apache.tapestry.event.PageEvent;
33 import org.apache.tapestry.util.ComponentAddress;
34
35 /**
36  */

37 public abstract class TreeView extends BaseComponent implements PageDetachListener,
38         PageBeginRenderListener, ITreeModelSource
39 {
40
41     private static final Log LOG = LogFactory.getLog(TreeView.class);
42
43     private IBinding m_objSessionStoreManagerBinding;
44
45     private IBinding m_objTreeModelBinding;
46
47     private IBinding m_objSessionStateManagerBinding;
48
49     private ITreeModel m_objTreeModel;
50
51     private ITreeSessionStateManager m_objTreeSessionStateManager;
52
53     private ISessionStoreManager m_objSessionStoreManager;
54
55     private Object JavaDoc m_objTreeSessionState;
56
57     private ComponentAddress m_objComponentAddress;
58
59     public TreeView()
60     {
61         super();
62         initialize();
63     }
64
65     private void initialize()
66     {
67         m_objTreeModel = null;
68         m_objTreeSessionStateManager = null;
69         m_objSessionStoreManager = null;
70         m_objTreeSessionState = null;
71         m_objComponentAddress = null;
72     }
73
74     /**
75      * @see org.apache.tapestry.AbstractComponent#renderComponent(IMarkupWriter, IRequestCycle)
76      */

77
78     /**
79      * @see org.apache.tapestry.event.PageDetachListener#pageDetached(PageEvent)
80      */

81     public void pageDetached(PageEvent arg0)
82     {
83         initialize();
84     }
85
86     /**
87      * @see org.apache.tapestry.event.PageRenderListener#pageBeginRender(PageEvent)
88      */

89     public void pageBeginRender(PageEvent arg0)
90     {
91         if (arg0.getRequestCycle().isRewinding())
92         {
93             return;
94         }
95         storeSesion();
96     }
97
98     /**
99      * Returns the treeModelBinding.
100      *
101      * @return IBinding
102      */

103     public IBinding getTreeModelBinding()
104     {
105         return m_objTreeModelBinding;
106     }
107
108     /**
109      * Sets the treeModelBinding.
110      *
111      * @param treeModelBinding
112      * The treeModelBinding to set
113      */

114     public void setTreeModelBinding(IBinding treeModelBinding)
115     {
116         m_objTreeModelBinding = treeModelBinding;
117     }
118
119     /**
120      * Returns the SessionStoreManagerBinding.
121      *
122      * @return IBinding
123      */

124     public IBinding getSessionStoreManagerBinding()
125     {
126         return m_objSessionStoreManagerBinding;
127     }
128
129     /**
130      * Returns the sessionStateManagerBinding.
131      *
132      * @return IBinding
133      */

134     public IBinding getSessionStateManagerBinding()
135     {
136         return m_objSessionStateManagerBinding;
137     }
138
139     /**
140      * Sets the SessionStoreManagerBinding.
141      *
142      * @param sessionStoreManagerBinding
143      * The SessionStoreManagerBinding to set
144      */

145     public void setSessionStoreManagerBinding(IBinding sessionStoreManagerBinding)
146     {
147         m_objSessionStoreManagerBinding = sessionStoreManagerBinding;
148     }
149
150     /**
151      * Sets the sessionStateManagerBinding.
152      *
153      * @param sessionStateManagerBinding
154      * The sessionStateManagerBinding to set
155      */

156     public void setSessionStateManagerBinding(IBinding sessionStateManagerBinding)
157     {
158         m_objSessionStateManagerBinding = sessionStateManagerBinding;
159     }
160
161     private void extractTreeModel()
162     {
163         if (LOG.isDebugEnabled())
164         {
165             LOG.debug("TreeView.extractTreeModel()");
166         }
167
168         ISessionStoreManager objHolder = getSessionStoreManager();
169         ITreeSessionStateManager objSessionManager = getTreeSessionStateMgr();
170         Object JavaDoc objSessionState;
171         if (objHolder == null)
172         {
173             objSessionState = getTreeSessionState();
174         }
175         else
176         {
177             objSessionState = objHolder.getSessionState(this.getPage(), "treeSessionState");
178         }
179
180         if (objSessionState != null)
181         {
182             m_objTreeModel = objSessionManager.getModel(objSessionState);
183         }
184         else
185         {
186             if (LOG.isDebugEnabled())
187             {
188                 LOG.debug("TreeView.extractTreeModel() from BINDING");
189             }
190
191             m_objTreeModel = (ITreeModel) getTreeModelBinding().getObject();
192         }
193
194     }
195
196     private void storeSesion()
197     {
198         if (LOG.isDebugEnabled())
199         {
200             LOG.debug("TreeView.storeSesion()");
201         }
202
203         ITreeSessionStateManager objSessionManager = getTreeSessionStateMgr();
204         Object JavaDoc objSessionState = objSessionManager.getSessionState(getTreeModel());
205
206         store(objSessionState);
207     }
208
209     private void store(Object JavaDoc objSessionState)
210     {
211         ISessionStoreManager objHolder = getSessionStoreManager();
212
213         if (objHolder == null)
214         {
215             Tapestry.fireObservedChange(this, "treeSessionState", objSessionState);
216         }
217         else
218         {
219             //String strPath = "treeSessionState";
220
String JavaDoc strPath = getExtendedId();
221             if (LOG.isDebugEnabled())
222                 LOG.debug("store(): setting state with: " + strPath);
223             objHolder.setSessionState(this.getPage(), strPath, objSessionState);
224         }
225     }
226
227     /**
228      * @see ITreeComponent#resetState()
229      */

230     public void resetState()
231     {
232         if (LOG.isDebugEnabled())
233         {
234             LOG.debug("TreeView.resetState()");
235         }
236         initialize();
237         store(null);
238     }
239
240     /**
241      * Returns the SessionStoreManager.
242      *
243      * @return ISessionStoreManager
244      */

245     public ISessionStoreManager getSessionStoreManager()
246     {
247         if (m_objSessionStoreManager == null && getSessionStoreManagerBinding() != null)
248         {
249             m_objSessionStoreManager = (ISessionStoreManager) getSessionStoreManagerBinding()
250                     .getObject();
251         }
252
253         return m_objSessionStoreManager;
254     }
255
256     /**
257      * Returns the wizardSessionStateMgr.
258      *
259      * @return IWizardSessionStateManager
260      */

261     public ITreeSessionStateManager getTreeSessionStateMgr()
262     {
263         if (m_objTreeSessionStateManager == null)
264         {
265             IBinding objBinding = getSessionStateManagerBinding();
266             if (objBinding != null)
267             {
268                 Object JavaDoc objManager = objBinding.getObject();
269                 m_objTreeSessionStateManager = (ITreeSessionStateManager) objManager;
270             }
271             else
272             {
273                 m_objTreeSessionStateManager = new FullTreeSessionStateManager();
274             }
275         }
276         return m_objTreeSessionStateManager;
277     }
278
279     public ComponentAddress getComponentPath()
280     {
281         if (m_objComponentAddress == null)
282         {
283             m_objComponentAddress = new ComponentAddress(this);
284         }
285         return m_objComponentAddress;
286     }
287
288     /**
289      * Returns the treeModel.
290      *
291      * @return ITreeModel
292      */

293     public ITreeModel getTreeModel()
294     {
295         if (m_objTreeModel == null)
296         {
297             extractTreeModel();
298         }
299         return m_objTreeModel;
300     }
301
302     /**
303      * Sets the treeModel.
304      *
305      * @param treeModel
306      * The treeModel to set
307      */

308     public void setTreeModel(ITreeModel treeModel)
309     {
310         m_objTreeModel = treeModel;
311     }
312
313     /**
314      * Returns the treeSessionState.
315      *
316      * @return Object
317      */

318     public Object JavaDoc getTreeSessionState()
319     {
320         return m_objTreeSessionState;
321     }
322
323     /**
324      * Sets the treeSessionState.
325      *
326      * @param treeSessionState
327      * The treeSessionState to set
328      */

329     public void setTreeSessionState(Object JavaDoc treeSessionState)
330     {
331         m_objTreeSessionState = treeSessionState;
332     }
333
334     public String JavaDoc getSelectedNodeStyleID()
335     {
336         return getId() + ":selected";
337     }
338
339     /**
340      * @see org.apache.tapestry.BaseComponent#renderComponent(org.apache.tapestry.IMarkupWriter,
341      * org.apache.tapestry.IRequestCycle)
342      */

343     protected void renderComponent(IMarkupWriter arg0, IRequestCycle arg1)
344     {
345         Object JavaDoc objExistedTreeModelSource = arg1
346                 .getAttribute(ITreeModelSource.TREE_MODEL_SOURCE_ATTRIBUTE);
347         arg1.setAttribute(ITreeModelSource.TREE_MODEL_SOURCE_ATTRIBUTE, this);
348
349         super.renderComponent(arg0, arg1);
350         arg1.setAttribute(ITreeModelSource.TREE_MODEL_SOURCE_ATTRIBUTE, objExistedTreeModelSource);
351     }
352
353     /**
354      * @see org.apache.tapestry.contrib.tree.model.ITreeModelSource#getTreeStateListener()
355      */

356     public ITreeStateListener getTreeStateListener()
357     {
358         ITreeStateListener objListener = null;
359         IBinding objBinding = getBinding("treeStateListener");
360         if (objBinding != null)
361         {
362             objListener = (ITreeStateListener) objBinding.getObject();
363         }
364         return objListener;
365     }
366
367 }
Popular Tags