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