00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPObject.j>
00024 @import <Foundation/CPURLConnection.j>
00025 @import <Foundation/CPURLRequest.j>
00026
00027 @import "_CPCibClassSwapper.j"
00028 @import "_CPCibCustomObject.j"
00029 @import "_CPCibCustomResource.j"
00030 @import "_CPCibCustomView.j"
00031 @import "_CPCibKeyedUnarchiver.j"
00032 @import "_CPCibObjectData.j"
00033 @import "_CPCibProxyObject.j"
00034 @import "_CPCibWindowTemplate.j"
00035
00036
00037 CPCibOwner = @"CPCibOwner",
00038 CPCibTopLevelObjects = @"CPCibTopLevelObjects",
00039 CPCibReplacementClasses = @"CPCibReplacementClasses",
00040 CPCibExternalObjects = @"CPCibExternalObjects";
00041
00042 var CPCibObjectDataKey = @"CPCibObjectDataKey";
00043
00048 @implementation CPCib : CPObject
00049 {
00050 CPData _data;
00051 CPBundle _bundle;
00052 BOOL _awakenCustomResources;
00053
00054 id _loadDelegate;
00055 }
00056
00057 - (id)initWithContentsOfURL:(CPURL)aURL
00058 {
00059 self = [super init];
00060
00061 if (self)
00062 {
00063 _data = [CPURLConnection sendSynchronousRequest:[CPURLRequest requestWithURL:aURL] returningResponse:nil];
00064 _awakenCustomResources = YES;
00065 }
00066
00067 return self;
00068 }
00069
00070 - (id)initWithContentsOfURL:(CPURL)aURL loadDelegate:(id)aLoadDelegate
00071 {
00072 self = [super init];
00073
00074 if (self)
00075 {
00076 [CPURLConnection connectionWithRequest:[CPURLRequest requestWithURL:aURL] delegate:self];
00077
00078 _awakenCustomResources = YES;
00079
00080 _loadDelegate = aLoadDelegate;
00081 }
00082
00083 return self;
00084 }
00085
00086 - (id)initWithCibNamed:(CPString)aName bundle:(CPBundle)aBundle
00087 {
00088 if (![aName hasSuffix:@".cib"])
00089 aName = [aName stringByAppendingString:@".cib"];
00090
00091
00092 self = [self initWithContentsOfURL:[aBundle || [CPBundle mainBundle] pathForResource:aName]];
00093
00094 if (self)
00095 _bundle = aBundle;
00096
00097 return self;
00098 }
00099
00100 - (id)initWithCibNamed:(CPString)aName bundle:(CPBundle)aBundle loadDelegate:(id)aLoadDelegate
00101 {
00102 if (![aName hasSuffix:@".cib"])
00103 aName = [aName stringByAppendingString:@".cib"];
00104
00105
00106 self = [self initWithContentsOfURL:[aBundle || [CPBundle mainBundle] pathForResource:aName] loadDelegate:aLoadDelegate];
00107
00108 if (self)
00109 _bundle = aBundle;
00110
00111 return self;
00112 }
00113
00114 - (void)_setAwakenCustomResources:(BOOL)shouldAwakenCustomResources
00115 {
00116 _awakenCustomResources = shouldAwakenCustomResources;
00117 }
00118
00119 - (BOOL)_awakenCustomResources
00120 {
00121 return _awakenCustomResources;
00122 }
00123
00124 - (BOOL)instantiateCibWithExternalNameTable:(CPDictionary)anExternalNameTable
00125 {
00126 var bundle = _bundle,
00127 owner = [anExternalNameTable objectForKey:CPCibOwner];
00128
00129 if (!bundle && owner)
00130 bundle = [CPBundle bundleForClass:[owner class]];
00131
00132 var unarchiver = [[_CPCibKeyedUnarchiver alloc] initForReadingWithData:_data bundle:bundle awakenCustomResources:_awakenCustomResources],
00133 replacementClasses = [anExternalNameTable objectForKey:CPCibReplacementClasses];
00134
00135 if (replacementClasses)
00136 {
00137 var key = nil,
00138 keyEnumerator = [replacementClasses keyEnumerator];
00139
00140 while (key = [keyEnumerator nextObject])
00141 [unarchiver setClass:[replacementClasses objectForKey:key] forClassName:key];
00142 }
00143
00144 [unarchiver setExternalObjectsForProxyIdentifiers:[anExternalNameTable objectForKey:CPCibExternalObjects]];
00145
00146 var objectData = [unarchiver decodeObjectForKey:CPCibObjectDataKey];
00147
00148 if (!objectData || ![objectData isKindOfClass:[_CPCibObjectData class]])
00149 return NO;
00150
00151 var topLevelObjects = [anExternalNameTable objectForKey:CPCibTopLevelObjects];
00152
00153 [objectData instantiateWithOwner:owner topLevelObjects:topLevelObjects]
00154 [objectData establishConnectionsWithOwner:owner topLevelObjects:topLevelObjects];
00155 [objectData awakeWithOwner:owner topLevelObjects:topLevelObjects];
00156
00157
00158 [objectData displayVisibleWindows];
00159
00160 return YES;
00161 }
00162
00163 - (BOOL)instantiateCibWithOwner:(id)anOwner topLevelObjects:(CPArray)topLevelObjects
00164 {
00165 return [self instantiateCibWithExternalNameTable:[CPDictionary dictionaryWithObjectsAndKeys:anOwner, CPCibOwner, topLevelObjects, CPCibTopLevelObjects]];
00166 }
00167
00168 @end
00169
00170 @implementation CPCib (CPURLConnectionDelegate)
00171
00172 - (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
00173 {
00174
00175 if (!data)
00176 return [self connection:aConnection didFailWithError:nil];
00177
00178 _data = [CPData dataWithRawString:data];
00179 }
00180
00181 - (void)connection:(CPURLConnection)aConnection didFailWithError:(CPError)anError
00182 {
00183 if ([_loadDelegate respondsToSelector:@selector(cibDidFailToLoad:)])
00184 [_loadDelegate cibDidFailToLoad:self];
00185
00186 _loadDelegate = nil;
00187 }
00188
00189 - (void)connectionDidFinishLoading:(CPURLConnection)aConnection
00190 {
00191 if ([_loadDelegate respondsToSelector:@selector(cibDidFinishLoading:)])
00192 [_loadDelegate cibDidFinishLoading:self];
00193
00194 _loadDelegate = nil;
00195 }
00196
00197 @end