Skip to content

Instantly share code, notes, and snippets.

@Alos
Created September 29, 2008 02:12
Show Gist options
  • Save Alos/13542 to your computer and use it in GitHub Desktop.
Save Alos/13542 to your computer and use it in GitHub Desktop.
import <Foundation/CPObject.j>
import "Song.j"
import "SFTable.j"
import "MyList.j"
import "SFBrowser.j"
import "SFColumnModel.j"
var BotonBrowserIdentifier = "BotonBrowserIdentifier" ,
BotonMiListaIdentifier = "BotonMiListaIdentifier",
AddSongToolbarItemIdentifier = "AddSongToolbarItemIdentifier",
RemoveSongToolbarItemIdentifier = "RemoveSongToolbarItemIdentifier";
@implementation AppController : CPObject
{
CPArray librarySongs;
CPToolbar toolbar;
MyList iList;
SFBrowser musicBrowser;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
var contentView = [theWindow contentView];
[contentView setBackgroundColor:[CPColor whiteColor]];
var bounds = [contentView bounds];
librarySongs = [[CPArray alloc]init];
//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 time:"4:00"];
[demoList addObject:song1];
var song2 = [[Song alloc] initWithSongTitle:@"Last night" setArtist:@"AZ Yet" setID:2 time:"4:28"];
[demoList addObject:song2];
var song3 = [[Song alloc] initWithSongTitle:@"My Last Breath (Live version)" setArtist:@"Evanescence" setID:3 time:"3:59"];
[demoList addObject:song3];
var song4 = [[Song alloc] initWithSongTitle:@"Heaven Knows" setArtist:@"Faith Evans" setID:4 time:"5:43"];
[demoList addObject:song4];
var song5 = [[Song alloc] initWithSongTitle:@"Trouble" setArtist:@"Pink" setID:5 time:"3:12"];
[demoList addObject:song5];
var song6 = [[Song alloc] initWithSongTitle:@"Jaded" setArtist:@"Aerosmith" setID:6 time:"3:27"];
[demoList addObject:song6];
var song7 = [[Song alloc] initWithSongTitle:@"Who Knew" setArtist:@"Pink" setID:7 time:"3:21"];
[demoList addObject:song7];
[demoList addObject:song1];
[self addSongList: demoList];
//brings the window to the front
[theWindow orderFront:self];
[self openBrowser];
}
-(void)addSong:(Song)aSong
{ if(!musicBrowser)
musicBrowser = [[SFBrowser alloc] initWithSource:librarySongs];
[musicBrowser addItem: aSong];
}
- (void)addSongList:(CPArray)songs
{
if(!musicBrowser){
musicBrowser = [[SFBrowser alloc] initWithSource:songs];
}
else{
console.log("Just adding a song...");
[musicBrowser addList:songs];
}
}
-(void)removeSong{//////////////////////////////////////////////<----problem here
if(musicBrowser)
[musicBrowser removeSelectedItems];
}
- (void)connection:(CPJSONPConnection)aConnection didReceiveData:(CPString)data
{
[self addSongList: data.songs];
}
- (void)connection:(CPJSONPConnection)aConnection didFailWithError:(CPString)error
{
alert(error);
}
-(void)openMyList{
if(!iList)
iList = [[MyList alloc]init];
if([iList isVisible])
[iList orderOut:self];
else
[iList orderFront:self];
}
/*Abre la ventana de canciones*/
-(void)openBrowser{
if(!musicBrowser){
console.log("Intentando crear nueva ventana...");
musicBrowser = [[SFBrowser alloc] initWithSource:librarySongs];
}
if([musicBrowser isVisible])
[musicBrowser orderOut:self];
else
[musicBrowser orderFront:self];
}
- (CPArray)toolbarAllowedItemIdentifiers:(CPToolbar)aToolbar
{
return [CPToolbarFlexibleSpaceItemIdentifier, BotonBrowserIdentifier,BotonMiListaIdentifier,AddSongToolbarItemIdentifier,RemoveSongToolbarItemIdentifier];
}
- (CPArray)toolbarDefaultItemIdentifiers:(CPToolbar)aToolbar
{
return [BotonBrowserIdentifier,BotonMiListaIdentifier,AddSongToolbarItemIdentifier,RemoveSongToolbarItemIdentifier, CPToolbarFlexibleSpaceItemIdentifier];
}
- (CPToolbarItem)toolbar:(CPToolbar)aToolbar itemForItemIdentifier:(CPString)anItemIdentifier willBeInsertedIntoToolbar:(BOOL)aFlag
{
var toolbarItem = [[CPToolbarItem alloc] initWithItemIdentifier: anItemIdentifier];
if (anItemIdentifier == BotonBrowserIdentifier)
{ //TODO crear una view y luego ponerle
// setAutoresizingMask: CPViewMinYMargin | CPViewMaxYMargin
var botonMiLista = [[CPButton alloc]initWithFrame: CGRectMake(0, 0, 80, 18)];
[botonMiLista setTitle:"MusicBrowser"];
[botonMiLista setTarget:self];
[botonMiLista setAction:@selector(openBrowser)];
[toolbarItem setView: botonMiLista];
[toolbarItem setMinSize:CGSizeMake(180, 32)];
[toolbarItem setMaxSize:CGSizeMake(180, 32)];
}
else 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"];
[botonMiLista setTarget:self];
[botonMiLista setAction:@selector(openMyList)];
[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
/*The Music browser*/
@implementation SFBrowser : CPWindow
{
SFTable libraryTable;
}
/*Una bonita contructora*/
- (id)initWithSource:(CPArray)list{
//Creamos un HUD como panel para que pueda esta arriba de todas las demas ventanas
self = [[CPPanel alloc] initWithContentRect:CGRectMake(400, 50, 800, 500)styleMask:CPHUDBackgroundWindowMask|CPClosableWindowMask];
if (self)//pa ver si no somos null :P
{
//le ponemos titulo al HUD lo centramos
[self setTitle:@"Music Browser"];
[self setFloatingPanel:YES];
[self setDelegate:self];
var contentView = [self contentView];
var bounds = [contentView bounds];
//para los titulos
var cmArray = [[CPArray alloc]init];
var titleLabel =[[SFColumnModel alloc] initWithFrame:CGRectMake(0, 0, 248, 31) title:"Name" color:NULL];
var artistLabel =[[SFColumnModel alloc] initWithFrame:CGRectMake(250, 0, 248, 31) title:"Artist" color: NULL];
var timeLabel =[[SFColumnModel alloc] initWithFrame:CGRectMake(500, 0, 48, 31) title:"Time" color: NULL];
var ratingLable =[[SFColumnModel alloc] initWithFrame:CGRectMake(550, 0, 48, 31) title:"Rating" color: NULL];
[cmArray addObject: titleLabel];
[cmArray addObject: artistLabel];
[cmArray addObject: timeLabel];
[cmArray addObject: ratingLable];
//a table
libraryTable = [[SFTable alloc] initWithColumnModel: cmArray model:list frame: bounds];
[contentView addSubview: libraryTable];
}
return self;
}
-(void)addItem:(CPObject)anObject{
[libraryTable addItem:anObject];
}
-(void)addList:(CPArray)aModel
{ console.log("addlist here!");
[libraryTable setModel: aModel];
}
-(void)removeSelectedItems{
console.log("removeSlectedItems in SFBrowser got a msg");
[libraryTable removeSelectedItems];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment