CPRuleEditor: -bind:toObject:withKeyPath:options: removed a useless check that was causing an exception when keyPath was a compound key path.

CPPredicateEditor test app: bind textfield and editor to a CPObjectController.
This commit is contained in:
cacaodev
2011-12-13 09:54:24 +01:00
parent d973670026
commit 93dc4d1bc9
6 changed files with 178 additions and 50 deletions
+44
View File
@@ -82,6 +82,14 @@
}
/*! @cond */
+ (Class)_binderClassForBinding:(CPString)theBinding
{
if (theBinding == CPValueBinding)
return [CPPredicateEditorValueBinder class];
return [super _binderClassForBinding:theBinding];
}
- (void)_initRuleEditorShared
{
[super _initRuleEditorShared];
@@ -514,4 +522,40 @@ var CPPredicateTemplatesKey = @"CPPredicateTemplates";
@end
@implementation CPPredicateEditorValueBinder : CPBinder
{
}
// temporary fix. See https://github.com/cacaodev/cappuccino/commits/CPKeyValueBinding for CPbinder fix.
- (void)setValueFor:(CPString)aBinding
{
var destination = [_info objectForKey:CPObservedObjectKey],
keyPath = [_info objectForKey:CPObservedKeyPathKey],
options = [_info objectForKey:CPOptionsKey],
newValue = [destination valueForKeyPath:keyPath];
var isMarker = CPIsControllerMarker(newValue);
if (isMarker)
newValue = [options objectForKey:newValue];
if (!isMarker || newValue == nil)
newValue = [self transformValue:newValue withOptions:options];
[_source setValue:newValue forKey:aBinding];
}
- (void)reverseSetValueFor:(CPString)aBinding
{
var destination = [_info objectForKey:CPObservedObjectKey],
keyPath = [_info objectForKey:CPObservedKeyPathKey],
options = [_info objectForKey:CPOptionsKey],
newValue = [_source valueForKeyPath:aBinding];
newValue = [self reverseTransformValue:newValue withOptions:options];
[destination removeObserver:self forKeyPath:keyPath];
[destination setValue:newValue forKeyPath:keyPath];
[destination addObserver:self forKeyPath:keyPath options:CPKeyValueObservingOptionNew context:aBinding];
}
@end
/*! @endcond */
+20 -23
View File
@@ -1657,25 +1657,22 @@ TODO: implement
}
}
- (void)bind:(CPString)binding toObject:(id)observableController withKeyPath:(CPString)keyPath options:(CPDictionary)options
- (void)bind:(CPString)aBinding toObject:(id)observableController withKeyPath:aKeyPath options:options
{
if (keyPath == nil || [observableController valueForKey:keyPath] == nil)
{
[CPException raise:CPInvalidArgumentException reason:"Keypath or bound object cannot be nil"];
return;
}
if ([binding isEqualToString:@"rows"])
{
if ([observableController respondsToSelector:@selector(objectClass)])
_rowClass = [observableController objectClass];
[self _setBoundDataSource:observableController withKeyPath:keyPath options:options];
}
else if ([binding isEqualToString:CPValueBinding])
[super bind:binding toObject:observableController withKeyPath:keyPath options:options];
else
[CPException raise:CPInvalidArgumentException reason:"Keypath or bound object cannot be nil"];
if ([aBinding isEqualToString:@"rows"])
{
[self unbind:aBinding];
[self _setBoundDataSource:observableController withKeyPath:aKeyPath options:options];
[_rowCache removeAllObjects];
[_slices removeAllObjects];
var newRows = [CPArray array];
var oldRows = [self _rootRowsArray];
[self _changedRowArray:newRows withOldRowArray:oldRows forParent:_boundArrayOwner];
}
else
[super bind:aBinding toObject:observableController withKeyPath:aKeyPath options:options];
}
- (void)unbind:(id)object
@@ -1686,18 +1683,18 @@ TODO: implement
- (void)_setBoundDataSource:(id)datasource withKeyPath:(CPString)keyPath options:(CPDictionary)options
{
if (_boundArrayOwner != nil)
[_boundArrayOwner removeObserver:self forKeyPath:_boundArrayKeyPath];
if ([observableController respondsToSelector:@selector(objectClass)])
_rowClass = [observableController objectClass];
_boundArrayKeyPath = keyPath;
_boundArrayOwner = datasource;
var boundRows = [_boundArrayOwner valueForKey:_boundArrayKeyPath];
//var boundRows = [_boundArrayOwner valueForKey:_boundArrayKeyPath];
[_boundArrayOwner addObserver:self forKeyPath:_boundArrayKeyPath options:CPKeyValueObservingOptionOld|CPKeyValueObservingOptionNew context:boundArrayContext];
if ([boundRows isKindOfClass:[CPArray class]] && [boundRows count] > 0)
[_boundArrayOwner setValue:boundRows forKey:_boundArrayKeyPath];
//if ([boundRows isKindOfClass:[CPArray class]] && [boundRows count] > 0)
// [_boundArrayOwner setValue:boundRows forKey:_boundArrayKeyPath];
}
- (void)_setPredicate:(CPPredicate)predicate
@@ -7,12 +7,12 @@
*/
@import <AppKit/CPScrollView.j>
@import "Predicatetransformer.j"
@implementation AppController : CPObject
{
CPWindow window;
CPPredicateEditor predicateEditor;
CPTextField predicateField;
CPTableView leftTable;
CPTableView rightTable;
@@ -25,6 +25,12 @@
CPMutableArray rightConstants;
}
+ (void)initialize
{
var transformer = [[PredicateTransformer alloc] init];
[CPValueTransformer setValueTransformer:transformer forName:@"PredicateTransformer"];
}
- (void)awakeFromCib
{
operators = [CPMutableArray new];
@@ -38,17 +44,9 @@
[window setFullBridge:YES];
}
- (IBAction)displayPredicate:(id)sender
{
var predicate = [CPPredicate predicateWithFormat:[sender stringValue]];
if (predicate)
[predicateEditor setObjectValue:predicate];
}
- (IBAction)predicateEditorAction:(id)sender
{
CPLogConsole(_cmd);
[predicateField setStringValue:[sender objectValue]];
}
- (void)ruleEditorRowsDidChange:(CPNotification)notification
@@ -0,0 +1,53 @@
//
// PredicateTransformer.m
// PredicateEditor
//
// Created by x on 22/05/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
@implementation PredicateTransformer : CPValueTransformer
{
}
+ (Class)transformedValueClass
{
return [CPString class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (CPString)transformedValue:(CPPredicate)predicate
{
if (predicate == nil || [predicate isEqual:[CPNull null]])
return @"";
return [predicate predicateFormat];
}
- (CPPredicate)reverseTransformedValue:(CPString)aValue
{
if (aValue == nil || [aValue isEqualToString:@""])
return nil;
var predicate;
try
{
predicate = [CPPredicate predicateWithFormat:aValue];
}
catch (e)
{
CPLogConsole(e);
predicate = nil;
}
finally
{
return predicate;
}
}
@end
File diff suppressed because one or more lines are too long
@@ -12,6 +12,7 @@
</object>
<array key="IBDocument.IntegratedClassDependencies">
<string>NSPopUpButtonCell</string>
<string>NSObjectController</string>
<string>NSScroller</string>
<string>NSPredicateEditor</string>
<string>NSMenuItem</string>
@@ -1148,7 +1149,7 @@
<object class="NSTextField" id="884566601">
<reference key="NSNextResponder" ref="439893737"/>
<int key="NSvFlags">270</int>
<string key="NSFrame">{{20, 476}, {611, 22}}</string>
<string key="NSFrame">{{20, 478}, {611, 22}}</string>
<reference key="NSSuperview" ref="439893737"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="869316024"/>
@@ -2719,6 +2720,14 @@
<object class="NSCustomObject" id="773916665">
<string key="NSClassName">AppController</string>
</object>
<object class="NSObjectController" id="958965449">
<array class="NSMutableArray" key="NSDeclaredKeys">
<string>predicate</string>
</array>
<bool key="NSEditable">YES</bool>
<bool key="NSAutomaticallyPreparesContent">YES</bool>
<object class="_NSManagedProxy" key="_NSManagedProxy"/>
</object>
</array>
<object class="IBObjectContainer" key="IBDocument.Objects">
<array class="NSMutableArray" key="connectionRecords">
@@ -3418,29 +3427,50 @@
</object>
<int key="connectionID">1345</int>
</object>
<object class="IBConnectionRecord">
<object class="IBBindingConnection" key="connection">
<string key="label">value: selection.predicate</string>
<reference key="source" ref="407607490"/>
<reference key="destination" ref="958965449"/>
<object class="NSNibBindingConnector" key="connector">
<reference key="NSSource" ref="407607490"/>
<reference key="NSDestination" ref="958965449"/>
<string key="NSLabel">value: selection.predicate</string>
<string key="NSBinding">value</string>
<string key="NSKeyPath">selection.predicate</string>
<int key="NSNibBindingConnectorVersion">2</int>
</object>
</object>
<int key="connectionID">1435</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">predicateEditorAction:</string>
<reference key="source" ref="773916665"/>
<reference key="destination" ref="407607490"/>
</object>
<int key="connectionID">1409</int>
<int key="connectionID">1436</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">displayPredicate:</string>
<reference key="source" ref="773916665"/>
<reference key="destination" ref="884566601"/>
<object class="IBBindingConnection" key="connection">
<string key="label">value: selection.predicate</string>
<reference key="source" ref="884566601"/>
<reference key="destination" ref="958965449"/>
<object class="NSNibBindingConnector" key="connector">
<reference key="NSSource" ref="884566601"/>
<reference key="NSDestination" ref="958965449"/>
<string key="NSLabel">value: selection.predicate</string>
<string key="NSBinding">value</string>
<string key="NSKeyPath">selection.predicate</string>
<dictionary key="NSOptions">
<boolean value="NO" key="NSAllowsEditingMultipleValuesSelection"/>
<boolean value="NO" key="NSConditionallySetsEditable"/>
<string key="NSValueTransformerName">PredicateTransformer</string>
</dictionary>
<int key="NSNibBindingConnectorVersion">2</int>
</object>
</object>
<int key="connectionID">1412</int>
</object>
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">predicateField</string>
<reference key="source" ref="773916665"/>
<reference key="destination" ref="884566601"/>
</object>
<int key="connectionID">1413</int>
<int key="connectionID">1437</int>
</object>
</array>
<object class="IBMutableOrderedSet" key="objectRecords">
@@ -3999,11 +4029,11 @@
<int key="objectID">372</int>
<reference key="object" ref="439893737"/>
<array class="NSMutableArray" key="children">
<reference ref="884566601"/>
<reference ref="869316024"/>
<reference ref="411662527"/>
<reference ref="196312022"/>
<reference ref="738552837"/>
<reference ref="884566601"/>
</array>
<reference key="parent" ref="972006081"/>
</object>
@@ -4755,6 +4785,11 @@
<reference key="object" ref="942261832"/>
<reference key="parent" ref="586880274"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">1414</int>
<reference key="object" ref="958965449"/>
<reference key="parent" ref="0"/>
</object>
</array>
</object>
<dictionary class="NSMutableDictionary" key="flattenedProperties">
@@ -4837,6 +4872,7 @@
<string key="1343.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="136.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<integer value="1" key="136.ImportedFromIB2"/>
<string key="1414.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<string key="143.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
<integer value="1" key="143.ImportedFromIB2"/>
<string key="144.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
@@ -5036,7 +5072,7 @@
<nil key="activeLocalization"/>
<dictionary class="NSMutableDictionary" key="localizations"/>
<nil key="sourceID"/>
<int key="maxID">1413</int>
<int key="maxID">1437</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">