1 30 package com.genimen.djeneric.web.renderers.tree; 31 32 import java.util.HashMap ; 33 34 import org.w3c.dom.DOMException ; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 38 import com.genimen.djeneric.language.Messages; 39 import com.genimen.djeneric.repository.DjObject; 40 import com.genimen.djeneric.repository.DjSession; 41 import com.genimen.djeneric.repository.exceptions.DjenericException; 42 import com.genimen.djeneric.repository.exceptions.NotPersistedException; 43 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 44 import com.genimen.djeneric.structure.ExtentUsage; 45 import com.genimen.djeneric.structure.RelationUsage; 46 import com.genimen.djeneric.tools.specifier.interfaces.DjenericObjectContainer; 47 48 public class WebNode extends AbstractWebNode implements DjenericObjectContainer 49 { 50 DjObject _obj; 51 boolean _deleted = false; 52 53 public WebNode(DjSession session) 54 { 55 super(session); 56 } 57 58 public DjObject getObject() 59 { 60 return _obj; 61 } 62 63 public void setObject(DjObject obj) 64 { 65 _obj = obj; 66 } 67 68 public String toString() 69 { 70 if (_deleted) return Messages.getString("global.Deleted"); 71 if (_obj == null) return "null"; 72 if (_obj.isMarkedForDelete()) return Messages.getString("global.Deleted"); 73 return _obj.getDescriptor(getExtentUsage().getDescriptorExpression()); 74 } 75 76 public boolean notInTreeAnymore() 77 { 78 return getParent() == null; 79 } 80 81 public void reload() throws DjenericException 82 { 83 _alreadyLoaded = false; 84 try 85 { 86 getObject().reload(); 87 } 88 catch (NotPersistedException npe) 89 { 90 _deleted = true; 91 removeAllChildren(); 92 } 93 setExpanded(true); 94 } 95 96 public void load() throws DjenericException 97 { 98 if (_alreadyLoaded) return; 99 100 ExtentUsage taus = getExtentUsage(); 101 if (taus != null) 102 { 103 removeAllChildren(); 104 105 for (int i = 0; i < taus.getDetailRelationCount(); i++) 106 { 107 RelationUsage usage = taus.getDetailRelation(i); 108 String className = usage.getDetail().getCustomNodeClass(); 109 110 WebFolder folder = new WebFolder(getSession()); 111 112 folder.setVia(usage); 113 insertAsChild(folder); 114 } 115 } 116 _alreadyLoaded = true; 117 } 118 119 public void delete() throws DjenericException 120 { 121 getObject().markForDelete(); 122 getSession().commit(); 123 AbstractWebNode parent = getParent(); 124 if (parent != null) parent.removeChild(this); 125 } 126 127 public boolean canCopy() throws DjenericException 128 { 129 return canEdit() && canCreate() && getExtentUsage().getEditorTarget() == null; 130 } 131 132 public boolean canExport() 133 { 134 return getExtentUsage() != null && getExtentUsage().getEditor() != null; 135 } 136 137 protected void collectParameters(HashMap parentIds) 138 { 139 if (getObject() != null && !parentIds.containsKey(getExtentUsage().getId())) 140 { 141 parentIds.put(getExtentUsage().getId(), getObject()); 142 } 143 super.collectParameters(parentIds); 144 } 145 146 public Element asXml(Document doc) throws DOMException , DjenericException 147 { 148 Element node = doc.createElement("node"); 149 150 node.setAttribute("title", toString()); 151 node.setAttribute("nodeid", String.valueOf(getPath())); 152 node.setAttribute("expandable", String.valueOf(isExpandable())); 153 node.setAttribute("expanded", String.valueOf(isExpanded())); 154 node.setAttribute("canedit", String.valueOf(canEdit())); 155 node.setAttribute("candelete", String.valueOf(canDelete())); 156 node.setAttribute("cancreate", String.valueOf(canCreate())); 157 node.setAttribute("canfilter", String.valueOf(canFilter())); 158 node.setAttribute("cancopy", String.valueOf(canCopy())); 159 node.setAttribute("canexport", String.valueOf(canExport())); 160 node.setAttribute("canimport", String.valueOf(getSession().canModify() && getSession().canCreate())); 161 162 if (getExtentUsage() != null && getExtentUsage().getImageIconResource() != null) 163 { 164 node.setAttribute("image", getExtentUsage().getImageIconResource().getAbsolutePath()); 165 } 166 if (isExpanded()) 167 { 168 for (int i = 0; i < getChildCount(); i++) 169 { 170 node.appendChild(getChildAt(i).asXml(doc)); 171 } 172 } 173 174 return node; 175 } 176 177 public void setExpanded(boolean expand) throws DjenericException 178 { 179 if (expand && !_alreadyLoaded) 180 { 181 load(); 182 } 183 _expanded = expand; 184 } 185 186 public boolean isExpandable() 187 { 188 return (_alreadyLoaded && getChildCount() > 0) || getExtentUsage().getDetailRelationCount() > 0; 189 } 190 191 public DjObject getTargetObject() throws ObjectNotDefinedException, DjenericException 192 { 193 String editorTarget = getExtentUsage().getEditorTarget(); 194 DjObject obj = getObject(); 195 196 if (editorTarget != null) 197 { 198 obj = (DjObject) obj.get(editorTarget); 199 } 200 return obj; 201 } 202 203 } | Popular Tags |