/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. @import <Foundation/CPObject.j>
  23. @import <Foundation/CPIndexPath.j>
  24. @implementation CPTreeNode : CPObject
  25. {
  26. id _representedObject @accessors(readonly, property=representedObject);
  27. CPTreeNode _parentNode @accessors(readonly, property=parentNode);
  28. CPMutableArray _childNodes;
  29. }
  30. + (id)treeNodeWithRepresentedObject:(id)anObject
  31. {
  32. return [[self alloc] initWithRepresentedObject:anObject];
  33. }
  34. - (id)initWithRepresentedObject:(id)anObject
  35. {
  36. self = [super init];
  37. if (self)
  38. {
  39. _representedObject = anObject;
  40. _childNodes = [];
  41. }
  42. return self;
  43. }
  44. - (BOOL)isLeaf
  45. {
  46. return [_childNodes count] <= 0;
  47. }
  48. - (CPArray)childNodes
  49. {
  50. return [_childNodes copy];
  51. }
  52. - (CPMutableArray)mutableChildNodes
  53. {
  54. return [self mutableArrayValueForKey:@"childNodes"];
  55. }
  56. - (void)insertObject:(id)aTreeNode inChildNodesAtIndex:(CPInteger)anIndex
  57. {
  58. [[aTreeNode._parentNode mutableChildNodes] removeObjectIdenticalTo:aTreeNode];
  59. aTreeNode._parentNode = self;
  60. [_childNodes insertObject:aTreeNode atIndex:anIndex];
  61. }
  62. - (void)removeObjectFromChildNodesAtIndex:(CPInteger)anIndex
  63. {
  64. [_childNodes objectAtIndex:anIndex]._parentNode = nil;
  65. [_childNodes removeObjectAtIndex:anIndex];
  66. }
  67. - (void)replaceObjectFromChildNodesAtIndex:(CPInteger)anIndex withObject:(id)aTreeNode
  68. {
  69. var oldTreeNode = [_childNodes objectAtIndex:anIndex];
  70. oldTreeNode._parentNode = nil;
  71. aTreeNode._parentNode = self;
  72. [_childNodes replaceObjectAtIndex:anIndex withObject:aTreeNode];
  73. }
  74. - (id)objectInChildNodesAtIndex:(CPInteger)anIndex
  75. {
  76. return _childNodes[anIndex];
  77. }
  78. - (void)sortWithSortDescriptors:(CPArray)sortDescriptors recursively:(BOOL)shouldSortRecursively
  79. {
  80. [_childNodes sortUsingDescriptors:sortDescriptors];
  81. if (!shouldSortRecursively)
  82. return;
  83. var count = [_childNodes count];
  84. while (count--)
  85. [_childNodes[count] sortWithSortDescriptors:sortDescriptors recursively:YES];
  86. }
  87. - (CPTreeNode)descendantNodeAtIndexPath:(CPIndexPath)indexPath
  88. {
  89. var index = 0,
  90. count = [indexPath length],
  91. node = self;
  92. for (; index < count; ++index)
  93. node = [node objectInChildNodesAtIndex:[indexPath indexAtPosition:index]];
  94. return node;
  95. }
  96. @end
  97. var CPTreeNodeRepresentedObjectKey = @"CPTreeNodeRepresentedObjectKey",
  98. CPTreeNodeParentNodeKey = @"CPTreeNodeParentNodeKey",
  99. CPTreeNodeChildNodesKey = @"CPTreeNodeChildNodesKey";
  100. @implementation CPTreeNode (CPCoding)
  101. - (id)initWithCoder:(CPCoder)aCoder
  102. {
  103. self = [super init];
  104. if (self)
  105. {
  106. _representedObject = [aCoder decodeObjectForKey:CPTreeNodeRepresentedObjectKey];
  107. _parentNode = [aCoder decodeObjectForKey:CPTreeNodeParentNodeKey];
  108. _childNodes = [aCoder decodeObjectForKey:CPTreeNodeChildNodesKey];
  109. }
  110. return self;
  111. }
  112. - (void)encodeWithCoder:(CPCoder)aCoder
  113. {
  114. [aCoder encodeObject:_representedObject forKey:CPTreeNodeRepresentedObjectKey];
  115. [aCoder encodeConditionalObject:_parentNode forKey:CPTreeNodeParentNodeKey];
  116. [aCoder encodeObject:_childNodes forKey:CPTreeNodeChildNodesKey];
  117. }
  118. @end