Skip to content

Instantly share code, notes, and snippets.

@alexanderweiss
Created December 29, 2009 11:57
Show Gist options
  • Save alexanderweiss/265269 to your computer and use it in GitHub Desktop.
Save alexanderweiss/265269 to your computer and use it in GitHub Desktop.
@import <Foundation/CPObject.j>
@import <AppKit/CPOutlineView.j>
@implementation SourceListController : CPObject
{
CPOutlineView outlineView;
CPView owner;
CPDictionary items;
AOURLConnection dataConnection;
id delegate;
}
- (id)initWithOwner:(CPView)anOwner delegate:(id)aDelegate
{
if (self = [super init])
{
delegate = aDelegate;
owner = anOwner;
outlineView = [[CPOutlineView alloc] initWithFrame:[owner bounds]];
var textColumn = [[CPTableColumn alloc] initWithIdentifier:@"TextColumn"];
[textColumn setMaxWidth:500.0];
var dataView = [[AODataView alloc] initWithFrame:CGRectMakeZero()];
[textColumn setDataView:dataView];
[outlineView setHeaderView:nil];
[outlineView setCornerView:nil];
[outlineView addTableColumn:textColumn];
[outlineView setOutlineTableColumn:textColumn];
//[outlineView setBackgroundColor:[CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"toolbar_bg.png"]]]];
[owner addSubview:outlineView];
[self loadData];
}
return self;
}
- (void)loadData
{
dataConnection = [[AOURLConnection alloc] initWithURL:@"resourcelist" method:@"GET" content:null delegate:self];
}
- (void)connection:(CPURLConnection) dataConnection didReceiveData:(CPString)data
{
var result = CPJSObjectCreateWithJSON(data);
//if(result.error == null)
//{
console.log(result);
items = [CPDictionary dictionaryWithJSObject:result recursively:YES];
console.log(items);
[outlineView setDataSource:self];
[outlineView reloadData];
//}
}
- (id)outlineView:(CPOutlineView)outlineView child:(int)index ofItem:(id)item
{
CPLog("outlineView:%@ child:%@ ofItem:%@", outlineView, index, item);
if (item === nil)
{
var values = [items objectForKey:'root'];
var children = [values objectForKey:@"children"];
var childID = [children objectForKey:index];
return [childID];
}
else
{
var values = [items objectForKey:item];
var children = [values objectForKey:@"children"];
var childID = [children objectForKey:index];
return [childID];
}
}
- (BOOL)outlineView:(CPOutlineView)outlineView isItemExpandable:(id)item
{
CPLog("outlineView:%@ isItemExpandable:%@", outlineView, item);
var values = [items objectForKey:item];
if([values objectForKey:@"children"])
{
console.log(true);
return true;
}
else
{
console.log(false);
return false;
}
}
- (int)outlineView:(CPOutlineView)outlineView numberOfChildrenOfItem:(id)item
{
CPLog("outlineView:%@ numberOfChildrenOfItem:%@", outlineView, item);
if (item === nil)
{
var values = [items objectForKey:'root'];
var children = [values objectForKey:@"children"];
return [children count];
}
else
{
var values = [items objectForKey:item];
var children = [values objectForKey:@"children"];
console.log([children count]);
return [children count];
}
}
- (id)outlineView:(CPOutlineView)outlineView objectValueForTableColumn:(CPTableColumn)tableColumn byItem:(id)item
{
CPLog("outlineView:%@ objectValueForTableColumn:%@ byItem:%@", outlineView, tableColumn, item);
return [[AOOutlineViewObjectData alloc] initWithItem:[items objectForKey:item] delegate:delegate];
}
@end
@implementation AODataView : CPView
{
CPTextField label;
CPImageView iconView;
AOOutlineViewObjectData object;
}
- (void)setObjectValue:(CPString)anObject
{
CPLog("setObjectValue:%@", anObject);
object = anObject;
if (!label)
{
label = [[CPTextField alloc] initWithFrame:CGRectInset([self bounds], 20.0 ,1.0)];
//[label setTextShadowOffset:CGSizeMake(0,1)];
//[label setTextShadowColor:[CPColor whiteColor]];
[self addSubview:label];
}
if(!iconView)
{
iconView = [[CPImageView alloc] initWithFrame:CGRectMake(2.0,0,18.0,18.0)];
[self addSubview:iconView];
}
[label setStringValue:[object label]];
[label sizeToFit];
[iconView setImage:[[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:[object iconName]]]];
}
- (void)mouseDown:(CPEvent)event
{
[super mouseDown:event];
console.log([object id]);
[[object delegate] loadPage:object];
}
@end
@implementation AOOutlineViewObjectData : CPObject
{
CPDictionary item;
CPString iconName;
id delegate @accessors;
}
- (void)initWithItem:(CPDictionary)anItem delegate:aDelegate
{
if([super init])
{
delegate = aDelegate
item = anItem
label = [item objectForKey:@"name"];
iconName = [[CPString alloc] initWithFormat:@"%@_%@.png", [item objectForKey:@"basic_type"], [item objectForKey:@"extra_type"]];
}
return self;
}
- (CPString)label
{
return [item objectForKey:@"name"];
}
- (CPString)iconName
{
return iconName;
}
- (id)id
{
return [item objectForKey:@"id"];
}
- (CPString)basicType
{
return [item objectForKey:@"basic_type"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment