1 56 package org.objectstyle.cayenne.modeler; 57 58 import java.util.Comparator ; 59 import java.util.Enumeration ; 60 import java.util.HashMap ; 61 import java.util.Map ; 62 63 import javax.swing.tree.DefaultMutableTreeNode ; 64 import javax.swing.tree.DefaultTreeModel ; 65 import javax.swing.tree.MutableTreeNode ; 66 import javax.swing.tree.TreeNode ; 67 68 import org.objectstyle.cayenne.access.DataDomain; 69 import org.objectstyle.cayenne.access.DataNode; 70 import org.objectstyle.cayenne.map.DataMap; 71 import org.objectstyle.cayenne.project.Project; 72 import org.objectstyle.cayenne.project.ProjectPath; 73 import org.objectstyle.cayenne.project.ProjectTraversal; 74 import org.objectstyle.cayenne.project.ProjectTraversalHandler; 75 76 81 public class ProjectTreeModel extends DefaultTreeModel { 82 83 87 public static DefaultMutableTreeNode wrapProject(Project project) { 88 return wrapProjectNode(project); 89 } 90 91 95 public static DefaultMutableTreeNode wrapProjectNode(Object node) { 96 TraversalHelper helper = new TraversalHelper(); 97 new ProjectTraversal(helper).traverse(node); 98 return helper.getStartNode(); 99 } 100 101 105 public static DefaultMutableTreeNode wrapProjectNode( 106 Object node, 107 DefaultMutableTreeNode parentPath) { 108 109 TraversalHelper helper = new TraversalHelper(); 110 111 ProjectPath path = new ProjectPath(); 113 if (parentPath != null) { 114 path = helper.registerNodes(parentPath.getPath()); 115 } 116 117 new ProjectTraversal(helper).traverse(node, path); 118 return helper.getStartNode(); 119 } 120 121 126 public ProjectTreeModel(Project project) { 127 super(wrapProject(project)); 128 } 129 130 134 public void positionNode( 135 MutableTreeNode parent, 136 DefaultMutableTreeNode treeNode, 137 Comparator comparator) { 138 139 if (treeNode == null) { 140 return; 141 } 142 143 if (parent == null && treeNode != getRoot()) { 144 parent = (MutableTreeNode ) treeNode.getParent(); 145 if (parent == null) { 146 parent = getRootNode(); 147 } 148 } 149 150 Object object = treeNode.getUserObject(); 151 152 int len = parent.getChildCount(); 153 int ins = -1; 154 int rm = -1; 155 156 for (int i = 0; i < len; i++) { 157 DefaultMutableTreeNode node = (DefaultMutableTreeNode ) parent.getChildAt(i); 158 159 if (node == treeNode) { 161 rm = i; 162 continue; 163 } 164 165 if (ins >= 0) { 167 continue; 168 } 169 170 if (comparator.compare(object, node.getUserObject()) <= 0) { 172 ins = i; 173 } 174 } 175 176 if (ins < 0) { 177 ins = len; 178 } 179 180 if (rm == ins) { 181 return; 182 } 183 184 if (rm >= 0) { 186 removeNodeFromParent(treeNode); 187 if (rm < ins) { 188 ins--; 189 } 190 } 191 192 insertNodeInto(treeNode, parent, ins); 194 } 195 196 199 public DefaultMutableTreeNode getRootNode() { 200 return (DefaultMutableTreeNode ) super.getRoot(); 201 } 202 203 public DefaultMutableTreeNode getNodeForObjectPath(Object [] path) { 204 if (path == null || path.length == 0) { 205 return null; 206 } 207 208 DefaultMutableTreeNode currentNode = getRootNode(); 209 210 int start = 0; 212 if (currentNode.getUserObject() == path[0]) { 213 start = 1; 214 } 215 216 for (int i = start; i < path.length; i++) { 217 DefaultMutableTreeNode foundNode = null; 218 Enumeration children = currentNode.children(); 219 while (children.hasMoreElements()) { 220 DefaultMutableTreeNode child = (DefaultMutableTreeNode ) children 221 .nextElement(); 222 if (child.getUserObject() == path[i]) { 223 foundNode = child; 224 break; 225 } 226 } 227 228 if (foundNode == null) { 229 return null; 230 } 231 else { 232 currentNode = foundNode; 233 } 234 } 235 236 return currentNode; 237 } 238 239 static class TraversalHelper implements ProjectTraversalHandler { 240 241 protected DefaultMutableTreeNode startNode; 242 protected Map nodesMap; 243 244 public TraversalHelper() { 245 this.nodesMap = new HashMap (); 246 } 247 248 public DefaultMutableTreeNode getStartNode() { 249 return startNode; 250 } 251 252 255 public ProjectPath registerNodes(TreeNode [] nodes) { 256 ProjectPath path = new ProjectPath(); 257 258 for (int i = 0; i < nodes.length; i++) { 259 DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode ) nodes[i]; 260 261 path = path.appendToPath(treeNode.getUserObject()); 262 263 registerNode(treeNode); 265 } 266 267 return path; 268 } 269 270 public void registerNode(DefaultMutableTreeNode node) { 271 nodesMap.put(node.getUserObject(), node); 272 } 273 274 public void projectNode(ProjectPath nodePath) { 275 276 Object parent = nodePath.getObjectParent(); 277 Object nodeObj = nodePath.getObject(); 278 DefaultMutableTreeNode node = new DefaultMutableTreeNode (nodeObj); 279 280 if (startNode == null) { 281 startNode = node; 282 } 283 else { 284 DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode ) nodesMap 285 .get(parent); 286 nodeParent.add(node); 287 } 288 289 registerNode(node); 290 } 291 292 public boolean shouldReadChildren(Object node, ProjectPath parentPath) { 293 if ((node instanceof DataMap) 295 && parentPath != null 296 && (parentPath.getObject() instanceof DataNode)) { 297 return false; 298 } 299 300 return (node instanceof Project) 301 || (node instanceof DataDomain) 302 || (node instanceof DataMap) 303 || (node instanceof DataNode); 304 } 305 } 306 307 311 class CopyTraversalHelper extends TraversalHelper { 312 313 public void projectNode(ProjectPath nodePath) { 314 DefaultMutableTreeNode node; 315 316 if (startNode == null) { 317 startNode = new DefaultMutableTreeNode (nodePath.getObject()); 318 node = startNode; 319 } 320 else { 321 DefaultMutableTreeNode original = ProjectTreeModel.this 322 .getNodeForObjectPath(nodePath.getPath()); 323 DefaultMutableTreeNode nodeParent = (DefaultMutableTreeNode ) nodesMap 324 .get(nodePath.getObjectParent()); 325 node = new DefaultMutableTreeNode (original.getUserObject()); 326 nodeParent.add(node); 327 } 328 329 registerNode(node); 330 } 331 } 332 } | Popular Tags |