PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/AppKit/Cib/_CPCibCustomResource.j

http://github.com/cacaodev/cappuccino
Unknown | 224 lines | 181 code | 43 blank | 0 comment | 0 complexity | 941a2c25b939323d9363d3999a931829 MD5 | raw file
  1/*
  2 * _CPCibCustomResource.j
  3 * AppKit
  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 2009, 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 <Foundation/CPObject.j>
 27@import <Foundation/CPString.j>
 28
 29@import "CPCompatibility.j"
 30@import "CPImage.j"
 31@import "CPTheme.j"
 32
 33@class CPButtonBar
 34@class CPView
 35
 36var _CPCibCustomResourceClassNameKey    = @"_CPCibCustomResourceClassNameKey",
 37    _CPCibCustomResourceResourceNameKey = @"_CPCibCustomResourceResourceNameKey",
 38    _CPCibCustomResourcePropertiesKey   = @"_CPCibCustomResourcePropertiesKey",
 39
 40    _CPCibCustomResourceTemplateImageMap = nil;
 41
 42
 43@implementation _CPCibCustomResource : CPObject
 44{
 45    CPString        _className;
 46    CPString        _resourceName;
 47    CPDictionary    _properties;
 48    CPBundle        _bundle;
 49}
 50
 51+ (void)initialize
 52{
 53    if (self !== [_CPCibCustomResource class])
 54        return;
 55
 56    _CPCibCustomResourceTemplateImageMap = @{
 57        "CPAddTemplate": "button-image-plus",
 58        "CPRemoveTemplate": "button-image-minus"
 59    };
 60}
 61+ (id)imageResourceWithName:(CPString)aResourceName size:(CGSize)aSize
 62{
 63    return [[self alloc] initWithClassName:@"CPImage" resourceName:aResourceName properties:@{ @"size": aSize }];
 64}
 65
 66+ (id)imageResourceWithName:(CPString)aResourceName size:(CGSize)aSize bundleClass:(CPString)aBundleClass
 67{
 68    return [[self alloc] initWithClassName:@"CPImage" resourceName:aResourceName properties:@{ @"size": aSize, @"bundleClass": aBundleClass }];
 69}
 70
 71- (id)initWithClassName:(CPString)aClassName resourceName:(CPString)aResourceName properties:(CPDictionary)properties
 72{
 73    self = [super init];
 74
 75    if (self)
 76    {
 77        _className = aClassName;
 78        _resourceName = aResourceName;
 79        _properties = properties;
 80        _bundle = nil;
 81    }
 82
 83    return self;
 84}
 85
 86- (id)initWithCoder:(CPCoder)aCoder
 87{
 88    self = [super init];
 89
 90    if (self)
 91    {
 92        _className = [aCoder decodeObjectForKey:_CPCibCustomResourceClassNameKey];
 93        _resourceName = [aCoder decodeObjectForKey:_CPCibCustomResourceResourceNameKey];
 94        _properties = [aCoder decodeObjectForKey:_CPCibCustomResourcePropertiesKey];
 95        _bundle = nil;
 96    }
 97
 98    return self;
 99}
100
101- (void)encodeWithCoder:(CPCoder)aCoder
102{
103    [aCoder encodeObject:_className forKey:_CPCibCustomResourceClassNameKey];
104    [aCoder encodeObject:_resourceName forKey:_CPCibCustomResourceResourceNameKey];
105    [aCoder encodeObject:_properties forKey:_CPCibCustomResourcePropertiesKey];
106}
107
108- (id)awakeAfterUsingCoder:(CPCoder)aCoder
109{
110    if ([aCoder respondsToSelector:@selector(bundle)] &&
111        (![aCoder respondsToSelector:@selector(awakenCustomResources)] || [aCoder awakenCustomResources]))
112        if (_className === @"CPImage")
113        {
114            var templateImage = [_CPCibCustomResourceTemplateImageMap objectForKey:_resourceName];
115
116            if (templateImage)
117                return [[CPTheme defaultTheme] valueForAttributeWithName:templateImage forClass:[CPButtonBar class]];
118            else
119                return [self imageFromCoder:aCoder];
120        }
121
122    return self;
123}
124
125@end
126
127@implementation _CPCibCustomResource (CPImage)
128
129- (CPBundle)imageBundleWithCoder:(CPCoder)aCoder
130{
131    if (_bundle)
132        return _bundle;
133
134    var bundleIdentifier = [_properties valueForKey:@"bundleIdentifier"];
135
136    if (bundleIdentifier)
137        _bundle = [CPBundle bundleWithIdentifier:bundleIdentifier];
138    else
139    {
140        var bundleClass = [_properties valueForKey:@"bundleClass"];
141
142        if (bundleClass)
143        {
144            bundleClass = CPClassFromString(bundleClass);
145
146            if (bundleClass)
147                _bundle = [CPBundle bundleForClass:bundleClass];
148        }
149    }
150
151    if (!_bundle)
152    {
153        var framework = [_properties valueForKey:@"framework"];
154
155        if (framework)
156        {
157            // Get AppKit and hope the framework is in the same directory
158            var appKit = [CPBundle bundleForClass:[CPView class]],
159                url = [[appKit bundleURL] URLByDeletingLastPathComponent];
160
161            url = [CPURL URLWithString:framework relativeToURL:url];
162            _bundle = [CPBundle bundleWithURL:url];
163        }
164    }
165
166    if (!_bundle)
167    {
168        if (aCoder)
169            _bundle = [aCoder bundle];
170        else
171            _bundle = [CPBundle mainBundle];
172    }
173
174    return _bundle;
175}
176
177- (CPImage)imageFromCoder:(CPCoder)aCoder
178{
179    return [[CPImage alloc] initWithContentsOfFile:[[self imageBundleWithCoder:aCoder] pathForResource:_resourceName] size:[_properties valueForKey:@"size"]];
180}
181
182- (CPString)filename
183{
184    return [[self imageBundleWithCoder:nil] pathForResource:_resourceName];
185}
186
187- (CGSize)size
188{
189    return [_properties objectForKey:@"size"];
190}
191
192- (BOOL)isSingleImage
193{
194    return YES;
195}
196
197- (BOOL)isThreePartImage
198{
199    return NO;
200}
201
202- (BOOL)isNinePartImage
203{
204    return NO;
205}
206
207- (unsigned)loadStatus
208{
209    return CPImageLoadStatusCompleted;
210}
211
212- (id)delegate
213{
214    return nil;
215}
216
217- (CPString)description
218{
219    var image = [self imageFromCoder:nil];
220
221    return [image description];
222}
223
224@end