forked from mikestew/Custom-OutlineView-Cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutlineDelegate.m
162 lines (137 loc) · 3.7 KB
/
OutlineDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// OutlineDelegate.m
// CustomOutlineViewCells
//
// Created by Mike Stewart on 1/24/11.
// Copyright 2011 Two Dogs Software, LLC. All rights reserved.
//
#import "OutlineDelegate.h"
#import "Group.h"
#import "OutlineEntry.h"
#import "CustomCell.h"
#import "CustomCellController.h"
@implementation TDSOutlineDelegate
#pragma mark -
#pragma mark OutlineView datasource/delegate
- (void) awakeFromNib {
// Use a dictionary to hold the custom
// view controllers to swap in and out
cellViewControllers =
[[NSMutableDictionary alloc] init];
CustomCell* cell = [[CustomCell alloc] init];
[cell setEditable:YES];
// Swap out the default NSCell with our own
[[[myOutlineView tableColumns]
objectAtIndex:0]
setDataCell:cell];
}
- (void)outlineView:(NSOutlineView *)oview
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn
item:(id)item
{
// Display the specific view for our cell.
// The item we get is an NSTreeControllerTreeNode,
// but can be cast to our NSManagedObject
OutlineEntry* node = [item representedObject];
CustomCellController *cellCtrl =
[cellViewControllers objectForKey:node];
if (!cellCtrl)
{
cellCtrl =
[[CustomCellController alloc] initWithNode:node];
[cellViewControllers setObject:cellCtrl forKey:node];
}
CustomCell *mycell = (CustomCell *)cell;
[mycell setController: cellCtrl];
}
-(void)hideViewsForItem:(OutlineEntry*)node
{
CustomCellController *cellCtrl = [cellViewControllers objectForKey:node];
if (cellCtrl)
{
[cellCtrl hideViews];
}
for (OutlineEntry* child in node.children)
[self hideViewsForItem:child];
}
-(void)removeItems:(OutlineEntry*)node
{
for (OutlineEntry* child in node.children)
[self removeItems:child];
CustomCellController *cellCtrl = [cellViewControllers objectForKey:node];
if (cellCtrl)
{
[cellCtrl hideViews];
[cellViewControllers removeObjectForKey:node];
}
}
-(void)outlineViewItemDidCollapse:(NSNotification*)notification
{
OutlineEntry* node = [[[notification userInfo]
objectForKey:@"NSObject"]
representedObject];
// The collapsed item is the parent of
// the nodes that disappear
for (OutlineEntry* child in node.children)
[self hideViewsForItem:child];
}
#pragma OutlineView Data Source
-(NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if (!item)
return [[outlineItemsTree arrangedObjects] count];
else
{
OutlineEntry* node = item;
return [[node children] count];
}
}
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
OutlineEntry* node = [item representedObject];
return node;
}
-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
OutlineEntry* node = item;
return [[node children] count];
}
-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
if (item)
{
OutlineEntry* node = item;
return [[node children] objectAtIndex:index];
}
else
return [[outlineItemsTree arrangedObjects] objectAtIndex:index];
}
// OutlineView Data Source
-(CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
{
return 75;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn*)col item:(id)item
{
return YES;
}
-(void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
// required to set the value when editing
OutlineEntry* node = item;
[node setTitle: object];
}
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
if ([[fieldEditor string] length] == 0)
{
// don't allow empty node names
return NO;
}
else
{
return YES;
}
}
@end