Revision: 3475
Author: silva.josemanuel1
Date: Tue Apr 27 10:08:58 2010
Log: ArchitectProject now maps every single node added underneath it, and keeps this up to date by listening to all of them.

JSONMessage has a useful toString method now.
http://code.google.com/p/power-architect/source/detail?r=3475

Modified:
 /trunk/src/main/java/ca/sqlpower/architect/ArchitectProject.java
 /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONMessage.java

=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/ArchitectProject.java Tue Apr 20 15:12:12 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/ArchitectProject.java Tue Apr 27 10:08:58 2010
@@ -35,8 +35,12 @@
 import ca.sqlpower.architect.swingui.PlayPenContentPane;
 import ca.sqlpower.enterprise.client.Group;
 import ca.sqlpower.enterprise.client.User;
+import ca.sqlpower.object.AbstractSPListener;
 import ca.sqlpower.object.AbstractSPObject;
+import ca.sqlpower.object.MappedSPTree;
 import ca.sqlpower.object.ObjectDependentException;
+import ca.sqlpower.object.SPChildEvent;
+import ca.sqlpower.object.SPListener;
 import ca.sqlpower.object.SPObject;
 import ca.sqlpower.object.annotation.Accessor;
 import ca.sqlpower.object.annotation.Constructor;
@@ -65,7 +69,7 @@
  *
  */

-public class ArchitectProject extends AbstractSPObject {
+public class ArchitectProject extends AbstractSPObject implements MappedSPTree {

     /**
      * Defines an absolute ordering of the child types of this class.
@@ -76,6 +80,47 @@
OLAPRootObject.class, PlayPenContentPane.class, ProfileManager.class, ProjectSettings.class, KettleSettings.class, User.class, Group.class, DomainCategory.class, UserDefinedSQLType.class)));

+    /**
+     * A hash map mapping all the descendants of this project.
+     * It must be kept up to date by listening to all its descendant nodes
+     * for child added and child removed events.
+     */
+    private final HashMap<String, SPObject> projectMap;
+
+    /**
+     * The listener used to keep the projectMap up to date.
+     */
+ private final SPListener projectMapListener = new AbstractSPListener() {
+        public void childAdded(SPChildEvent e) {
+            populateTreeMap(e.getChild());
+        }
+
+        public void childRemoved(SPChildEvent e) {
+            unpopulateTreeMap(e.getChild());
+        }
+
+        private void populateTreeMap(SPObject addedChild) {
+            if (projectMap.put(addedChild.getUUID(), addedChild) != null) {
+ throw new IllegalStateException("Object added under project with same UUID!");
+            }
+            addedChild.addSPListener(this);
+            for (SPObject o : addedChild.getChildren()) {
+                populateTreeMap(o);
+            }
+        }
+
+        private void unpopulateTreeMap(SPObject removedChild) {
+ if (projectMap.remove(removedChild.getUUID()) != removedChild) { + throw new IllegalStateException("Inconsistent project map: " + + "removed child's entry in map was either null, or different object.");
+            }
+            removedChild.removeSPListener(this);
+            for (SPObject o : removedChild.getChildren()) {
+                unpopulateTreeMap(o);
+            }
+        }
+    };
+
     /**
      * There is a 1:1 ratio between the session and the project.
      */
@@ -156,6 +201,9 @@
             setProfileManager(profileManager);
         }
         setName("Architect Project");
+        projectMap = new HashMap<String, SPObject>();
+        projectMap.put(uuid, this);
+        addSPListener(projectMapListener);
     }

     /**
@@ -510,4 +558,19 @@
     public List<UserDefinedSQLType> getSqlTypes() {
         return sqlTypes;
     }
-}
+
+    @NonBound
+    public SPObject getObjectInTree(String uuid) {
+        return projectMap.get(uuid);
+    }
+
+    /**
+     * Locates the SPObject which has the given UUID, under this project,
+     * returning null if the item is not found. Throws ClassCastException
+     * if in item is found, but it is not of the expected type.
+     */
+    @NonBound
+ public <T extends SPObject> T getObjectInTree(String uuid, Class<T> expectedType) {
+        return expectedType.cast(getObjectInTree(uuid));
+    }
+}
=======================================
--- /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONMessage.java Fri Apr 23 14:24:33 2010 +++ /trunk/src/main/java/ca/sqlpower/architect/enterprise/JSONMessage.java Tue Apr 27 10:08:58 2010
@@ -58,4 +58,8 @@
     public boolean isSuccessful() {
         return (statusCode >= 200 && statusCode < 300);
     }
-}
+
+    public String toString() {
+        return "Message (" + statusCode + "): " + message;
+    }
+}


--
Subscription settings: 
http://groups.google.com/group/architect-commits/subscribe?hl=en

Reply via email to