From 83a95680c9047e0b1136d6d08f2021fced6fe352 Mon Sep 17 00:00:00 2001 From: Marco Conti Date: Mon, 11 Sep 2017 08:01:00 +0200 Subject: [PATCH] Rename TableConfiguration class --- EasyTables.xcodeproj/project.pbxproj | 8 ++-- ...bleConfiguration.swift => EasyTable.swift} | 38 ++++++++++--------- EasyTablesExample/ViewController.swift | 10 ++--- 3 files changed, 30 insertions(+), 26 deletions(-) rename EasyTables/{TableConfiguration.swift => EasyTable.swift} (89%) diff --git a/EasyTables.xcodeproj/project.pbxproj b/EasyTables.xcodeproj/project.pbxproj index b702c64..1e958ea 100644 --- a/EasyTables.xcodeproj/project.pbxproj +++ b/EasyTables.xcodeproj/project.pbxproj @@ -12,7 +12,7 @@ 5415A6AF1F3F05D8000FF9BB /* ClosureControls.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5415A6AA1F3F002B000FF9BB /* ClosureControls.framework */; }; 5415A6B11F3F05EB000FF9BB /* ClosureControls.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 5415A6AA1F3F002B000FF9BB /* ClosureControls.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 5415A6B61F3F0BA4000FF9BB /* EasyTables.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 54CEC1C61F21D4DE00FA3BC6 /* EasyTables.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - 5415A6B81F3F0FB5000FF9BB /* TableConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CEC20D1F21D6D900FA3BC6 /* TableConfiguration.swift */; }; + 5415A6B81F3F0FB5000FF9BB /* EasyTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CEC20D1F21D6D900FA3BC6 /* EasyTable.swift */; }; 5415A6BD1F3F2396000FF9BB /* NSTableView+Easy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5415A6BC1F3F2396000FF9BB /* NSTableView+Easy.swift */; }; 54CEC1D01F21D4DE00FA3BC6 /* EasyTables.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54CEC1C61F21D4DE00FA3BC6 /* EasyTables.framework */; }; 54CEC1D51F21D4DE00FA3BC6 /* EasyTablesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54CEC1D41F21D4DE00FA3BC6 /* EasyTablesTests.swift */; }; @@ -64,7 +64,7 @@ 54CEC1F61F21D52500FA3BC6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 54CEC1F91F21D52500FA3BC6 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 54CEC1FB1F21D52500FA3BC6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 54CEC20D1F21D6D900FA3BC6 /* TableConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableConfiguration.swift; sourceTree = ""; }; + 54CEC20D1F21D6D900FA3BC6 /* EasyTable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EasyTable.swift; sourceTree = ""; }; 54CEC20F1F21D85D00FA3BC6 /* GenericTableDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GenericTableDataSource.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -131,7 +131,7 @@ children = ( 54CEC1C91F21D4DE00FA3BC6 /* EasyTables.h */, 54CEC1CA1F21D4DE00FA3BC6 /* Info.plist */, - 54CEC20D1F21D6D900FA3BC6 /* TableConfiguration.swift */, + 54CEC20D1F21D6D900FA3BC6 /* EasyTable.swift */, 54CEC20F1F21D85D00FA3BC6 /* GenericTableDataSource.swift */, 5415A6BC1F3F2396000FF9BB /* NSTableView+Easy.swift */, ); @@ -308,7 +308,7 @@ files = ( 5415A6BD1F3F2396000FF9BB /* NSTableView+Easy.swift in Sources */, 54CEC2101F21D85D00FA3BC6 /* GenericTableDataSource.swift in Sources */, - 5415A6B81F3F0FB5000FF9BB /* TableConfiguration.swift in Sources */, + 5415A6B81F3F0FB5000FF9BB /* EasyTable.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/EasyTables/TableConfiguration.swift b/EasyTables/EasyTable.swift similarity index 89% rename from EasyTables/TableConfiguration.swift rename to EasyTables/EasyTable.swift index d26f5b2..dae077b 100644 --- a/EasyTables/TableConfiguration.swift +++ b/EasyTables/EasyTable.swift @@ -66,11 +66,15 @@ let TextCellViewIdentifier = "EasyDialogs_TextCellViewIdentifier" /// Table data source backed by an array /// At initialization, it is associated with a specific instance of NSTableView /// and will create the necessary columns on that NSTableView -public class TableConfiguration { +public class EasyTableSource { /// Internal data source associated with the table public let dataSource: GenericTableDataSource + public var table: NSTableView { + return self.dataSource.table + } + /// Creates a table configuration and applies it to a table /// The configuration needs to be retained as long as the table /// is retained @@ -79,17 +83,19 @@ public class TableConfiguration { /// column in the table is discarded /// - parameter contextMenuOperations: entries of the contextual menu to display /// when right-clicking on a table row - /// - parameter table: the table to apply this configuration to + /// - parameter table: the table to apply this configuration to. If not specified, + /// will create a new one /// - allowMultipleSelection: whether multiple rows can be selected in the table public init(initialObjects: Objects, columns: [ColumnDefinition], contextMenuOperations: [ObjectOperation], - table: NSTableView, + table: NSTableView? = nil, allowMultipleSelection: Bool, selectionCallback: @escaping ([Object])->(Void)) where Objects.Iterator.Element == Object { + let table = table ?? NSTableView() self.dataSource = GenericTableDataSource( initialObjects: Array(initialObjects), columns: columns, @@ -107,8 +113,8 @@ public class TableConfiguration { columnsLookup[$0.name.lowercased()] = $0 } - TableConfiguration.setupTable(table, columns: columns, multiSelection: allowMultipleSelection) - self.setupMenu(for: table, operations: contextMenuOperations) + self.setupTable(columns: columns, multiSelection: allowMultipleSelection) + self.setupMenu(operations: contextMenuOperations) } /// Content of the table @@ -122,17 +128,16 @@ public class TableConfiguration { } } -extension TableConfiguration { +extension EasyTableSource { /// Sets up table columns and selection methods - fileprivate static func setupTable( - _ table: NSTableView, + fileprivate func setupTable( columns: [ColumnDefinition], multiSelection: Bool) { - let preColumns = table.tableColumns + let preColumns = self.table.tableColumns preColumns.forEach { - table.removeTableColumn($0) + self.table.removeTableColumn($0) } columns.forEach { cdef in @@ -150,19 +155,18 @@ extension TableConfiguration { table.addTableColumn(column) } - table.allowsEmptySelection = true - table.allowsColumnSelection = false - table.allowsTypeSelect = true - table.allowsMultipleSelection = multiSelection + self.table.allowsEmptySelection = true + self.table.allowsColumnSelection = false + self.table.allowsTypeSelect = true + self.table.allowsMultipleSelection = multiSelection } /// Sets up the contextual menu for the table fileprivate func setupMenu( - for table: NSTableView, operations: [ObjectOperation] ) { guard !operations.isEmpty else { - table.menu = nil + self.table.menu = nil return } let menu = NSMenu() @@ -176,7 +180,7 @@ extension TableConfiguration { menu.addItem(item) item.isEnabled = true } - table.menu = menu + self.table.menu = menu } /// Objects that should be affected by a contextual operation diff --git a/EasyTablesExample/ViewController.swift b/EasyTablesExample/ViewController.swift index ef611e9..1b80bf9 100644 --- a/EasyTablesExample/ViewController.swift +++ b/EasyTablesExample/ViewController.swift @@ -26,7 +26,7 @@ import EasyTables /// An example of how to use `TableConfiguration` class ViewController: NSViewController { - var configuration: TableConfiguration! + var tableSource: EasyTableSource! var objects = Set(["Action", "Engineering", "Cod", "Doodle"]) @@ -38,7 +38,7 @@ class ViewController: NSViewController { scroll.createConstraintsToFillParent(self.view) - self.configuration = TableConfiguration( + self.tableSource = EasyTableSource( initialObjects: objects, columns: [ ColumnDefinition("Word", { $0 }), @@ -52,7 +52,7 @@ class ViewController: NSViewController { items.forEach { self.objects.remove($0) } - self.configuration.setContent(self.objects) // changing the content automatically updates the table + self.tableSource.setContent(self.objects) // changing the content automatically updates the table }), // Uppercase the string(s) ObjectOperation(label: "Convert to uppercase", action: { @@ -62,7 +62,7 @@ class ViewController: NSViewController { self.objects.remove($0) self.objects.insert($0.uppercased()) } - self.configuration.setContent(self.objects) // changing the content automatically updates the table + self.tableSource.setContent(self.objects) // changing the content automatically updates the table }) ], table: table, @@ -75,6 +75,6 @@ class ViewController: NSViewController { } override func keyUp(with event: NSEvent) { - self.configuration.dataSource.select(items: ["Cod", "Action"]) + self.tableSource.dataSource.select(items: ["Cod", "Action"]) } }