malkomalko
5/12/2009 - 7:11 PM

gistfile1.js

@import <Foundation/CPObject.j>
@import "ToolbarDelegate.j"
@import "PostListView.j"
@import "PostItemView.j"
@import "Post.j"
@import "PostsController.j"
@import "PostDetailsView.j"

@implementation AppController : CPObject
{
    CPTextField label ;
    CPURLConnection _connection ;
}

- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
    var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
        contentView = [theWindow contentView];
        
    // -- the posts controller
    var postsController  = [[PostsController alloc] init] ;

    // --- the toolbar
    var toolbar = [[CPToolbar alloc] initWithIdentifier:"Posts"] ;
    var toolbarDelegate = [[ToolbarDelegate alloc] init] ;
    [toolbar setDelegate:toolbarDelegate] ;
    [toolbar setVisible:YES] ;
    [theWindow setToolbar:toolbar] ;

    // -- the split view
    var splitView = [[CPSplitView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([contentView bounds]), CGRectGetHeight([contentView bounds]))];
    [splitView setVertical:NO] ;
    [splitView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable ]; 
    [contentView addSubview:splitView] ;

    // --- the posts scroll view
    var postListScrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([contentView bounds]), 200.0)] ;
    [postListScrollView setHasHorizontalScroller:NO] ;
    [postListScrollView setAutohidesScrollers:YES] ;
    [splitView addSubview:postListScrollView] ;
    
    // --- the posts collection view
    var postListView = [[PostListView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([contentView bounds]), 200.0)] ;
    [postListView setDelegate:postsController] ;
    [postsController setPostListView:postListView] ;
    [postsController loadExamplePosts] ;
    [postListScrollView setDocumentView:postListView] ;

    // --- the post details scroll view
    var postDetailsScrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 200.0, CGRectGetWidth([contentView bounds]), CGRectGetHeight([contentView bounds]) - 200.0)] ;
    [postDetailsScrollView setHasHorizontalScroller:NO] ;
    [postDetailsScrollView setAutohidesScrollers:YES] ;
    [splitView addSubview:postDetailsScrollView] ;
    
    // --- the post details view
    var detailsListView = [[PostDetailsView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([contentView bounds]), CGRectGetHeight([contentView bounds]) - 200.0)] ;
    [postsController setPostDetailsView:detailsListView] ;
    [postDetailsScrollView setDocumentView:detailsListView] ;

    // --- testing other cappuccino views
    
    var request = [CPURLRequest requestWithURL:"posts.json"];
    var _connection = [CPURLConnection connectionWithRequest:request delegate:self];
    
    var thePanel = [[CPPanel alloc] initWithContentRect:CGRectMake(
        CGRectGetWidth([contentView bounds]) / 2 - 150,
        CGRectGetHeight([contentView bounds]) / 2,
        300.0, 150.0) styleMask:CPHUDBackgroundWindowMask | CPClosableWindowMask],
        panelContentView = [thePanel contentView];

    [thePanel setDelegate:self];
    
    label = [[CPTextField alloc] initWithFrame:CGRectMake(5.0, 10.0, 290.0, 33.0)] ;
    [label setBezeled:YES];
    [label setBezelStyle:CPTextFieldSquareBezel];
    [label setPlaceholderString:@"Enter text"];
    [label setEditable:YES];

    [panelContentView addSubview:label];
    
    [thePanel orderFront:self];
    [theWindow orderFront:self];
    
}

- (void)connection:(CPURLConnection) connection didReceiveData:(CPString)data
{
    //This method is called when a connection receives a response. in a
    //multi-part request, this method will (eventually) be called multiple times,
    //once for each part in the response.
    console.info('CPURLConnection(connection): ' + connection)
    console.info('CPURLConnection(didReceiveData): ' + data);
    
    //get a javascript object from the json response
    var result = CPJSObjectCreateWithJSON(data);
    console.info('CPJSObjectCreateWithJSON(data): ' + result);
    
    if (connection == _connection)
        console.info('connection == ivar _connection');
}

- (void)connection:(CPURLConnection)connection didFailWithError:(CPString)error
{
    //This method is called if the request fails for any reason.
    console.info('Error: ' + error);
}

-(BOOL)windowShouldClose:(id)window
{
    console.info('Button Pressed');
}

@end