1 30 package com.genimen.djeneric.tools.specifier.tree; 31 32 import java.awt.Container ; 33 import java.awt.Frame ; 34 import java.util.HashMap ; 35 import java.util.StringTokenizer ; 36 37 import javax.swing.ImageIcon ; 38 import javax.swing.RepaintManager ; 39 import javax.swing.tree.DefaultMutableTreeNode ; 40 import javax.swing.tree.DefaultTreeModel ; 41 import javax.swing.tree.TreeNode ; 42 import javax.swing.tree.TreePath ; 43 44 import com.genimen.djeneric.language.Messages; 45 import com.genimen.djeneric.repository.DjPersistenceManager; 46 import com.genimen.djeneric.repository.DjSession; 47 import com.genimen.djeneric.repository.exceptions.DjenericException; 48 import com.genimen.djeneric.structure.ExtentUsage; 49 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanelContainer; 50 import com.genimen.djeneric.util.DjStatusDisplayer; 51 52 public abstract class DjenericTreeNode extends DefaultMutableTreeNode 53 { 54 protected boolean _alreadyLoaded = false; 55 protected DjenericTree _tree = null; 56 protected ExtentUsage _extentUsageBasedOn; 57 protected DjStatusDisplayer _statusDisplayer = null; 58 public static final int MAX_NESTED_LEVEL = 25; 59 SpecifierPanelContainer _specifier; 60 61 public DjenericTreeNode() 62 { 63 } 64 65 public void setExtentUsage(ExtentUsage usage) 66 { 67 _extentUsageBasedOn = usage; 68 } 69 70 public void setTree(DjenericTree tree) 71 { 72 _tree = tree; 73 } 74 75 public void setStatusMessage(String msg, boolean informative) 76 { 77 if (_statusDisplayer != null) 78 { 79 _statusDisplayer.setStatusMessage(msg, informative); 80 } 81 else if (_tree == null) 82 { 83 System.out.println(msg); 84 } 85 else 86 { 87 Container cont = _tree.getParent(); 88 while (cont != null && !(cont instanceof DjStatusDisplayer)) 89 cont = cont.getParent(); 90 _statusDisplayer = (DjStatusDisplayer) cont; 91 } 92 93 if (_statusDisplayer != null) _statusDisplayer.setStatusMessageNow(msg, informative); 94 } 95 96 public Frame getFrame() 97 { 98 if (_tree == null) 99 { 100 return null; 101 } 102 else 103 { 104 Container cont = _tree.getParent(); 105 while (cont != null && !(cont instanceof Frame )) 106 cont = cont.getParent(); 107 return (Frame ) cont; 108 } 109 } 110 111 public ExtentUsage getExtentUsage() 112 { 113 return _extentUsageBasedOn; 114 } 115 116 public String toString() 117 { 118 if (_extentUsageBasedOn != null) return _extentUsageBasedOn.getTitle(); 119 return "Model"; 120 } 121 122 public DefaultTreeModel getModel() 123 { 124 return (DefaultTreeModel ) _tree.getModel(); 125 } 126 127 public DjPersistenceManager getManager() 128 { 129 return _tree.getManager(); 130 } 131 132 public DjSession getSession() 133 { 134 return _tree.getSession(); 135 } 136 137 public DjenericTreeNode getParentTreeNode() 138 { 139 return (DjenericTreeNode) getParent(); 140 } 141 142 protected void copyInternalReferences(DjenericTreeNode node) 143 { 144 node.setTree(_tree); 145 node.setSpecifierPanelContainer(_specifier); 146 } 147 148 public void insertAsChild(DjenericTreeNode node) 149 { 150 getModel().insertNodeInto(node, this, getChildCount()); 151 copyInternalReferences(node); 152 } 153 154 public void insertAsFolder(DjenericTreeNode node) 155 { 156 getModel().insertNodeInto(node, this, getChildCount()); 157 getModel().insertNodeInto(new DefaultMutableTreeNode (), node, 0); 158 copyInternalReferences(node); 159 } 160 161 public void insertAsSibling(DjenericTreeNode node) 162 { 163 getModel().insertNodeInto(node, parent, parent.getChildCount()); 164 copyInternalReferences(node); 165 } 166 167 public void removeFromTree() 168 { 169 getModel().removeNodeFromParent(this); 170 } 171 172 public void repaintTree() 173 { 174 RepaintManager rm = RepaintManager.currentManager(_tree); 175 if (rm != null) 176 { 177 rm.validateInvalidComponents(); 178 rm.paintDirtyRegions(); 179 } 180 } 181 182 public void reloadParent() throws Exception 183 { 184 DjenericTreeNode p = getParentTreeNode(); 185 if (p != null) p.reload(); 186 } 187 188 public void expandAll(int level) 189 { 190 if (level >= MAX_NESTED_LEVEL) 191 { 192 setStatusMessage(Messages.getString("DjenericTreeNode.MaximumNested", String.valueOf(MAX_NESTED_LEVEL)), true); 193 return; 194 } 195 196 _tree.expandPath(new TreePath (this.getPath())); 197 int cc = getChildCount(); 198 for (int i = 0; i < cc; i++) 199 { 200 DjenericTreeNode child = (DjenericTreeNode) getChildAt(i); 201 child.expandAll(level + 1); 202 } 203 } 204 205 public void collapseAll() 206 { 207 int cc = getChildCount(); 208 for (int i = 0; i < cc; i++) 209 { 210 if (!(getChildAt(i) instanceof DjenericTreeNode)) continue; 212 DjenericTreeNode child = (DjenericTreeNode) getChildAt(i); 213 child.collapseAll(); 214 } 215 _tree.collapsePath(new TreePath (this.getPath())); 216 } 217 218 public boolean canDelete() 219 { 220 return _extentUsageBasedOn.isDeleteAllowed(); 221 } 222 223 public boolean canCreate() 224 { 225 if (getExtentUsage() == null) return false; 226 227 return getExtentUsage().isInsertAllowed() && canEdit(); 228 } 229 230 public boolean canFilter() 231 { 232 return false; 233 } 234 235 public boolean canEdit() 236 { 237 if (getExtentUsage() == null) return false; 238 239 return getExtentUsage().getEditor() != null; 240 } 241 242 public void filter() throws DjenericException 243 { 244 throw new DjenericException(Messages.getString("DjenericTreeNode.CanNotFilter")); 246 } 247 248 public abstract void reload() throws DjenericException; 249 250 public abstract void expandNode() throws Exception ; 251 252 public abstract ImageIcon getImageIcon(); 253 254 public abstract void delete() throws Exception ; 255 256 public String getPathString() 257 258 { 259 TreeNode [] nodePath = getPath(); 260 StringBuffer result = new StringBuffer (100); 261 for (int i = 1; i < nodePath.length; i++) 263 { 264 result.append(nodePath[i].toString()); 265 if (i < nodePath.length - 1) result.append("/"); 266 } 267 return result.toString(); 268 } 269 270 public TreePath selectNode(String path) 271 { 272 StringTokenizer st = new StringTokenizer (path, "/"); 273 return selectNode(this, st); 274 } 275 276 protected TreePath selectNode(DefaultMutableTreeNode parent, StringTokenizer st) 277 { 278 if (!st.hasMoreElements()) 279 { 280 TreePath tp = new TreePath (parent.getPath()); 281 _tree.setSelectionPath(tp); 282 return tp; 283 } 284 285 while (st.hasMoreElements()) 286 { 287 String currentNodeName = st.nextToken(); 288 289 for (int i = 0; i < parent.getChildCount(); i++) 290 { 291 if (currentNodeName.equals(parent.getChildAt(i).toString())) 292 { 293 _tree.expandPath(new TreePath (((DefaultMutableTreeNode ) (parent.getChildAt(i))).getPath())); 294 return selectNode((DefaultMutableTreeNode ) parent.getChildAt(i), st); 295 } 296 } 297 } 298 return null; 299 } 300 301 public boolean canCopy() 302 { 303 return false; 304 } 305 306 public boolean canExport() 307 { 308 return false; 309 } 310 311 public SpecifierPanelContainer getSpecifierPanelContainer() 312 { 313 return _specifier; 314 } 315 316 public void setSpecifierPanelContainer(SpecifierPanelContainer specifier) 317 { 318 _specifier = specifier; 319 } 320 321 protected void collectParameters(HashMap parentIds) 322 { 323 if (getParent() != null) getParentTreeNode().collectParameters(parentIds); 324 } 325 326 public HashMap getParameters() 327 { 328 HashMap result = new HashMap (); 329 collectParameters(result); 330 return result; 331 } 332 333 } | Popular Tags |