Skip to content

Commit

Permalink
CPBinder: move placeholder handling to superclass, add 2 subclassable…
Browse files Browse the repository at this point in the history
… methods for appying the regular value or a placeholder value.

Update binder subclasses for CPTextField, CPCheckBox, CPColorWeel.
  • Loading branch information
cacaodev committed Apr 10, 2012
1 parent 1e2964a commit f222d7b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 60 deletions.
39 changes: 7 additions & 32 deletions AppKit/CPCheckBox.j
Original file line number Diff line number Diff line change
Expand Up @@ -110,40 +110,15 @@ CPCheckBoxImageOffset = 4.0;
[self _setPlaceholder:CPOffState forMarker:CPNullMarker isDefault:YES];
}

- (void)setValueFor:(CPString)theBinding
- (void)setPlaceholderValue:(id)aValue withMarker:(CPString)aMarker forBinding:(CPString)aBinding
{
var destination = [_info objectForKey:CPObservedObjectKey],
keyPath = [_info objectForKey:CPObservedKeyPathKey],
options = [_info objectForKey:CPOptionsKey],
newValue = [destination valueForKeyPath:keyPath],
isPlaceholder = CPIsControllerMarker(newValue);

if (isPlaceholder)
{
if (newValue === CPNotApplicableMarker && [options objectForKey:CPRaisesForNotApplicableKeysBindingOption])
{
[CPException raise:CPGenericException
reason:@"can't transform non applicable key on: " + _source + " value: " + newValue];
}

newValue = [self _placeholderForMarker:newValue];

if (newValue === CPMixedState)
{
[_source setAllowsMixedState:YES];
}
else
{
// Cocoa will always set allowsMixedState to NO
// This behavior will be fine for Cappuccino as well if we (like Cocoa)
// default the CPConditionallySetsEnabledBindingOption to YES
[_source setAllowsMixedState:NO];
}
}
else
newValue = [self transformValue:newValue withOptions:options];
[_source setAllowsMixedState:(aValue === CPMixedState)];
[_source setState:aValue];
}

[_source setState:newValue];
- (void)setValue:(id)aValue forBinding:(CPString)aBinding
{
[_source setState:aValue];
}

@end
33 changes: 29 additions & 4 deletions AppKit/CPKeyValueBinding.j
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,40 @@ var CPBindingOperationAnd = 0,
return self;
}

- (void)setValueFor:(CPString)aBinding
- (void)setValueFor:(CPString)theBinding
{
var destination = [_info objectForKey:CPObservedObjectKey],
keyPath = [_info objectForKey:CPObservedKeyPathKey],
options = [_info objectForKey:CPOptionsKey],
newValue = [destination valueForKeyPath:keyPath];
newValue = [destination valueForKeyPath:keyPath],
isPlaceholder = CPIsControllerMarker(newValue);

if (isPlaceholder)
{
if (newValue === CPNotApplicableMarker && [options objectForKey:CPRaisesForNotApplicableKeysBindingOption])
{
[CPException raise:CPGenericException
reason:@"Can't transform non applicable key on: " + _source + " Key Path:" + keyPath + " value: " + newValue];
}

var value = [self _placeholderForMarker:newValue];
[self setPlaceholderValue:value withMarker:newValue forBinding:theBinding];
}
else
{
var value = [self transformValue:newValue withOptions:options];
[self setValue:value forBinding:theBinding];
}
}

newValue = [self transformValue:newValue withOptions:options];
[_source setValue:newValue forKey:aBinding];
- (void)setPlaceholderValue:(id)aValue withMarker:(CPString)aMarker forBinding:(CPString)aBinding
{
[_source setValue:aValue forKey:aBinding];
}

- (void)setValue:(id)aValue forBinding:(CPString)aBinding
{
[_source setValue:aValue forKey:aBinding];
}

- (void)reverseSetValueFor:(CPString)aBinding
Expand Down
31 changes: 7 additions & 24 deletions AppKit/CPTextField.j
Original file line number Diff line number Diff line change
Expand Up @@ -1521,32 +1521,15 @@ var CPTextFieldIsEditableKey = "CPTextFieldIsEditableKey",
[self _setPlaceholder:@"" forMarker:CPNullMarker isDefault:YES];
}

- (void)setValueFor:(CPString)theBinding
- (void)setPlaceholderValue:(id)aValue withMarker:(CPString)aMarker forBinding:(CPString)aBinding
{
var destination = [_info objectForKey:CPObservedObjectKey],
keyPath = [_info objectForKey:CPObservedKeyPathKey],
options = [_info objectForKey:CPOptionsKey],
newValue = [destination valueForKeyPath:keyPath],
isPlaceholder = CPIsControllerMarker(newValue);

if (isPlaceholder)
{
if (newValue === CPNotApplicableMarker && [options objectForKey:CPRaisesForNotApplicableKeysBindingOption])
{
[CPException raise:CPGenericException
reason:@"can't transform non applicable key on: " + _source + " value: " + newValue];
}

newValue = [self _placeholderForMarker:newValue];
[_source setPlaceholderString:aValue];
[_source setObjectValue:nil];
}

[_source setPlaceholderString:newValue];
[_source setObjectValue:nil];
}
else
{
newValue = [self transformValue:newValue withOptions:options];
[_source setObjectValue:newValue];
}
- (void)setValue:(id)aValue forBinding:(CPString)aBinding
{
[_source setObjectValue:aValue];
}

@end
Expand Down

0 comments on commit f222d7b

Please sign in to comment.