-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYEDSelectionManager.j
More file actions
94 lines (78 loc) · 2.06 KB
/
YEDSelectionManager.j
File metadata and controls
94 lines (78 loc) · 2.06 KB
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 <Foundation/CPNotificationCenter.j>
@import <Foundation/CPObject.j>
@import <Foundation/CPSet.j>
YEDSelectedItemNotification = "YEDSelectedItemNotification";
@implementation YEDSelectionManager : CPObject
{
CPSet selectedItems @accessors;
id delegate @accessors;
}
- (id)init
{
self = [super init];
if(self)
{
selectedItems = [CPSet set];
[[CPNotificationCenter defaultCenter] addObserver:self
selector:@selector(selectedItemNotification:)
name:YEDSelectedItemNotification
object:nil];
}
return self;
}
- (BOOL)allowMultipleSelections
{
return false;
}
- (void)selectItem:(id)item
{
//Selecting a nil item deselectes all items;
if(!item)
{
[self deselectAll];
return;
}
if(![self handlesSelectableItem:item])
return;
if([selectedItems containsObject:item])
return;
if(![self allowMultipleSelections]) {
[self deselectAll];
}
[selectedItems addObject:item];
[item setIsSelected:YES];
}
- (void)deselectAll
{
CPLog.trace("Deselecting all items");
var itemIter = [selectedItems objectEnumerator],
oldItem = nil;
while(oldItem = [itemIter nextObject])
{
[oldItem setIsSelected:NO];
[selectedItems removeObject:oldItem];
}
}
/**
Handles selection notifications from selectable views
*/
- (void)selectedItemNotification:(CPNotification)notification
{
var item = [notification object],
info = [notification userInfo],
mouseDown = [info objectForKey:@"mouseDown"];
[self selectItem:item];
}
/**
Ask the delegate if we should handle a given item
*/
- (BOOL)handlesSelectableItem:(id)item
{
if(!([item respondsToSelector:@selector(isSelected)] && [item respondsToSelector:@selector(setIsSelected:)]))
return false;
if([delegate respondsToSelector:@selector(selectionManager:shouldHandle:)])
return [delegate selectionManager:self shouldHandle:item];
else
false;
}
@end