/Tools/nib2cib/NSIBObjectData.j

http://github.com/cacaodev/cappuccino · Unknown · 163 lines · 124 code · 39 blank · 0 comment · 0 complexity · 68aad54a3252de81f3b3a2553daf3bfa MD5 · raw file

  1. /*
  2. * NSIBObjectData.j
  3. * nib2cib
  4. *
  5. * Created by Francisco Tolmasky.
  6. * Copyright 2008, 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 <AppKit/_CPCibObjectData.j>
  23. @import "NSCell.j"
  24. @implementation _CPCibObjectData (NSCoding)
  25. - (id)NS_initWithCoder:(CPCoder)aCoder
  26. {
  27. self = [self init];
  28. if (self)
  29. {
  30. /* id owner;
  31. if((owner=[nameTable objectForKey:NSNibOwner])!=nil)
  32. [nameTable setObject:owner forKey:@"File's Owner"];
  33. [nameTable setObject:[NSFontManager sharedFontManager] forKey:@"Font Manager"];
  34. */
  35. _namesKeys = [aCoder decodeObjectForKey:@"NSNamesKeys"];
  36. _namesValues = [aCoder decodeObjectForKey:@"NSNamesValues"];
  37. //_accessibilityConnectors = [aCoder decodeObjectForKey:@"NSAccessibilityConnectors"];
  38. //_accessibilityOidsKeys = [aCoder decodeObjectForKey:@"NSAccessibilityOidsKeys"];
  39. //_accessibilityOidsValues = [aCoder decodeObjectForKey:@"NSAccessibilityOidsValues"];
  40. _classesKeys = [aCoder decodeObjectForKey:@"NSClassesKeys"];
  41. _classesValues = [aCoder decodeObjectForKey:@"NSClassesValues"];
  42. _connections = [aCoder decodeObjectForKey:@"NSConnections"];
  43. //_fontManager = [aCoder decodeObjectForKey:@"NSFontManager"] retain];
  44. _framework = [aCoder decodeObjectForKey:@"NSFramework"];
  45. //_nextOid = [aCoder decodeIntForKey:@"NSNextOid"];
  46. _objectsKeys = [aCoder decodeObjectForKey:@"NSObjectsKeys"];
  47. _objectsValues = [aCoder decodeObjectForKey:@"NSObjectsValues"];
  48. [self removeCellsFromObjectGraph];
  49. //_oidKeys = [aCoder decodeObjectForKey:@"NSOidsKeys"];
  50. //_oidValues = [aCoder decodeObjectForKey:@"NSOidsValues"];
  51. _fileOwner = [aCoder decodeObjectForKey:@"NSRoot"];
  52. _visibleWindows = [aCoder decodeObjectForKey:@"NSVisibleWindows"];
  53. }
  54. return self;
  55. }
  56. - (void)removeCellsFromObjectGraph
  57. {
  58. // FIXME: Remove from top level objects and connections?
  59. // Most cell references should be naturally removed by the fact that we don't manually
  60. // encode them anywhere, however, they remain in our object graph. For each cell found,
  61. // take its children and promote them to our parent object's children.
  62. var count = _objectsKeys.length,
  63. parentForCellUIDs = { },
  64. promotedChildrenForCellUIDs = { };
  65. while (count--)
  66. {
  67. var child = _objectsKeys[count];
  68. if (!child)
  69. continue;
  70. var parent = _objectsValues[count];
  71. // If this object is a cell, remember it's parent.
  72. if ([child isKindOfClass:[NSCell class]])
  73. {
  74. parentForCellUIDs[[child UID]] = parent;
  75. continue;
  76. }
  77. // If parent also isn't a cell, we don't care about it.
  78. if (![parent isKindOfClass:[NSCell class]])
  79. continue;
  80. // Remember this child for later promotion.
  81. var parentUID = [parent UID],
  82. children = promotedChildrenForCellUIDs[parentUID];
  83. if (!children)
  84. {
  85. children = [];
  86. promotedChildrenForCellUIDs[parentUID] = children;
  87. }
  88. children.push(child);
  89. _objectsKeys.splice(count, 1);
  90. _objectsValues.splice(count, 1);
  91. }
  92. for (var cellUID in promotedChildrenForCellUIDs)
  93. if (promotedChildrenForCellUIDs.hasOwnProperty(cellUID))
  94. {
  95. var children = promotedChildrenForCellUIDs[cellUID],
  96. parent = parentForCellUIDs[cellUID];
  97. children.forEach(function(aChild)
  98. {
  99. CPLog.debug("NSIBObjectData: promoted " + aChild + " to child of " + parent);
  100. _objectsKeys.push(aChild);
  101. _objectsValues.push(parent);
  102. });
  103. }
  104. var count = _objectsKeys.length;
  105. while (count--)
  106. {
  107. var object = _objectsKeys[count];
  108. if ([object respondsToSelector:@selector(swapCellsForParents:)])
  109. [object swapCellsForParents:parentForCellUIDs];
  110. }
  111. }
  112. @end
  113. @implementation NSIBObjectData : _CPCibObjectData
  114. {
  115. }
  116. - (id)initWithCoder:(CPCoder)aCoder
  117. {
  118. return [self NS_initWithCoder:aCoder];
  119. }
  120. - (Class)classForKeyedArchiver
  121. {
  122. return [_CPCibObjectData class];
  123. }
  124. @end