Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix the charset/collation selection when creating a new db/table (min…
…or leftover from #2237)
  • Loading branch information
dmoagx committed Nov 3, 2015
1 parent edd4ae3 commit 47d9bf6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
18 changes: 9 additions & 9 deletions Interfaces/English.lproj/DBView.xib
Expand Up @@ -7474,14 +7474,14 @@
<object class="NSWindowTemplate" id="971880172">
<int key="NSWindowStyleMask">9</int>
<int key="NSWindowBacking">2</int>
<string key="NSWindowRect">{{343, 433}, {384, 162}}</string>
<string key="NSWindowRect">{{343, 433}, {425, 162}}</string>
<int key="NSWTFlags">1886912512</int>
<string key="NSWindowTitle">New Table</string>
<string key="NSWindowClass">NSWindow</string>
<nil key="NSViewClass"/>
<nil key="NSUserInterfaceItemIdentifier"/>
<string key="NSWindowContentMaxSize">{600, 162}</string>
<string key="NSWindowContentMinSize">{384, 142}</string>
<string key="NSWindowContentMinSize">{425, 162}</string>
<object class="NSView" key="NSWindowView" id="381160134">
<nil key="NSNextResponder"/>
<int key="NSvFlags">256</int>
Expand Down Expand Up @@ -7527,7 +7527,7 @@
<object class="NSTextField" id="374259664">
<reference key="NSNextResponder" ref="381160134"/>
<int key="NSvFlags">258</int>
<string key="NSFrame">{{138, 124}, {226, 18}}</string>
<string key="NSFrame">{{138, 124}, {267, 18}}</string>
<reference key="NSSuperview" ref="381160134"/>
<reference key="NSNextKeyView" ref="513414376"/>
<bool key="NSEnabled">YES</bool>
Expand All @@ -7547,7 +7547,7 @@
<object class="NSButton" id="671615720">
<reference key="NSNextResponder" ref="381160134"/>
<int key="NSvFlags">257</int>
<string key="NSFrame">{{291, 13}, {78, 28}}</string>
<string key="NSFrame">{{332, 13}, {78, 28}}</string>
<reference key="NSSuperview" ref="381160134"/>
<string key="NSHuggingPriority">{250, 750}</string>
<int key="NSTag">1</int>
Expand All @@ -7572,7 +7572,7 @@
<object class="NSPopUpButton" id="1050266816">
<reference key="NSNextResponder" ref="381160134"/>
<int key="NSvFlags">258</int>
<string key="NSFrame">{{135, 95}, {232, 22}}</string>
<string key="NSFrame">{{135, 95}, {273, 22}}</string>
<reference key="NSSuperview" ref="381160134"/>
<reference key="NSNextKeyView" ref="289339603"/>
<bool key="NSEnabled">YES</bool>
Expand Down Expand Up @@ -7624,7 +7624,7 @@
<object class="NSPopUpButton" id="467430961">
<reference key="NSNextResponder" ref="381160134"/>
<int key="NSvFlags">258</int>
<string key="NSFrame">{{135, 70}, {232, 22}}</string>
<string key="NSFrame">{{135, 70}, {273, 22}}</string>
<reference key="NSSuperview" ref="381160134"/>
<reference key="NSNextKeyView" ref="725803993"/>
<bool key="NSEnabled">YES</bool>
Expand Down Expand Up @@ -7676,7 +7676,7 @@
<object class="NSPopUpButton" id="189903457">
<reference key="NSNextResponder" ref="381160134"/>
<int key="NSvFlags">258</int>
<string key="NSFrame">{{135, 45}, {232, 22}}</string>
<string key="NSFrame">{{135, 45}, {273, 22}}</string>
<reference key="NSSuperview" ref="381160134"/>
<reference key="NSNextKeyView" ref="635170907"/>
<bool key="NSEnabled">YES</bool>
Expand Down Expand Up @@ -7709,7 +7709,7 @@
<object class="NSButton" id="635170907">
<reference key="NSNextResponder" ref="381160134"/>
<int key="NSvFlags">257</int>
<string key="NSFrame">{{209, 13}, {84, 28}}</string>
<string key="NSFrame">{{250, 13}, {84, 28}}</string>
<reference key="NSSuperview" ref="381160134"/>
<reference key="NSNextKeyView" ref="671615720"/>
<string key="NSHuggingPriority">{250, 750}</string>
Expand All @@ -7731,7 +7731,7 @@
<bool key="NSAllowsLogicalLayoutDirection">NO</bool>
</object>
</array>
<string key="NSFrameSize">{384, 162}</string>
<string key="NSFrameSize">{425, 162}</string>
<reference key="NSNextKeyView" ref="1005413998"/>
</object>
<string key="NSScreenRect">{{0, 0}, {1920, 1178}}</string>
Expand Down
22 changes: 22 additions & 0 deletions Source/SPCharsetCollationHelper.h
Expand Up @@ -51,6 +51,7 @@
NSString *selectedCharset;
NSString *selectedCollation;
NSString *defaultCharsetFormatString;
NSString *defaultCollationFormatString;
NSString *_oldCharset;

BOOL _enabled;
Expand Down Expand Up @@ -105,6 +106,27 @@
*/
@property(readwrite,retain) NSString *defaultCharsetFormatString;

/**
* This is the format string that will be used for formatting the defaultCollation.
* It must contain one %@ variable (the collation name).
*
* Note that this is only used as long as the user keeps the _implicit_ defaultCharset.
* E.g. if the charset menu has those items:
* @code
* Inherited from db (latin1)
* -------------------------------
* ...
* cp1521 Western Europe (latin1)
* ...
* @endcode
* This item will only be used for the FIRST appearance of latin1.
* If the user picks ANY item below the line (thus making the charset explicit) both the defaultCollation
* and the defaultCollationFormatString no longer apply and the item at the top of
* the collation list will be the global default for the given charset (not the inherited one)
* and named "Default (...)", because that is how MySQL applies the settings.
*/
@property(readwrite,retain) NSString *defaultCollationFormatString;

/**
* Set this to YES before showing the UI and NO after dismissing it.
* This will cause the charsets to be re-read and the selection to be reset.
Expand Down
27 changes: 16 additions & 11 deletions Source/SPCharsetCollationHelper.m
Expand Up @@ -53,6 +53,7 @@ @implementation SPCharsetCollationHelper
@synthesize selectedCharset;
@synthesize selectedCollation;
@synthesize defaultCharsetFormatString;
@synthesize defaultCollationFormatString;
@synthesize _oldCharset;

- (id)initWithCharsetButton:(NSPopUpButton *)aCharsetButton CollationButton:(NSPopUpButton *)aCollationButton
Expand All @@ -63,7 +64,8 @@ - (id)initWithCharsetButton:(NSPopUpButton *)aCharsetButton CollationButton:(NSP
self = [super init];
if (self != nil) {
[self setPromoteUTF8:YES];
[self setDefaultCharsetFormatString:NSLocalizedString(@"Default (%@)",@"Charset Dropdown : Default item ($1 = name)")];
[self setDefaultCharsetFormatString:NSLocalizedString(@"Default (%@)",@"Charset Dropdown : Default item ($1 = charset name)")];
[self setDefaultCollationFormatString:NSLocalizedString(@"Default (%@)",@"Collation Dropdown : Default collation for given charset ($1 = collation name)")];
charsetButton = aCharsetButton;
collationButton = aCollationButton;
//connect the charset button with ourselves
Expand Down Expand Up @@ -210,6 +212,7 @@ - (void)refreshCollations {

//get the charset id
NSString *charsetId = [[charsetButton selectedItem] representedObject];
BOOL charsetIsInherited = ([self selectedCharset] == nil);

//now let's get the list of collations for the selected charset id
NSArray *applicableCollations = [databaseData getDatabaseCollationsForEncoding:charsetId];
Expand All @@ -220,16 +223,18 @@ - (void)refreshCollations {

//add a separator
[[collationButton menu] addItem:[NSMenuItem separatorItem]];

//if this is the defaultCharset and we have a defaultCollation use that instead
BOOL useGivenDefaultCollation = (defaultCharset && defaultCollation && [charsetId isEqualToString:defaultCharset]);

if(useGivenDefaultCollation) {
NSString *userDefaultCollateTitle = [NSString stringWithFormat:fmtStrDefaultId,defaultCollation];

// there are two kinds of default collations:
// - the inherited default (which is only used if NEITHER charset NOR collation is explicitly set), and
// - the charset default (which is used if charset is explicitly set, but collation is not)
// - that even applies if the selectedCharset is the same as the defaultCharset!
if(charsetIsInherited) {
// implies [charsetId isEqualToString:defaultCharset]
NSString *userInheritedCollateTitle = [NSString stringWithFormat:defaultCollationFormatString,defaultCollation];
//remove the dummy default item.
[collationButton removeItemAtIndex:0];
//add it to the top of the list
[collationButton insertItemWithTitle:userDefaultCollateTitle atIndex:0];
[collationButton insertItemWithTitle:userInheritedCollateTitle atIndex:0];
}

//add the real items
Expand All @@ -238,16 +243,16 @@ - (void)refreshCollations {
NSString *collationName = [collation objectForKey:@"COLLATION_NAME"];
[collationButton addItemWithTitle:collationName];

//is this the default collation for this charset (and we didn't override it)?
if(!useGivenDefaultCollation && [[collation objectForKey:@"IS_DEFAULT"] isEqualToString:@"Yes"]) {
//is this the default collation for this charset and charset was given explicitly (ie. breaking inheritance)?
if(!charsetIsInherited && [[collation objectForKey:@"IS_DEFAULT"] isEqualToString:@"Yes"]) {
NSString *defaultCollateTitle = [NSString stringWithFormat:fmtStrDefaultId,collationName];
//remove the dummy default item.
[collationButton removeItemAtIndex:0];
//add it to the top of the list
[collationButton insertItemWithTitle:defaultCollateTitle atIndex:0];
}
}
//reset selection to first item (it may moved when adding the default item)
//reset selection to first item (it may have moved when adding the default item)
[collationButton selectItemAtIndex:0];

//honor selectedCollation
Expand Down
2 changes: 2 additions & 0 deletions Source/SPDatabaseDocument.m
Expand Up @@ -809,6 +809,8 @@ - (IBAction)addDatabase:(id)sender

// Setup the charset and collation dropdowns
[addDatabaseCharsetHelper setDatabaseData:databaseDataInstance];
[addDatabaseCharsetHelper setDefaultCharsetFormatString:NSLocalizedString(@"Server Default (%@)", @"Add Database : Charset dropdown : default item ($1 = charset name)")];
[addDatabaseCharsetHelper setDefaultCollationFormatString:NSLocalizedString(@"Server Default (%@)", @"Add Database : Collation dropdown : default item ($1 = collation name)")];
[addDatabaseCharsetHelper setServerSupport:serverSupport];
[addDatabaseCharsetHelper setPromoteUTF8:YES];
[addDatabaseCharsetHelper setSelectedCharset:nil];
Expand Down
1 change: 1 addition & 0 deletions Source/SPTablesList.m
Expand Up @@ -391,6 +391,7 @@ - (IBAction)addTable:(id)sender
[addTableCharsetHelper setServerSupport:[tableDocumentInstance serverSupport]];
[addTableCharsetHelper setPromoteUTF8:YES];
[addTableCharsetHelper setDefaultCharsetFormatString:NSLocalizedString(@"Inherit from database (%@)", @"New Table Sheet : Table Encoding Dropdown : Default inherited from database")];
[addTableCharsetHelper setDefaultCollationFormatString:NSLocalizedString(@"Inherit from database (%@)", @"New Table Sheet : Table Collation Dropdown : Default inherited from database")];
[addTableCharsetHelper setDefaultCharset:[databaseDataInstance getDatabaseDefaultCharacterSet]];
[addTableCharsetHelper setDefaultCollation:[databaseDataInstance getDatabaseDefaultCollation]];
[addTableCharsetHelper setSelectedCharset:nil]; //reset to not carry over state from last time sheet was shown
Expand Down

0 comments on commit 47d9bf6

Please sign in to comment.