Revision: 3207
Author: [email protected]
Date: Tue Dec 15 15:05:26 2009
Log: Fixed an error recently introduced with loading a new project in the model. The tables in the root given in the constructor did not have folders created for them so an NPE was thrown later when the folders were attempted to be accessed. The folders also need to be created for nodes that are added with descendants of children. A recursive loop was also fixed when the tree model would get the child count of a node which was not populated. This caused the getChildCount method and populate of the node to cycle.
http://code.google.com/p/power-architect/source/detail?r=3207

Modified:
/branches/sqlobject-spobject-model/src/ca/sqlpower/architect/swingui/dbtree/DBTreeModel.java

=======================================
--- /branches/sqlobject-spobject-model/src/ca/sqlpower/architect/swingui/dbtree/DBTreeModel.java Tue Dec 15 13:50:36 2009 +++ /branches/sqlobject-spobject-model/src/ca/sqlpower/architect/swingui/dbtree/DBTreeModel.java Tue Dec 15 15:05:26 2009
@@ -175,16 +175,16 @@

if (e.getChild() instanceof SQLTable && foldersInTables.get(e.getChild()) == null) {
                 SQLTable table = (SQLTable) e.getChild();
-                List<FolderNode> folderList = new ArrayList<FolderNode>();
-                foldersInTables.put(table, folderList);
- FolderNode SQLColumnFolder = new FolderNode(table, SQLColumn.class);
-                folderList.add(SQLColumnFolder);
- FolderNode SQLRelationshipFolder = new FolderNode(table, SQLRelationship.class);
-                folderList.add(SQLRelationshipFolder);
- FolderNode SQLIndexFolder = new FolderNode(table, SQLIndex.class);
-                folderList.add(SQLIndexFolder);
+                createFolders(table);
+                List<FolderNode> folderList = foldersInTables.get(table);
+                int[] positions = new int[folderList.size()];
+                for (int i = 0; i < folderList.size(); i++) {
+                    positions[i] = i;
+                }
fireTreeNodesInserted(new TreeModelEvent(table, getPathToNode(table), - new int[]{0, 1, 2}, new Object[]{SQLColumnFolder, SQLRelationshipFolder, SQLIndexFolder}));
+                        positions, folderList.toArray()));
+            } else {
+                setupTreeForNode((SQLObject) e.getChild());
             }

         }
@@ -289,7 +289,13 @@

        protected SQLObject root;

- protected final Map<SQLTable, List<FolderNode>> foldersInTables = new HashMap<SQLTable, List<FolderNode>>();
+       /**
+ * Each table in the tree model is entered in the map when it is added to the tree + * and is mapped to folders that contains the children of the table broken into their
+        * types.
+        */
+       protected final Map<SQLTable, List<FolderNode>> foldersInTables =
+           new HashMap<SQLTable, List<FolderNode>>();

        /**
         * Creates a tree model with all of the SQLDatabase objects in the
@@ -304,6 +310,20 @@
                this.root = root;
                this.treeModelListeners = new LinkedList();
                SQLPowerUtils.listenToHierarchy(root, treeListener);
+               setupTreeForNode(root);
+       }
+
+       /**
+        * Recursively walks the tree doing any necessary setup for each node.
+        * At current this just adds folders for {...@link SQLTable} objects.
+        */
+       private void setupTreeForNode(SQLObject node) {
+           if (node instanceof SQLTable) {
+               createFolders((SQLTable) node);
+           }
+           for (SQLObject child : node.getChildrenWithoutPopulating()) {
+               setupTreeForNode(child);
+           }
        }

        public Object getRoot() {
@@ -340,8 +360,8 @@

                SQLObject sqlParent = (SQLObject) parent;
                try {
- if (logger.isDebugEnabled()) logger.debug("returning "+sqlParent.getChildCount()); //$NON-NLS-1$
-                       return sqlParent.getChildCount();
+ if (logger.isDebugEnabled()) logger.debug("returning "+sqlParent.getChildrenWithoutPopulating().size()); //$NON-NLS-1$
+                       return sqlParent.getChildrenWithoutPopulating().size();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
@@ -536,4 +556,21 @@
     public void setRefireEventsOnAnyThread(boolean v) {
         refireOnAnyThread = v;
     }
-}
+
+    /**
+ * Creates all of the folders the given table should contain for its children
+     * and adds them to the {...@link #foldersInTables} map.
+     */
+    private void createFolders(SQLTable table) {
+        if (foldersInTables.get(table) == null) {
+            List<FolderNode> folderList = new ArrayList<FolderNode>();
+            foldersInTables.put(table, folderList);
+ FolderNode SQLColumnFolder = new FolderNode(table, SQLColumn.class);
+            folderList.add(SQLColumnFolder);
+ FolderNode SQLRelationshipFolder = new FolderNode(table, SQLRelationship.class);
+            folderList.add(SQLRelationshipFolder);
+ FolderNode SQLIndexFolder = new FolderNode(table, SQLIndex.class);
+            folderList.add(SQLIndexFolder);
+        }
+    }
+}

Reply via email to