1 30 package com.genimen.djeneric.tools.specifier.tree; 31 32 import java.util.HashMap ; 33 34 import javax.swing.ImageIcon ; 35 import javax.swing.tree.DefaultMutableTreeNode ; 36 import javax.swing.tree.DefaultTreeModel ; 37 import javax.swing.tree.TreePath ; 38 39 import com.genimen.djeneric.language.Messages; 40 import com.genimen.djeneric.repository.DjObject; 41 import com.genimen.djeneric.repository.exceptions.DjenericException; 42 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 43 import com.genimen.djeneric.structure.ExtentUsage; 44 import com.genimen.djeneric.structure.RelationUsage; 45 import com.genimen.djeneric.structure.ResourceDefinition; 46 import com.genimen.djeneric.tools.specifier.Specifier; 47 import com.genimen.djeneric.tools.specifier.interfaces.DjenericObjectContainer; 48 49 public class DjenericNode extends DjenericTreeNode implements DjenericObjectContainer 50 { 51 private static final long serialVersionUID = 1L; 52 DjObject _obj; 53 ImageIcon _imageIcon = null; 54 55 public DjenericNode() 56 { 57 } 58 59 public ImageIcon getImageIcon() 60 { 61 if (_imageIcon != null) return _imageIcon; 62 63 ExtentUsage usg = getExtentUsage(); 64 65 if (usg != null) 66 { 67 ResourceDefinition res = usg.getImageIconResource(); 68 if (res != null) 69 { 70 _imageIcon = new ImageIcon (res.getBytes()); 71 } 72 } 73 if (_imageIcon == null) _imageIcon = Specifier.getImageIcon("document.gif"); 74 return _imageIcon; 75 } 76 77 public DjObject getObject() 78 { 79 return _obj; 80 } 81 82 public void setObject(DjObject obj) 83 { 84 _obj = obj; 85 } 86 87 public String toString() 88 { 89 if (_obj.isMarkedForDelete()) return Messages.getString("global.Deleted"); 90 return _obj.getDescriptor(getExtentUsage().getDescriptorExpression()); 91 } 92 93 public boolean notInTreeAnymore() 94 { 95 return getParent() == null; 96 } 97 98 public void delegateReload(Object someNode) throws DjenericException 99 { 100 if (!(someNode instanceof DefaultMutableTreeNode )) return; 101 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) someNode; 102 103 DefaultTreeModel mdl = getModel(); 104 for (int i = 0; i < mdl.getChildCount(node); i++) 105 { 106 Object n = mdl.getChild(node, i); 107 if (n instanceof DjenericNode) 108 { 109 DjenericNode dn = (DjenericNode) n; 110 if (dn._obj.getObjectId() == _obj.getObjectId()) dn.reload(); 111 } 112 else delegateReload(n); 113 } 114 } 115 116 public void reload() throws DjenericException 117 { 118 if (notInTreeAnymore()) 119 { 120 delegateReload(getModel().getRoot()); 121 return; 122 } 123 _alreadyLoaded = false; 124 getObject().reload(); 125 expandNode(); 126 } 127 128 public void delegateExpandNode(Object someNode) throws DjenericException 129 { 130 if (!(someNode instanceof DefaultMutableTreeNode )) return; 131 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) someNode; 132 133 DefaultTreeModel mdl = getModel(); 134 for (int i = 0; i < mdl.getChildCount(node); i++) 135 { 136 Object n = mdl.getChild(node, i); 137 if (n instanceof DjenericNode) 138 { 139 DjenericNode dn = (DjenericNode) n; 140 if (dn._obj.getObjectId() == _obj.getObjectId()) dn.expandNode(); 141 } 142 else delegateReload(n); 143 } 144 } 145 146 public void expandNode() throws DjenericException 147 { 148 if (_alreadyLoaded) return; 149 150 if (notInTreeAnymore()) 151 { 152 delegateExpandNode(getModel().getRoot()); 153 return; 154 } 155 156 boolean isCollapsed = _tree.isCollapsed(new TreePath (this.getPath())); 157 removeAllChildren(); 158 159 try 160 { 161 ExtentUsage taus = getExtentUsage(); 162 for (int i = 0; i < taus.getDetailRelationCount(); i++) 163 { 164 RelationUsage usage = taus.getDetailRelation(i); 165 String className = usage.getDetail().getCustomNodeClass(); 166 167 DjenericFolder folder; 168 169 if (className == null) folder = new DjenericFolder(); 170 else folder = (DjenericFolder) getSpecifierPanelContainer().loadClass(className).newInstance(); 171 172 folder.setVia(usage); 173 folder.setSpecifierPanelContainer(getSpecifierPanelContainer()); 174 175 insertAsFolder(folder); 176 } 177 _alreadyLoaded = true; 178 } 179 catch (InstantiationException e) 180 { 181 throw new DjenericException(e); 182 } 183 catch (IllegalAccessException e) 184 { 185 throw new DjenericException(e); 186 } 187 catch (ClassNotFoundException e) 188 { 189 throw new DjenericException(e); 190 } 191 finally 192 { 193 getModel().nodeStructureChanged(this); 194 if (!isCollapsed) _tree.expandPath(new TreePath (this.getPath())); 195 } 196 } 197 198 public void delete() throws Exception 199 { 200 getObject().markForDelete(); 201 getSession().commit(); 202 DjenericTreeNode parent = getParentTreeNode(); 203 removeFromParent(); 204 getModel().nodeStructureChanged(parent); 205 } 206 207 public boolean canCopy() 208 { 209 return canEdit() && canCreate() && getExtentUsage().getEditorTarget() == null; 210 } 211 212 public boolean canExport() 213 { 214 return getExtentUsage().getEditor() != null; 215 } 216 217 protected void collectParameters(HashMap parentIds) 218 { 219 if (getObject() != null && !parentIds.containsKey(getExtentUsage().getId())) 220 { 221 parentIds.put(getExtentUsage().getId(), getObject()); 222 } 223 super.collectParameters(parentIds); 224 } 225 226 public DjObject getTargetObject() throws ObjectNotDefinedException, DjenericException 227 { 228 String editorTarget = getExtentUsage().getEditorTarget(); 229 DjObject obj = getObject(); 230 231 if (editorTarget != null) 232 { 233 obj = (DjObject) obj.get(editorTarget); 234 } 235 return obj; 236 } 237 238 } | Popular Tags |