PageRenderTime 28ms CodeModel.GetById 24ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Tools/nib2cib/NSCustomResource.j

http://github.com/cacaodev/cappuccino
Unknown | 137 lines | 113 code | 24 blank | 0 comment | 0 complexity | 28f6c8e5575ccf072c82408f9e9d762f MD5 | raw file
  1/*
  2 * NSCustomResource.j
  3 * nib2cib
  4 *
  5 * Portions based on NSCustomResource.m (01/08/2009) in Cocotron (http://www.cocotron.org/)
  6 * Copyright (c) 2006-2007 Christopher J. W. Lloyd
  7 *
  8 * Created by Francisco Tolmasky.
  9 * Copyright 2008, 280 North, Inc.
 10 *
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation; either
 14 * version 2.1 of the License, or (at your option) any later version.
 15 *
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 19 * Lesser General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 24 */
 25
 26@import <AppKit/_CPCibCustomResource.j>
 27
 28@import "Nib2CibException.j"
 29
 30@global CP_NSMapClassName
 31
 32var FILE = require("file"),
 33    imageSize = require("cappuccino/imagesize").imagesize,
 34    supportedTemplateImages = {
 35        "NSAddTemplate": "CPAddTemplate",
 36        "NSRemoveTemplate": "CPRemoveTemplate",
 37        "NSToolbarShowColors": "CPImageNameColorPanel"
 38    };
 39
 40@implementation _CPCibCustomResource (NSCoding)
 41
 42- (id)NS_initWithCoder:(CPCoder)aCoder
 43{
 44    self = [super init];
 45
 46    if (self)
 47    {
 48        _className = CP_NSMapClassName([aCoder decodeObjectForKey:@"NSClassName"]);
 49        _resourceName = [aCoder decodeObjectForKey:@"NSResourceName"];
 50
 51        var size = CGSizeMakeZero(),
 52            framework = @"",
 53            bundleIdentifier = @"";
 54
 55        if (_resourceName == "NSSwitch" || _resourceName == "NSRadioButton")
 56            return nil;
 57        else if (/^NS[A-Z][A-Za-z]+$/.test(_resourceName))
 58        {
 59            if (supportedTemplateImages[_resourceName])
 60            {
 61                // Defer resolving this path until runtime.
 62                _resourceName = supportedTemplateImages[_resourceName];
 63            }
 64            else
 65                [CPException raise:Nib2CibException format:@"The built in image “%@” is not supported.", _resourceName];
 66        }
 67        else
 68        {
 69            var match = /^(.+)@(.+)$/.exec(_resourceName);
 70
 71            if (match)
 72            {
 73                _resourceName = match[1];
 74                framework = match[2];
 75            }
 76
 77            var resourceInfo = [aCoder resourceInfoForName:_resourceName inFramework:framework];
 78
 79            if (!resourceInfo)
 80                CPLog.warn("Resource \"" + _resourceName + "\" not found in the Resources directories");
 81            else
 82            {
 83                size = imageSize(FILE.canonical(resourceInfo.path)) || CGSizeMakeZero();
 84                framework = resourceInfo.framework;
 85            }
 86
 87            // Account for the fact that an extension may have been inferred.
 88            if (resourceInfo && resourceInfo.path)
 89            {
 90                // Include subdirectories in the name
 91                match = /^.+\/Resources\/(.+)$/.exec(resourceInfo.path)
 92                _resourceName = match[1];
 93            }
 94        }
 95
 96        if (resourceInfo && resourceInfo.path && resourceInfo.framework)
 97        {
 98            var frameworkPath = FILE.dirname(FILE.dirname(resourceInfo.path)),
 99                bundle = [CPBundle bundleWithPath:frameworkPath];
100
101            [bundle loadWithDelegate:nil];
102            bundleIdentifier = [bundle bundleIdentifier] || @"";
103        }
104
105        _properties = @{ @"size":size, @"bundleIdentifier":bundleIdentifier, @"framework":framework };
106
107        CPLog.debug("    Resource: %s\n   Framework: %s%s\n        Path: %s\n        Size: %d x %d",
108                    _resourceName,
109                    framework ? framework : "<none>",
110                    bundleIdentifier ? " (" + bundleIdentifier + ")" :
111                                        framework ? " (<no bundle identifier>)" : "",
112                    resourceInfo ? FILE.canonical(resourceInfo.path) : "",
113                    size.width,
114                    size.height);
115    }
116
117    return self;
118}
119
120@end
121
122
123@implementation NSCustomResource : _CPCibCustomResource
124{
125}
126
127- (id)initWithCoder:(CPCoder)aCoder
128{
129    return [self NS_initWithCoder:aCoder];
130}
131
132- (Class)classForKeyedArchiver
133{
134    return [_CPCibCustomResource class];
135}
136
137@end