API  1.0.0
CPTreeNode.j
Go to the documentation of this 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 
24 
25 @implementation CPTreeNode : CPObject
26 {
27  id _representedObject;
28 
29  CPTreeNode _parentNode;
30  CPMutableArray _childNodes;
31 }
32 
33 + (id)treeNodeWithRepresentedObject:(id)anObject
34 {
35  return [[self alloc] initWithRepresentedObject:anObject];
36 }
37 
38 - (id)initWithRepresentedObject:(id)anObject
39 {
40  self = [super init];
41 
42  if (self)
43  {
44  _representedObject = anObject;
45  _childNodes = [];
46  }
47 
48  return self;
49 }
50 
51 - (BOOL)isLeaf
52 {
53  return [_childNodes count] <= 0;
54 }
55 
56 - (CPArray)childNodes
57 {
58  return [_childNodes copy];
59 }
60 
61 - (CPMutableArray)mutableChildNodes
62 {
63  return [self mutableArrayValueForKey:@"childNodes"];
64 }
65 
66 - (void)insertObject:(id)aTreeNode inChildNodesAtIndex:(CPInteger)anIndex
67 {
68  [[aTreeNode._parentNode mutableChildNodes] removeObjectIdenticalTo:aTreeNode];
69 
70  aTreeNode._parentNode = self;
71 
72  [_childNodes insertObject:aTreeNode atIndex:anIndex];
73 }
74 
75 - (void)removeObjectFromChildNodesAtIndex:(CPInteger)anIndex
76 {
77  [_childNodes objectAtIndex:anIndex]._parentNode = nil;
78 
79  [_childNodes removeObjectAtIndex:anIndex];
80 }
81 
82 - (void)replaceObjectFromChildNodesAtIndex:(CPInteger)anIndex withObject:(id)aTreeNode
83 {
84  var oldTreeNode = [_childNodes objectAtIndex:anIndex];
85 
86  oldTreeNode._parentNode = nil;
87  aTreeNode._parentNode = self;
88 
89  [_childNodes replaceObjectAtIndex:anIndex withObject:aTreeNode];
90 }
91 
92 - (id)objectInChildNodesAtIndex:(CPInteger)anIndex
93 {
94  return _childNodes[anIndex];
95 }
96 
97 - (void)sortWithSortDescriptors:(CPArray)sortDescriptors recursively:(BOOL)shouldSortRecursively
98 {
99  [_childNodes sortUsingDescriptors:sortDescriptors];
100 
101  if (!shouldSortRecursively)
102  return;
103 
104  var count = [_childNodes count];
105 
106  while (count--)
107  [_childNodes[count] sortWithSortDescriptors:sortDescriptors recursively:YES];
108 }
109 
110 - (CPTreeNode)descendantNodeAtIndexPath:(CPIndexPath)indexPath
111 {
112  var index = 0,
113  count = [indexPath length],
114  node = self;
115 
116  for (; index < count; ++index)
117  node = [node objectInChildNodesAtIndex:[indexPath indexAtPosition:index]];
118 
119  return node;
120 }
121 
122 @end
123 
124 var CPTreeNodeRepresentedObjectKey = @"CPTreeNodeRepresentedObjectKey",
125  CPTreeNodeParentNodeKey = @"CPTreeNodeParentNodeKey",
126  CPTreeNodeChildNodesKey = @"CPTreeNodeChildNodesKey";
127 
128 @implementation CPTreeNode (CPCoding)
129 
130 - (id)initWithCoder:(CPCoder)aCoder
131 {
132  self = [super init];
133 
134  if (self)
135  {
136  _representedObject = [aCoder decodeObjectForKey:CPTreeNodeRepresentedObjectKey];
137  _parentNode = [aCoder decodeObjectForKey:CPTreeNodeParentNodeKey];
138  _childNodes = [aCoder decodeObjectForKey:CPTreeNodeChildNodesKey];
139  }
140 
141  return self;
142 }
143 
144 - (void)encodeWithCoder:(CPCoder)aCoder
145 {
146  [aCoder encodeObject:_representedObject forKey:CPTreeNodeRepresentedObjectKey];
147  [aCoder encodeConditionalObject:_parentNode forKey:CPTreeNodeParentNodeKey];
148  [aCoder encodeObject:_childNodes forKey:CPTreeNodeChildNodesKey];
149 }
150 
151 @end
152 
154 
158 - (id)representedObject
159 {
160  return _representedObject;
161 }
162 
166 - (CPTreeNode)parentNode
167 {
168  return _parentNode;
169 }
170 
171 @end
id initWithRepresentedObject:(id anObject)
Definition: CPTreeNode.j:38
var CPTreeNodeParentNodeKey
Definition: CPTreeNode.j:125
id mutableArrayValueForKey:(id aKey)
Definition: CPArray+KVO.j:27
var CPTreeNodeRepresentedObjectKey
Definition: CPTreeNode.j:124
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
id init()
Definition: CPObject.j:145
var CPTreeNodeChildNodesKey
Definition: CPTreeNode.j:126
id alloc()
Definition: CPObject.j:130