Saturday, August 4, 2012

Implementation of a tree structure in OSX - Step 3 (NSOutlineView, NSTreeController, NSTreeNode)


Continuation of the tree structure implementation ..

Now we have to add a new class to create nodes for the tree structure.
We shall create a new Objective-C class named "TreeNode". The steps are shown in the screen shots below.






















You can see the New class in the project navigator bar.


Now it's time to add relevant codes and make the connections.

Open the newly created TreeNode.h file.
Add the following code.

<TreeNode.h>
 #import <Foundation/Foundation.h>  
 @interface TreeNode : NSObject{  
   NSString *commandName;  
   NSInteger commandLevel;  
 }  
 @property NSString * commandName;  
 @property NSInteger commandLevel;  
 +(TreeNode *) commandDataName:(NSString *)commandName commandLevel:(NSInteger)commandLevel;  
 @end  

<TreeNode.m>
 #import "TreeNode.h"  
 @implementation TreeNode  
 @synthesize commandName;  
 @synthesize commandLevel;  
 +(TreeNode *) commandDataName:(NSString *)commandName commandLevel:(NSInteger)commandLevel{  
   TreeNode *treeNode = [[TreeNode alloc]init];  
   treeNode.commandName = commandName;  
   treeNode.commandLevel = commandLevel;  
   return treeNode;  
 }  
 @end  

Now command node creation in the AppDelagate.m class. (We added necessary codes for the header file in the previous article)

<AppDelegate.m>
 #import "AppDelegate.h"  
 #import "TreeNode.h"  
 NSTreeNode *mainCommand;  
 NSTreeNode *subCommand1;  
 NSTreeNode *subCommand2;  
 static NSArray * generateCommandTree(){  
   TreeNode *treeNode = [TreeNode commandDataName:@"name" commandLevel:0];  
   NSMutableArray *rootNodes = [NSMutableArray array];  
   treeNode = [TreeNode commandDataName:@"Command 1" commandLevel:0];  
   mainCommand = [NSTreeNode treeNodeWithRepresentedObject:treeNode];  
   treeNode = [TreeNode commandDataName:@"Command 1.1" commandLevel:1];  
   subCommand1 = [NSTreeNode treeNodeWithRepresentedObject:treeNode];   
   [[mainCommand mutableChildNodes]addObject:subCommand1];  
   treeNode = [TreeNode commandDataName:@"Command 1.1.1" commandLevel:2];  
   subCommand2 = [NSTreeNode treeNodeWithRepresentedObject:treeNode];   
   [[subCommand1 mutableChildNodes]addObject:subCommand2];  
   treeNode = [TreeNode commandDataName:@"Command 1.2" commandLevel:1];  
   subCommand1 = [NSTreeNode treeNodeWithRepresentedObject:treeNode];   
   [[mainCommand mutableChildNodes]addObject:subCommand1];  
   [rootNodes addObject:mainCommand];  
   return rootNodes;  
 }  
 @implementation AppDelegate  
 @synthesize window = _window;  
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification  
 {  
   // Insert code here to initialize your application  
 }  
 - (void)awakeFromNib{  
   [treeController setContent:generateCommandTree()];  
   [outlineView reloadData];  
 }  
 @end  

Now we have completed adding necessary codes to the project.
Shall we create necessary bindings in the next article .....

No comments:

Post a Comment