diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPGotoDatabaseController.h | 4 | ||||
-rw-r--r-- | Source/SPGotoDatabaseController.m | 88 |
2 files changed, 65 insertions, 27 deletions
diff --git a/Source/SPGotoDatabaseController.h b/Source/SPGotoDatabaseController.h index 99dfc289..08b0ea0e 100644 --- a/Source/SPGotoDatabaseController.h +++ b/Source/SPGotoDatabaseController.h @@ -37,7 +37,8 @@ * keyboard-based navigation between databases. The dialog also enables * jumping to a database by C&P-ing its full name. */ -@interface SPGotoDatabaseController : NSWindowController <NSTableViewDataSource,NSControlTextEditingDelegate> { +@interface SPGotoDatabaseController : NSWindowController <NSTableViewDataSource,NSControlTextEditingDelegate> +{ IBOutlet NSSearchField *searchField; IBOutlet NSButton *okButton; IBOutlet NSButton *cancelButton; @@ -83,4 +84,5 @@ * This method will only return once the dialog was closed again. */ - (BOOL)runModal; + @end diff --git a/Source/SPGotoDatabaseController.m b/Source/SPGotoDatabaseController.m index 0c6cbef0..8bf7505e 100644 --- a/Source/SPGotoDatabaseController.m +++ b/Source/SPGotoDatabaseController.m @@ -32,24 +32,29 @@ #import "SPDatabaseDocument.h" @interface SPGotoDatabaseController (Private) + - (void)_buildHightlightedFilterList:(NSString *)filter didFindExactMatch:(BOOL *)exactMatch; - (IBAction)okClicked:(id)sender; - (IBAction)cancelClicked:(id)sender; - (IBAction)searchChanged:(id)sender; + @end @implementation SPGotoDatabaseController +@synthesize allowCustomNames; + - (id)init { - self = [super initWithWindowNibName:@"GotoDatabaseDialog"]; - if (self) { + if ((self = [super initWithWindowNibName:@"GotoDatabaseDialog"])) { unfilteredList = [[NSMutableArray alloc] init]; filteredList = [[NSMutableArray alloc] init]; isFiltered = NO; + [self setAllowCustomNames:YES]; } + return self; } @@ -59,38 +64,50 @@ - (IBAction)okClicked:(id)sender { [NSApp stopModalWithCode:YES]; + [[self window] orderOut:nil]; } - (IBAction)cancelClicked:(id)sender { [NSApp stopModalWithCode:NO]; + [[self window] orderOut:nil]; } - (IBAction)searchChanged:(id)sender { [filteredList removeAllObjects]; + NSString *newFilter = [searchField stringValue]; - if(!newFilter || [newFilter isEqualToString:@""]) { + + if (!newFilter || [newFilter isEqualToString:@""]) { isFiltered = NO; } else { isFiltered = YES; + BOOL exactMatch = NO; + [self _buildHightlightedFilterList:newFilter didFindExactMatch:&exactMatch]; + //always add the search string to the end of the list (in case the user //wants to switch to a DB not in the list) unless there was an exact match - if([self allowCustomNames] && !exactMatch) { + if ([self allowCustomNames] && !exactMatch) { NSMutableAttributedString *searchValue = [[NSMutableAttributedString alloc] initWithString:newFilter]; + [searchValue applyFontTraits:NSItalicFontMask range:NSMakeRange(0, [newFilter length])]; + [filteredList addObject:[searchValue autorelease]]; } } + [databaseListView reloadData]; - //ensure we have a selection - if([databaseListView selectedRow] < 0) + + // Ensure we have a selection + if ([databaseListView selectedRow] < 0) { [databaseListView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO]; + } [okButton setEnabled:([databaseListView selectedRow] >= 0)]; } @@ -98,38 +115,46 @@ #pragma mark - #pragma mark Public -- (NSString *)selectedDatabase { +- (NSString *)selectedDatabase +{ NSInteger row = [databaseListView selectedRow]; + id attrValue; - if(isFiltered) { + + if (isFiltered) { attrValue = [filteredList objectOrNilAtIndex:row]; } else { attrValue = [unfilteredList objectOrNilAtIndex:row]; } - if([attrValue isKindOfClass:[NSAttributedString class]]) + + if ([attrValue isKindOfClass:[NSAttributedString class]]) { return [attrValue string]; + } + return attrValue; } - (void)setDatabaseList:(NSArray *)list { - //update list of databases + // Update list of databases [unfilteredList removeAllObjects]; [unfilteredList addObjectsFromArray:list]; } - (BOOL)runModal { - //NSWindowController is lazy with loading nibs + // NSWindowController is lazy with loading nibs [self window]; - //reset the search field + // Reset the search field [searchField setStringValue:@""]; [self searchChanged:nil]; - //give focus to search field + + // Give focus to search field [[self window] makeFirstResponder:searchField]; - //start modal dialog + + // Start modal dialog return [NSApp runModalForWindow:[self window]]; } @@ -144,18 +169,23 @@ [NSNumber numberWithInt:NSUnderlineStyleSingle],NSUnderlineStyleAttributeName, nil]; - for(NSString *db in unfilteredList) { + for (NSString *db in unfilteredList) + { NSRange match = [db rangeOfString:filter]; - if(match.location == NSNotFound) - continue; + + if (match.location == NSNotFound) continue; + //check for exact match? - if(exactMatch && !*exactMatch) { - if(match.location == 0 && match.length == [db length]) + if (exactMatch && !*exactMatch) { + if (match.location == 0 && match.length == [db length]) { *exactMatch = YES; + } } NSMutableAttributedString *attrMatch = [[NSMutableAttributedString alloc] initWithString:db]; + [attrMatch setAttributes:attrs range:match]; + [filteredList addObject:[attrMatch autorelease]]; } @@ -167,7 +197,7 @@ - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView { - if(!isFiltered) { + if (!isFiltered) { return [unfilteredList count]; } else { @@ -177,10 +207,12 @@ - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { - if(!isFiltered) + if (!isFiltered) { return [unfilteredList objectAtIndex:rowIndex]; - else + } + else { return [filteredList objectAtIndex:rowIndex]; + } } #pragma mark - @@ -189,21 +221,24 @@ - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector { //the ESC key will usually clear the search field. we want to close the dialog - if(commandSelector == @selector(cancelOperation:)) { + if (commandSelector == @selector(cancelOperation:)) { [cancelButton performClick:control]; return YES; } + //arrow down/up will usually go to start/end of the text field. we want to change the selected table row. - if(commandSelector == @selector(moveDown:)) { + if (commandSelector == @selector(moveDown:)) { [databaseListView selectRowIndexes:[NSIndexSet indexSetWithIndex:([databaseListView selectedRow]+1)] byExtendingSelection:NO]; return YES; } - if(commandSelector == @selector(moveUp:)) { + + if (commandSelector == @selector(moveUp:)) { [databaseListView selectRowIndexes:[NSIndexSet indexSetWithIndex:([databaseListView selectedRow]-1)] byExtendingSelection:NO]; return YES; } //forward return to OK button (enter will not be caught by search field) - if(commandSelector == @selector(insertNewline:)) { + + if (commandSelector == @selector(insertNewline:)) { [okButton performClick:control]; return YES; } @@ -217,6 +252,7 @@ { [unfilteredList release], unfilteredList = nil; [filteredList release], filteredList = nil; + [super dealloc]; } |