1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
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 |
38 | |
PageDetachListener, PageBeginRenderListener, ITreeModelSource |
39 | |
{ |
40 | |
|
41 | 0 | 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 m_objTreeSessionState; |
56 | |
|
57 | |
private ComponentAddress m_objComponentAddress; |
58 | |
|
59 | |
public TreeView() |
60 | |
{ |
61 | 0 | super(); |
62 | 0 | initialize(); |
63 | 0 | } |
64 | |
|
65 | |
private void initialize() |
66 | |
{ |
67 | 0 | m_objTreeModel = null; |
68 | 0 | m_objTreeSessionStateManager = null; |
69 | 0 | m_objSessionStoreManager = null; |
70 | 0 | m_objTreeSessionState = null; |
71 | 0 | m_objComponentAddress = null; |
72 | 0 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void pageDetached(PageEvent arg0) |
83 | |
{ |
84 | 0 | initialize(); |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void pageBeginRender(PageEvent arg0) |
91 | |
{ |
92 | 0 | if (arg0.getRequestCycle().isRewinding()) { return; } |
93 | 0 | storeSesion(); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public IBinding getTreeModelBinding() |
102 | |
{ |
103 | 0 | return m_objTreeModelBinding; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public void setTreeModelBinding(IBinding treeModelBinding) |
113 | |
{ |
114 | 0 | m_objTreeModelBinding = treeModelBinding; |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public IBinding getSessionStoreManagerBinding() |
123 | |
{ |
124 | 0 | return m_objSessionStoreManagerBinding; |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public IBinding getSessionStateManagerBinding() |
133 | |
{ |
134 | 0 | return m_objSessionStateManagerBinding; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public void setSessionStoreManagerBinding( |
144 | |
IBinding sessionStoreManagerBinding) |
145 | |
{ |
146 | 0 | m_objSessionStoreManagerBinding = sessionStoreManagerBinding; |
147 | 0 | } |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public void setSessionStateManagerBinding( |
156 | |
IBinding sessionStateManagerBinding) |
157 | |
{ |
158 | 0 | m_objSessionStateManagerBinding = sessionStateManagerBinding; |
159 | 0 | } |
160 | |
|
161 | |
private void extractTreeModel() |
162 | |
{ |
163 | 0 | if (LOG.isDebugEnabled()) |
164 | |
{ |
165 | 0 | LOG.debug("TreeView.extractTreeModel()"); |
166 | |
} |
167 | |
|
168 | 0 | ISessionStoreManager objHolder = getSessionStoreManager(); |
169 | 0 | ITreeSessionStateManager objSessionManager = getTreeSessionStateMgr(); |
170 | |
Object objSessionState; |
171 | 0 | if (objHolder == null) |
172 | |
{ |
173 | 0 | objSessionState = getTreeSessionState(); |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | 0 | objSessionState = objHolder.getSessionState(this.getPage(), |
178 | |
"treeSessionState"); |
179 | |
} |
180 | |
|
181 | 0 | if (objSessionState != null) |
182 | |
{ |
183 | 0 | m_objTreeModel = objSessionManager.getModel(objSessionState); |
184 | |
} |
185 | |
else |
186 | |
{ |
187 | 0 | if (LOG.isDebugEnabled()) |
188 | |
{ |
189 | 0 | LOG.debug("TreeView.extractTreeModel() from BINDING"); |
190 | |
} |
191 | |
|
192 | 0 | m_objTreeModel = (ITreeModel) getTreeModelBinding().getObject(); |
193 | |
} |
194 | |
|
195 | 0 | } |
196 | |
|
197 | |
private void storeSesion() |
198 | |
{ |
199 | 0 | if (LOG.isDebugEnabled()) |
200 | |
{ |
201 | 0 | LOG.debug("TreeView.storeSesion()"); |
202 | |
} |
203 | |
|
204 | 0 | ITreeSessionStateManager objSessionManager = getTreeSessionStateMgr(); |
205 | 0 | Object objSessionState = objSessionManager |
206 | |
.getSessionState(getTreeModel()); |
207 | |
|
208 | 0 | store(objSessionState); |
209 | 0 | } |
210 | |
|
211 | |
private void store(Object objSessionState) |
212 | |
{ |
213 | 0 | ISessionStoreManager objHolder = getSessionStoreManager(); |
214 | |
|
215 | 0 | if (objHolder == null) |
216 | |
{ |
217 | 0 | Tapestry.fireObservedChange(this, "treeSessionState", |
218 | |
objSessionState); |
219 | |
} |
220 | |
else |
221 | |
{ |
222 | |
|
223 | 0 | String strPath = getExtendedId(); |
224 | 0 | if (LOG.isDebugEnabled()) |
225 | 0 | LOG.debug("store(): setting state with: " + strPath); |
226 | 0 | objHolder.setSessionState(this.getPage(), strPath, objSessionState); |
227 | |
} |
228 | 0 | } |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public void resetState() |
234 | |
{ |
235 | 0 | if (LOG.isDebugEnabled()) |
236 | |
{ |
237 | 0 | LOG.debug("TreeView.resetState()"); |
238 | |
} |
239 | 0 | initialize(); |
240 | 0 | store(null); |
241 | 0 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
public ISessionStoreManager getSessionStoreManager() |
249 | |
{ |
250 | 0 | if (m_objSessionStoreManager == null |
251 | |
&& getSessionStoreManagerBinding() != null) |
252 | |
{ |
253 | 0 | m_objSessionStoreManager = (ISessionStoreManager) getSessionStoreManagerBinding() |
254 | |
.getObject(); |
255 | |
} |
256 | |
|
257 | 0 | return m_objSessionStoreManager; |
258 | |
} |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public ITreeSessionStateManager getTreeSessionStateMgr() |
266 | |
{ |
267 | 0 | if (m_objTreeSessionStateManager == null) |
268 | |
{ |
269 | 0 | IBinding objBinding = getSessionStateManagerBinding(); |
270 | 0 | if (objBinding != null) |
271 | |
{ |
272 | 0 | Object objManager = objBinding.getObject(); |
273 | 0 | m_objTreeSessionStateManager = (ITreeSessionStateManager) objManager; |
274 | 0 | } |
275 | |
else |
276 | |
{ |
277 | 0 | m_objTreeSessionStateManager = new FullTreeSessionStateManager(); |
278 | |
} |
279 | |
} |
280 | 0 | return m_objTreeSessionStateManager; |
281 | |
} |
282 | |
|
283 | |
public ComponentAddress getComponentPath() |
284 | |
{ |
285 | 0 | if (m_objComponentAddress == null) |
286 | |
{ |
287 | 0 | m_objComponentAddress = new ComponentAddress(this); |
288 | |
} |
289 | 0 | return m_objComponentAddress; |
290 | |
} |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public ITreeModel getTreeModel() |
298 | |
{ |
299 | 0 | if (m_objTreeModel == null) |
300 | |
{ |
301 | 0 | extractTreeModel(); |
302 | |
} |
303 | 0 | return m_objTreeModel; |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
public void setTreeModel(ITreeModel treeModel) |
313 | |
{ |
314 | 0 | m_objTreeModel = treeModel; |
315 | 0 | } |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
public Object getTreeSessionState() |
323 | |
{ |
324 | 0 | return m_objTreeSessionState; |
325 | |
} |
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
public void setTreeSessionState(Object treeSessionState) |
334 | |
{ |
335 | 0 | m_objTreeSessionState = treeSessionState; |
336 | 0 | } |
337 | |
|
338 | |
public String getSelectedNodeStyleID() |
339 | |
{ |
340 | 0 | return getId() + ":selected"; |
341 | |
} |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
protected void renderComponent(IMarkupWriter arg0, IRequestCycle arg1) |
348 | |
{ |
349 | 0 | Object objExistedTreeModelSource = arg1 |
350 | |
.getAttribute(ITreeModelSource.TREE_MODEL_SOURCE_ATTRIBUTE); |
351 | 0 | arg1.setAttribute(ITreeModelSource.TREE_MODEL_SOURCE_ATTRIBUTE, this); |
352 | |
|
353 | 0 | super.renderComponent(arg0, arg1); |
354 | 0 | arg1.setAttribute(ITreeModelSource.TREE_MODEL_SOURCE_ATTRIBUTE, |
355 | |
objExistedTreeModelSource); |
356 | 0 | } |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
public ITreeStateListener getTreeStateListener() |
362 | |
{ |
363 | 0 | ITreeStateListener objListener = null; |
364 | 0 | IBinding objBinding = getBinding("treeStateListener"); |
365 | 0 | if (objBinding != null) |
366 | |
{ |
367 | 0 | objListener = (ITreeStateListener) objBinding.getObject(); |
368 | |
} |
369 | 0 | return objListener; |
370 | |
} |
371 | |
|
372 | |
} |