From 45f74386b82e31b20982620c080c0a8191065657 Mon Sep 17 00:00:00 2001 From: Ross Boucher Date: Thu, 18 Nov 2010 15:52:54 -0800 Subject: [PATCH] Add bindings support to nib2cib --- AppKit/AppKit.j | 1 + AppKit/CPArrayController.j | 37 +++++++++++++----- AppKit/CPObjectController.j | 21 ++++++---- AppKit/Cib/CPCibBindingConnector.j | 60 +++++++++++++++++++++++++++++ AppKit/Cib/_CPCibObjectData.j | 1 + Tools/nib2cib/NSArrayController.j | 61 +++++++++++++++++++++++++++++ Tools/nib2cib/NSNibConnector.j | 36 +++++++++++++++++ Tools/nib2cib/NSObjectController.j | 62 ++++++++++++++++++++++++++++++ 8 files changed, 262 insertions(+), 17 deletions(-) create mode 100644 AppKit/Cib/CPCibBindingConnector.j create mode 100644 Tools/nib2cib/NSArrayController.j create mode 100644 Tools/nib2cib/NSObjectController.j diff --git a/AppKit/AppKit.j b/AppKit/AppKit.j index ea93e99f8..f27c19e37 100644 --- a/AppKit/AppKit.j +++ b/AppKit/AppKit.j @@ -32,6 +32,7 @@ @import "CPButtonBar.j" @import "CPCheckBox.j" @import "CPCib.j" +@import "CPCibBindingConnector.j" @import "CPCibConnector.j" @import "CPCibControlConnector.j" @import "CPCibLoading.j" diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index abfad1a7e..bdb3a38cb 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -636,30 +636,49 @@ @end +var CPArrayControllerAvoidsEmptySelection = @"CPArrayControllerAvoidsEmptySelection", + CPArrayControllerClearsFilterPredicateOnInsertion = @"CPArrayControllerClearsFilterPredicateOnInsertion", + CPArrayControllerFilterRestrictsInsertion = @"CPArrayControllerFilterRestrictsInsertion", + CPArrayControllerPreservesSelection = @"CPArrayControllerPreservesSelection", + CPArrayControllerSelectsInsertedObjects = @"CPArrayControllerSelectsInsertedObjects", + CPArrayControllerAlwaysUsesMultipleValuesMarker = @"CPArrayControllerAlwaysUsesMultipleValuesMarker"; + @implementation CPArrayController (CPCoding) - (id)initWithCoder:(CPCoder)aCoder { - self = [super initWithCoder:coder]; + self = [super initWithCoder:aCoder]; if (self) { - _avoidsEmptySelection = [coder decodeBoolForKey:@"CPArrayControllerAvoidsEmptySelection"]; - _clearsFilterPredicateOnInsertion = [coder decodeBoolForKey:@"CPClearsFilterPredicateOnInsertion"]; - _filterRestrictsInsertion = [coder decodeBoolForKey:@"CPArrayControllerFilterRestrictsInsertion"]; - _preservesSelection = [coder decodeBoolForKey:@"CPArrayControllerPreservesSelection"]; - _selectsInsertedObjects = [coder decodeBoolForKey:@"CPArrayControllerSelectsInsertedObjects"]; - _alwaysUsesMultipleValuesMarker = [coder decodeBoolForKey:@"CPArrayControllerAlwaysUsesMultipleValuesMarker"]; + _avoidsEmptySelection = [aCoder decodeBoolForKey:CPArrayControllerAvoidsEmptySelection]; + _clearsFilterPredicateOnInsertion = [aCoder decodeBoolForKey:CPArrayControllerClearsFilterPredicateOnInsertion]; + _filterRestrictsInsertion = [aCoder decodeBoolForKey:CPArrayControllerFilterRestrictsInsertion]; + _preservesSelection = [aCoder decodeBoolForKey:CPArrayControllerPreservesSelection]; + _selectsInsertedObjects = [aCoder decodeBoolForKey:CPArrayControllerSelectsInsertedObjects]; + _alwaysUsesMultipleValuesMarker = [aCoder decodeBoolForKey:CPArrayControllerAlwaysUsesMultipleValuesMarker]; - if ([self automaticallyPreparesContent]) + if (![self content] && [self automaticallyPreparesContent]) [self prepareContent]; - else + else if (![self content]) [self _setContentArray:[]]; } return self; } +- (void)encodeWithCoder:(CPCoder)aCoder +{ + [super encodeWithCoder:aCoder]; + + [aCoder encodeBool:_avoidsEmptySelection forKey:CPArrayControllerAvoidsEmptySelection]; + [aCoder encodeBool:_clearsFilterPredicateOnInsertion forKey:CPArrayControllerClearsFilterPredicateOnInsertion]; + [aCoder encodeBool:_filterRestrictsInsertion forKey:CPArrayControllerFilterRestrictsInsertion]; + [aCoder encodeBool:_preservesSelection forKey:CPArrayControllerPreservesSelection]; + [aCoder encodeBool:_selectsInsertedObjects forKey:CPArrayControllerSelectsInsertedObjects]; + [aCoder encodeBool:_alwaysUsesMultipleValuesMarker forKey:CPArrayControllerAlwaysUsesMultipleValuesMarker]; +} + - (void)awakeFromCib { [self _selectionWillChange]; diff --git a/AppKit/CPObjectController.j b/AppKit/CPObjectController.j index 75c24d07f..190a34b0e 100644 --- a/AppKit/CPObjectController.j +++ b/AppKit/CPObjectController.j @@ -11,6 +11,7 @@ id _selection; Class _objectClass; + CPString _objectClassName; BOOL _isEditable; BOOL _automaticallyPreparesContent; @@ -218,7 +219,8 @@ @end -var CPObjectControllerObjectClassNameKey = @"CPObjectControllerObjectClassNameKey", +var CPObjectControllerContentKey = @"CPObjectControllerContentKey", + CPObjectControllerObjectClassNameKey = @"CPObjectControllerObjectClassNameKey", CPObjectControllerIsEditableKey = @"CPObjectControllerIsEditableKey", CPObjectControllerAutomaticallyPreparesContentKey = @"CPObjectControllerAutomaticallyPreparesContentKey"; @@ -233,11 +235,10 @@ var CPObjectControllerObjectClassNameKey = @"CPObjectControllerOb var objectClassName = [aCoder decodeObjectForKey:CPObjectControllerObjectClassNameKey], objectClass = CPClassFromString(objectClassName); - // FIXME: Error if objectClass === nil - - [self setObjectClass:objectClass]; + [self setObjectClass:objectClass || [CPMutableDictionary class]]; [self setEditable:[aCoder decodeBoolForKey:CPObjectControllerIsEditableKey]]; [self setAutomaticallyPreparesContent:[aCoder decodeBoolForKey:CPObjectControllerAutomaticallyPreparesContentKey] || NO]; + [self setContent:[aCoder decodeObjectForKey:CPObjectControllerContentKey]]; _observedKeys = [[CPCountedSet alloc] init]; } @@ -247,11 +248,15 @@ var CPObjectControllerObjectClassNameKey = @"CPObjectControllerOb - (void)encodeWithCoder:(CPCoder)aCoder { - [aCoder encodeObject:CPStringFromClass(objectClass) forKey:CPObjectControllerObjectClassNameKey]; - [aCoder encodeObject:[self isEditable] forKey:CPObjectControllerIsEditableKey]; + [aCoder encodeObject:[self content] forKey:CPObjectControllerContentKey]; - if (![self automaticallyPreparesContent]) - [aCoder encodeBOOL:YES forKey:CPObjectControllerAutomaticallyPreparesContentKey]; + if (_objectClass) + [aCoder encodeObject:CPStringFromClass(_objectClass) forKey:CPObjectControllerObjectClassNameKey]; + else if (_objectClassName) + [aCoder encodeObject:_objectClassName forKey:CPObjectControllerObjectClassNameKey]; + + [aCoder encodeBool:[self isEditable] forKey:CPObjectControllerIsEditableKey]; + [aCoder encodeBool:[self automaticallyPreparesContent] forKey:CPObjectControllerAutomaticallyPreparesContentKey]; } @end diff --git a/AppKit/Cib/CPCibBindingConnector.j b/AppKit/Cib/CPCibBindingConnector.j new file mode 100644 index 000000000..a37e210fe --- /dev/null +++ b/AppKit/Cib/CPCibBindingConnector.j @@ -0,0 +1,60 @@ +/* + * CPCibBindingConnector.j + * AppKit + * + * Created by Ross Boucher. + * Copyright 2010, 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" + +var CPCibBindingConnectorBindingKey = @"CPCibBindingConnectorBindingKey", + CPCibBindingConnectorKeyPathKey = @"CPCibBindingConnectorKeyPathKey", + CPCibBindingConnectorOptionsKey = @"CPCibBindingConnectorOptionsKey"; + +@implementation CPCibBindingConnector : CPCibConnector +{ + id _binding; + id _keyPath; + id _options; +} + +- (id)initWithCoder:(CPCoder)aCoder +{ + if (self = [super initWithCoder:coder]) + { + _binding = [aCoder decodeObjectForKey:CPCibBindingConnectorBindingKey]; + _keyPath = [aCoder decodeObjectForKey:CPCibBindingConnectorKeyPathKey]; + _options = [aCoder decodeObjectForKey:CPCibBindingConnectorOptionsKey]; + } + + return self; +} + +- (void)encodeWithCoder:(CPCoder)aCoder +{ + [aCoder encodeObject:_binding forKey:CPCibBindingConnectorBindingKey]; + [aCoder encodeObject:_keyPath forKey:CPCibBindingConnectorKeyPathKey]; + [aCoder encodeObject:_options forKey:CPCibBindingConnectorOptionsKey]; +} + +- (void)establishConnection +{ + [_source bind:_binding toObject:_destination withKeyPath:_keyPath options:_options]; +} + +@end diff --git a/AppKit/Cib/_CPCibObjectData.j b/AppKit/Cib/_CPCibObjectData.j index 13e8a0826..5efe4e3ba 100644 --- a/AppKit/Cib/_CPCibObjectData.j +++ b/AppKit/Cib/_CPCibObjectData.j @@ -28,6 +28,7 @@ @import "CPCibConnector.j" @import "CPCibControlConnector.j" @import "CPCibOutletConnector.j" +@import "CPCibBindingConnector.j" @implementation _CPCibObjectData : CPObject diff --git a/Tools/nib2cib/NSArrayController.j b/Tools/nib2cib/NSArrayController.j new file mode 100644 index 000000000..003042f5e --- /dev/null +++ b/Tools/nib2cib/NSArrayController.j @@ -0,0 +1,61 @@ +/* + * NSArrayController.j + * nib2cib + * + * Created by Ross Boucher. + * Copyright 2010, 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 + + +@implementation CPArrayController (NSCoding) + +- (id)NS_initWithCoder:(CPCoder)aCoder +{ + self = [super initWithCoder:aCoder]; + + if (self) + { + _avoidsEmptySelection = [aCoder decodeBoolForKey:@"NSAvoidsEmptySelection"]; + _clearsFilterPredicateOnInsertion = [aCoder decodeBoolForKey:@"NSClearsFilterPredicateOnInsertion"]; + _filterRestrictsInsertion = [aCoder decodeBoolForKey:@"NSFilterRestrictsInsertion"]; + _preservesSelection = [aCoder decodeBoolForKey:@"NSPreservesSelection"]; + _selectsInsertedObjects = [aCoder decodeBoolForKey:@"NSSelectsInsertedObjects"]; + _alwaysUsesMultipleValuesMarker = [aCoder decodeBoolForKey:@"NSAlwaysUsesMultipleValuesMarker"]; + } + + return self; +} + +@end + +@implementation NSArrayController : CPArrayController +{ +} + +- (id)initWithCoder:(CPCoder)aCoder +{ + return [self NS_initWithCoder:aCoder]; +} + +- (Class)classForKeyedArchiver +{ + return [CPArrayController class]; +} + +@end diff --git a/Tools/nib2cib/NSNibConnector.j b/Tools/nib2cib/NSNibConnector.j index 53573a8de..05a8643da 100644 --- a/Tools/nib2cib/NSNibConnector.j +++ b/Tools/nib2cib/NSNibConnector.j @@ -108,3 +108,39 @@ NIB_CONNECTION_EQUIVALENCY_TABLE = {}; } @end + +@implementation CPCibBindingConnector (NSCoding) + +- (id)NS_initWithCoder:(CPCoder)aCoder +{ + self = [super init]; + + if (self) + { + _binding = [aCoder decodeObjectForKey:@"NSBinding"]; + _keyPath = [aCoder decodeObjectForKey:@"NSKeyPath"]; + _options = [aCoder decodeObjectForKey:@"NSOptions"]; + + CPLog.debug(@"Binding Connector: " + [_binding description] + " " + [_keyPath description] + " " + [_options description]); + } + + return self; +} + +@end + +@implementation NSNibBindingConnector : CPCibBindingConnector +{ +} + +- (id)initWithCoder:(CPCoder)aCoder +{ + return [self NS_initWithCoder:aCoder]; +} + +- (Class)classForKeyedArchiver +{ + return [CPCibBindingConnector class]; +} + +@end diff --git a/Tools/nib2cib/NSObjectController.j b/Tools/nib2cib/NSObjectController.j new file mode 100644 index 000000000..1aa435dd0 --- /dev/null +++ b/Tools/nib2cib/NSObjectController.j @@ -0,0 +1,62 @@ +/* + * NSObjectController.j + * nib2cib + * + * Created by Ross Boucher. + * Copyright 2010, 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 + +@implementation CPObjectController (NSCoding) + +- (id)NS_initWithCoder:(CPCoder)aCoder +{ + self = [super init]; + + if (self) + { + _declaredKeys = [aCoder decodeObjectForKey:@"NSDeclaredKeys"]; + _objectClassName = CP_NSMapClassName([aCoder decodeObjectForKey:@"NSObjectClassName"]); + + if (!_objectClassName) + _objectClass = [CPMutableDictionary class]; + + _editable = [aCoder decodeBoolForKey:@"NSEditable"]; + _automaticallyPreparesContent = [aCoder decodeBoolForKey:@"NSAutomaticallyPreparesContent"]; + } + + return self; +} + +@end + +@implementation NSObjectController : CPObjectController +{ +} + +- (id)initWithCoder:(CPCoder)aCoder +{ + return [self NS_initWithCoder:aCoder]; +} + +- (Class)classForKeyedArchiver +{ + return [CPObjectController class]; +} + +@end