Re: Placing SimpleTreeData for NSOutline on pasteboard
Re: Placing SimpleTreeData for NSOutline on pasteboard
- Subject: Re: Placing SimpleTreeData for NSOutline on pasteboard
- From: Quentin Mathé <email@hidden>
- Date: Tue, 15 Apr 2003 12:12:25 +0200
Le vendredi, 11 avr 2003, ` 03:11 Europe/Paris, Renaud Boisjoly a icrit
:
I've been playing around with the TreeData and SimpleTree stuff from
Apple's DragNDropOutlineView example.
Boy, is it confusing! Well ok, I'm not a very high-level programmer,
but I find it confusing. There are four pieces in all this,
SimpleTreeNode which seems to be a subclass of TreeNode
SimpleNoteData which looks like a subclass of TreeNodeData
TreeNode and TreeNodeData
i read on this list that some people prefer to create their data
structures otherwise. Does any one know where I could read some good
stuff on doing this? any approaches which would make more sense?
The issue I have is that I can get all this to work within a single
document, but in order to support the Pasteboard between documents, I
need to archive all this using NSArchiver and NSUnarchiver, but that
too seems to be complicated because of this different structure.
has anyone built a simple tree structure for storing outline data
which is simpler to use and allows Archiving in a simpler manner? Or
know of another example which could help me understand all this?
The DragNDropOutlineView example is poorly written. The best solution
is to create you own tree structure To know how to do it, you could
read books on data structures and take a look at the Composite pattern
(in the Design Patterns book by Gamma and the others).
Here is a tree structure example I have written :
#import <Foundation/Foundation.h>
extern BOOL isLimitedEncoding;
@interface Tree : NSObject {
NSMutableArray *children;
Tree *parent;
id data;
}
- (id)initWithTreeData:(id)something;
- (id)initWithCoder:(NSCoder *)coder;
- (void)encodeWithCoder:(NSCoder *)coder;
- (id)childAtIndex:(int)index;
- (NSArray *)children;
- (void)addChild:(id)child;
- (void)addChildren:(NSArray *)childrenToAdd;
- (void)removeChildAtIndex:(int)index;
- (void)removeChild:(Tree *)child;
- (void)removeChildren;
- (int)childrenCount;
- (void)setParent:(Tree *)parentTemp;
- (Tree *)parent;
- (id)data;
@end
***
#import "Tree.h"
@implementation Tree
- (id)init {
return [self initWithTree
Data:nil];
}
- (id)initWithTreeData:(id)something {
if (self = [super init]) {
data = [something retain];
children = [[NSMutableArray alloc] init];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder {
[super init];
children = [[coder decodeObject] retain];
NSLog(@"Decode children");
data = [[coder decodeObject] retain];
NSLog(@"Decode data");
if (children == nil) children = [[NSMutableArray alloc] init];
[children makeObjectsPerformSelector:@selector(setParent:)
withObject:self];
NSLog(@"%@ decoded", data);
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:children];
NSLog(@"Encode children");
[coder encodeObject:data];
NSLog(@"Encode data");
NSLog(@"%@ encoded", data);
}
- (BOOL)isEqual:(id)object {
if ([[self data] isEqual:[object data]]) {
return YES;
}
else return NO;
}
- (id)childAtIndex:(int)index {
return [children objectAtIndex:index];
}
- (NSArray *)children {
return children;
}
- (void)addChild:(id)child {
[children addObject:child];
[child setParent:self];
}
- (void)addChildren:(NSArray *)childrenToAdd {
[children makeObjectsPerformSelector:@selector(setParent:)
withObject:self];
[children addObjectsFromArray:childrenToAdd];
}
- (void)removeChildAtIndex:(int)index {
[children removeObjectAtIndex:index];
}
- (void)removeChild:(Tree *)child {
[children removeObject:child];
}
- (void)removeChildren {
[children release];
children = [[NSMutableArray alloc] init];
}
- (int)childrenCount {
return [children count];
}
- (void)setParent:(Tree *)parentTemp {
[parentTemp retain];
[parent release];
parent = parentTemp;
}
- (Tree *)parent {
return parent;
}
- (id)data {
return data;
}
- (void)dealloc {
[children release];
[parent release];
[data release];
[super dealloc];
}
- (NSString *)description {
id parentDescription = [parent description];
NSMutableString *description = [[NSMutableString alloc]
initWithCapacity:500];
if ([data isKindOfClass:[NSString class]]) {
if ([parentDescription isKindOfClass:[NSString class]]) {
[description setString:parentDescription];
[description appendString:@" - "];
if (data != nil) [description appendString:data];
return description;
}
else return data;
}
else return @"";
}
@end
That's it.
Quentin.
--
Quentin Mathi
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.