PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/AppKit/CPTreeNode.j

http://github.com/cacaodev/cappuccino
Unknown | 153 lines | 118 code | 35 blank | 0 comment | 0 complexity | 9bfed717f89b292ab546890fba4ae2e6 MD5 | raw file
  1/*
  2 * CPTreeNode.j
  3 * AppKit
  4 *
  5 * Created by Francisco Tolmasky.
  6 * Copyright 2009, 280 North, Inc.
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2.1 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 16 * Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 21 */
 22
 23@import <Foundation/CPObject.j>
 24@import <Foundation/CPIndexPath.j>
 25
 26
 27@implementation CPTreeNode : CPObject
 28{
 29    id              _representedObject @accessors(readonly, property=representedObject);
 30
 31    CPTreeNode      _parentNode @accessors(readonly, property=parentNode);
 32    CPMutableArray  _childNodes;
 33}
 34
 35+ (id)treeNodeWithRepresentedObject:(id)anObject
 36{
 37    return [[self alloc] initWithRepresentedObject:anObject];
 38}
 39
 40- (id)initWithRepresentedObject:(id)anObject
 41{
 42    self = [super init];
 43
 44    if (self)
 45    {
 46        _representedObject = anObject;
 47        _childNodes = [];
 48    }
 49
 50    return self;
 51}
 52
 53- (BOOL)isLeaf
 54{
 55    return [_childNodes count] <= 0;
 56}
 57
 58- (CPArray)childNodes
 59{
 60    return [_childNodes copy];
 61}
 62
 63- (CPMutableArray)mutableChildNodes
 64{
 65    return [self mutableArrayValueForKey:@"childNodes"];
 66}
 67
 68- (void)insertObject:(id)aTreeNode inChildNodesAtIndex:(CPInteger)anIndex
 69{
 70    [[aTreeNode._parentNode mutableChildNodes] removeObjectIdenticalTo:aTreeNode];
 71
 72    aTreeNode._parentNode = self;
 73
 74    [_childNodes insertObject:aTreeNode atIndex:anIndex];
 75}
 76
 77- (void)removeObjectFromChildNodesAtIndex:(CPInteger)anIndex
 78{
 79    [_childNodes objectAtIndex:anIndex]._parentNode = nil;
 80
 81    [_childNodes removeObjectAtIndex:anIndex];
 82}
 83
 84- (void)replaceObjectFromChildNodesAtIndex:(CPInteger)anIndex withObject:(id)aTreeNode
 85{
 86    var oldTreeNode = [_childNodes objectAtIndex:anIndex];
 87
 88    oldTreeNode._parentNode = nil;
 89    aTreeNode._parentNode = self;
 90
 91    [_childNodes replaceObjectAtIndex:anIndex withObject:aTreeNode];
 92}
 93
 94- (id)objectInChildNodesAtIndex:(CPInteger)anIndex
 95{
 96    return _childNodes[anIndex];
 97}
 98
 99- (void)sortWithSortDescriptors:(CPArray)sortDescriptors recursively:(BOOL)shouldSortRecursively
100{
101    [_childNodes sortUsingDescriptors:sortDescriptors];
102
103    if (!shouldSortRecursively)
104        return;
105
106    var count = [_childNodes count];
107
108    while (count--)
109        [_childNodes[count] sortWithSortDescriptors:sortDescriptors recursively:YES];
110}
111
112- (CPTreeNode)descendantNodeAtIndexPath:(CPIndexPath)indexPath
113{
114    var index = 0,
115        count = [indexPath length],
116        node = self;
117
118    for (; index < count; ++index)
119        node = [node objectInChildNodesAtIndex:[indexPath indexAtPosition:index]];
120
121    return node;
122}
123
124@end
125
126var CPTreeNodeRepresentedObjectKey  = @"CPTreeNodeRepresentedObjectKey",
127    CPTreeNodeParentNodeKey         = @"CPTreeNodeParentNodeKey",
128    CPTreeNodeChildNodesKey         = @"CPTreeNodeChildNodesKey";
129
130@implementation CPTreeNode (CPCoding)
131
132- (id)initWithCoder:(CPCoder)aCoder
133{
134    self = [super init];
135
136    if (self)
137    {
138        _representedObject = [aCoder decodeObjectForKey:CPTreeNodeRepresentedObjectKey];
139        _parentNode = [aCoder decodeObjectForKey:CPTreeNodeParentNodeKey];
140        _childNodes = [aCoder decodeObjectForKey:CPTreeNodeChildNodesKey];
141    }
142
143    return self;
144}
145
146- (void)encodeWithCoder:(CPCoder)aCoder
147{
148    [aCoder encodeObject:_representedObject forKey:CPTreeNodeRepresentedObjectKey];
149    [aCoder encodeConditionalObject:_parentNode forKey:CPTreeNodeParentNodeKey];
150    [aCoder encodeObject:_childNodes forKey:CPTreeNodeChildNodesKey];
151}
152
153@end