API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
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 
27 @implementation CPObject (CPCibLoading)
28 
29 - (void)awakeFromCib
30 {
31 }
32 
33 @end
34 
35 @implementation CPBundle (CPCibLoading)
36 
37 + (CPCib)loadCibFile:(CPString)anAbsolutePath externalNameTable:(CPDictionary)aNameTable
38 {
39  return [[[CPCib alloc] initWithContentsOfURL:anAbsolutePath] instantiateCibWithExternalNameTable:aNameTable];
40 }
41 
42 + (CPCib)loadCibNamed:(CPString)aName owner:(id)anOwner
43 {
44  if (![aName hasSuffix:@".cib"])
45  aName = [aName stringByAppendingString:@".cib"];
46 
47  // Path is based solely on anOwner:
48  var bundle = anOwner ? [CPBundle bundleForClass:[anOwner class]] : [CPBundle mainBundle],
49  path = [bundle pathForResource:aName];
50 
51  return [self loadCibFile:path externalNameTable:[CPDictionary dictionaryWithObject:anOwner forKey:CPCibOwner]];
52 }
53 
54 - (CPCib)loadCibFile:(CPString)aFileName externalNameTable:(CPDictionary)aNameTable
55 {
56  return [[[CPCib alloc] initWithContentsOfURL:aFileName] instantiateCibWithExternalNameTable:aNameTable];
57 }
58 
59 + (CPCib)loadCibFile:(CPString)anAbsolutePath externalNameTable:(CPDictionary)aNameTable loadDelegate:aDelegate
60 {
61  return ([[CPCib alloc]
62  initWithContentsOfURL:anAbsolutePath
63  loadDelegate:[[_CPCibLoadDelegate alloc]
64  initWithLoadDelegate:aDelegate
65  externalNameTable:aNameTable]]);
66 }
67 
68 + (CPCib)loadCibNamed:(CPString)aName owner:(id)anOwner loadDelegate:(id)aDelegate
69 {
70  if (![aName hasSuffix:@".cib"])
71  aName = [aName stringByAppendingString:@".cib"];
72 
73  // Path is based solely on anOwner:
74  var bundle = anOwner ? [CPBundle bundleForClass:[anOwner class]] : [CPBundle mainBundle],
75  path = [bundle pathForResource:aName];
76 
77  return [self loadCibFile:path externalNameTable:[CPDictionary dictionaryWithObject:anOwner forKey:CPCibOwner] loadDelegate:aDelegate];
78 }
79 
80 - (CPCib)loadCibFile:(CPString)aFileName externalNameTable:(CPDictionary)aNameTable loadDelegate:(id)aDelegate
81 {
82  return ([[CPCib alloc]
83  initWithCibNamed:aFileName
84  bundle:self
85  loadDelegate:[[_CPCibLoadDelegate alloc]
86  initWithLoadDelegate:aDelegate
87  externalNameTable:aNameTable]]);
88 }
89 
90 @end
91 
92 @implementation _CPCibLoadDelegate : CPObject
93 {
94  id _loadDelegate;
95  CPDictionary _externalNameTable;
96 }
97 
98 - (id)initWithLoadDelegate:(id)aLoadDelegate externalNameTable:(id)anExternalNameTable
99 {
100  self = [self init];
101 
102  if (self)
103  {
104  _loadDelegate = aLoadDelegate;
105  _externalNameTable = anExternalNameTable;
106  }
107 
108  return self;
109 }
110 
111 - (void)cibDidFinishLoading:(CPCib)aCib
112 {
113  [aCib instantiateCibWithExternalNameTable:_externalNameTable];
114 
115  [_loadDelegate cibDidFinishLoading:aCib];
116 }
117 
118 - (void)cibDidFailToLoad:(CPCib)aCib
119 {
120  [_loadDelegate cibDidFailToLoad:aCib];
121 }
122 
123 @end