API 0.9.5
AppKit/CPTreeNode.j
Go to the documentation of this file.
00001 
00002 
00003 
00004 @implementation CPTreeNode : CPObject
00005 {
00006     id              _representedObject;
00007 
00008     CPTreeNode      _parentNode;
00009     CPMutableArray  _childNodes;
00010 }
00011 
00012 + (id)treeNodeWithRepresentedObject:(id)anObject
00013 {
00014     return [[self alloc] initWithRepresentedObject:anObject];
00015 }
00016 
00017 - (id)initWithRepresentedObject:(id)anObject
00018 {
00019     self = [super init];
00020 
00021     if (self)
00022     {
00023         _representedObject = anObject;
00024         _childNodes = [];
00025     }
00026 
00027     return self;
00028 }
00029 
00030 - (BOOL)isLeaf
00031 {
00032     return [_childNodes count] <= 0;
00033 }
00034 
00035 - (CPArray)childNodes
00036 {
00037     return [_childNodes copy];
00038 }
00039 
00040 - (CPMutableArray)mutableChildNodes
00041 {
00042     return [self mutableArrayValueForKey:@"childNodes"];
00043 }
00044 
00045 - (void)insertObject:(id)aTreeNode inChildNodesAtIndex:(CPInteger)anIndex
00046 {
00047     [[aTreeNode._parentNode mutableChildNodes] removeObjectIdenticalTo:aTreeNode];
00048 
00049     aTreeNode._parentNode = self;
00050 
00051     [_childNodes insertObject:aTreeNode atIndex:anIndex];
00052 }
00053 
00054 - (void)removeObjectFromChildNodesAtIndex:(CPInteger)anIndex
00055 {
00056     [_childNodes objectAtIndex:anIndex]._parentNode = nil;
00057 
00058     [_childNodes removeObjectAtIndex:anIndex];
00059 }
00060 
00061 - (void)replaceObjectFromChildNodesAtIndex:(CPInteger)anIndex withObject:(id)aTreeNode
00062 {
00063     var oldTreeNode = [_childNodes objectAtIndex:anIndex];
00064 
00065     oldTreeNode._parentNode = nil;
00066     aTreeNode._parentNode = self;
00067 
00068     [_childNodes replaceObjectAtIndex:anIndex withObject:aTreeNode];
00069 }
00070 
00071 - (id)objectInChildNodesAtIndex:(CPInteger)anIndex
00072 {
00073     return _childNodes[anIndex];
00074 }
00075 
00076 - (void)sortWithSortDescriptors:(CPArray)sortDescriptors recursively:(BOOL)shouldSortRecursively
00077 {
00078     [_childNodes sortUsingDescriptors:sortDescriptors];
00079 
00080     if (!shouldSortRecursively)
00081         return;
00082 
00083     var count = [_childNodes count];
00084 
00085     while (count--)
00086         [_childNodes[count] sortWithSortDescriptors:sortDescriptors recursively:YES];
00087 }
00088 
00089 - (CPTreeNode)descendantNodeAtIndexPath:(CPIndexPath)indexPath
00090 {
00091     var index = 0,
00092         count = [indexPath length],
00093         node = self;
00094 
00095     for (; index < count; ++index)
00096         node = [node objectInChildNodesAtIndex:[indexPath indexAtPosition:index]];
00097 
00098     return node;
00099 }
00100 
00101 @end
00102 
00103 var CPTreeNodeRepresentedObjectKey  = @"CPTreeNodeRepresentedObjectKey",
00104     CPTreeNodeParentNodeKey         = @"CPTreeNodeParentNodeKey",
00105     CPTreeNodeChildNodesKey         = @"CPTreeNodeChildNodesKey";
00106 
00107 @implementation CPTreeNode (CPCoding)
00108 
00109 - (id)initWithCoder:(CPCoder)aCoder
00110 {
00111     self = [super init];
00112 
00113     if (self)
00114     {
00115         _representedObject = [aCoder decodeObjectForKey:CPTreeNodeRepresentedObjectKey];
00116         _parentNode = [aCoder decodeObjectForKey:CPTreeNodeParentNodeKey];
00117         _childNodes = [aCoder decodeObjectForKey:CPTreeNodeChildNodesKey];
00118     }
00119 
00120     return self;
00121 }
00122 
00123 - (void)encodeWithCoder:(CPCoder)aCoder
00124 {
00125     [aCoder encodeObject:_representedObject forKey:CPTreeNodeRepresentedObjectKey];
00126     [aCoder encodeConditionalObject:_parentNode forKey:CPTreeNodeParentNodeKey];
00127     [aCoder encodeObject:_childNodes forKey:CPTreeNodeChildNodesKey];
00128 }
00129 
00130 @end
00131 
00132 @implementation CPTreeNode (CPSynthesizedAccessors)
00133 
00137 - (id)representedObject
00138 {
00139     return _representedObject;
00140 }
00141 
00145 - (CPTreeNode)parentNode
00146 {
00147     return _parentNode;
00148 }
00149 
00150 @end
 All Classes Files Functions Variables Defines