This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
KeywurlPreferences.m
366 lines (321 loc) · 11.2 KB
/
KeywurlPreferences.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#import "KeywurlPlugin.h"
#import "KeywurlPreferences.h"
@interface KeywurlPreferences (Private)
- (void) saveEdit;
- (void) leaveEdit;
- (void) reloadKeywords;
- (void) selectKeyword: (NSString*) keyword;
- (void) selectKeyword: (NSString*) keyword edit: (BOOL) edit;
- (void) updateForm;
@end
@implementation KeywurlPreferences
+ (NSImage*) preloadImage: (NSString*) theName
{
NSImage* image = nil;
NSString* imagePath = [[NSBundle bundleWithIdentifier: @"net.purefiction.keywurl"]
pathForImageResource: theName];
if (!imagePath)
{
NSLog(@"imagePath for %@ is nil", theName);
return nil;
}
image = [[NSImage alloc] initByReferencingFile: imagePath];
if (!image)
{
NSLog(@"image for %@ is nil", theName);
return nil;
}
[image setName: theName];
return image;
}
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver: self];
[mapper release];
[keywords release];
if (mappingBeingEdited) [mappingBeingEdited release];
[super dealloc];
}
- (void) awakeFromNib {
mapper = [[[KeywurlPlugin sharedInstance] keywordMapper] retain];
keywords = [NSMutableArray new];
[self reloadKeywords];
/* NSDictionary* infoDictionary = [[NSBundle bundleWithIdentifier: @"net.purefiction.keywurl"]
infoDictionary];
*/
[tableView setDataSource: self];
[sourceTokenField setObjectValue: [NSArray arrayWithObjects:
[[InputToken alloc] init],
[[QueryToken alloc] init],
[[QueryPartToken alloc] initWithPartNumber: 1],
[[QueryPartToken alloc] initWithPartNumber: 2],
[[QueryPartToken alloc] initWithPartNumber: 3],
[[QueryPartToken alloc] initWithPartNumber: 4],
[[QueryPartToken alloc] initWithPartNumber: 5],
[[QueryPartToken alloc] initWithPartNumber: 6],
[[QueryPartToken alloc] initWithPartNumber: 7],
[[QueryPartToken alloc] initWithPartNumber: 8],
[[QueryPartToken alloc] initWithPartNumber: 9],
nil
]];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(mappingsDidChange:)
name: KeywordMapperMappingsDidChangeNotification
object: mapper];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(tableViewSelectionDidChange:)
name: NSTableViewSelectionDidChangeNotification
object: tableView];
}
- (NSImage*) imageForPreferenceNamed: (NSString*) theName
{
NSImage* image = [NSImage imageNamed: @"Keywurl.png"];
if (image == nil) {
image = [KeywurlPreferences preloadImage: @"Keywurl.png"];
}
return image;
}
- (NSString*) preferencesNibName
{
return @"KeywurlPreferences";
}
- (void) didChange
{
[super didChange];
}
- (NSView*) viewForPreferenceNamed: (NSString*) aName
{
if ([[KeywurlPlugin sharedInstance] isLoaded] == NO) {
return nil;
}
NSView* view = [super viewForPreferenceNamed: aName];
return view;
}
- (void) willBeDisplayed
{
if ([[KeywurlPlugin sharedInstance] isLoaded] == NO)
return;
}
- (void) saveChanges
{
if ([[KeywurlPlugin sharedInstance] isLoaded]) {
[self saveEdit];
[mapper saveMappings];
}
}
- (BOOL) hasChangesPending
{
return [super hasChangesPending];
}
- (void) moduleWillBeRemoved
{
[super moduleWillBeRemoved];
}
- (void) moduleWasInstalled
{
[super moduleWasInstalled];
}
- (IBAction) addKeyword: (id) sender {
[self saveEdit];
[mapper addKeyword: @"" expansion: @""];
[self selectKeyword: @"" edit: YES];
}
- (IBAction) removeKeyword: (id) sender {
int rowIndex = [tableView selectedRow];
if (rowIndex >= 0) {
[self leaveEdit];
NSString* keyword = [keywords objectAtIndex: rowIndex];
[mapper removeKeyword: keyword];
}
}
- (void) tableViewSelectionDidChange: (NSNotification*) notification {
[self saveEdit];
NSString* keyword = [tableView selectedRow] >= 0 ? [keywords objectAtIndex: [tableView selectedRow]] : nil;
if (keyword) {
KeywordMapping* mapping = [mapper mappingForKeyword: keyword];
mappingBeingEdited = [mapping retain];
} else {
if (mappingBeingEdited) {
[mappingBeingEdited release];
mappingBeingEdited = nil;
}
}
[self updateForm];
}
- (void) updateForm {
if (mappingBeingEdited) {
[expansionTokenField setObjectValue: [mappingBeingEdited expansionAsTokens]];
[encodeSpacesCheckbox setState: [mappingBeingEdited encodeSpaces] ? NSOnState : NSOffState];
[dontUseUnicodeCheckBox setState: [mappingBeingEdited dontUseUnicode] ? NSOnState : NSOffState];
} else {
[expansionTokenField setStringValue: @""];
[encodeSpacesCheckbox setState: NSOffState];
[dontUseUnicodeCheckBox setState: NSOffState];
}
[expansionTokenField setEnabled: mappingBeingEdited != nil];
[encodeSpacesCheckbox setEnabled: mappingBeingEdited != nil];
[dontUseUnicodeCheckBox setEnabled: mappingBeingEdited != nil];
[sourceTokenField setEnabled: mappingBeingEdited != nil];
}
- (NSTokenStyle) tokenField: (NSTokenField*) tokenField
styleForRepresentedObject: (id) representedObject {
if ([representedObject isKindOfClass: [NSString class]]) {
return NSPlainTextTokenStyle;
} else {
return NSRoundedTokenStyle;
}
}
- (id) tokenField: (NSTokenField*) tokenField
representedObjectForEditingString: (NSString*) editingString {
if ([editingString isEqualToString: @"{query}"]) {
return [[QueryToken alloc] init];
} else if ([editingString isEqualToString: @"{input}"]) {
return [[InputToken alloc] init];
} else if ([editingString hasPrefix: @"{"] && [editingString hasSuffix: @"}"] &&
[[editingString substringWithRange: NSMakeRange(1, [editingString length] - 2)] intValue] >= 1) {
int n = [[editingString substringWithRange: NSMakeRange(1, [editingString length] - 2)] intValue];
return [[QueryPartToken alloc] initWithPartNumber: n];
} else {
return [editingString description];
}
}
- (NSString*) tokenField: (NSTokenField*) tokenField
displayStringForRepresentedObject: (id) representedObject {
if ([representedObject isKindOfClass: [NSString class]]) {
return representedObject;
} else {
return [representedObject label];
}
}
- (NSString*) tokenField: (NSTokenField*) tokenField
editingStringForRepresentedObject: (id) representedObject {
if ([representedObject isKindOfClass: [NSString class]]) {
return representedObject;
} else {
return [representedObject description];
}
}
- (NSArray*) tokenField: (NSTokenField*) tokenField
shouldAddObjects: (NSArray*) tokens
atIndex: (NSUInteger) index {
NSMutableArray* result = [NSMutableArray new];
for (unsigned i = 0; i < [tokens count]; i++) {
id item = [tokens objectAtIndex: i];
if ([item isEqualToString: @"complete query"]) {
[result addObject: [[QueryToken alloc] init]];
} else if ([item isEqualToString: @"complete location field"]) {
[result addObject: [[InputToken alloc] init]];
} else if ([item hasPrefix: @"query word "] && [[item substringFromIndex: 11] intValue] >= 1) {
int n = [[item substringFromIndex: 11] intValue];
[result addObject: [[QueryPartToken alloc] initWithPartNumber: n]];
} else {
[result addObject: item];
}
}
return result;
}
- (void) saveEdit {
KeywordMapping* mapping = mappingBeingEdited;
if (mapping) {
[mapping setEncodeSpaces: [encodeSpacesCheckbox state] == NSOnState];
[mapping setDontUseUnicode: [dontUseUnicodeCheckBox state] == NSOnState];
[mapping setExpansion: [[expansionTokenField objectValue] componentsJoinedByString: @""]];
[mapper modified];
}
}
- (void) leaveEdit {
if (mappingBeingEdited) {
[mappingBeingEdited release];
mappingBeingEdited = nil;
[self updateForm];
}
}
- (void) reloadKeywords {
if (!reloading) {
reloading = YES;
@try {
[self saveEdit];
// Get currently selected keywod
int rowIndex = [tableView selectedRow];
NSString* keyword = rowIndex >= 0 && rowIndex < [keywords count] ?
[keywords objectAtIndex: [tableView selectedRow]] : nil;
if (keyword) [keyword retain];
// Build new, sorted keyword list
[keywords removeAllObjects];
NSArray* mappings = [mapper mappings];
for (int i = 0; i < [mappings count]; i++) {
KeywordMapping* mapping = [mappings objectAtIndex: i];
[keywords addObject: [mapping keyword]];
}
[keywords sortUsingSelector: @selector(caseInsensitiveCompare:)];
NSInteger emptyIndex = [keywords indexOfObject: @""];
if (emptyIndex != NSNotFound) {
[keywords removeObjectAtIndex: emptyIndex];
[keywords addObject: @""];
}
[tableView reloadData];
// Reselect previously selected
[self selectKeyword: keyword];
if (keyword) [keyword release];
} @finally {
reloading = NO;
}
}
}
- (void) selectKeyword: (NSString*) keyword edit: (BOOL) edit {
if (keyword) {
NSInteger rowIndex = [keywords indexOfObject: keyword];
if (rowIndex != NSNotFound) {
[tableView scrollRowToVisible: rowIndex];
[tableView selectRowIndexes: [NSIndexSet indexSetWithIndex: rowIndex] byExtendingSelection: NO];
if (edit) {
[tableView editColumn: 0 row: rowIndex withEvent: nil select: YES];
}
}
}
}
- (void) selectKeyword: (NSString*) keyword {
[self selectKeyword: keyword edit: NO];
}
/* Mapper notifications */
- (void) mappingsDidChange: (NSNotification*) notification {
[self reloadKeywords];
}
/* Table view data source methods */
- (NSInteger) numberOfRowsInTableView: (NSTableView*) aTableView {
return [keywords count];
}
- (id) tableView: (NSTableView*) aTableView
objectValueForTableColumn: (NSTableColumn*) aTableColumn
row: (NSInteger) rowIndex {
if ([[aTableColumn identifier] isEqualToString: @"Keyword"]) {
NSString* keyword = [keywords objectAtIndex: rowIndex];
if (keyword) {
KeywordMapping* mapping = [mapper mappingForKeyword: keyword];
return [mapping keyword];
}
}
return nil;
}
- (void) tableView: (NSTableView*) aTableView
setObjectValue: (id) newValue
forTableColumn: (NSTableColumn*) aTableColumn
row: (NSInteger) rowIndex {
if ([[aTableColumn identifier] isEqualToString: @"Keyword"]) {
NSString* keyword = [keywords objectAtIndex: rowIndex];
if (keyword) {
KeywordMapping* mapping = [mapper mappingForKeyword: keyword];
if (mapping) {
if ([newValue length] == 0) {
[keywords removeObject: keyword];
[mapper removeKeyword: keyword];
mapping = nil;
} else {
[mapping setKeyword: newValue];
[mapper modified];
[self selectKeyword: [mapping keyword]];
}
}
}
}
}
@end