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