1 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 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 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 77 78 81 public void pageDetached(PageEvent arg0) 82 { 83 initialize(); 84 } 85 86 89 public void pageBeginRender(PageEvent arg0) 90 { 91 if (arg0.getRequestCycle().isRewinding()) 92 { 93 return; 94 } 95 storeSesion(); 96 } 97 98 103 public IBinding getTreeModelBinding() 104 { 105 return m_objTreeModelBinding; 106 } 107 108 114 public void setTreeModelBinding(IBinding treeModelBinding) 115 { 116 m_objTreeModelBinding = treeModelBinding; 117 } 118 119 124 public IBinding getSessionStoreManagerBinding() 125 { 126 return m_objSessionStoreManagerBinding; 127 } 128 129 134 public IBinding getSessionStateManagerBinding() 135 { 136 return m_objSessionStateManagerBinding; 137 } 138 139 145 public void setSessionStoreManagerBinding(IBinding sessionStoreManagerBinding) 146 { 147 m_objSessionStoreManagerBinding = sessionStoreManagerBinding; 148 } 149 150 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 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 objSessionState = objSessionManager.getSessionState(getTreeModel()); 205 206 store(objSessionState); 207 } 208 209 private void store(Object 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 = getExtendedId(); 221 if (LOG.isDebugEnabled()) 222 LOG.debug("store(): setting state with: " + strPath); 223 objHolder.setSessionState(this.getPage(), strPath, objSessionState); 224 } 225 } 226 227 230 public void resetState() 231 { 232 if (LOG.isDebugEnabled()) 233 { 234 LOG.debug("TreeView.resetState()"); 235 } 236 initialize(); 237 store(null); 238 } 239 240 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 261 public ITreeSessionStateManager getTreeSessionStateMgr() 262 { 263 if (m_objTreeSessionStateManager == null) 264 { 265 IBinding objBinding = getSessionStateManagerBinding(); 266 if (objBinding != null) 267 { 268 Object 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 293 public ITreeModel getTreeModel() 294 { 295 if (m_objTreeModel == null) 296 { 297 extractTreeModel(); 298 } 299 return m_objTreeModel; 300 } 301 302 308 public void setTreeModel(ITreeModel treeModel) 309 { 310 m_objTreeModel = treeModel; 311 } 312 313 318 public Object getTreeSessionState() 319 { 320 return m_objTreeSessionState; 321 } 322 323 329 public void setTreeSessionState(Object treeSessionState) 330 { 331 m_objTreeSessionState = treeSessionState; 332 } 333 334 public String getSelectedNodeStyleID() 335 { 336 return getId() + ":selected"; 337 } 338 339 343 protected void renderComponent(IMarkupWriter arg0, IRequestCycle arg1) 344 { 345 Object 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 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 |