forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextMenuActionPrefsController.m
234 lines (202 loc) · 7.15 KB
/
ContextMenuActionPrefsController.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// ContextMenuActionPrefsController.m
// iTerm
//
// Created by George Nachman on 11/18/11.
// Copyright 2011 Georgetech. All rights reserved.
//
#import "ContextMenuActionPrefsController.h"
#import "NSStringITerm.h"
#import "FutureMethods.h"
static NSString* kTitleKey = @"title";
static NSString* kActionKey = @"action";
static NSString* kParameterKey = @"parameter";
@implementation ContextMenuActionPrefsController
@synthesize delegate = delegate_;
@synthesize hasSelection = hasSelection_;
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
model_ = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[model_ release];
[super dealloc];
}
+ (ContextMenuActions)actionForActionDict:(NSDictionary *)dict
{
return (ContextMenuActions) [[dict objectForKey:kActionKey] intValue];
}
+ (NSString *)titleForActionDict:(NSDictionary *)dict withCaptureComponents:(NSArray *)components
{
NSString *title = [dict objectForKey:kTitleKey];
for (int i = 0; i < 9; i++) {
NSString *repl = @"";
if (i < components.count) {
repl = [components objectAtIndex:i];
}
title = [title stringByReplacingBackreference:i withString:repl];
}
return title;
}
+ (NSString *)parameterValue:(NSString *)parameter
encodedForAction:(ContextMenuActions)action
{
switch (action) {
case kRunCommandContextMenuAction:
case kRunCoprocessContextMenuAction:
return [parameter stringWithEscapedShellCharacters];
case kOpenFileContextMenuAction:
return parameter;
case kOpenUrlContextMenuAction:
return [parameter stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
return nil;
}
+ (NSString *)parameterForActionDict:(NSDictionary *)dict withCaptureComponents:(NSArray *)components
{
NSString *parameter = [dict objectForKey:kParameterKey];
ContextMenuActions action = (ContextMenuActions) [[dict objectForKey:kActionKey] intValue];
for (int i = 0; i < 9; i++) {
NSString *repl = @"";
if (i < components.count) {
repl = [self parameterValue:[components objectAtIndex:i]
encodedForAction:action];
}
parameter = [parameter stringByReplacingBackreference:i withString:repl];
}
return parameter;
}
- (IBAction)ok:(id)sender
{
[delegate_ contextMenuActionsChanged:model_];
}
- (IBAction)add:(id)sender
{
NSDictionary *defaultAction = [NSDictionary dictionaryWithObjectsAndKeys:
@"", kTitleKey,
[NSNumber numberWithInt:kOpenFileContextMenuAction], kActionKey,
nil];
[model_ addObject:defaultAction];
[tableView_ reloadData];
}
- (IBAction)remove:(id)sender
{
[tableView_ reloadData];
[model_ removeObjectAtIndex:[tableView_ selectedRow]];
[tableView_ reloadData];
}
- (void)setActions:(NSArray *)newActions
{
if (!newActions) {
newActions = [NSMutableArray array];
}
[model_ autorelease];
model_ = [newActions mutableCopy];
[tableView_ reloadData];
}
#pragma mark NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [model_ count];
}
- (NSString *)keyForColumn:(NSTableColumn *)aTableColumn
{
if (aTableColumn == titleColumn_) {
return kTitleKey;
} else if (aTableColumn == actionColumn_) {
return kActionKey;
} else if (aTableColumn == parameterColumn_) {
return kParameterKey;
} else {
return nil;
}
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
NSString *key = [self keyForColumn:aTableColumn];
NSDictionary *row = [model_ objectAtIndex:rowIndex];
return key ? [row objectForKey:key] : nil;
}
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
NSString *key = [self keyForColumn:aTableColumn];
if (key) {
NSMutableDictionary *temp = [[[model_ objectAtIndex:rowIndex] mutableCopy] autorelease];
[temp setObject:anObject forKey:key];
[model_ replaceObjectAtIndex:rowIndex withObject:temp];
[aTableView reloadData];
}
}
- (NSCell *)tableView:(NSTableView *)tableView
dataCellForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
// These two arrays and the enum in the header file must be parallel
NSArray *actionNames = [NSArray arrayWithObjects:
@"Open File…",
@"Open URL…",
@"Run Command…",
@"Run Coprocess…",
nil];
NSArray *paramPlaceholders = [NSArray arrayWithObjects:
@"Enter file name",
@"Enter URL",
@"Enter command",
@"Enter coprocess command",
nil];
if (tableColumn == titleColumn_) {
NSTextFieldCell *cell = [[[NSTextFieldCell alloc] initTextCell:@""] autorelease];
[cell setPlaceholderString:@"Enter Title"];
[cell setEditable:YES];
[cell setTruncatesLastVisibleLine:YES];
[cell setLineBreakMode:NSLineBreakByTruncatingTail];
return cell;
} else if (tableColumn == actionColumn_) {
NSPopUpButtonCell *cell =
[[[NSPopUpButtonCell alloc] initTextCell:[actionNames objectAtIndex:0] pullsDown:NO] autorelease];
for (int i = 0; i < actionNames.count; i++) {
[cell addItemWithTitle:[actionNames objectAtIndex:i]];
NSMenuItem *lastItem = [[[cell menu] itemArray] lastObject];
[lastItem setTag:i];
}
[cell setBordered:NO];
return cell;
} else if (tableColumn == parameterColumn_) {
NSDictionary *actionDict = [model_ objectAtIndex:row];
int actionNum = [[actionDict objectForKey:kActionKey] intValue];
NSString *placeholder = [paramPlaceholders objectAtIndex:actionNum];
if (placeholder.length) {
NSTextFieldCell *cell = [[[NSTextFieldCell alloc] initTextCell:@""] autorelease];
[cell setPlaceholderString:placeholder];
[cell setEditable:YES];
[cell setTruncatesLastVisibleLine:YES];
[cell setLineBreakMode:NSLineBreakByTruncatingTail];
return cell;
} else {
NSTextFieldCell *cell = [[[NSTextFieldCell alloc] initTextCell:@""] autorelease];
[cell setEditable:NO];
return cell;
}
}
return nil;
}
#pragma mark NSTableViewDelegate
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return YES;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
self.hasSelection = [tableView_ numberOfSelectedRows] > 0;
}
#pragma mark NSWindowDelegate
- (void)windowWillClose:(NSNotification *)notification
{
[tableView_ reloadData];
}
@end