Skip to content

Instantly share code, notes, and snippets.

@alexanderweiss
Created December 26, 2009 12:56
Show Gist options
  • Save alexanderweiss/263944 to your computer and use it in GitHub Desktop.
Save alexanderweiss/263944 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];
//[outlineView setDataSource:self];
}
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];
//keys = [values allKeys];
//console.log([keys objectAtIndex:index]);
return [childID];
}
else
{
//console.log('item: '+item);
//console.log(index);
var values = [items objectForKey:item];
var children = [values objectForKey:@"children"];
var childID = [children objectForKey:index];
//keys = [values allKeys];
//console.log([keys objectAtIndex: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);
//value = [items objectForKey:item];
//var view = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
//var imageView = [[CPImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
//[imageView setImage:[[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"guest.jpg"]]];
//[view addSubview:imageView];
var values = [items objectForKey:item];
var label = [values objectForKey:@"name"];
var fileName = [[CPString alloc] initWithFormat:@"%@_%@.png", [values objectForKey:@"basic_type"], [values objectForKey:@"extra_type"]];
console.log(fileName);
return [[AOOutlineViewObjectData alloc] initWithLabel:label iconName:fileName];
//return [[items objectForKey:item] objectForKey:@"name"];
//return view;
}
@end
@implementation AODataView : CPView
{
CPTextField label;
CPImageView iconView;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:label forKey:"theLabel"];
[aCoder encodeObject:iconView forKey:"theIconView"];
}
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super initWithCoder:aCoder];
if (self)
{
label = [aCoder decodeObjectForKey:"theLabel"];
iconView = [aCoder decodeObjectForKey:"theIconView"];
}
return self;
}
- (void)setObjectValue:(JSObject)object
{
CPLog("setObjectValue:%@", object);
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)
{
var icon = [[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"globe.png"] size:CGSizeMake(16.0, 16.0)];
iconView = [[CPImageView alloc] initWithFrame:CGRectMake(2.0,0,18.0,18.0)];
[iconView setImage:[[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:[object iconName]]]];
[self addSubview:iconView];
}
[label setStringValue:[object label]];
[label sizeToFit];
}
@end
@implementation AOOutlineViewObjectData : CPObject
{
CPTextField label;
CPImageView iconName;
}
- (void)initWithLabel:(CPString)aLabel iconName:(CPString)anIconName
{
if([super init])
{
console.log('init label');
label = aLabel;
iconName = anIconName;
}
return self;
}
- (CPString)label
{
console.log(label);
return label;
}
- (CPString)iconName
{
return iconName;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment