1 30 package com.genimen.djeneric.web.renderers.tree; 31 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 import java.util.StringTokenizer ; 35 36 import org.w3c.dom.DOMException ; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 40 import com.genimen.djeneric.language.Messages; 41 import com.genimen.djeneric.repository.DjSession; 42 import com.genimen.djeneric.repository.exceptions.DjenericException; 43 import com.genimen.djeneric.structure.ExtentUsage; 44 import com.genimen.djeneric.util.DjStatusDisplayer; 45 46 public abstract class AbstractWebNode 47 { 48 public static final int MAX_NESTED_LEVEL = 25; 49 private static int _uidCounter = 1; 50 51 protected boolean _alreadyLoaded = false; 52 protected ExtentUsage _extentUsageBasedOn; 53 protected DjStatusDisplayer _statusDisplayer = null; 54 AbstractWebNode _parent = null; 55 ArrayList _children = new ArrayList (); 56 boolean _expanded = false; 57 private String _uid; 58 private String _path; 59 DjSession _session; 60 61 public AbstractWebNode(DjSession session) 62 { 63 _session = session; 64 _uid = String.valueOf(_uidCounter++); 65 if (_uidCounter > 999999999) _uidCounter = 1; 66 } 67 68 public abstract Element asXml(Document doc) throws DOMException , DjenericException; 69 70 public abstract void reload() throws DjenericException; 71 72 public abstract void delete() throws Exception ; 73 74 public abstract void setExpanded(boolean expand) throws DjenericException; 75 76 public void setExtentUsage(ExtentUsage usage) 77 { 78 _extentUsageBasedOn = usage; 79 } 80 81 public void setStatusMessage(String msg, boolean informative) 82 { 83 if (_statusDisplayer != null) 84 { 85 _statusDisplayer.setStatusMessage(msg, informative); 86 } 87 } 88 89 public ExtentUsage getExtentUsage() 90 { 91 return _extentUsageBasedOn; 92 } 93 94 public String toString() 95 { 96 if (_extentUsageBasedOn != null) return _extentUsageBasedOn.getTitle(); 97 return "Model"; 98 } 99 100 public AbstractWebNode getParent() 101 { 102 return _parent; 103 } 104 105 public void insertAsChild(AbstractWebNode node) 106 { 107 _children.add(node); 108 node.setParent(this); 109 } 110 111 public void setParent(AbstractWebNode node) 112 { 113 _parent = node; 114 _path = null; 116 } 117 118 public void reloadParent() throws Exception 119 { 120 AbstractWebNode p = getParent(); 121 if (p != null) p.reload(); 122 } 123 124 protected void expandAll(int level) throws DjenericException 125 { 126 if (level >= MAX_NESTED_LEVEL) 127 { 128 setStatusMessage(Messages.getString("DjenericTreeNode.MaximumNested", String.valueOf(MAX_NESTED_LEVEL)), true); 129 } 130 else 131 { 132 133 setExpanded(true); 134 135 for (int i = 0; i < getChildCount(); i++) 136 { 137 getChildAt(i).expandAll(level + 1); 138 } 139 } 140 } 141 142 public AbstractWebNode getChildAt(int i) 143 { 144 return (AbstractWebNode) _children.get(i); 145 } 146 147 public int getChildCount() 148 { 149 return _children.size(); 150 } 151 152 public void collapseAll() throws DjenericException 153 { 154 setExpanded(false); 155 156 for (int i = 0; i < getChildCount(); i++) 157 { 158 getChildAt(i).collapseAll(); 159 } 160 } 161 162 public boolean canDelete() throws DjenericException 163 { 164 return getExtentUsage() != null && getExtentUsage().isDeleteAllowed() && getSession().canDelete(); 165 } 166 167 public boolean canCreate() throws DjenericException 168 { 169 if (getExtentUsage() == null) return false; 170 171 return getExtentUsage() != null && getExtentUsage().isInsertAllowed() && canEdit() && getSession().canCreate(); 172 } 173 174 public boolean canFilter() throws DjenericException 175 { 176 return false; 177 } 178 179 public boolean canEdit() throws DjenericException 180 { 181 return getExtentUsage() != null && getExtentUsage().getEditor() != null && getSession().canModify(); 182 } 183 184 public void filter() throws DjenericException 185 { 186 throw new DjenericException(Messages.getString("DjenericTreeNode.CanNotFilter")); 188 } 189 190 public AbstractWebNode getNode(String path) 191 { 192 StringTokenizer st = new StringTokenizer (path, "/"); 193 return selectNode(st); 194 } 195 196 protected AbstractWebNode selectNode(StringTokenizer st) 197 { 198 if (!st.hasMoreElements()) 199 { 200 return this; 201 } 202 203 while (st.hasMoreElements()) 204 { 205 String currentNodeUid = st.nextToken(); 206 207 for (int i = 0; i < getChildCount(); i++) 208 { 209 if (currentNodeUid.equals(getChildAt(i).getUid())) 210 { 211 return getChildAt(i).selectNode(st); 212 } 213 } 214 } 215 return null; 216 } 217 218 public boolean canCopy() throws DjenericException 219 { 220 return false; 221 } 222 223 public boolean canExport() throws DjenericException 224 { 225 return false; 226 } 227 228 protected void collectParameters(HashMap parentIds) 229 { 230 if (getParent() != null) getParent().collectParameters(parentIds); 231 } 232 233 public HashMap getParameters() 234 { 235 HashMap result = new HashMap (); 236 collectParameters(result); 237 return result; 238 } 239 240 public DjStatusDisplayer getStatusDisplayer() 241 { 242 return _statusDisplayer; 243 } 244 245 public void setStatusDisplayer(DjStatusDisplayer statusDisplayer) 246 { 247 _statusDisplayer = statusDisplayer; 248 } 249 250 public boolean isExpanded() 251 { 252 return _expanded; 253 } 254 255 public void expandPath() throws DjenericException 256 { 257 setExpanded(true); 258 if (getParent() != null) getParent().expandPath(); 259 } 260 261 public void removeAllChildren() 262 { 263 _children.clear(); 264 265 } 266 267 public void removeChild(AbstractWebNode node) 268 { 269 _children.remove(node); 270 271 } 272 273 protected String getUid() 274 { 275 return _uid; 276 } 277 278 public String getPath() 279 { 280 if (_path != null) return _path; 281 if (getParent() != null) 282 { 283 _path = getParent().getPath() + "/" + _uid; 284 } 285 else 286 { 287 _path = ""; 289 } 290 291 return _path; 292 } 293 294 public boolean isExpandable() 295 { 296 return (_alreadyLoaded && getChildCount() > 0) || getExtentUsage().getDetailRelationCount() > 0 || !_alreadyLoaded; 297 } 298 299 public DjSession getSession() 300 { 301 return _session; 302 } 303 304 public void expandAll() throws DjenericException 305 { 306 expandAll(0); 307 } 308 309 } | Popular Tags |