Update add method, fix white space issue, remove duplicate methods

This commit is contained in:
Gregory John Casamento 2024-07-02 04:05:53 -04:00
parent ba9c12ddf8
commit ebf6bdb8d2
5 changed files with 72 additions and 54 deletions

View file

@ -281,6 +281,9 @@ APPKIT_EXPORT_CLASS
/**
* Sets the count key path. This needs to be key-value compliant.
* Setting this key path will disable add:, addChild:, remove:,
* removeChild:, and insert: methods. If this is not specified,
* the tree controller is in "object" mode.
*/
- (void) setCountKeyPath: (NSString *)path;
@ -332,35 +335,11 @@ APPKIT_EXPORT_CLASS
- (void) moveNodes: (NSArray *)nodes toIndexPath: (NSIndexPath *)startingIndexPath;
/**
* Array containing all selected nodes
* Set the descriptors by which the content of this tree controller
* is sorted.
*/
- (void) setSortDescriptors: (NSArray *)descriptors;
/**
* children key path for the given NSTreeNode.
*/
- (NSString*) childrenKeyPathForNode: (NSTreeNode *)node;
/**
* count key path for the given NSTreeNode.
*/
- (NSString*) countKeyPathForNode: (NSTreeNode *)node;
/**
* leaf key path for the given NSTreeNode.
*/
- (NSString*) leafKeyPathForNode: (NSTreeNode *)node;
/**
* Moves node to given indexPath
*/
- (void) moveNode: (NSTreeNode *)node toIndexPath: (NSIndexPath *)indexPath;
/**
* Move nodes to position at startingIndexPath
*/
- (void) moveNodes: (NSArray *)nodes toIndexPath: (NSIndexPath *)startingIndexPath;
/**
* Array containing all selected nodes
*/

View file

@ -38,10 +38,13 @@ extern "C" {
NSTreeController *_controller;
}
+ (NSMutableDictionary *) dictionaryWithChildren: (NSMutableArray *)children;
- (instancetype) initWithRepresentedObject: (id)representedObject
withController: (id)controller;
- (NSUInteger) count;
- (NSMutableArray *) childNodes;
@end
#if defined(__cplusplus)

View file

@ -33,6 +33,13 @@
@implementation GSControllerTreeProxy
+ (NSMutableDictionary *) dictionaryWithChildren: (NSMutableArray *)children
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObject: children
forKey: @"children"];
return dictionary;
}
- (instancetype) initWithRepresentedObject: (id)representedObject
withController: (id)controller
{
@ -46,12 +53,17 @@
- (NSUInteger) count
{
NSDictionary *ro = [self representedObject];
NSArray *children = [ro objectForKey: @"children"];
NSArray *children = [[self representedObject] objectForKey: @"children"];
return [children count];
}
- (NSMutableArray *) childNodes
{
NSDictionary *ro = [self representedObject];
NSMutableArray *children = [ro objectForKey: @"children"];
return children;
}
- (id) value
{
return nil;

View file

@ -99,6 +99,8 @@
{
if (self == [NSObjectController class])
{
[self setVersion: 1];
[self exposeBinding: NSContentObjectBinding];
[self setKeys: [NSArray arrayWithObject: @"editable"]
triggerChangeNotificationsForDependentKey: @"canAdd"];
@ -155,11 +157,13 @@
[coder encodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
[coder encodeValueOfObjCType: @encode(BOOL) at: &_automatically_prepares_content];
[coder encodeConditionalObject: _managed_proxy];
[coder encodeObject: NSStringFromClass([self objectClass])];
}
}
- (id) initWithCoder: (NSCoder *)coder
{
{
int version = [coder versionForClassName: @"NSObjectController"];
if ((self = [super initWithCoder: coder]) != nil)
{
if ([self automaticallyPreparesContent])
@ -190,6 +194,11 @@
[coder decodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
[coder decodeValueOfObjCType: @encode(BOOL) at: &_automatically_prepares_content];
ASSIGN(_managed_proxy, [coder decodeObject]);
if (version > 0)
{
NSString *className = [coder decodeObject];
[self setObjectClass: NSClassFromString(className)];
}
}
}

View file

@ -57,20 +57,27 @@
}
}
- (void) _initDefaults
{
_childrenKeyPath = nil;
_countKeyPath = nil;
_leafKeyPath = nil;
_sortDescriptors = nil;
_selection_index_paths = [[NSMutableArray alloc] init];
_canInsert = YES;
_canInsertChild = YES;
_canAddChild = YES;
[self setObjectClass: [NSMutableDictionary class]];
}
- (id) initWithContent: (id)content
{
NSLog(@"Content = %@", content);
if ((self = [super initWithContent: content]) != nil)
{
_childrenKeyPath = nil;
_countKeyPath = nil;
_leafKeyPath = nil;
_sortDescriptors = nil;
_selection_index_paths = [[NSMutableArray alloc] init];
_canInsert = YES;
_canInsertChild = YES;
_canAddChild = YES;
[self _initDefaults];
}
return self;
@ -184,8 +191,7 @@
if ([_content isKindOfClass: [NSArray class]])
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObject: _content
forKey: @"children"];
NSMutableDictionary *dictionary = [GSControllerTreeProxy dictionaryWithChildren: _content];
_arranged_objects = [[GSControllerTreeProxy alloc] initWithRepresentedObject: dictionary
withController: self];
}
@ -233,29 +239,37 @@
- (void) add: (id)sender
{
if ([self canAddChild])
if ([self canAddChild]
&& [self countKeyPath] == nil)
{
id new = [self newObject];
id newObject = [self newObject];
[self addChild: new];
RELEASE(new);
if (newObject != nil)
{
NSMutableArray *newContent = [NSMutableArray arrayWithArray: [self content]];
GSControllerTreeProxy *node = [[GSControllerTreeProxy alloc]
initWithRepresentedObject: newObject
withController: self];
[newContent addObject: node];
[self setContent: newContent];
RELEASE(newObject);
}
}
}
- (void) addChild: (id)obj
{
GSKeyValueBinding *theBinding;
[self setContent: obj];
theBinding = [GSKeyValueBinding getBinding: NSContentObjectBinding
forObject: self];
if (theBinding != nil)
[theBinding reverseSetValueFor: @"content"];
if ([self canAddChild]
&& [self countKeyPath] == nil)
{
}
}
- (void) remove: (id)sender
{
if ([self canRemove])
if ([self canRemove]
&& [self countKeyPath] == nil)
{
[self removeObject: [self content]];
}
@ -408,6 +422,7 @@
if (self != nil)
{
[self _initDefaults]; // set up default values...
if ([coder allowsKeyedCoding])
{
// These names do not stick to convention. Usually it would be