API  0.9.10
CPCibLoading.j
Go to the documentation of this file.
1 /*
2  * _CPCibLoading.j
3  * AppKit
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 
24 
25 var CPCibOwner = @"CPCibOwner",
26  CPBundleDefaultLanguage = @"CPBundleDefaultLanguage",
27  CPBundleTypeOfLocalization = @"CPBundleTypeOfLocalization",
28  CPBundleBaseLocalizationType = @"CPBundleBaseLocalizationType",
29  CPBundleInterfaceBuilderLocalizationType = @"CPBundleInterfaceBuilderLocalizationType";
30 
31 @implementation CPObject (CPCibLoading)
32 
33 - (void)awakeFromCib
34 {
35 }
36 
37 @end
38 
39 @implementation CPBundle (CPCibLoading)
40 
41 + (CPCib)loadCibFile:(CPString)anAbsolutePath externalNameTable:(CPDictionary)aNameTable
42 {
43  return [[[CPCib alloc] initWithContentsOfURL:anAbsolutePath] instantiateCibWithExternalNameTable:aNameTable];
44 }
45 
46 + (CPCib)loadCibNamed:(CPString)aName owner:(id)anOwner
47 {
48  return [self loadCibFile:[self _cibPathForName:aName withOwner:anOwner] externalNameTable:@{ CPCibOwner: anOwner }];
49 }
50 
51 - (CPCib)loadCibFile:(CPString)aFileName externalNameTable:(CPDictionary)aNameTable
52 {
53  return [[[CPCib alloc] initWithContentsOfURL:aFileName] instantiateCibWithExternalNameTable:aNameTable];
54 }
55 
56 + (CPCib)loadCibFile:(CPString)anAbsolutePath externalNameTable:(CPDictionary)aNameTable loadDelegate:aDelegate
57 {
58  return ([[CPCib alloc]
59  initWithContentsOfURL:anAbsolutePath
60  loadDelegate:[[_CPCibLoadDelegate alloc]
61  initWithLoadDelegate:aDelegate
62  externalNameTable:aNameTable]]);
63 }
64 
65 + (CPCib)loadCibNamed:(CPString)aName owner:(id)anOwner loadDelegate:(id)aDelegate
66 {
67  return [self loadCibFile:[self _cibPathForName:aName withOwner:anOwner] externalNameTable:@{ CPCibOwner: anOwner } loadDelegate:aDelegate];
68 }
69 
70 - (CPCib)loadCibFile:(CPString)aFileName externalNameTable:(CPDictionary)aNameTable loadDelegate:(id)aDelegate
71 {
72  return ([[CPCib alloc]
73  initWithCibNamed:aFileName
74  bundle:self
75  loadDelegate:[[_CPCibLoadDelegate alloc]
76  initWithLoadDelegate:aDelegate
77  externalNameTable:aNameTable]]);
78 }
79 
80 - (CPString)_cibPathForResource:(CPString)aName
81 {
82  var defaultBundleLanguage = [self objectForInfoDictionaryKey:CPBundleDefaultLanguage],
83  typeOfLocalization = [self objectForInfoDictionaryKey:CPBundleTypeOfLocalization];
84 
85  if (defaultBundleLanguage && (!typeOfLocalization || typeOfLocalization == CPBundleBaseLocalizationType))
86  aName = @"Base.lproj/" + aName;
87  else if (defaultBundleLanguage && typeOfLocalization == CPBundleInterfaceBuilderLocalizationType)
88  aName = _bundle.loadedLanguage() + ".lproj/" + aName;
89 
90  return [self pathForResource:aName];
91 }
92 
93 + (CPString)_cibPathForName:(CPString)aName withOwner:(id)anOwner
94 {
95  if (![aName hasSuffix:@".cib"])
96  aName = [aName stringByAppendingString:@".cib"];
97 
98  // Path is based solely on anOwner:
99  var bundle = anOwner ? [CPBundle bundleForClass:[anOwner class]] : [CPBundle mainBundle];
100 
101  return [bundle _cibPathForResource:aName];
102 }
103 
104 @end
105 
106 @implementation _CPCibLoadDelegate : CPObject
107 {
108  id _loadDelegate;
109  CPDictionary _externalNameTable;
110 }
111 
112 - (id)initWithLoadDelegate:(id)aLoadDelegate externalNameTable:(id)anExternalNameTable
113 {
114  self = [self init];
115 
116  if (self)
117  {
118  _loadDelegate = aLoadDelegate;
119  _externalNameTable = anExternalNameTable;
120  }
121 
122  return self;
123 }
124 
125 - (void)cibDidFinishLoading:(CPCib)aCib
126 {
127  [aCib instantiateCibWithExternalNameTable:_externalNameTable];
128 
129  [_loadDelegate cibDidFinishLoading:aCib];
130 }
131 
132 - (void)cibDidFailToLoad:(CPCib)aCib
133 {
134  [_loadDelegate cibDidFailToLoad:aCib];
135 }
136 
137 @end
var CPBundleBaseLocalizationType
Definition: CPCibLoading.j:28
CPCib loadCibFile:externalNameTable:loadDelegate:(CPString anAbsolutePath, [externalNameTable] CPDictionary aNameTable, [loadDelegate] id aDelegate)
Definition: CPCibLoading.j:56
Definition: CPCib.h:2
var CPBundleTypeOfLocalization
Definition: CPCibLoading.j:27
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPString pathForResource:(CPString aFilename)
Definition: CPBundle.j:158
An immutable string (collection of characters).
Definition: CPString.h:2
CPBundle mainBundle()
Definition: CPBundle.j:82
CPCib loadCibFile:externalNameTable:(CPString anAbsolutePath, [externalNameTable] CPDictionary aNameTable)
Definition: CPCibLoading.j:41
var CPBundleInterfaceBuilderLocalizationType
Definition: CPCibLoading.j:29
id objectForInfoDictionaryKey:(CPString aKey)
Definition: CPBundle.j:183
id init()
Definition: CPObject.j:145
var CPCibOwner
Definition: CPCibLoading.j:25
CPBundle bundleForClass:(Class aClass)
Definition: CPBundle.j:77
CPString stringByAppendingString:(CPString aString)
Definition: CPString.j:221
var CPBundleDefaultLanguage
Definition: CPCibLoading.j:26
id alloc()
Definition: CPObject.j:130