Index: NSOutlineView.m
===================================================================
--- NSOutlineView.m	(revision 26251)
+++ NSOutlineView.m	(working copy)
@@ -108,6 +108,7 @@
 - (void) _initOutlineDefaults;
 - (void) _autosaveExpandedItems;
 - (void) _autoloadExpandedItems;
+- (BOOL) _isItemLoaded: (id)item;
 - (void) _collectItemsStartingWith: (id)startitem
                               into: (NSMutableArray *)allChildren;
 - (void) _loadDictionaryStartingWith: (id) startitem
@@ -1708,33 +1709,56 @@
     }
 }
 
+- (BOOL) _isItemLoaded: (id)item
+{
+  id sitem = (item == nil) ? (id)[NSNull null] : (id)item;
+  id object = NSMapGet(_itemDict, sitem);
+
+  //NSLog(@"_isItemLoaded %@ count %d", item, [object count]);
+
+  // TODO: We should store the loaded items in a map to ensure we only load 
+  // the children of item when it gets expanded for the first time. This would
+  // allow to write: return (NSMapGet(_loadedItemDict, sitem) != nil);
+  // The last line isn't truly correct because it implies an item without 
+  // children will get incorrectly reloaded automatically on each 
+  // expand/collapse.
+  return ([object count] != 0);
+}
+
 - (void) _loadDictionaryStartingWith: (id) startitem
                              atLevel: (int) level
 {
-  int num = [_dataSource outlineView: self
-                         numberOfChildrenOfItem: startitem];
-  int i = 0;
   id sitem = (startitem == nil) ? (id)[NSNull null] : (id)startitem;
-  NSMutableArray *anarray = nil;
+	  
+  NSMapInsert(_levelOfItems, sitem, [NSNumber numberWithInt: level]);
+	  
+  if ([self isItemExpanded: startitem])
+  {
+    int num = [_dataSource outlineView: self
+			 numberOfChildrenOfItem: startitem];
+    int i = 0;
+    NSMutableArray *anarray = nil;
 
-  if (num > 0)
-    {
-      anarray = [NSMutableArray array];
-      NSMapInsert(_itemDict, sitem, anarray);
-    }
+    if (num > 0)
+      {
+        anarray = [NSMutableArray array];
+        NSMapInsert(_itemDict, sitem, anarray);
+      }
 
-  NSMapInsert(_levelOfItems, sitem, [NSNumber numberWithInt: level]);
+    //NSLog(@"_loadDictionaryStartingWith: %@ atLevel: %d nbOfItems %d @"isExpanded %d", 
+    //  startitem, level, num, [self isItemExpanded: startitem]);
 
-  for (i = 0; i < num; i++)
-    {
-      id anitem = [_dataSource outlineView: self
-                               child: i
-                               ofItem: startitem];
+    for (i = 0; i < num; i++)
+      {
+        id anitem = [_dataSource outlineView: self
+                                 child: i
+                                 ofItem: startitem];
       
-      [anarray addObject: anitem];
-      [self _loadDictionaryStartingWith: anitem
-            atLevel: level + 1]; 
-    }
+        [anarray addObject: anitem];
+        [self _loadDictionaryStartingWith: anitem
+              atLevel: level + 1]; 
+      }
+   }
 }
 
 - (void)_closeItem: (id)item
@@ -1771,13 +1795,30 @@
 
   object = NSMapGet(_itemDict, sitem);
   numchildren = [object count];
-  
+
+  //NSLog(@"-- 1 _openItem: %@ nbOfItems %d isExpanded %d", item, numchildren, 
+  //  [self isItemExpanded: item]);
+
   // open the item...
   if (item != nil)
     {
       [_expandedItems addObject: item];
     }
 
+  // load the children of the item if needed
+  // If -autosaveExpandedItems returns YES, we should always reload the children 
+  // of item (even if the item has already been expanded/collapsed).
+  if ([self autosaveExpandedItems] == NO || [self _isItemLoaded: item] == NO)
+    {
+      [self _loadDictionaryStartingWith: item atLevel: [self levelForItem: item]];
+    }
+
+  object = NSMapGet(_itemDict, sitem);
+  numchildren = [object count];
+
+  //NSLog(@"-- 2 _openItem: %@ nbOfItems %d isExpanded %d", item, numchildren, 
+  //  [self isItemExpanded: item]);
+
   insertionPoint = [_items indexOfObject: item];
   if (insertionPoint == NSNotFound)
     {
