Skip to content

Instantly share code, notes, and snippets.

@joehoyle
Created March 29, 2010 17:15
Show Gist options
  • Save joehoyle/348122 to your computer and use it in GitHub Desktop.
Save joehoyle/348122 to your computer and use it in GitHub Desktop.
@import <Foundation/CPObject.j>
@import <AppKit/CPOutlineView.j>
@import <AppKit/CPWindowController.j>
@import "AppController.j"
@implementation MenuController : CPObject {
CPMutableArray sites;
@outlet CPScrollView scrollView;
}
- (void)awakeFromCib {
var frame = [[scrollView documentView] bounds];
outlineView = [[CPOutlineView alloc] initWithFrame:frame];
[outlineView setDelegate:self];
[outlineView setIndentationPerLevel:15];
var textColumn = [[CPTableColumn alloc] initWithIdentifier:@"TextColumn"];
[textColumn setWidth:220];
[textColumn setResizingMask:CPTableColumnAutoresizingMask];
[textColumn setDataView:[[outlineViewItemView alloc] initWithFrame:CGRectMake( 0, 0, 800, 20 )]];
[outlineView setHeaderView:nil];
[outlineView setCornerView:nil];
[outlineView addTableColumn:textColumn];
[outlineView setOutlineTableColumn:textColumn];
[outlineView setDataSource:self];
[scrollView setDocumentView:outlineView];
}
//delegates
- (id)outlineView:(CPOutlineView)outlineView child:(int)index ofItem:(id)item {
// my outlineview has a few differnt "types" of row, so I return different items based on row number
if( item === nil && index == 0 )
return @"Overview";
else if ( item === nil && index == 1 )
return @"Sites";
if( item == @"Sites" )
return index == 0 ? @"Search" : shownSites[index - 1];
}
- (BOOL)outlineView:(CPOutlineView)outlineView isItemExpandable:(id)item {
// onlye the "Sites" items is exandable
if( item == @"Sites" )
return true;
return false;
}
- (int)outlineView:(CPOutlineView)outlineView numberOfChildrenOfItem:(id)item {
if( item === null )
return [items count];
if( item == @"Sites" )
return [shownSites count] + 1;
return 0;
}
- (id)outlineView:(CPOutlineView)outlineView objectValueForTableColumn:(CPTableColumn)tableColumn byItem:(id)anObject {
return anObject;
}
- (BOOL)outlineView:(CPOutlineView)outlineView shouldSelectItem:(id)item {
// disable selection of Search and Sites items
if( item == @"Search" || item == @"Sites" )
return NO;
return YES;
}
@end
var searchField = nil;
@implementation outlineViewItemView : CPView {
id object;
}
- (void)setObjectValue:(id)anObject {
object = anObject;
//depending on what object is passed, I draw different views
if( [object isKindOfClass:Site] ) {
[self drawSite];
} else if( [object isKindOfClass:CPString] ) {
if( object == @"Overview" )
[self drawOverview];
else if( object == @"Sites" )
[self drawCollapsibleString];
else if( object == @"Search" )
[self drawSearch];
else
[self drawCollapsibleString];
}
}
- (void)drawSite {
//remove an subviews that have previously been added
[self clearView];
//setup the 3 subviews (2 images, 1 textfield)
var siteNameView = [[CPTextField alloc] initWithFrame:CGRectMake(20, 3, 300, 20)];
var faviconView = [[CPImageView alloc] initWithFrame:CGRectMake(0, 4, 16, 16)];
var loadingView = [[CPImageView alloc] initWithFrame:CGRectMake(165, 4, 16, 16)];
if( [object isLoading] == YES ) {
var loadingImage = [[CPImage alloc] initWithContentsOfFile:[[[CPBundle mainBundle] resourcePath] stringByAppendingString:@"spinner-outlineview.gif"]];
[loadingView setImage:loadingImage];
}
//only show the favicon if there is one
if( [object favicon] ) {
[faviconView setImage:[object favicon]];
}
//add the views to the current view
[self addSubview:faviconView];
[self addSubview:siteNameView];
[self addSubview:loadingView];
[siteNameView setObjectValue:[object nicename]];
[siteNameView sizeToFit];
}
- (void)drawSearch {
[self clearView];
if( !searchField ) {
searchField = [[CPSearchField alloc] initWithFrame:CGRectMake(0, 0, 175, 30)];
[searchField setPlaceholderString:@"Filter..."];
}
[self addSubview:searchField];
//make the searchfield firstResponder (the problem is when the text is changed, the outlineview is refreshed, causing the searchField to lose focus)
[[CPApp mainWindow] makeFirstResponder:searchField];
}
- (void)drawCollapsibleString {
//draws the uppercase collapsible title
[self clearView];
var collapsibleTextView = [[CPTextField alloc] initWithFrame:CGRectMake(0, 3, 100, 20)];
[collapsibleTextView setTextColor:[CPColor colorWithHexString:@"88A4D6"]];
[collapsibleTextView setFont:[CPFont boldSystemFontOfSize:12 ]];
[collapsibleTextView setTextShadowColor:[CPColor whiteColor]];
[collapsibleTextView setTextShadowOffset:CGSizeMake(1, 1)];
[collapsibleTextView setStringValue:[object uppercaseString]];
[self addSubview:collapsibleTextView];
}
- (void)drawOverview {
[self clearView];
var overviewTextView = [[CPTextField alloc] initWithFrame:CGRectMake(0, 3, 100, 20)]
[self addSubview:overviewTextView];
[overviewTextView setStringValue:object];
}
- (void)clearView {
//removes any sibviews that have previously been added
[[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
@end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment