From b369aa6affe93f99de010766cbb0db635bcb2056 Mon Sep 17 00:00:00 2001 From: Francisco Ryan Tolmasky I Date: Mon, 24 Aug 2009 00:21:15 -0700 Subject: [PATCH] Made CPCibConnector, CPCibOutletConnector, and CPCibControlConnector public classes. Reviewed by me. --- AppKit/AppKit.j | 3 + AppKit/Cib/CPCibConnector.j | 88 ++++++++++++++++++++ AppKit/Cib/CPCibControlConnector.j | 76 +++++++++++++++++ AppKit/Cib/CPCibOutletConnector.j | 48 +++++++++++ AppKit/Cib/_CPCibConnector.j | 126 ----------------------------- Tests/TableTest/AppController.j | 5 -- 6 files changed, 215 insertions(+), 131 deletions(-) create mode 100644 AppKit/Cib/CPCibConnector.j create mode 100644 AppKit/Cib/CPCibControlConnector.j create mode 100644 AppKit/Cib/CPCibOutletConnector.j delete mode 100644 AppKit/Cib/_CPCibConnector.j diff --git a/AppKit/AppKit.j b/AppKit/AppKit.j index 1687acd26..454480cc8 100644 --- a/AppKit/AppKit.j +++ b/AppKit/AppKit.j @@ -29,7 +29,10 @@ @import "CPButtonBar.j" @import "CPCheckBox.j" @import "CPCib.j" +@import "CPCibConnector.j" +@import "CPCibControlConnector.j" @import "CPCibLoading.j" +@import "CPCibOutletConnector.j" @import "CPClipView.j" @import "CPCollectionView.j" @import "CPColor.j" diff --git a/AppKit/Cib/CPCibConnector.j b/AppKit/Cib/CPCibConnector.j new file mode 100644 index 000000000..dc2e4c46b --- /dev/null +++ b/AppKit/Cib/CPCibConnector.j @@ -0,0 +1,88 @@ +/* + * CPCibConnector.j + * AppKit + * + * Created by Francisco Tolmasky. + * Copyright 2008, 280 North, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +@import +@import + + +var _CPCibConnectorSourceKey = @"_CPCibConnectorSourceKey", + _CPCibConnectorDestinationKey = @"_CPCibConnectorDestinationKey", + _CPCibConnectorLabelKey = @"_CPCibConnectorLabelKey"; + +@implementation CPCibConnector : CPObject +{ + id _source @accessors(property=source); + id _destination @accessors(property=destination); + CPString _label @accessors(property=label); +} + +- (void)replaceObject:(id)anObject withObject:(id)anotherObject +{ + if (_source === anObject) + _source = anotherObject; + + if (_destination === anObject) + _destination = anotherObject; +} + +- (void)replaceObjects:(Object)replacementObjects +{ + var replacement = replacementObjects[[_source UID]]; + + if (replacement !== undefined) + _source = replacement; + + replacement = replacementObjects[[_destination UID]]; + + if (replacement !== undefined) + _destination = replacement; +} + +@end + +@implementation CPCibConnector (CPCoding) + +- (id)initWithCoder:(CPCoder)aCoder +{ + self = [super init]; + + if (self) + { + _source = [aCoder decodeObjectForKey:_CPCibConnectorSourceKey]; + _destination = [aCoder decodeObjectForKey:_CPCibConnectorDestinationKey]; + _label = [aCoder decodeObjectForKey:_CPCibConnectorLabelKey]; + } + + return self; +} + +- (void)encodeWithCoder:(CPCoder)aCoder +{ + [aCoder encodeObject:_source forKey:_CPCibConnectorSourceKey]; + [aCoder encodeObject:_destination forKey:_CPCibConnectorDestinationKey]; + [aCoder encodeObject:_label forKey:_CPCibConnectorLabelKey]; +} + +@end + +// For backwards compatibility. +@implementation _CPCibConnector : CPCibConnector { } @end diff --git a/AppKit/Cib/CPCibControlConnector.j b/AppKit/Cib/CPCibControlConnector.j new file mode 100644 index 000000000..f921d806b --- /dev/null +++ b/AppKit/Cib/CPCibControlConnector.j @@ -0,0 +1,76 @@ +/* + * CPCibControlConnector.j + * AppKit + * + * Created by Francisco Tolmasky. + * Copyright 2008, 280 North, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +@import "CPCibConnector.j" + + +@implementation CPCibControlConnector : CPCibConnector +{ +} + +- (void)establishConnection +{ + var selectorName = _label, + selectorNameLength = [selectorName length]; + + if (selectorNameLength && selectorName[selectorNameLength - 1] !== ':') + selectorName += ':'; + + var selector = CPSelectorFromString(selectorName); + + // Not having a selector is a fatal error. + if (!selector) + [CPException + raise:CPInvalidArgumentException + reason:@"-[" + [self className] + ' ' + _cmd + @"] selector " + selectorName + @" does not exist."]; + + // If the destination doesn't respond to this selector, warn but don't die. + if (_destination && ![_destination respondsToSelector:selector]) + { + CPLog.warn(@"Could not connect the action " + selector + @" to target of class " + [_destination className]); + + return; + } + + // Not being able to set the action is a fatal error. + if ([_source respondsToSelector:@selector(setAction:)]) + objj_msgSend(_source, @selector(setAction:), selector); + + else + [CPException + raise:CPInvalidArgumentException + reason:@"-[" + [self className] + ' ' + _cmd + @"] " + [_source description] + " does not respond to setAction:"]; + + // Not being able to set the target is a fatal error. + if ([_source respondsToSelector:@selector(setTarget:)]) + objj_msgSend(_source, @selector(setTarget:), _destination); + + else + [CPException + raise:CPInvalidArgumentException + reason:@"-[" + [self className] + ' ' + _cmd + @"] " + [_source description] + " does not respond to setTarget:"]; +} + +@end + +@implementation _CPCibControlConnector : CPCibControlConnector { } @end + diff --git a/AppKit/Cib/CPCibOutletConnector.j b/AppKit/Cib/CPCibOutletConnector.j new file mode 100644 index 000000000..d06c460c1 --- /dev/null +++ b/AppKit/Cib/CPCibOutletConnector.j @@ -0,0 +1,48 @@ +/* + * CPCibOutletConnector.j + * AppKit + * + * Created by Francisco Tolmasky. + * Copyright 2008, 280 North, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +@import "CPCibConnector.j" + + +@implementation CPCibOutletConnector : CPCibConnector +{ +} + +- (void)establishConnection +{ + try + { + [_source setValue:_destination forKey:_label]; + } + catch (anException) + { + if ([anException name] === CPUndefinedKeyException) + CPLog.warn(@"Could not connect the outlet " + _label + @" of target of class " + [_source className]); + + else + throw anException; + } +} + +@end + +@implementation _CPCibOutletConnector : CPCibOutletConnector { } @end diff --git a/AppKit/Cib/_CPCibConnector.j b/AppKit/Cib/_CPCibConnector.j deleted file mode 100644 index 52b3a1a50..000000000 --- a/AppKit/Cib/_CPCibConnector.j +++ /dev/null @@ -1,126 +0,0 @@ - -@import -@import - - -var _CPCibConnectorSourceKey = @"_CPCibConnectorSourceKey", - _CPCibConnectorDestinationKey = @"_CPCibConnectorDestinationKey", - _CPCibConnectorLabelKey = @"_CPCibConnectorLabelKey"; - -@implementation _CPCibConnector : CPObject -{ - id _source; - id _destination; - CPString _label; -} - -- (void)replaceObjects:(JSObject)replacementObjects -{ - var replacement = replacementObjects[[_source UID]]; - - if (replacement !== undefined) - _source = replacement; - - replacement = replacementObjects[[_destination UID]]; - - if (replacement !== undefined) - _destination = replacement; -} - -@end - -@implementation _CPCibConnector (CPCoding) - -- (id)initWithCoder:(CPCoder)aCoder -{ - self = [super init]; - - if (self) - { - _source = [aCoder decodeObjectForKey:_CPCibConnectorSourceKey]; - _destination = [aCoder decodeObjectForKey:_CPCibConnectorDestinationKey]; - _label = [aCoder decodeObjectForKey:_CPCibConnectorLabelKey]; - } - - return self; -} - -- (void)encodeWithCoder:(CPCoder)aCoder -{ - [aCoder encodeObject:_source forKey:_CPCibConnectorSourceKey]; - [aCoder encodeObject:_destination forKey:_CPCibConnectorDestinationKey]; - [aCoder encodeObject:_label forKey:_CPCibConnectorLabelKey]; -} - -@end - -@implementation _CPCibControlConnector : _CPCibConnector -{ -} - -- (void)establishConnection -{ - var selectorName = _label, - selectorNameLength = [selectorName length]; - - if (selectorNameLength && selectorName[selectorNameLength - 1] !== ':') - selectorName += ':'; - - var selector = CPSelectorFromString(selectorName); - - // Not having a selector is a fatal error. - if (!selector) - [CPException - raise:CPInvalidArgumentException - reason:@"-[" + [self className] + ' ' + _cmd + @"] selector " + selectorName + @" does not exist."]; - - // If the destination doesn't respond to this selector, warn but don't die. - if (_destination && ![_destination respondsToSelector:selector]) - { - CPLog.warn(@"Could not connect the action " + selector + @" to target of class " + [_destination className]); - - return; - } - - // Not being able to set the action is a fatal error. - if ([_source respondsToSelector:@selector(setAction:)]) - objj_msgSend(_source, @selector(setAction:), selector); - - else - [CPException - raise:CPInvalidArgumentException - reason:@"-[" + [self className] + ' ' + _cmd + @"] " + [_source description] + " does not respond to setAction:"]; - - // Not being able to set the target is a fatal error. - if ([_source respondsToSelector:@selector(setTarget:)]) - objj_msgSend(_source, @selector(setTarget:), _destination); - - else - [CPException - raise:CPInvalidArgumentException - reason:@"-[" + [self className] + ' ' + _cmd + @"] " + [_source description] + " does not respond to setTarget:"]; -} - -@end - -@implementation _CPCibOutletConnector : _CPCibConnector -{ -} - -- (void)establishConnection -{ - try - { - [_source setValue:_destination forKey:_label]; - } - catch (anException) - { - if ([anException name] === CPUndefinedKeyException) - CPLog.warn(@"Could not connect the outlet " + _label + @" of target of class " + [_source className]); - - else - throw anException; - } -} - -@end diff --git a/Tests/TableTest/AppController.j b/Tests/TableTest/AppController.j index c6a8ad806..10c271dd4 100644 --- a/Tests/TableTest/AppController.j +++ b/Tests/TableTest/AppController.j @@ -16,11 +16,6 @@ CPLogRegister(CPLogConsole); - (void)applicationDidFinishLaunching:(CPNotification)aNotification { - indexes = [CPMutableIndexSet indexSetWithIndex:5]; - - [indexes addIndexesInRange:CPMakeRange(4, 1)]; - alert(indexes); - return; var view = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)]; [view setBackgroundColor:[CPColor whiteColor]];