Skip to content

Instantly share code, notes, and snippets.

@Alos
Created September 29, 2008 04:08
Show Gist options
  • Save Alos/13550 to your computer and use it in GitHub Desktop.
Save Alos/13550 to your computer and use it in GitHub Desktop.
import <Foundation/CPObject.j>
import "Song.j"
@implementation SFTable : CPView
{
/*Para las tablas*/
CPCollectionView collectionView;
CPArray model;
/*Cosas para los titulos*/
CPArray columnModel
/*Cosas para las celdas*/
SFCell celdas;
var pos=0;
/*Para drag&drop*/
CPPasteboard aPasteboard;
}
-(void) initWithColumnModel:(CPArray)aColumnModel model:(CPArray)aModel frame:(CGRect)bounds{
self = [super initWithFrame:bounds];
[self setModel:aModel];
[aPasteboard generalPasteboard];
//para nuestro grid
collectionView = [[CPCollectionView alloc] initWithFrame: CGRectMake(0, 30, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
//los scrolls por si son muchos
var scrollView = [[CPScrollView alloc] initWithFrame: CGRectMake(0, 30, CGRectGetWidth(bounds), CGRectGetHeight(bounds))];
[scrollView setAutohidesScrollers: YES];
[scrollView setDocumentView: collectionView];
[[scrollView contentView] setBackgroundColor: NULL];
[scrollView setHasHorizontalScroller:NO]
[scrollView setAutoresizesSubviews:YES];
//los items q representan los renglones
var listItem = [[CPCollectionViewItem alloc] init];
celdas = [[SFCell alloc] initWithFrame:CGRectMakeZero()];
[listItem setView: celdas];
//Drag and drop
var item [listItem view]
[collectionView setItemPrototype: listItem];
[collectionView setMaxNumberOfColumns:1];
[collectionView setMinItemSize:CPSizeMake(CGRectGetWidth(bounds), 20)];
[collectionView setMaxItemSize:CPSizeMake(CGRectGetWidth(bounds), 20)];
[collectionView setContent: model];
[self addSubview:scrollView];
//la q esta arriba del Collectionview
var borderArriba = [[CPView alloc] initWithFrame:CGRectMake(0, 30 , CGRectGetWidth(bounds), 1)];
[borderArriba setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: borderArriba];
//la de arriba
var borderTop = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), 1)];
[borderTop setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: borderTop];
[self setColumnModel:aColumnModel];
return self;
}
-(void)setColumnModel:(CPArray)aColumnModel{
console.log("Setting the column model...");
columnModel = aColumnModel;
for(var i=0; i<[columnModel count];i++){
var thisColumn = [columnModel objectAtIndex:i];
[self addSubview: thisColumn];
if(i>0 && i<[columnModel count]){
pos= pos+CGRectGetWidth([[columnModel objectAtIndex: i-1] bounds])+1;
var border = [[CPView alloc] initWithFrame:CGRectMake(pos, 0, 1, CGRectGetHeight([self bounds]))];
console.log("Setting line at: %d",pos);
[border setBackgroundColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: border];
}
}
}
-(void)setModel:(CPArray)aModel{
model = aModel;
[collectionView setContent: model];
[collectionView reloadContent];
}
/**
Adds an item to the table
@param the item to add to the table
*/
-(void)addItem():(CPObject)anItem{
[model addObject:anItem];
[collectionView setModel: model];
[collectionView reloadContent];
}
/**
@param anIndex the value where the item you want t remove is
*/
-(void)removeItem():(int)anIndex{
[model removeObjectAtIndex: anIndex];
[collectionView reloadContent];
}
/**
Returns the item that is currently selected
@return
*/
-(int)getSelectedItem{
return [[collectionView selectionIndexes] firstIndex];
}
/**
Removes selected items
*/
-(void)removeSelectedItems{
console.log("removeSelectedItems in SFTable got the msg");
var indexes= [collectionView selectionIndexes];
var a = [indexes firstIndex];
[model removeObjectAtIndex: a];
[collectionView reloadContent];
}
-(CPIndexSet)getSelectedItems{
return [collectionView selectionIndexes];
}
@end
@implementation SFCell : CPView
{
CPTextField titleView;
CPTextField authorView;
CPTextField time;
CPView highlightView;
Song theSong;
}
- (void)setRepresentedObject:(JSObject)anObject
{
theSong = anObject;
if(!titleView)
{
titleView = [[CPTextField alloc] initWithFrame:CGRectInset( [self bounds], 4, 4)];
[titleView setFont: [CPFont systemFontOfSize: 12.0]];
[titleView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: titleView];
}
[titleView setStringValue: [anObject songTitle]];
[titleView sizeToFit];
[titleView setFrameOrigin: CGPointMake(5,0.0)];
if(!authorView)
{
authorView = [[CPTextField alloc] initWithFrame:CGRectInset([self bounds], 4, 4)];
[authorView setFont: [CPFont systemFontOfSize: 12.0]];
[authorView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: authorView];
}
[authorView setStringValue: [anObject artist]];
[authorView sizeToFit];
[authorView setFrameOrigin: CGPointMake(251,0.0)];
if(!time)
{
time = [[CPTextField alloc] initWithFrame:CGRectInset([self bounds], 4, 4)];
[time setFont: [CPFont systemFontOfSize: 12.0]];
[time setTextColor: [CPColor colorWithHexString:"33FF00"]];
[self addSubview: time];
}
[time setStringValue: [anObject time]];
[time sizeToFit];
[time setFrameOrigin: CGPointMake(500,0.0)];
}
- (void)setSelected:(BOOL)flag
{
if(!highlightView)
{
highlightView = [[CPView alloc] initWithFrame:CGRectCreateCopy([self bounds])];
[highlightView setBackgroundColor: [CPColor greenColor]];
}
if(flag)
{
[self addSubview:highlightView positioned:CPWindowBelow relativeTo: titleView];
[titleView setTextColor: [CPColor blackColor]];
[authorView setTextColor: [CPColor blackColor]];
[time setTextColor: [CPColor blackColor]];
}
else
{
[highlightView removeFromSuperview];
[titleView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[authorView setTextColor: [CPColor colorWithHexString:"33FF00"]];
[time setTextColor: [CPColor colorWithHexString:"33FF00"]];
}
}
- (void)mouseDragged:(CPEvent)anEvent
{
var someView = [[TheCell alloc] initWithFrame:[self bounds]];
[someView setRepresentedObject:myRepresentedObject];
[self dragView:someView at:[self convertPoint:[anEvent locationInWindow] toView:nil] offset:CGPointMakeZero() event:anEvent pasteboard:aPasteboard source:self slideBack:NO];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment