PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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