Skip to content

Commit

Permalink
Rename TableConfiguration class
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoconti83 committed Sep 11, 2017
1 parent 88f2477 commit 83a9568
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
8 changes: 4 additions & 4 deletions EasyTables.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -64,7 +64,7 @@
54CEC1F61F21D52500FA3BC6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
54CEC1F91F21D52500FA3BC6 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
54CEC1FB1F21D52500FA3BC6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
54CEC20D1F21D6D900FA3BC6 /* TableConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableConfiguration.swift; sourceTree = "<group>"; };
54CEC20D1F21D6D900FA3BC6 /* EasyTable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EasyTable.swift; sourceTree = "<group>"; };
54CEC20F1F21D85D00FA3BC6 /* GenericTableDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GenericTableDataSource.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -131,7 +131,7 @@
children = (
54CEC1C91F21D4DE00FA3BC6 /* EasyTables.h */,
54CEC1CA1F21D4DE00FA3BC6 /* Info.plist */,
54CEC20D1F21D6D900FA3BC6 /* TableConfiguration.swift */,
54CEC20D1F21D6D900FA3BC6 /* EasyTable.swift */,
54CEC20F1F21D85D00FA3BC6 /* GenericTableDataSource.swift */,
5415A6BC1F3F2396000FF9BB /* NSTableView+Easy.swift */,
);
Expand Down Expand Up @@ -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;
};
Expand Down
38 changes: 21 additions & 17 deletions EasyTables/TableConfiguration.swift → EasyTables/EasyTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object: Equatable> {
public class EasyTableSource<Object: Equatable> {

/// Internal data source associated with the table
public let dataSource: GenericTableDataSource<Object>

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
Expand All @@ -79,17 +83,19 @@ public class TableConfiguration<Object: Equatable> {
/// 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<Objects: Collection>(initialObjects: Objects,
columns: [ColumnDefinition<Object>],
contextMenuOperations: [ObjectOperation<Object>],
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,
Expand All @@ -107,8 +113,8 @@ public class TableConfiguration<Object: Equatable> {
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
Expand All @@ -122,17 +128,16 @@ public class TableConfiguration<Object: Equatable> {
}
}

extension TableConfiguration {
extension EasyTableSource {

/// Sets up table columns and selection methods
fileprivate static func setupTable(
_ table: NSTableView,
fileprivate func setupTable(
columns: [ColumnDefinition<Object>],
multiSelection: Bool)
{
let preColumns = table.tableColumns
let preColumns = self.table.tableColumns
preColumns.forEach {
table.removeTableColumn($0)
self.table.removeTableColumn($0)
}

columns.forEach { cdef in
Expand All @@ -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<Object>]
) {
guard !operations.isEmpty else {
table.menu = nil
self.table.menu = nil
return
}
let menu = NSMenu()
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions EasyTablesExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import EasyTables
/// An example of how to use `TableConfiguration`
class ViewController: NSViewController {

var configuration: TableConfiguration<String>!
var tableSource: EasyTableSource<String>!

var objects = Set(["Action", "Engineering", "Cod", "Doodle"])

Expand All @@ -38,7 +38,7 @@ class ViewController: NSViewController {

scroll.createConstraintsToFillParent(self.view)

self.configuration = TableConfiguration(
self.tableSource = EasyTableSource(
initialObjects: objects,
columns: [
ColumnDefinition("Word", { $0 }),
Expand All @@ -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: {
Expand All @@ -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,
Expand All @@ -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"])
}
}

0 comments on commit 83a9568

Please sign in to comment.