From 79156f6fccf7b49d02c95df0f43627b78584d80f Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Sun, 1 Mar 2009 16:50:55 +0000 Subject: - In the table status view use the user's long date format and medium time format when displaying the create and update date/time of the selected table. - In the table info pane set the date formatter's behaviour otherwise setting it's date and time style have no effect. Also consolidate the date formatting code into a single private method to reduce the amount of code duplication. --- Source/SPTableInfo.m | 133 +++++++++++++++++++++++++++------------------------ Source/TableStatus.m | 38 ++++++++++++--- 2 files changed, 102 insertions(+), 69 deletions(-) (limited to 'Source') diff --git a/Source/SPTableInfo.m b/Source/SPTableInfo.m index 3857f858..cb494ea1 100644 --- a/Source/SPTableInfo.m +++ b/Source/SPTableInfo.m @@ -31,6 +31,12 @@ #import +@interface SPTableInfo (PrivateAPI) + +- (NSString *)_getUserDefinedDateStringFromMySQLDate:(NSString *)mysqlDate; + +@end + @implementation SPTableInfo - (id)init @@ -49,7 +55,7 @@ name:NSTableViewSelectionDidChangeNotification object:tableList]; - [info addObject:NSLocalizedString(@"TABLE INFORMATION",@"header for table info pane")]; + [info addObject:NSLocalizedString(@"TABLE INFORMATION", @"header for table info pane")]; [infoTable reloadData]; } @@ -58,49 +64,10 @@ [[NSNotificationCenter defaultCenter] removeObserver:self]; [info release]; - + [super dealloc]; } -- (int)numberOfRowsInTableView:(NSTableView *)aTableView -{ - return [info count]; -} - -- (id)tableView:(NSTableView *)aTableView -objectValueForTableColumn:(NSTableColumn *)aTableColumn - row:(int)rowIndex -{ - return [info objectAtIndex:rowIndex]; -} - -- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex -{ - // row 1 and 6 should be editable - ie be able to rename the table and change the auto_increment value. - return NO;//(rowIndex == 1 || rowIndex == 6 ); -} - - -- (BOOL)tableView:(NSTableView *)aTableView isGroupRow:(int)row -{ - // This makes the top row (TABLE INFORMATION) have the diff styling - return (row == 0); -} - -- (void)tableView:(NSTableView *)aTableView - willDisplayCell:(id)aCell - forTableColumn:(NSTableColumn *)aTableColumn - row:(int)rowIndex -{ - if ((rowIndex > 0) && [[aTableColumn identifier] isEqualToString:@"info"]) { - [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:@"TablePropertyIcon"]]; - [(ImageAndTextCell*)aCell setIndentationLevel:1]; - } else { - [(ImageAndTextCell*)aCell setImage:nil]; - [(ImageAndTextCell*)aCell setIndentationLevel:0]; - } -} - - (void)tableChanged:(NSNotification *)notification { NSDictionary *tableStatus; @@ -117,12 +84,12 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn [info addObject:@"TABLE INFORMATION"]; - if ([tableListInstance tableName]) - { + if ([tableListInstance tableName]) { if ([[tableListInstance tableName] isEqualToString:@""]) { [info addObject:@"multiple tables"]; - } else { + } + else { // Retrieve the table status information via the data cache tableStatus = [tableDataInstance statusValues]; @@ -136,31 +103,15 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn // Check for "Create_time" == NULL if (![[tableStatus objectForKey:@"Create_time"] isNSNull]) { - // Set up our data formatter - NSDateFormatter *createDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; - [createDateFormatter setDateStyle:NSDateFormatterShortStyle]; - [createDateFormatter setTimeStyle:NSDateFormatterNoStyle]; - - // Convert our string date from the result to an NSDate. - NSDate *create_date = [NSDate dateWithNaturalLanguageString:[tableStatus objectForKey:@"Create_time"]]; - // Add the creation date to the infoTable - [info addObject:[NSString stringWithFormat:@"created: %@", [createDateFormatter stringFromDate:create_date]]]; + [info addObject:[NSString stringWithFormat:@"created: %@", [self _getUserDefinedDateStringFromMySQLDate:[tableStatus objectForKey:@"Create_time"]]]]; } // Check for "Update_time" == NULL - InnoDB tables don't have an update time if (![[tableStatus objectForKey:@"Update_time"] isNSNull]) { - - // Setup our data formatter - NSDateFormatter *updateDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; - [updateDateFormatter setDateStyle:NSDateFormatterShortStyle]; - [updateDateFormatter setTimeStyle:NSDateFormatterNoStyle]; - - // Convert our string date from the result to an NSDate. - NSDate *update_date = [NSDate dateWithNaturalLanguageString:[tableStatus objectForKey:@"Update_time"]]; - + // Add the update date to the infoTable - [info addObject:[NSString stringWithFormat:@"updated: %@", [updateDateFormatter stringFromDate:update_date]]]; + [info addObject:[NSString stringWithFormat:@"updated: %@", [self _getUserDefinedDateStringFromMySQLDate:[tableStatus objectForKey:@"Update_time"]]]]; } [info addObject:[NSString stringWithFormat:@"rows: ~%@", [tableStatus objectForKey:@"Rows"]]]; @@ -176,4 +127,60 @@ objectValueForTableColumn:(NSTableColumn *)aTableColumn [infoTable reloadData]; } +#pragma mark - +#pragma mark TableView datasource methods + +- (int)numberOfRowsInTableView:(NSTableView *)aTableView +{ + return [info count]; +} + +- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex +{ + return [info objectAtIndex:rowIndex]; +} + +- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex +{ + // row 1 and 6 should be editable - ie be able to rename the table and change the auto_increment value. + return NO;//(rowIndex == 1 || rowIndex == 6 ); +} + +- (BOOL)tableView:(NSTableView *)aTableView isGroupRow:(int)row +{ + // This makes the top row (TABLE INFORMATION) have the diff styling + return (row == 0); +} + +- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex +{ + if ((rowIndex > 0) && [[aTableColumn identifier] isEqualToString:@"info"]) { + [(ImageAndTextCell*)aCell setImage:[NSImage imageNamed:@"TablePropertyIcon"]]; + [(ImageAndTextCell*)aCell setIndentationLevel:1]; + } else { + [(ImageAndTextCell*)aCell setImage:nil]; + [(ImageAndTextCell*)aCell setIndentationLevel:0]; + } +} + +@end + +@implementation SPTableInfo (PrivateAPI) + +- (NSString *)_getUserDefinedDateStringFromMySQLDate:(NSString *)mysqlDate +{ + // Setup our data formatter + NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; + + [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; + + [dateFormatter setDateStyle:NSDateFormatterShortStyle]; + [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; + + // Convert our string date from the result to an NSDate. + NSDate *updateDate = [NSDate dateWithNaturalLanguageString:mysqlDate]; + + return [dateFormatter stringFromDate:updateDate]; +} + @end diff --git a/Source/TableStatus.m b/Source/TableStatus.m index d1a4d45b..4edaa4e0 100644 --- a/Source/TableStatus.m +++ b/Source/TableStatus.m @@ -17,16 +17,42 @@ - (NSString*)formatValueWithKey:(NSString *)aKey inDictionary:(NSDictionary*)statusDict withLabel:(NSString*)label { - NSString* value = [statusDict objectForKey:aKey]; - if([value isKindOfClass:[NSNull class]]) { + NSString *value = [statusDict objectForKey:aKey]; + + if ([value isKindOfClass:[NSNull class]]) { value = @"--"; - } else { - + } + else { // Format size strings - if ([aKey isEqualToString:@"Data_length"] || [aKey isEqualToString:@"Max_data_length"] - || [aKey isEqualToString:@"Index_length"] || [aKey isEqualToString:@"Data_free"]) { + if ([aKey isEqualToString:@"Data_length"] || + [aKey isEqualToString:@"Max_data_length"] || + [aKey isEqualToString:@"Index_length"] || + [aKey isEqualToString:@"Data_free"]) { + value = [NSString stringForByteSize:[value intValue]]; } + // Format date strings to the user's long date format + else if ([aKey isEqualToString:@"Create_time"] || + [aKey isEqualToString:@"Update_time"]) { + + // Create date formatter + NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; + + // Set the date format returned by MySQL + [dateFormatter setDateFormat:@"%Y-%m-%d %H:%M:%S"]; + + // Get the date instance + NSDate *date = [dateFormatter dateFromString:value]; + + // This behaviour should be set after the above date string is parsed to a date object so we can + // use the below style methods. + [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; + + [dateFormatter setDateStyle:NSDateFormatterLongStyle]; + [dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; + + value = [dateFormatter stringFromDate:date]; + } } NSString* labelVal = [NSString stringWithFormat:@"%@: %@", label, value]; -- cgit v1.2.3