00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <AppKit/CPResponder.j>
00024
00025 var CPViewControllerCachedCibs;
00026
00048 @implementation CPViewController : CPResponder
00049 {
00050 CPView _view;
00051
00052 id _representedObject @accessors(property=representedObject);
00053 CPString _title @accessors(property=title);
00054
00055 CPString _cibName @accessors(property=cibName, readonly);
00056 CPBundle _cibBundle @accessors(property=cibBundle, readonly);
00057 CPDictionary _cibExternalNameTable @accessors(property=cibExternalNameTable, readonly);
00058 }
00059
00060 + (void)initialize
00061 {
00062 if (self === CPViewController)
00063 CPViewControllerCachedCibs = [CPDictionary dictionary];
00064 }
00065
00069 - (id)init
00070 {
00071 return [self initWithCibName:nil bundle:nil];
00072 }
00073
00074 - (id)initWithCibName:(CPString)aCibNameOrNil bundle:(CPBundle)aCibBundleOrNil
00075 {
00076 return [self initWithCibName:aCibNameOrNil bundle:aCibBundleOrNil externalNameTable:nil];
00077 }
00078
00079 - (id)initWithCibName:(CPString)aCibNameOrNil bundle:(CPBundle)aCibBundleOrNil owner:(id)anOwner
00080 {
00081 return [self initWithCibName:aCibNameOrNil bundle:aCibBundleOrNil externalNameTable:[CPDictionary dictionaryWithObject:anOwner forKey:CPCibOwner]];
00082 }
00083
00092 - (id)initWithCibName:(CPString)aCibNameOrNil bundle:(CPBundle)aCibBundleOrNil externalNameTable:(CPDictionary)anExternalNameTable
00093 {
00094 self = [super init];
00095
00096 if (self)
00097 {
00098
00099 _cibName = aCibNameOrNil;
00100 _cibBundle = aCibBundleOrNil || [CPBundle mainBundle];
00101 _cibExternalNameTable = anExternalNameTable || [CPDictionary dictionaryWithObject:self forKey:CPCibOwner];
00102 }
00103
00104 return self;
00105 }
00106
00119 - (void)loadView
00120 {
00121 if (_view)
00122 return;
00123
00124
00125 var cib = [CPViewControllerCachedCibs objectForKey:_cibName];
00126
00127 if (!cib)
00128 {
00129
00130 cib = [[CPCib alloc] initWithContentsOfURL:[_cibBundle pathForResource:_cibName + @".cib"]];
00131 [CPViewControllerCachedCibs setObject:cib forKey:_cibName];
00132 }
00133
00134 [cib instantiateCibWithExternalNameTable:_cibExternalNameTable];
00135 }
00136
00142 - (CPView)view
00143 {
00144 if (!_view)
00145 {
00146 var cibOwner = [_cibExternalNameTable objectForKey:CPCibOwner];
00147
00148 if ([cibOwner respondsToSelector:@selector(viewControllerWillLoadCib:)])
00149 [cibOwner viewControllerWillLoadCib:self];
00150
00151 [self loadView];
00152
00153 if (_view === nil && [cibOwner isKindOfClass:[CPDocument class]])
00154 [self setView:[cibOwner valueForKey:@"view"]];
00155
00156 if (!_view)
00157 {
00158 var reason = [CPString stringWithFormat:@"View for %@ could not be loaded from Cib or no view specified. Override loadView to load the view manually.", self];
00159
00160 [CPException raise:CPInternalInconsistencyException reason:reason];
00161 }
00162
00163 if ([cibOwner respondsToSelector:@selector(viewControllerDidLoadCib:)])
00164 [cibOwner viewControllerDidLoadCib:self];
00165
00166 [self viewDidLoad];
00167 }
00168
00169 return _view;
00170 }
00171
00172
00178 - (void)viewDidLoad
00179 {
00180
00181 }
00182
00183
00190 - (void)setView:(CPView)aView
00191 {
00192 _view = aView;
00193 }
00194
00195 @end
00196
00197
00198 var CPViewControllerViewKey = @"CPViewControllerViewKey",
00199 CPViewControllerTitleKey = @"CPViewControllerTitleKey",
00200 CPViewControllerCibNameKey = @"CPViewControllerCibNameKey",
00201 CPViewControllerBundleKey = @"CPViewControllerBundleKey";
00202
00203 @implementation CPViewController (CPCoding)
00204
00210 - (id)initWithCoder:(CPCoder)aCoder
00211 {
00212 self = [super initWithCoder:aCoder];
00213
00214 if (self)
00215 {
00216 _view = [aCoder decodeObjectForKey:CPViewControllerViewKey];
00217 _title = [aCoder decodeObjectForKey:CPViewControllerTitleKey];
00218 _cibName = [aCoder decodeObjectForKey:CPViewControllerCibNameKey];
00219
00220 var bundlePath = [aCoder decodeObjectForKey:CPViewControllerBundleKey];
00221 _cibBundle = bundlePath ? [CPBundle bundleWithPath:bundlePath] : [CPBundle mainBundle];
00222
00223 _cibExternalNameTable = [CPDictionary dictionaryWithObject:self forKey:CPCibOwner];
00224 }
00225
00226 return self;
00227 }
00228
00233 - (void)encodeWithCoder:(CPCoder)aCoder
00234 {
00235 [super encodeWithCoder:aCoder];
00236
00237 [aCoder encodeObject:_view forKey:CPViewControllerViewKey];
00238 [aCoder encodeObject:_title forKey:CPViewControllerTitleKey];
00239 [aCoder encodeObject:_cibName forKey:CPViewControllerCibNameKey];
00240 [aCoder encodeObject:[_cibBundle bundlePath] forKey:CPViewControllerBundleKey];
00241 }
00242
00243 @end