Index: /code/branches/core6/src/libraries/core/object/Listable.cc
===================================================================
--- /code/branches/core6/src/libraries/core/object/Listable.cc	(revision 9626)
+++ /code/branches/core6/src/libraries/core/object/Listable.cc	(revision 9627)
@@ -80,7 +80,14 @@
     void Listable::setContext(Context* context)
     {
+        std::vector<ObjectListBaseElement*> copy = this->elements_;
+        this->elements_.clear();
+
+        for (size_t i = 0; i < copy.size(); ++i)
+        {
+            copy[i]->changeContext(this->context_, context);
+            delete copy[i];
+        }
+
         this->context_ = context;
-        for (size_t i = 0; i < this->elements_.size(); ++i)
-            this->elements_[i]->changeContext(context);
     }
 
Index: /code/branches/core6/src/libraries/core/object/ObjectListBase.h
===================================================================
--- /code/branches/core6/src/libraries/core/object/ObjectListBase.h	(revision 9626)
+++ /code/branches/core6/src/libraries/core/object/ObjectListBase.h	(revision 9627)
@@ -60,5 +60,5 @@
             virtual ~ObjectListBaseElement() { this->removeFromList(); }
 
-            virtual void changeContext(Context* context) = 0;
+            virtual void changeContext(Context* oldContext, Context* newContext) = 0;
 
             ObjectListBaseElement* next_;       //!< The next element in the list
@@ -82,8 +82,13 @@
             ObjectListElement(T* object) : ObjectListBaseElement(static_cast<Listable*>(object)), object_(object) {}
 
-            virtual void changeContext(Context* context)
+            virtual void changeContext(Context* oldContext, Context* newContext)
             {
+                // add object to new context, but only if this element belongs exactly to the old context (and not to a sub-context to avoid re-adding objects
+                // multiple times if they are in multiple contexts)
+                if (oldContext->getObjectList<T>() == this->list_)
+                    newContext->addObject(this->object_);
+
+                // remove from old list
                 this->removeFromList();
-                context->getObjectList<T>()->addElement(this);
             }
 
Index: /code/branches/core6/test/core/object/ListableTest.cc
===================================================================
--- /code/branches/core6/test/core/object/ListableTest.cc	(revision 9626)
+++ /code/branches/core6/test/core/object/ListableTest.cc	(revision 9627)
@@ -79,15 +79,51 @@
     }
 
+    TEST(ListableTest, CanAddObjectToContext)
+    {
+        Context context(NULL);
+        ListableSubclassTest test;
+
+        test.setContext(&context);
+
+        // object is in new context
+        EXPECT_EQ(1u, context.getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, &context));
+        EXPECT_EQ(1u, context.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &context));
+    }
+
+    TEST(ListableTest, CanAddObjectToSubContext)
+    {
+        Context baseContext(NULL);
+        Context subContext(&baseContext);
+        ListableSubclassTest test;
+
+        test.setContext(&subContext);
+
+        // object is in both contexts
+        EXPECT_EQ(1u, baseContext.getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, &baseContext));
+        EXPECT_EQ(1u, baseContext.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &baseContext));
+
+        EXPECT_EQ(1u, subContext.getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, &subContext));
+        EXPECT_EQ(1u, subContext.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &subContext));
+    }
+
     TEST(ListableTest, CanChangeContext)
     {
         Context* rootContext = Context::getRootContext();
-        Context newContext;
-        ListableSubclassTest test;
-
-        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
-        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
-        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
-        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
-
+        Context newContext(NULL);
+        ListableSubclassTest test;
+
+        // object is in root context
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
+
+        // object is NOT in new context
         EXPECT_EQ(0u, newContext.getObjectList<ListableTest>()->size());
         EXPECT_FALSE(objectListContains<ListableTest>(&test, &newContext));
@@ -97,4 +133,5 @@
         test.setContext(&newContext);
 
+        // object was removed from root context
         EXPECT_EQ(0u, rootContext->getObjectList<ListableTest>()->size());
         EXPECT_FALSE(objectListContains<ListableTest>(&test, rootContext));
@@ -102,4 +139,5 @@
         EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, rootContext));
 
+        // object is now in new context
         EXPECT_EQ(1u, newContext.getObjectList<ListableTest>()->size());
         EXPECT_TRUE(objectListContains<ListableTest>(&test, &newContext));
@@ -107,3 +145,84 @@
         EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &newContext));
     }
+
+    TEST(ListableTest, CanChangeToSubContext)
+    {
+        Context* rootContext = Context::getRootContext();
+        Context subContext(rootContext);
+        ListableSubclassTest test;
+
+        // object is in root context
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
+
+        // object is NOT in sub context
+        EXPECT_EQ(0u, subContext.getObjectList<ListableTest>()->size());
+        EXPECT_FALSE(objectListContains<ListableTest>(&test, &subContext));
+        EXPECT_EQ(0u, subContext.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, &subContext));
+
+        test.setContext(&subContext);
+
+        // object is still in root context
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
+
+        // object is now also in sub context
+        EXPECT_EQ(1u, subContext.getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, &subContext));
+        EXPECT_EQ(1u, subContext.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &subContext));
+    }
+
+    TEST(ListableTest, CanChangeFromSubcontextToSubContext)
+    {
+        Context* rootContext = Context::getRootContext();
+        Context subContext1(rootContext);
+        Context subContext2(rootContext);
+        ListableSubclassTest test;
+
+        test.setContext(&subContext1);
+
+        // object is in root context
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
+
+        // object is also in sub context 1
+        EXPECT_EQ(1u, subContext1.getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, &subContext1));
+        EXPECT_EQ(1u, subContext1.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &subContext1));
+
+        // object is NOT in sub context 2
+        EXPECT_EQ(0u, subContext2.getObjectList<ListableTest>()->size());
+        EXPECT_FALSE(objectListContains<ListableTest>(&test, &subContext2));
+        EXPECT_EQ(0u, subContext2.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, &subContext2));
+
+        test.setContext(&subContext2);
+
+        // object is still in root context
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, rootContext));
+        EXPECT_EQ(1u, rootContext->getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, rootContext));
+
+        // object was removed from sub context 1
+        EXPECT_EQ(0u, subContext1.getObjectList<ListableTest>()->size());
+        EXPECT_FALSE(objectListContains<ListableTest>(&test, &subContext1));
+        EXPECT_EQ(0u, subContext1.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_FALSE(objectListContains<ListableSubclassTest>(&test, &subContext1));
+
+        // object is now in sub context 2
+        EXPECT_EQ(1u, subContext2.getObjectList<ListableTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableTest>(&test, &subContext2));
+        EXPECT_EQ(1u, subContext2.getObjectList<ListableSubclassTest>()->size());
+        EXPECT_TRUE(objectListContains<ListableSubclassTest>(&test, &subContext2));
+    }
 }
