Skip to content

Instantly share code, notes, and snippets.

@Alos
Created September 25, 2008 02:21
Show Gist options
  • Save Alos/12732 to your computer and use it in GitHub Desktop.
Save Alos/12732 to your computer and use it in GitHub Desktop.
import <Foundation/CPObject.j>
import "Song.j"
var BotonMiListaIdentifier = "BotonMiListaIdentifier",
AddSongToolbarItemIdentifier = "AddSongToolbarItemIdentifier",
RemoveSongToolbarItemIdentifier = "RemoveSongToolbarItemIdentifier";
@implementation AppController : CPObject
{
CPCollectionView libraryCollectionView;
CPArray librarySongs;
CPToolbar toolbar;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
var contentView = [theWindow contentView];
[contentView setBackgroundColor:[CPColor blackColor]];
var bounds = [contentView bounds];
librarySongs = [[CPArray alloc]init];
//el label del titulo
var titleLabel =[[CPTextField alloc] initWithFrame:CGRectMake(0, 0, 399, 31)];
[titleLabel setStringValue:@"Name:"];
[titleLabel setFont:[CPFont boldSystemFontOfSize:18.0]];
[titleLabel setBackgroundColor:[CPColor colorWithHexString:"99CCFF"]];
[contentView addSubview:titleLabel];
//el label del titulo
var artistLabel =[[CPTextField alloc] initWithFrame:CGRectMake(399, 0, CGRectGetWidth(bounds) -399 , 31)];
[artistLabel setStringValue:@"Artist:"];
[artistLabel setFont:[CPFont boldSystemFontOfSize:18.0]];
[artistLabel setBackgroundColor:[CPColor colorWithHexString:"99CCFF"]];
[contentView addSubview:artistLabel];
//para nuestro grid
libraryCollectionView = [[CPCollectionView alloc] initWithFrame: CGRectMake(0, 30, CGRectGetWidth(bounds), CGRectGetHeight(bounds)/ 4.0 )];
//los scrolls por si son muchos
var libraryScrollView = [[CPScrollView alloc] initWithFrame: CGRectMake(0, 30, CGRectGetWidth(bounds), CGRectGetHeight(bounds)/ 4.0 )];
[libraryScrollView setAutohidesScrollers: YES];
[libraryScrollView setDocumentView: libraryCollectionView];
[[libraryScrollView contentView] setBackgroundColor: [CPColor colorWithCalibratedRed:213.0/255.0 green:221.0/255.0 blue:230.0/255.0 alpha:1.0]];
[libraryScrollView setHasHorizontalScroller:NO]
[libraryScrollView setAutoresizingMask: CPViewWidthSizable];
//los items q representan los renglones
var songsListItem = [[CPCollectionViewItem alloc] init];
[songsListItem setView: [[SongCell alloc] initWithFrame:CGRectMakeZero()]];
[libraryCollectionView setItemPrototype: songsListItem];
[libraryCollectionView setMaxNumberOfColumns:1];
[libraryCollectionView setMinItemSize:CPSizeMake(CGRectGetWidth(bounds), 30)];
[libraryCollectionView setMaxItemSize:CPSizeMake(CGRectGetWidth(bounds), 30)];
[libraryCollectionView setContent: librarySongs];
//se agrega a la ventana
[contentView addSubview: libraryScrollView];
//debajo del CollectionView
var borderAbajo = [[CPView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(bounds)/ 4.0 + 30 , CGRectGetWidth(bounds), 1)];
[borderAbajo setBackgroundColor: [CPColor blackColor]];
[borderAbajo setAutoresizingMask: CPViewWidthSizable];
[contentView addSubview: borderAbajo];
//la q esta arriba del Collectionview
var borderArriba = [[CPView alloc] initWithFrame:CGRectMake(0, 30 , CGRectGetWidth(bounds), 1)];
[borderArriba setBackgroundColor: [CPColor blackColor]];
[borderArriba setAutoresizingMask: CPViewWidthSizable];
[contentView addSubview: borderArriba];
//la de arriba
var borderTop = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), 1)];
[borderTop setBackgroundColor: [CPColor blackColor]];
[borderTop setAutoresizingMask: CPViewWidthSizable];
[contentView addSubview: borderTop];
//divisor de columnas
var borderMid = [[CPView alloc] initWithFrame:CGRectMake(399, 0, 1, CGRectGetHeight(bounds)/4.0 +30)];
[borderMid setBackgroundColor: [CPColor blackColor]];
[contentView addSubview: borderMid];
//el rectangulo de los controles
toolbar= [[CPToolbar alloc] initWithIdentifier:@"main-toolbar"];
[theWindow setToolbar: toolbar];
[toolbar setDelegate:self];
//testing...
var demoList = [[CPArray alloc]init];
var song1 = [[Song alloc] initWithSongTitle:@"I just can live a lie" setArtist:@"Carrie Underwood" setID:1];
[demoList addObject:song1];
var song2 = [[Song alloc] initWithSongTitle:@"Last night" setArtist:@"AZ Yet" setID:2];
[demoList addObject:song2];
var song3 = [[Song alloc] initWithSongTitle:@"My Last Breath (Live version)" setArtist:@"Evanescence" setID:3];
[demoList addObject:song3];
var song4 = [[Song alloc] initWithSongTitle:@"Heaven Knows" setArtist:@"Faith Evans" setID:4];
[demoList addObject:song4];
var song5 = [[Song alloc] initWithSongTitle:@"Trouble" setArtist:@"Pink" setID:5];
[demoList addObject:song5];
var song6 = [[Song alloc] initWithSongTitle:@"Jaded" setArtist:@"Aerosmith" setID:6];
[demoList addObject:song6];
var song7 = [[Song alloc] initWithSongTitle:@"Who Knew" setArtist:@"Pink" setID:7];
[demoList addObject:song7];
[demoList addObject:song1];
[self addSongList: demoList];
//brings the window to the front
[theWindow orderFront:self];
}
-(void)addSong:(Song)aSong
{
[librarySongs addObject:aSong];
[libraryCollectionView reloadContent];
}
- (void)addSongList:(CPArray)songs
{
[libraryCollectionView setContent: songs];
[libraryCollectionView reloadContent];
}
-(void)removeSong{
alert([librarySongs count]);
var indexSet = [[libraryCollectionView selectionIndexes] firstIndex];
alert(indexSet);
[librarySongs removeObjectAtIndex: indexSet];
alert([librarySongs count]);
[libraryCollectionView reloadContent];
}
- (void)connection:(CPJSONPConnection)aConnection didReceiveData:(CPString)data
{
[self addSongList: data.songs];
}
- (void)connection:(CPJSONPConnection)aConnection didFailWithError:(CPString)error
{
alert(error);
}
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
return [CPToolbarFlexibleSpaceItemIdentifier, BotonMiListaIdentifier,AddSongToolbarItemIdentifier,RemoveSongToolbarItemIdentifier];
}
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
return [BotonMiListaIdentifier,AddSongToolbarItemIdentifier,RemoveSongToolbarItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier];
}
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anItemIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
{
var toolbarItem = [[CPToolbarItem alloc] initWithItemIdentifier: anItemIdentifier];
if (anItemIdentifier == BotonMiListaIdentifier)
{ //TODO crear una view y luego ponerle
// setAutoresizingMask: CPViewMinYMargin | CPViewMaxYMargin
var botonMiLista = [[CPButton alloc]initWithFrame: CGRectMake(0, 0, 80, 18)];
[botonMiLista setTitle:"Check list"];
[toolbarItem setView: botonMiLista];
[toolbarItem setMinSize:CGSizeMake(180, 32)];
[toolbarItem setMaxSize:CGSizeMake(180, 32)];
}
else if (anItemIdentifier == AddSongToolbarItemIdentifier)
{
var image = [[CPImage alloc] initWithContentsOfFile:"Resources/add.png" size:CPSizeMake(30, 25)],
highlighted = [[CPImage alloc] initWithContentsOfFile:"Resources/addHighlighted.png" size:CPSizeMake(30, 25)];
[toolbarItem setImage: image];
[toolbarItem setAlternateImage: highlighted];
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(addSong:)];
[toolbarItem setLabel: "Add a song"];
[toolbarItem setMinSize:CGSizeMake(32, 32)];
[toolbarItem setMaxSize:CGSizeMake(32, 32)];
}
else if (anItemIdentifier == RemoveSongToolbarItemIdentifier)
{
var image = [[CPImage alloc] initWithContentsOfFile:"Resources/remove.png" size:CPSizeMake(30, 25)],
highlighted = [[CPImage alloc] initWithContentsOfFile:"Resources/removeHighlighted.png" size:CPSizeMake(30, 25)];
[toolbarItem setImage: image];
[toolbarItem setAlternateImage: highlighted];
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(removeSong)];
[toolbarItem setLabel: "Remove a song"];
[toolbarItem setMinSize:CGSizeMake(32, 32)];
[toolbarItem setMaxSize:CGSizeMake(32, 32)];
}
return toolbarItem;
}
@end
@implementation SongCell : CPView
{
CPTextField titleView;
CPTextField authorView;
CPView highlightView;
BOOL colored;
}
- (void)setRepresentedObject:(JSObject)anObject
{
if(!titleView)
{
titleView = [[CPTextField alloc] initWithFrame:CGRectInset( [self bounds], 4, 4)];
[titleView setFont: [CPFont systemFontOfSize: 16.0]];
[titleView setTextColor: [CPColor blackColor]];
[self addSubview: titleView];
}
[titleView setStringValue: [anObject songTitle]];
[titleView sizeToFit];
[titleView setFrameOrigin: CGPointMake(10,0.0)];
if(!authorView)
{
authorView = [[CPTextField alloc] initWithFrame:CGRectInset([self bounds], 4, 4)];
[authorView setFont: [CPFont systemFontOfSize: 16.0]];
[authorView setTextColor: [CPColor blackColor]];
[self addSubview: authorView];
}
[authorView setStringValue: [anObject artist]];
[authorView sizeToFit];
[authorView setFrameOrigin: CGPointMake(400,0.0)];
}
- (void)setSelected:(BOOL)flag
{
if(!highlightView)
{
highlightView = [[CPView alloc] initWithFrame:CGRectCreateCopy([self bounds])];
[highlightView setBackgroundColor: [CPColor lightGrayColor]];
}
if(flag)
{
[self addSubview:highlightView positioned:CPWindowBelow relativeTo: titleView];
[titleView setTextColor: [CPColor whiteColor]];
[authorView setTextColor: [CPColor whiteColor]];
}
else
{
[highlightView removeFromSuperview];
[titleView setTextColor: [CPColor blackColor]];
[authorView setTextColor: [CPColor blackColor]];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment