-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlineTreeController.m
94 lines (75 loc) · 2.6 KB
/
outlineTreeController.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
#import "outlineTreeController.h"
@implementation outlineTreeController
- (void)awakeFromNib
{
[self addObserver:self
forKeyPath:@"selection"
options:0 context:nil];
[super awakeFromNib];
}
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog( @"Value changed.(%@, %@)\n", keyPath, change );
if ([keyPath isEqualToString:@"selection"]) {
[self didChangeValueForKey:@"canIndent"];
[self didChangeValueForKey:@"canDedent"];
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (BOOL)canIndent {
NSIndexPath *selectionPath = [self selectionIndexPath];
if (!selectionPath) {
return NO;
}
unsigned index = [selectionPath indexAtPosition:([selectionPath length] - 1)];
if (index == 0) {
return NO;
}
return YES;
}
- (BOOL)canDedent {
id selection = [self selection];
id parent = [selection valueForKeyPath:@"parent"];
if (parent == nil || parent == NSMultipleValuesMarker || parent == NSNoSelectionMarker || parent == NSNotApplicableMarker) {
return NO;
}
return YES;
}
- (IBAction)indentNode:(id)sender {
NSIndexPath *selectionPath = [self selectionIndexPath];
if (!selectionPath) {
NSBeep();
return;
}
id selection = [self selection];
// The selection is one of the root notes.
// Get all the root notes to find our sibling.
id parentNote = [selection valueForKeyPath:@"parent"];
NSArray *children = (parentNote == nil) ? (NSArray *)[self content] : [[parentNote valueForKeyPath:@"children"] allObjects];
children = [children sortedArrayUsingDescriptors:[self sortDescriptors]];
unsigned index = [selectionPath indexAtPosition:([selectionPath length] - 1)];
if (index == 0) { // Cannot indent the top root node
NSBeep();
}
else {
id sibling = [children objectAtIndex:index - 1];
[selection setValue:sibling forKeyPath:@"parent"];
}
}
- (IBAction)dedentNode:(id)sender {
/* The controller's -selection method will return a proxy to the object or objects that are actually selected.
*/
id selection = [self selection];
id parent = [selection valueForKeyPath:@"parent"];
// make sure exactly one object is selected.
if (parent == nil || parent == NSMultipleValuesMarker || parent == NSNoSelectionMarker || parent == NSNotApplicableMarker) {
NSBeep();
return;
}
parent = [parent valueForKeyPath:@"parent"];
[selection setValue:parent forKeyPath:@"parent"];
}
@end