forked from pieter/gitx
/
PBSourceViewItem.m
113 lines (89 loc) · 2.34 KB
/
PBSourceViewItem.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
//
// PBSourceViewItem.m
// GitX
//
// Created by Pieter de Bie on 9/8/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "PBSourceViewItem.h"
#import "PBGitRevSpecifier.h"
@implementation PBSourceViewItem
@synthesize parent, title, isGroupItem, children, revSpecifier, isUncollapsible;
- (id)init
{
if (!(self = [super init]))
return nil;
children = [NSMutableArray array];
return self;
}
+ (id)groupItemWithTitle:(NSString *)title
{
PBSourceViewItem *item = [[[self class] alloc] init];
item.title = title;
item.isGroupItem = YES;
return item;
}
+ (id)itemWithRevSpec:(PBGitRevSpecifier *)revSpecifier
{
PBSourceViewItem *item = [[[self class] alloc] init];
item.revSpecifier = revSpecifier;
return item;
}
+ (id)itemWithTitle:(NSString *)title;
{
PBSourceViewItem *item = [[[self class] alloc] init];
item.title = title;
return item;
}
- (void)addChild:(PBSourceViewItem *)child
{
[self.children addObject:child];
child.parent = self;
}
- (void)addRev:(PBGitRevSpecifier *)theRevSpecifier toPath:(NSArray *)path
{
if ([path count] == 1) {
PBSourceViewItem *item = [PBSourceViewItem itemWithRevSpec:theRevSpecifier];
[self addChild:item];
return;
}
NSString *firstTitle = [path objectAtIndex:0];
PBSourceViewItem *node = nil;
for (PBSourceViewItem *child in [self children])
if ([child.title isEqualToString:firstTitle])
node = child;
if (!node) {
node = [PBSourceViewItem itemWithTitle:firstTitle];
[self addChild:node];
}
[node addRev:theRevSpecifier toPath:[path subarrayWithRange:NSMakeRange(1, [path count] - 1)]];
}
- (PBSourceViewItem *)findRev:(PBGitRevSpecifier *)rev
{
if (rev == revSpecifier)
return self;
PBSourceViewItem *item = nil;
for (PBSourceViewItem *child in children)
if (item = [child findRev:rev])
return item;
return nil;
}
- (NSString *)title
{
if (title)
return title;
return [[revSpecifier description] lastPathComponent];
}
- (NSImage *)icon
{
if ([self isGroupItem])
return nil;
if (self.parent && !self.parent.parent && [self.parent.title isEqualToString:@"Remotes"])
return [NSImage imageNamed:@"remote"];
if (self.parent && !self.parent.parent && [self.parent.title isEqualToString:@"Tags"])
return [NSImage imageNamed:@"tag"];
if ([[self children] count])
return [NSImage imageNamed:@"folder"];
return [NSImage imageNamed:@"branch"];
}
@end