PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/YEDSelectionManager.j

http://github.com/rheimbuch/YED
Unknown | 94 lines | 78 code | 16 blank | 0 comment | 0 complexity | 50b22f4af900a7ae321a84671dd562d9 MD5 | raw file
 1@import <Foundation/CPNotificationCenter.j>
 2@import <Foundation/CPObject.j>
 3@import <Foundation/CPSet.j>
 4
 5YEDSelectedItemNotification = "YEDSelectedItemNotification";
 6
 7@implementation YEDSelectionManager : CPObject 
 8{
 9    CPSet       selectedItems   @accessors;
10    id          delegate        @accessors;
11}
12
13- (id)init
14{
15    self = [super init];
16    if(self)
17    {
18        selectedItems = [CPSet set];
19        [[CPNotificationCenter defaultCenter] addObserver:self
20                selector:@selector(selectedItemNotification:)
21                name:YEDSelectedItemNotification
22                object:nil];
23    }
24    return self;
25}
26
27
28- (BOOL)allowMultipleSelections
29{
30    return false;
31}
32
33- (void)selectItem:(id)item
34{
35    //Selecting a nil item deselectes all items;
36    if(!item)
37    {
38        [self deselectAll];
39        return;
40    }
41    if(![self handlesSelectableItem:item])
42        return;
43    if([selectedItems containsObject:item])
44        return;
45    
46    if(![self allowMultipleSelections]) {
47        [self deselectAll];
48    }
49
50    [selectedItems addObject:item];
51    [item setIsSelected:YES];
52}
53
54- (void)deselectAll
55{
56    CPLog.trace("Deselecting all items");
57    var itemIter = [selectedItems objectEnumerator],
58        oldItem = nil;
59    while(oldItem = [itemIter nextObject])
60    {
61        [oldItem setIsSelected:NO];
62        [selectedItems removeObject:oldItem];
63    }
64}
65
66/**
67    Handles selection notifications from selectable views
68*/
69- (void)selectedItemNotification:(CPNotification)notification
70{
71    var item = [notification object],
72        info = [notification userInfo],
73        mouseDown  = [info objectForKey:@"mouseDown"];
74        
75    [self selectItem:item];
76}
77
78/**
79    Ask the delegate if we should handle a given item
80*/
81- (BOOL)handlesSelectableItem:(id)item
82{
83    if(!([item respondsToSelector:@selector(isSelected)] && [item respondsToSelector:@selector(setIsSelected:)]))
84        return false;
85    
86    if([delegate respondsToSelector:@selector(selectionManager:shouldHandle:)])
87        return [delegate selectionManager:self shouldHandle:item];
88    else
89        false;
90}
91
92
93
94@end