|
38 | 38 | #import "SPTableStructureLoading.h"
|
39 | 39 | #import "SPServerSupport.h"
|
40 | 40 | #import "SPTablesList.h"
|
| 41 | +#import "SPPillAttachmentCell.h" |
41 | 42 |
|
42 | 43 | #import <SPMySQL/SPMySQL.h>
|
43 | 44 |
|
| 45 | +struct _cmpMap { |
| 46 | + NSString *title; // the title of the "pill" |
| 47 | + NSString *tooltipPart; // the tooltip of the menuitem |
| 48 | + NSString *cmpWith; // the string to match against |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * This function will compare the representedObject of every item in menu against |
| 53 | + * every map->cmpWith. If they match it will append a pill-like (similar to a TokenFieldCell's token) |
| 54 | + * element labelled map->title to the menu item's title. If map->tooltipPart is set, |
| 55 | + * it will also be added to the menu item's tooltip. |
| 56 | + * |
| 57 | + * This is used with the encoding/collation popup menus to add visual indicators for the |
| 58 | + * table-level and default encoding/collation. |
| 59 | + */ |
| 60 | +static void _BuildMenuWithPills(NSMenu *menu,struct _cmpMap *map,size_t mapEntries); |
| 61 | + |
44 | 62 | @interface SPTableStructure (PrivateAPI)
|
45 | 63 |
|
46 | 64 | - (void)sheetDidEnd:(id)sheet returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo;
|
@@ -69,6 +87,7 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum
|
69 | 87 | NSString *columnEncoding = [rowData objectForKey:@"encodingName"];
|
70 | 88 | NSString *columnCollation = [rowData objectForKey:@"collationName"]; // loadTable: has already inferred it, if not set explicit
|
71 | 89 |
|
| 90 | +#warning Building the collation menu here is a big performance hog. This should be done in menuNeedsUpdate: below! |
72 | 91 | NSPopUpButtonCell *collationCell = [tableColumn dataCell];
|
73 | 92 | [collationCell removeAllItems];
|
74 | 93 | [collationCell addItemWithTitle:@"dummy"];
|
@@ -698,4 +717,111 @@ - (NSString *)comboBoxCell:(NSComboBoxCell *)aComboBoxCell completedString:(NSSt
|
698 | 717 | return @"";
|
699 | 718 | }
|
700 | 719 |
|
| 720 | +#pragma mark - |
| 721 | +#pragma mark Menu delegate methods (encoding/collation dropdown menu) |
| 722 | + |
| 723 | +- (void)menuNeedsUpdate:(NSMenu *)menu |
| 724 | +{ |
| 725 | + //NOTE: NSTableView will usually copy the menu and call this method on the copy. Matching with == won't work! |
| 726 | + |
| 727 | + //walk through the menu and clear the attributedTitle if set. This will remove the gray color from the default items |
| 728 | + for(NSMenuItem *item in [menu itemArray]) { |
| 729 | + if([item attributedTitle]) { |
| 730 | + [item setAttributedTitle:nil]; |
| 731 | + } |
| 732 | + } |
| 733 | + |
| 734 | + NSDictionary *rowData = NSArrayObjectAtIndex(tableFields, [tableSourceView selectedRow]); |
| 735 | + |
| 736 | + if([[menu title] isEqualToString:@"encodingPopupMenu"]) { |
| 737 | + NSString *tableEncoding = [tableDataInstance tableEncoding]; |
| 738 | + //NSString *databaseEncoding = [databaseDataInstance getDatabaseDefaultCharacterSet]; |
| 739 | + //NSString *serverEncoding = [databaseDataInstance getServerDefaultCharacterSet]; |
| 740 | + |
| 741 | + struct _cmpMap defaultCmp[] = { |
| 742 | + { |
| 743 | + NSLocalizedString(@"Table",@"Table Structure : Encoding dropdown : 'item is table default' marker"), |
| 744 | + [NSString stringWithFormat:NSLocalizedString(@"This is the default encoding of table “%@”.", @"Table Structure : Encoding dropdown : table marker tooltip"),selectedTable], |
| 745 | + tableEncoding |
| 746 | + }, |
| 747 | + /* //we could, but that might confuse users even more plus there is no inheritance between a columns charset and the db/server default |
| 748 | + { |
| 749 | + NSLocalizedString(@"Database",@"Table Structure : Encoding dropdown : 'item is database default' marker"), |
| 750 | + [NSString stringWithFormat:NSLocalizedString(@"This is the default encoding of database “%@”.", @"Table Structure : Encoding dropdown : database marker tooltip"),[tableDocumentInstance database]], |
| 751 | + databaseEncoding |
| 752 | + }, |
| 753 | + { |
| 754 | + NSLocalizedString(@"Server",@"Table Structure : Encoding dropdown : 'item is server default' marker"), |
| 755 | + NSLocalizedString(@"This is the default encoding of this server.", @"Table Structure : Encoding dropdown : server marker tooltip"), |
| 756 | + serverEncoding |
| 757 | + } */ |
| 758 | + }; |
| 759 | + |
| 760 | + _BuildMenuWithPills(menu, defaultCmp, COUNT_OF(defaultCmp)); |
| 761 | + } |
| 762 | + else if([[menu title] isEqualToString:@"collationPopupMenu"]) { |
| 763 | + NSString *encoding = [rowData objectForKey:@"encodingName"]; |
| 764 | + NSString *encodingDefaultCollation = [databaseDataInstance getDefaultCollationForEncoding:encoding]; |
| 765 | + NSString *tableCollation = [tableDataInstance statusValueForKey:@"Collation"]; |
| 766 | + //NSString *databaseCollation = [databaseDataInstance getDatabaseDefaultCollation]; |
| 767 | + //NSString *serverCollation = [databaseDataInstance getServerDefaultCollation]; |
| 768 | + |
| 769 | + struct _cmpMap defaultCmp[] = { |
| 770 | + { |
| 771 | + NSLocalizedString(@"Default",@"Table Structure : Collation dropdown : 'item is the same as the default collation of the row's charset' marker"), |
| 772 | + [NSString stringWithFormat:NSLocalizedString(@"This is the default collation of encoding “%@”.", @"Table Structure : Collation dropdown : default marker tooltip"),encoding], |
| 773 | + encodingDefaultCollation |
| 774 | + }, |
| 775 | + { |
| 776 | + NSLocalizedString(@"Table",@"Table Structure : Collation dropdown : 'item is the same as the collation of table' marker"), |
| 777 | + [NSString stringWithFormat:NSLocalizedString(@"This is the default collation of table “%@”.", @"Table Structure : Collation dropdown : table marker tooltip"),selectedTable], |
| 778 | + tableCollation |
| 779 | + }, |
| 780 | + /* // see the comment for charset above |
| 781 | + { |
| 782 | + NSLocalizedString(@"Database",@"Table Structure : Collation dropdown : 'item is the same as the collation of database' marker"), |
| 783 | + [NSString stringWithFormat:NSLocalizedString(@"This is the default collation of database “%@”.", @"Table Structure : Collation dropdown : database marker tooltip"),[tableDocumentInstance database]], |
| 784 | + databaseCollation |
| 785 | + }, |
| 786 | + { |
| 787 | + NSLocalizedString(@"Server",@"Table Structure : Collation dropdown : 'item is the same as the collation of server' marker"), |
| 788 | + NSLocalizedString(@"This is the default collation of this server.", @"Table Structure : Collation dropdown : server marker tooltip"), |
| 789 | + serverCollation |
| 790 | + } */ |
| 791 | + }; |
| 792 | + |
| 793 | + _BuildMenuWithPills(menu, defaultCmp, COUNT_OF(defaultCmp)); |
| 794 | + } |
| 795 | +} |
| 796 | + |
701 | 797 | @end
|
| 798 | + |
| 799 | +void _BuildMenuWithPills(NSMenu *menu,struct _cmpMap *map,size_t mapEntries) |
| 800 | +{ |
| 801 | + NSDictionary *baseAttrs = @{NSFontAttributeName:[menu font],NSParagraphStyleAttributeName: [NSParagraphStyle defaultParagraphStyle]}; |
| 802 | + |
| 803 | + for(NSMenuItem *item in [menu itemArray]) { |
| 804 | + NSMutableAttributedString *itemStr = [[NSMutableAttributedString alloc] initWithString:[item title] attributes:baseAttrs]; |
| 805 | + NSString *value = [item representedObject]; |
| 806 | + |
| 807 | + NSMutableArray *tooltipParts = [NSMutableArray array]; |
| 808 | + for (unsigned int i = 0; i < mapEntries; ++i) { |
| 809 | + struct _cmpMap *cmp = &map[i]; |
| 810 | + if([cmp->cmpWith isEqualToString:value]) { |
| 811 | + SPPillAttachmentCell *cell = [[SPPillAttachmentCell alloc] init]; |
| 812 | + [cell setStringValue:cmp->title]; |
| 813 | + NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; |
| 814 | + [attachment setAttachmentCell:[cell autorelease]]; |
| 815 | + NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:[attachment autorelease]]; |
| 816 | + |
| 817 | + [[itemStr mutableString] appendString:@" "]; |
| 818 | + [itemStr appendAttributedString:attachmentString]; |
| 819 | + |
| 820 | + if(cmp->tooltipPart) [tooltipParts addObject:cmp->tooltipPart]; |
| 821 | + } |
| 822 | + } |
| 823 | + if([tooltipParts count]) [item setToolTip:[tooltipParts componentsJoinedByString:@" "]]; |
| 824 | + |
| 825 | + [item setAttributedTitle:[itemStr autorelease]]; |
| 826 | + } |
| 827 | +} |
0 commit comments