libs-gui/Source/NSDictionaryController.m

277 lines
5.9 KiB
Mathematica
Raw Normal View History

/* Implementation of class NSDictionaryController
Copyright (C) 2021 Free Software Foundation, Inc.
By: Gregory John Casamento
Date: 16-10-2021
This file is part of the GNUstep Library.
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 USA.
*/
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSKeyValueObserving.h>
2021-10-19 17:07:22 +00:00
#import <Foundation/NSIndexSet.h>
#import <Foundation/NSException.h>
#import "AppKit/NSDictionaryController.h"
#import "AppKit/NSKeyValueBinding.h"
2021-10-19 17:07:22 +00:00
#import "GSBindingHelpers.h"
#import "GSFastEnumeration.h"
@implementation NSDictionaryController
+ (void) initialize
{
if (self == [NSDictionaryController class])
{
[self exposeBinding: NSContentDictionaryBinding];
[self exposeBinding: NSIncludedKeysBinding];
//[self exposeBinding: NSExcludedKeysBinding];
//[self exposeBinding: NSInitialKeyBinding];
//[self exposeBinding: NSInitialValueBinding];
//[self setKeys: [NSArray arrayWithObjects: NSContentBinding, NSContentObjectBinding, nil]
// triggerChangeNotificationsForDependentKey: @"arrangedObjects"];
}
}
2021-10-16 17:14:14 +00:00
- (NSDictionaryControllerKeyValuePair *) newObject
{
NSDictionaryControllerKeyValuePair *o = [[NSDictionaryControllerKeyValuePair alloc] init];
NSString *k = [NSString stringWithFormat: @"%@%lu", _initialKey, _count];
NSString *v = [NSString stringWithFormat: @"%@%lu", _initialValue, _count];
[o setKey: k];
[o setValue: v];
_count++;
AUTORELEASE(o);
return o;
2021-10-16 17:14:14 +00:00
}
- (NSString *) initialKey
{
return [_initialKey copy];
2021-10-16 17:14:14 +00:00
}
- (void) setInitialKey: (NSString *)key
{
ASSIGN(_initialKey, key);
2021-10-16 17:14:14 +00:00
}
- (id) initialValue
{
return [_initialValue copy];
2021-10-16 17:14:14 +00:00
}
- (void) setInitialValue: (id)value
{
ASSIGN(_initialValue, value);
2021-10-16 17:14:14 +00:00
}
- (NSArray *) includedKeys
{
return [_includedKeys copy];
2021-10-16 17:14:14 +00:00
}
- (void) setIncludedKeys: (NSArray *)includedKeys
{
ASSIGN(_includedKeys, includedKeys);
2021-10-16 17:14:14 +00:00
}
- (NSArray *) excludedKeys
{
return [_excludedKeys copy];
2021-10-16 17:14:14 +00:00
}
- (void) setExcludedKeys: (NSArray *)excludedKeys
{
ASSIGN(_excludedKeys, excludedKeys);
2021-10-16 17:14:14 +00:00
}
- (NSDictionary *) localizedKeyDictionary
{
return [_localizedKeyDictionary copy];
2021-10-16 17:14:14 +00:00
}
- (void) setLocalizedKeyDictionary: (NSDictionary *)dict
{
ASSIGN(_localizedKeyDictionary, dict);
2021-10-16 17:14:14 +00:00
}
- (NSString *) localizedKeyTable
{
return [_localizedKeyTable copy];
2021-10-16 17:14:14 +00:00
}
- (void) setLocalizedKeyTable: (NSString *)keyTable
{
ASSIGN(_localizedKeyTable, keyTable);
2021-10-16 17:14:14 +00:00
}
- (NSArray *) _buildArray: (NSDictionary *)content
{
NSArray *allKeys = [content allKeys];
NSMutableArray *result = [NSMutableArray arrayWithCapacity: [allKeys count]];
NSLog(@"includedKeys = %@", _includedKeys);
FOR_IN(id, k, allKeys)
{
NSDictionaryControllerKeyValuePair *kvp =
AUTORELEASE([[NSDictionaryControllerKeyValuePair alloc] init]);
id v = [[self content] objectForKey: k];
[kvp setKey: k];
[kvp setValue: v];
if (![_excludedKeys containsObject: k])
{
[kvp setExplicitlyIncluded: NO];
}
[result addObject: kvp];
}
END_FOR_IN(allKeys);
return result;
}
- (void) setContent: (id)content
2021-10-19 17:07:22 +00:00
{
[super setContent: [self _buildArray: content]];
}
- (void) observeValueForKeyPath: (NSString*)aPath
ofObject: (id)anObject
change: (NSDictionary*)aChange
context: (void*)aContext
{
[NSException raise: NSInvalidArgumentException
format: @"-%@ cannot be sent to %@ ..."
@" create an instance overriding this",
NSStringFromSelector(_cmd), NSStringFromClass([self class])];
return;
}
- (void) bind: (NSString *)binding
toObject: (id)anObject
withKeyPath: (NSString *)keyPath
options: (NSDictionary *)options
{
if ([binding isEqual: NSContentDictionaryBinding])
{
GSKeyValueBinding *kvb;
[self unbind: binding];
kvb = [[GSKeyValueBinding alloc] initWithBinding: @"content"
withName: binding
toObject: anObject
withKeyPath: keyPath
options: options
fromObject: self];
// The binding will be retained in the binding table
RELEASE(kvb);
}
else
{
[super bind: binding
toObject: anObject
withKeyPath: keyPath
options: options];
}
}
- (Class) valueClassForBinding: (NSString *)binding
{
if ([binding isEqual: NSContentDictionaryBinding])
{
return [NSDictionary class];
}
else
{
return [super valueClassForBinding: binding];
}
}
@end
@implementation NSDictionaryControllerKeyValuePair
- (instancetype) init
{
self = [super init];
if (self != nil)
{
_key = nil;
_value = nil;
_localizedKey = nil;
_explicitlyIncluded = YES;
}
return self;
}
/**
* Returns a copy of the key
*/
- (NSString *) key
{
return [_key copy];
}
- (void) setKey: (NSString *)key
{
ASSIGN(_key, key);
}
/**
* Returns a strong reference to the value
*/
- (id) value
{
return [_value copy];
}
- (void) setValue: (id)value
{
ASSIGN(_value, value);
}
- (NSString *) localizedKey
{
return [_localizedKey copy];
}
- (void) setLocalizedKey: (NSString *)localizedKey
{
ASSIGN(_localizedKey, localizedKey);
}
- (BOOL) isExplicitlyIncluded
{
return _explicitlyIncluded;
}
- (void) setExplicitlyIncluded: (BOOL)flag
{
_explicitlyIncluded = flag;
}
@end