Created
November 7, 2009 00:21
-
-
Save folsen/228409 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* AppController.j | |
* NewApplication | |
* | |
* Created by You on July 5, 2009. | |
* Copyright 2009, Your Company All rights reserved. | |
*/ | |
@import <Foundation/CPObject.j> | |
@import "FileUpload.j" | |
@implementation AppController : CPObject | |
{ | |
CPTextField label; | |
CPTableView tableView; | |
CPArray files; | |
} | |
- (void)applicationDidFinishLaunching:(CPNotification)aNotification | |
{ | |
request = [[CPURLRequest alloc] initWithURL:@"http://localhost:3000/uploads"]; ; | |
[CPURLConnection connectionWithRequest:request delegate:self]; | |
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask], | |
contentView = [theWindow contentView]; | |
label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()] | |
[label setStringValue:@"Hello, upload something!"]; | |
[label setFont:[CPFont boldSystemFontOfSize:24.0]]; | |
[label sizeToFit]; | |
[label setCenter:CGPointMake(CGRectGetWidth([contentView frame])/2.0, 20)]; | |
[label setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin]; | |
[contentView addSubview:label]; | |
// create a CPScrollView that will contain the CPTableView | |
var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(CGRectGetWidth([contentView bounds])/2-150, 50.0, 300.0, CGRectGetHeight([contentView bounds])-300)]; | |
[scrollView setAutohidesScrollers:YES]; | |
[scrollView setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin ]; | |
// create the CPTableView | |
tableView = [[CPTableView alloc] initWithFrame:[scrollView bounds]]; | |
[tableView setDataSource:self]; | |
[tableView setUsesAlternatingRowBackgroundColors:YES]; | |
// define the header color | |
var headerColor = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"button-bezel-center.png"]]]; | |
[[tableView cornerView] setBackgroundColor:headerColor]; | |
// add the first column | |
var column = [[CPTableColumn alloc] initWithIdentifier:@"Filename"]; | |
[[column headerView] setStringValue:"Filename"]; | |
[[column headerView] setBackgroundColor:headerColor]; | |
[column setWidth:280.0]; | |
[tableView addTableColumn:column]; | |
//create download button and add as subview to second column | |
downloadImage = [[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"download.png"]]; | |
[downloadImage setSize:CGSizeMake(20,20)]; | |
downloadButton = [[CPButton alloc] initWithFrame:CGRectMake( 0, 0, 20, 20 )]; | |
[downloadButton setBordered:NO]; | |
[downloadButton setTitle:@"hej"]; | |
[downloadButton setImage:downloadImage]; | |
[downloadButton setAlternateImage:downloadImage]; | |
[downloadButton setImagePosition:CPImageOnly]; | |
[downloadButton setTarget:self]; | |
[downloadButton setAction:@selector(downloader:)]; | |
[downloadButton sendActionOn:CPLeftMouseUpMask]; | |
var downloadColumn = [[CPTableColumn alloc] initWithIdentifier:"Download"]; | |
[downloadColumn setWidth:20]; | |
[[downloadColumn headerView] setBackgroundColor:headerColor]; | |
[downloadColumn setDataView:downloadButton]; | |
[tableView addTableColumn:downloadColumn]; | |
[scrollView setDocumentView:tableView]; | |
[contentView addSubview:scrollView]; | |
uploadButton = [[UploadButton alloc] initWithFrame: CGRectMakeZero()] ; | |
[uploadButton setTitle:@"Select File"] ; | |
[uploadButton setName:@"upload[attachment]"] | |
[uploadButton setURL:@"http://localhost:3000/uploads/create"]; | |
[uploadButton setDelegate: self]; | |
[uploadButton setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin]; | |
[uploadButton setBordered:YES]; | |
[uploadButton sizeToFit]; | |
[uploadButton setCenter:CGPointMake(CGRectGetWidth([contentView bounds])/2.0, 470)]; | |
[contentView addSubview:uploadButton]; | |
[theWindow orderFront:self]; | |
// Uncomment the following line to turn on the standard menu bar. | |
//[CPMenu setMenuBarVisible:YES]; | |
} | |
// --- | |
// CPTableView datasource methods | |
- (int)numberOfRowsInTableView:(CPTableView)tableView | |
{ | |
return [files count]; | |
} | |
- (id)tableView:(CPTableView)tableView objectValueForTableColumn:(CPTableColumn)tableColumn row:(int)row | |
{ | |
return [files objectAtIndex:row].upload.attachment_file_name; | |
} | |
- (void)uploadButton: uploadButton didChangeSelection: selection | |
{ | |
[uploadButton submit]; | |
} | |
- (void)uploadButton: uploadButton didFinishUploadWithData: response | |
{ | |
[label setStringValue:@"File uploaded!"]; | |
[label sizeToFit]; | |
files = [files arrayByAddingObject:CPJSObjectCreateWithJSON(response)]; | |
[tableView reloadData]; | |
[label setCenter:[contentView center]]; | |
} | |
- (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data | |
{ | |
//get a javascript object from the json response | |
files = CPJSObjectCreateWithJSON(data); | |
[tableView reloadData]; | |
//clear out this connection's reference | |
[self clearConnection:aConnection]; | |
} | |
- (void)connection:(CPURLConnection)aConnection didFailWithError:(CPString)error | |
{ | |
alert("There was an error getting the list of uploaded files."); | |
[self clearConnection:aConnection]; | |
} | |
- (void)clearConnection:(CPURLConnection)aConnection | |
{ | |
_deletePhotoConnection = nil; | |
} | |
- (void)downloader:(CPButton)aButton | |
{ | |
console.log([aButton superview]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment