Skip to content

Commit 74a9025

Browse files
committed
Fix an issue where changing a table collation could cause an exception (fixes #2320)
This issue probably was introduced in f02fb78 The empty item at the top of the collation list will now no longer have a selection mark, though.
1 parent 2ae3aec commit 74a9025

File tree

5 files changed

+121
-7
lines changed

5 files changed

+121
-7
lines changed

Interfaces/English.lproj/DBView.xib

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26580,6 +26580,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
2658026580
</object>
2658126581
<string key="7491.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
2658226582
<boolean value="NO" key="7491.showNotes"/>
26583+
<string key="7493.CustomClassName">SPPopUpButtonCell</string>
2658326584
<string key="7493.IBPluginDependency">com.apple.InterfaceBuilder.CocoaPlugin</string>
2658426585
<boolean value="NO" key="7493.showNotes"/>
2658526586
<string key="7494.CustomClassName">SPIdMenu</string>
@@ -30321,6 +30322,14 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
3032130322
<string key="minorKey">../Source/SPIndexesController.m</string>
3032230323
</object>
3032330324
</object>
30325+
<object class="IBPartialClassDescription">
30326+
<string key="className">SPPopUpButtonCell</string>
30327+
<string key="superclassName">NSPopUpButtonCell</string>
30328+
<object class="IBClassDescriptionSource" key="sourceIdentifier">
30329+
<string key="majorKey">IBProjectSource</string>
30330+
<string key="minorKey">../Source/SPPopUpButtonCell.h</string>
30331+
</object>
30332+
</object>
3032430333
<object class="IBPartialClassDescription">
3032530334
<string key="className">SPProcessListController</string>
3032630335
<string key="superclassName">NSWindowController</string>

Source/SPPopUpButtonCell.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// SPPopUpButtonCell.h
3+
// sequel-pro
4+
//
5+
// Created by Max Lohrmann on 10.11.15.
6+
// Copyright (c) 2015 Max Lohrmann. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the "Software"), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
//
29+
// More info at <https://github.com/sequelpro/sequelpro>
30+
31+
#import <Cocoa/Cocoa.h>
32+
33+
/**
34+
* The only difference here is that objectValue/setObjectValue:
35+
* will use the representedObject of the menu instead of the menu item's index
36+
*
37+
* nil will act the same as @(-1) would with the regular NSPopUpButtonCell.
38+
*
39+
* Note that in theory multiple menu items could have the same representedObject
40+
* and identification is done via isEqual: so this class would only ever
41+
* pick the first one.
42+
*/
43+
@interface SPPopUpButtonCell : NSPopUpButtonCell
44+
45+
- (id)objectValue;
46+
- (void)setObjectValue:(id)objectValue;
47+
48+
@end

Source/SPPopUpButtonCell.m

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// SPPopUpButtonCell.m
3+
// sequel-pro
4+
//
5+
// Created by Max Lohrmann on 10.11.15.
6+
// Copyright (c) 2015 Max Lohrmann. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the "Software"), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
//
29+
// More info at <https://github.com/sequelpro/sequelpro>
30+
31+
#import "SPPopUpButtonCell.h"
32+
33+
@implementation SPPopUpButtonCell
34+
35+
- (id)objectValue
36+
{
37+
NSInteger n = [[super objectValue] integerValue];
38+
39+
// this method can be called for invalid selections, which return -1, which fails with itemAtIndex:
40+
if(n < 0 || n >= [self numberOfItems]) return nil;
41+
42+
return [[self itemAtIndex:n] representedObject];
43+
}
44+
45+
- (void)setObjectValue:(id)objectValue
46+
{
47+
NSInteger n = [self indexOfItemWithRepresentedObject:objectValue];
48+
[super setObjectValue:@(n)];
49+
}
50+
51+
@end

Source/SPTableStructureDelegate.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum
102102
[[collationCell lastItem] setTitle:@""];
103103

104104
//if this is not set the column either has no encoding (numeric etc.) or retrieval failed. Either way we can't provide collations
105-
if(columnEncoding) {
105+
if([columnEncoding length]) {
106106
collations = [databaseDataInstance getDatabaseCollationsForEncoding:columnEncoding];
107107

108108
if ([collations count] > 0) {
@@ -129,19 +129,18 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum
129129
}
130130
}
131131

132-
//look up the right item
133-
NSInteger idx = [collationCell indexOfItemWithRepresentedObject:columnCollation];
134-
if(idx > 0) return @(idx);
132+
// the popup cell is subclassed to take the representedObject instead of the item index
133+
return columnCollation;
135134
}
136135
}
137136

138-
return @0;
137+
return nil;
139138
}
140139
else if ([[tableColumn identifier] isEqualToString:@"encoding"]) {
141140
// the encoding menu was already configured during setTableDetails:
142141
NSString *columnEncoding = [rowData objectForKey:@"encodingName"];
143142

144-
if(columnEncoding) {
143+
if([columnEncoding length]) {
145144
NSInteger idx = [encodingPopupCell indexOfItemWithRepresentedObject:columnEncoding];
146145
if(idx > 0) return @(idx);
147146
}
@@ -193,7 +192,8 @@ - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTable
193192
return;
194193
}
195194
else if ([[aTableColumn identifier] isEqualToString:@"collation"]) {
196-
NSString *newCollation = [[(NSPopUpButtonCell *)[aTableColumn dataCell] itemAtIndex:[anObject integerValue]] representedObject];
195+
//the popup button is subclassed to return the representedObject instead of the item index
196+
NSString *newCollation = anObject;
197197

198198
if(!newCollation)
199199
[currentRow removeObjectForKey:@"collationName"];

sequel-pro.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
507FF26A1BC8450100104523 /* SPExportSettingsPersistence.m in Sources */ = {isa = PBXBuildFile; fileRef = 507FF2691BC8450100104523 /* SPExportSettingsPersistence.m */; };
201201
507FF2A11BCD27A700104523 /* SPFunctions.m in Sources */ = {isa = PBXBuildFile; fileRef = 507FF1111BBCC57600104523 /* SPFunctions.m */; };
202202
507FF2A21BCD27AE00104523 /* SPOSInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 50EAB5B71A8FBB08008F627A /* SPOSInfo.m */; };
203+
50805B0D1BF2A068005F7A99 /* SPPopUpButtonCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 50805B0C1BF2A068005F7A99 /* SPPopUpButtonCell.m */; };
203204
5089B0271BE714E300E226CD /* SPIdMenu.m in Sources */ = {isa = PBXBuildFile; fileRef = 5089B0261BE714E300E226CD /* SPIdMenu.m */; };
204205
50A9F8B119EAD4B90053E571 /* SPGotoDatabaseController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A9F8B019EAD4B90053E571 /* SPGotoDatabaseController.m */; };
205206
50D3C3491A75B8A800B5429C /* GotoDatabaseDialog.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50D3C34B1A75B8A800B5429C /* GotoDatabaseDialog.xib */; };
@@ -923,6 +924,8 @@
923924
507FF1111BBCC57600104523 /* SPFunctions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPFunctions.m; sourceTree = "<group>"; };
924925
507FF2681BC8450100104523 /* SPExportSettingsPersistence.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPExportSettingsPersistence.h; sourceTree = "<group>"; };
925926
507FF2691BC8450100104523 /* SPExportSettingsPersistence.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPExportSettingsPersistence.m; sourceTree = "<group>"; };
927+
50805B0B1BF2A068005F7A99 /* SPPopUpButtonCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPPopUpButtonCell.h; sourceTree = "<group>"; };
928+
50805B0C1BF2A068005F7A99 /* SPPopUpButtonCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPPopUpButtonCell.m; sourceTree = "<group>"; };
926929
5089B0251BE714E300E226CD /* SPIdMenu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPIdMenu.h; sourceTree = "<group>"; };
927930
5089B0261BE714E300E226CD /* SPIdMenu.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPIdMenu.m; sourceTree = "<group>"; };
928931
50A9F8AF19EAD4B90053E571 /* SPGotoDatabaseController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPGotoDatabaseController.h; sourceTree = "<group>"; };
@@ -2326,6 +2329,8 @@
23262329
17FDB04B1280778B00DBBBC2 /* SPFontPreviewTextField.m */,
23272330
58D2A6A516FBDEFF002EB401 /* SPComboPopupButton.h */,
23282331
58D2A6A616FBDEFF002EB401 /* SPComboPopupButton.m */,
2332+
50805B0B1BF2A068005F7A99 /* SPPopUpButtonCell.h */,
2333+
50805B0C1BF2A068005F7A99 /* SPPopUpButtonCell.m */,
23292334
);
23302335
name = Controls;
23312336
sourceTree = "<group>";
@@ -3286,6 +3291,7 @@
32863291
173C837A11AAD2AE00B8B084 /* SPHTMLExporter.m in Sources */,
32873292
173C837B11AAD2AE00B8B084 /* SPPDFExporter.m in Sources */,
32883293
173C839011AAD32A00B8B084 /* SPCSVExporterDelegate.m in Sources */,
3294+
50805B0D1BF2A068005F7A99 /* SPPopUpButtonCell.m in Sources */,
32893295
173C839111AAD32A00B8B084 /* SPDotExporterDelegate.m in Sources */,
32903296
173C839211AAD32A00B8B084 /* SPHTMLExporterDelegate.m in Sources */,
32913297
173C839311AAD32A00B8B084 /* SPPDFExporterDelegate.m in Sources */,

0 commit comments

Comments
 (0)