Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TNTableViewDataSource] Enhancements #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TNKit.j
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
@import "TNLocalizationCenter.j";
@import "TNMessageBoard.j"
@import "TNMessageView.j"
@import "TNMultiViewDataSource.j"
@import "TNOutlineViewDataSource.j"
@import "TNStackView.j"
@import "TNSwipeView.j"
107 changes: 107 additions & 0 deletions TNMultiViewDataSource.j
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* TNMultiViewDataSource.j
*
* Copyright (C) 2012 Luc Vauvillier <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

@import <Foundation/Foundation.j>
@import <AppKit/CPCollectionView.j>

@import "TNTableViewDataSource.j"

/*! @ingroup tnkit
Synchronize and share datasource between a table view and a collection view
Designed to display a collection of items with grid mode / list mode switch possibility
*/
@implementation TNMultiViewDataSource: TNTableViewDataSource
{
CPCollectionView _collectionView @accessors(property=collectionView);
}

#pragma mark -
#pragma mark Setters and getters

/*! used to set the collection view
@param aCollectionView the collection view
*/
- (void)setCollectionView:(CPCollectionView)aCollectionView
{
if (_collectionView == aCollectionView)
return;

_collectionView = aCollectionView;
[_collectionView setContent:[self filteredContent]];
[self synchronizeSelectionFromView:[self table]];
}

/*! used to set the table view
@param aTableView the table view
*/
- (void)setTableView:(CPTableView)aTableView
{
[super setTable:aTableView];
[self synchronizeSelectionFromView:_collectionView];
}

#pragma mark -
#pragma mark data synchronization

/*! this action should be bound to a CPSearchField
it will filter the content of the datasource according to the sender value
@param sender the sender of the action
*/
- (IBAction)filterObjects:(id)sender
{
[super filterObjects:sender];
[_collectionView setContent:[self filteredContent]];
}

/*! set the given array of object as the content of the datasource
@param aContent CPArray containing the datas of the datasource
*/
- (void)setContent:(CPArray)aContent
{
[super setContent:aContent];
[_collectionView setContent:[self filteredContent]];
}

- (void)tableView:(CPTableView)aTableView sortDescriptorsDidChange:(CPArray)oldDescriptors
{
[super tableView:aTableView sortDescriptorsDidChange:oldDescriptors];
[_collectionView reloadContent];
}

#pragma mark -
#pragma mark selection synchronization

/*! synchronize selection between the two components
@param aView The master view
*/
- (void)synchronizeSelectionFromView:(CPView)aView
{
if (!aView)
return;

if (aView == _collectionView)
{
[[self table] selectRowIndexes:[_collectionView selectionIndexes] byExtendingSelection:NO];
}
else if (aView == [self table])
{
[_collectionView setSelectionIndexes:[[self table] selectedRowIndexes]];
}
}

@end
14 changes: 13 additions & 1 deletion TNTableViewDataSource.j
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@
CPTableView _table @accessors(property=table);
CPPredicate _displayFilter @accessors(property=displayFilter);

CPArray _filteredContent;
CPArray _filteredContent @accessors(property=filteredContent, readonly);
CPSearchField _searchField;
CPString _filter;
BOOL _needsFilter;
@@ -294,6 +294,18 @@
return [_filteredContent count];
}

#pragma mark -
#pragma mark Helpers

/*! expose selected objects
@return array of selected objects or nil if _table is not setted
*/
- (CPArray)selectedObjects
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to check if _table is not nil here

{
if (!_table)
return nil;
return [_filteredContent objectsAtIndexes:[_table selectedRowIndexes]];
}

#pragma mark -
#pragma mark Datasource implementation