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 "_CPCibWindowTemplate.j"
00034
00035
00036 CPCibOwner = @"CPCibOwner",
00037 CPCibTopLevelObjects = @"CPCibTopLevelObjects",
00038 CPCibReplacementClasses = @"CPCibReplacementClasses";
00039
00040 var CPCibObjectDataKey = @"CPCibObjectDataKey";
00041
00046 @implementation CPCib : CPObject
00047 {
00048 CPData _data;
00049 CPBundle _bundle;
00050 BOOL _awakenCustomResources;
00051
00052 id _loadDelegate;
00053 }
00054
00055 - (id)initWithContentsOfURL:(CPURL)aURL
00056 {
00057 self = [super init];
00058
00059 if (self)
00060 {
00061 _data = [CPURLConnection sendSynchronousRequest:[CPURLRequest requestWithURL:aURL] returningResponse:nil error:nil];
00062 _awakenCustomResources = YES;
00063 }
00064
00065 return self;
00066 }
00067
00068 - (id)initWithContentsOfURL:(CPURL)aURL loadDelegate:(id)aLoadDelegate
00069 {
00070 self = [super init];
00071
00072 if (self)
00073 {
00074 [CPURLConnection connectionWithRequest:[CPURLRequest requestWithURL:aURL] delegate:self];
00075
00076 _awakenCustomResources = YES;
00077
00078 _loadDelegate = aLoadDelegate;
00079 }
00080
00081 return self;
00082 }
00083
00084 - (id)initWithCibNamed:(CPString)aName bundle:(CPBundle)aBundle loadDelegate:(id)aLoadDelegate
00085 {
00086 if (![aName hasSuffix:@".cib"])
00087 aName = [aName stringByAppendingString:@".cib"];
00088
00089
00090 self = [self initWithContentsOfURL:[aBundle || [CPBundle mainBundle] pathForResource:aName] loadDelegate:aLoadDelegate];
00091
00092 if (self)
00093 _bundle = aBundle;
00094
00095 return self;
00096 }
00097
00098 - (void)_setAwakenCustomResources:(BOOL)shouldAwakenCustomResources
00099 {
00100 _awakenCustomResources = shouldAwakenCustomResources;
00101 }
00102
00103 - (BOOL)_awakenCustomResources
00104 {
00105 return _awakenCustomResources;
00106 }
00107
00108 - (BOOL)instantiateCibWithExternalNameTable:(CPDictionary)anExternalNameTable
00109 {
00110 var bundle = _bundle,
00111 owner = [anExternalNameTable objectForKey:CPCibOwner];
00112
00113 if (!bundle && owner)
00114 bundle = [CPBundle bundleForClass:[owner class]];
00115
00116 var unarchiver = [[_CPCibKeyedUnarchiver alloc] initForReadingWithData:_data bundle:bundle awakenCustomResources:_awakenCustomResources],
00117 replacementClasses = [anExternalNameTable objectForKey:CPCibReplacementClasses];
00118
00119 if (replacementClasses)
00120 {
00121 var key = nil,
00122 keyEnumerator = [replacementClasses keyEnumerator];
00123
00124 while (key = [keyEnumerator nextObject])
00125 [unarchiver setClass:[replacementClasses objectForKey:key] forClassName:key];
00126 }
00127
00128 var objectData = [unarchiver decodeObjectForKey:CPCibObjectDataKey];
00129
00130 if (!objectData || ![objectData isKindOfClass:[_CPCibObjectData class]])
00131 return NO;
00132
00133 var topLevelObjects = [anExternalNameTable objectForKey:CPCibTopLevelObjects];
00134
00135 [objectData instantiateWithOwner:owner topLevelObjects:topLevelObjects]
00136 [objectData establishConnectionsWithOwner:owner topLevelObjects:topLevelObjects];
00137 [objectData awakeWithOwner:owner topLevelObjects:topLevelObjects];
00138
00139 var menu;
00140
00141 if ((menu = [objectData mainMenu]) != nil)
00142 {
00143 [CPApp setMainMenu:menu];
00144 [CPMenu setMenuBarVisible:YES];
00145 }
00146
00147
00148 [objectData displayVisibleWindows];
00149
00150 return YES;
00151 }
00152
00153 - (BOOL)instantiateCibWithOwner:(id)anOwner topLevelObjects:(CPArray)topLevelObjects
00154 {
00155 return [self instantiateCibWithExternalNameTable:[CPDictionary dictionaryWithObjectsAndKeys:anOwner, CPCibOwner, topLevelObjects, CPCibTopLevelObjects]];
00156 }
00157
00158 @end
00159
00160 @implementation CPCib (CPURLConnectionDelegate)
00161
00162 - (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
00163 {
00164 _data = [CPData dataWithString:data];
00165 }
00166
00167 - (void)connection:(CPURLConnection)aConnection didFailWithError:(CPError)anError
00168 {
00169 alert("cib: connection failed.");
00170
00171 _loadDelegate = nil;
00172 }
00173
00174 - (void)connectionDidFinishLoading:(CPURLConnection)aConnection
00175 {
00176 if ([_loadDelegate respondsToSelector:@selector(cibDidFinishLoading:)])
00177 [_loadDelegate cibDidFinishLoading:self];
00178
00179 _loadDelegate = nil;
00180 }
00181
00182 @end