diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPFieldMapperController.h | 10 | ||||
-rw-r--r-- | Source/SPFieldMapperController.m | 48 |
2 files changed, 53 insertions, 5 deletions
diff --git a/Source/SPFieldMapperController.h b/Source/SPFieldMapperController.h index 62139b7d..4b9a3178 100644 --- a/Source/SPFieldMapperController.h +++ b/Source/SPFieldMapperController.h @@ -37,10 +37,11 @@ IBOutlet id importFieldNamesHeaderSwitch; IBOutlet id importButton; IBOutlet NSPopUpButton *alignByPopup; - + IBOutlet NSMenuItem *matchingNameMenuItem; + id theDelegate; id fieldMappingImportArray; - + NSInteger fieldMappingCurrentRow; NSMutableArray *fieldMappingArray; NSMutableArray *fieldMappingTableColumnNames; @@ -49,7 +50,7 @@ NSMutableArray *fieldMappingButtonOptions; NSMutableArray *fieldMappingOperatorOptions; NSMutableArray *fieldMappingOperatorArray; - + NSNumber *doImport; NSNumber *doNotImport; NSNumber *isEqual; @@ -87,10 +88,13 @@ - (IBAction)changeTableTarget:(id)sender; - (IBAction)changeImportMethod:(id)sender; - (IBAction)changeFieldAlignment:(id)sender; +- (IBAction)changeHasHeaderCheckbox:(id)sender; - (IBAction)stepRow:(id)sender; +- (IBAction)addGlobalSourceVariable:(id)sender; - (IBAction)closeSheet:(id)sender; // Others +- (void)matchHeaderNames; - (void)setupFieldMappingArray; - (void)updateFieldMappingButtonCell; - (void)updateFieldMappingOperatorOptions; diff --git a/Source/SPFieldMapperController.m b/Source/SPFieldMapperController.m index 84dfc4c8..42ac3b0d 100644 --- a/Source/SPFieldMapperController.m +++ b/Source/SPFieldMapperController.m @@ -27,6 +27,7 @@ #import "SPTableData.h" #import "TablesList.h" #import "SPArrayAdditions.h" +#import "SPStringAdditions.h" #import "SPConstants.h" @implementation SPFieldMapperController @@ -95,6 +96,7 @@ [importFieldNamesHeaderSwitch setState:importFieldNamesHeader]; + [self changeHasHeaderCheckbox:self]; [self changeTableTarget:self]; [[self window] makeFirstResponder:fieldMapperTableView]; if([fieldMappingTableColumnNames count]) @@ -162,7 +164,7 @@ - (BOOL)importFieldNamesHeader { - return importFieldNamesHeader; + return ([importFieldNamesHeaderSwitch state] == NSOnState)?YES:NO; } #pragma mark - @@ -279,7 +281,9 @@ for(i=possibleImports; i>=0; i--) [fieldMappingArray replaceObjectAtIndex:possibleImports-i withObject:[NSNumber numberWithInteger:i]]; break; - + case 2: // try to align header and table target field names via Levenshtein distance + [self matchHeaderNames]; + break; } [fieldMapperTableView reloadData]; } @@ -304,6 +308,46 @@ [rowUpButton setEnabled:(fieldMappingCurrentRow != ([fieldMappingImportArray count]-1))]; } +- (IBAction)changeHasHeaderCheckbox:(id)sender +{ + [matchingNameMenuItem setEnabled:([importFieldNamesHeaderSwitch state] == NSOnState)?YES:NO]; +} + +- (IBAction)addGlobalSourceVariable:(id)sender +{ + +} + +#pragma mark - +#pragma mark Others + +- (void)matchHeaderNames +{ + if(![fieldMappingImportArray count]) return; + + NSMutableArray *fileHeaderNames = [NSMutableArray array]; + [fileHeaderNames setArray:NSArrayObjectAtIndex(fieldMappingImportArray, 0)]; + NSMutableArray *tableHeaderNames = [NSMutableArray array]; + [tableHeaderNames setArray:fieldMappingTableColumnNames]; + + NSInteger i,j; + NSMutableArray *matchedHeaderNames = [NSMutableArray array]; + for(i=0; i < [tableHeaderNames count]; i++) { + CGFloat minDist = 1e6; + NSInteger minIndex = 0; + for(j=0; j < [fileHeaderNames count]; j++) { + NSString *headerName = [NSArrayObjectAtIndex(fileHeaderNames,j) lowercaseString]; + CGFloat dist = [[NSArrayObjectAtIndex(tableHeaderNames,i) lowercaseString] levenshteinDistanceWithWord:headerName]; + if(dist < minDist && ![matchedHeaderNames containsObject:headerName]) { + minDist = dist; + minIndex = j; + } + if(dist == 0.0f) [matchedHeaderNames addObject:headerName]; + } + [fieldMappingArray replaceObjectAtIndex:i withObject:[NSNumber numberWithInteger:minIndex]]; + } +} + /* * Sets up the fieldMapping array to be shown in the tableView */ |