API 0.9.5
AppKit/CPViewController.j
Go to the documentation of this file.
00001 /*
00002  * CPViewController.j
00003  * AppKit
00004  *
00005  * Created by Nicholas Small and Francisco Tolmasky.
00006  * Copyright 2009, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 
00026 var CPViewControllerCachedCibs;
00027 
00061 @implementation CPViewController : CPResponder
00062 {
00063     CPView          _view;
00064     BOOL            _isLoading;
00065 
00066     id              _representedObject;
00067     CPString        _title;
00068 
00069     CPString        _cibName;
00070     CPBundle        _cibBundle;
00071     CPDictionary    _cibExternalNameTable;
00072 }
00073 
00074 + (void)initialize
00075 {
00076     if (self === CPViewController)
00077         CPViewControllerCachedCibs = [CPDictionary dictionary];
00078 }
00079 
00083 - (id)init
00084 {
00085     return [self initWithCibName:nil bundle:nil];
00086 }
00087 
00088 - (id)initWithCibName:(CPString)aCibNameOrNil bundle:(CPBundle)aCibBundleOrNil
00089 {
00090     return [self initWithCibName:aCibNameOrNil bundle:aCibBundleOrNil externalNameTable:nil];
00091 }
00092 
00093 - (id)initWithCibName:(CPString)aCibNameOrNil bundle:(CPBundle)aCibBundleOrNil owner:(id)anOwner
00094 {
00095     return [self initWithCibName:aCibNameOrNil bundle:aCibBundleOrNil externalNameTable:[CPDictionary dictionaryWithObject:anOwner forKey:CPCibOwner]];
00096 }
00097 
00111 - (id)initWithCibName:(CPString)aCibNameOrNil bundle:(CPBundle)aCibBundleOrNil externalNameTable:(CPDictionary)anExternalNameTable
00112 {
00113     self = [super init];
00114 
00115     if (self)
00116     {
00117         // Don't load the cib until someone actually requests the view. The user may just be intending to use setView:.
00118         _cibName = aCibNameOrNil;
00119         _cibBundle = aCibBundleOrNil || [CPBundle mainBundle];
00120         _cibExternalNameTable = anExternalNameTable || [CPDictionary dictionaryWithObject:self forKey:CPCibOwner];
00121 
00122         _isLoading = NO;
00123     }
00124 
00125     return self;
00126 }
00127 
00144 - (void)loadView
00145 {
00146     if (_view)
00147         return;
00148 
00149     // check if a cib is already cached for the current _cibName
00150     var cib = [CPViewControllerCachedCibs objectForKey:_cibName];
00151 
00152     if (!cib)
00153     {
00154         // if the cib isn't cached yet : fetch it and cache it
00155         cib = [[CPCib alloc] initWithContentsOfURL:[_cibBundle pathForResource:_cibName + @".cib"]];
00156         [CPViewControllerCachedCibs setObject:cib forKey:_cibName];
00157     }
00158 
00159     [cib instantiateCibWithExternalNameTable:_cibExternalNameTable];
00160 }
00161 
00169 - (CPView)view
00170 {
00171     if (!_view)
00172     {
00173         _isLoading = YES;
00174 
00175         var cibOwner = [_cibExternalNameTable objectForKey:CPCibOwner];
00176 
00177         if ([cibOwner respondsToSelector:@selector(viewControllerWillLoadCib:)])
00178             [cibOwner viewControllerWillLoadCib:self];
00179 
00180         [self loadView];
00181 
00182         if (_view === nil && [cibOwner isKindOfClass:[CPDocument class]])
00183             [self setView:[cibOwner valueForKey:@"view"]];
00184 
00185         if (!_view)
00186         {
00187             var reason = [CPString stringWithFormat:@"View for %@ could not be loaded from Cib or no view specified. Override loadView to load the view manually.", self];
00188 
00189             [CPException raise:CPInternalInconsistencyException reason:reason];
00190         }
00191 
00192         if ([cibOwner respondsToSelector:@selector(viewControllerDidLoadCib:)])
00193             [cibOwner viewControllerDidLoadCib:self];
00194 
00195         _isLoading = NO;
00196         [self viewDidLoad];
00197     }
00198 
00199     return _view;
00200 }
00201 
00202 
00211 - (void)viewDidLoad
00212 {
00213 
00214 }
00215 
00216 
00225 - (void)setView:(CPView)aView
00226 {
00227     var viewWasLoaded = !_view;
00228 
00229     _view = aView;
00230 
00231     // Make sure the viewDidLoad method is called if the view is set directly
00232     if (!_isLoading && viewWasLoaded)
00233         [self viewDidLoad];
00234 }
00235 
00236 @end
00237 
00238 
00239 var CPViewControllerViewKey     = @"CPViewControllerViewKey",
00240     CPViewControllerTitleKey    = @"CPViewControllerTitleKey",
00241     CPViewControllerCibNameKey  = @"CPViewControllerCibNameKey",
00242     CPViewControllerBundleKey   = @"CPViewControllerBundleKey";
00243 
00244 @implementation CPViewController (CPCoding)
00245 
00251 - (id)initWithCoder:(CPCoder)aCoder
00252 {
00253     self = [super initWithCoder:aCoder];
00254 
00255     if (self)
00256     {
00257         _view = [aCoder decodeObjectForKey:CPViewControllerViewKey];
00258         _title = [aCoder decodeObjectForKey:CPViewControllerTitleKey];
00259         _cibName = [aCoder decodeObjectForKey:CPViewControllerCibNameKey];
00260 
00261         var bundlePath = [aCoder decodeObjectForKey:CPViewControllerBundleKey];
00262         _cibBundle = bundlePath ? [CPBundle bundleWithPath:bundlePath] : [CPBundle mainBundle];
00263 
00264         _cibExternalNameTable = [CPDictionary dictionaryWithObject:self forKey:CPCibOwner];
00265     }
00266 
00267     return self;
00268 }
00269 
00274 - (void)encodeWithCoder:(CPCoder)aCoder
00275 {
00276     [super encodeWithCoder:aCoder];
00277 
00278     [aCoder encodeObject:_view forKey:CPViewControllerViewKey];
00279     [aCoder encodeObject:_title forKey:CPViewControllerTitleKey];
00280     [aCoder encodeObject:_cibName forKey:CPViewControllerCibNameKey];
00281     [aCoder encodeObject:[_cibBundle bundlePath] forKey:CPViewControllerBundleKey];
00282 }
00283 
00284 @end
00285 
00286 @implementation CPViewController (CPSynthesizedAccessors)
00287 
00291 - (CPView)view
00292 {
00293     return _view;
00294 }
00295 
00299 - (void)setView:(CPView)aValue
00300 {
00301     _view = aValue;
00302 }
00303 
00307 - (id)representedObject
00308 {
00309     return _representedObject;
00310 }
00311 
00315 - (void)setRepresentedObject:(id)aValue
00316 {
00317     _representedObject = aValue;
00318 }
00319 
00323 - (CPString)title
00324 {
00325     return _title;
00326 }
00327 
00331 - (void)setTitle:(CPString)aValue
00332 {
00333     _title = aValue;
00334 }
00335 
00339 - (CPString)cibName
00340 {
00341     return _cibName;
00342 }
00343 
00347 - (CPBundle)cibBundle
00348 {
00349     return _cibBundle;
00350 }
00351 
00355 - (CPDictionary)cibExternalNameTable
00356 {
00357     return _cibExternalNameTable;
00358 }
00359 
00360 @end
 All Classes Files Functions Variables Defines