Skip to content

Instantly share code, notes, and snippets.

@orlaqp
Created January 11, 2011 13:13
Show Gist options
  • Save orlaqp/774393 to your computer and use it in GitHub Desktop.
Save orlaqp/774393 to your computer and use it in GitHub Desktop.
@import <AppKit/CPView.j>
@import "../Models/Menu.j"
@implementation WorkflowTreeViewTemplate : CPView
{
CPImageView _imageView @accessors(property=image);
CPString _text @accessors(property=text);
RoundedView _countView @accessors(property=countView);
CPTextField _countText @accessors(property=countText);
}
- (void)initWithFrame:(CGRect)aRect
{
self = [super initWithFrame:aRect];
if (self)
{
_imageView = [[CPImageView alloc] initWithFrame:CGRectMakeZero()];
[self addSubview:_imageView];
_text = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[_text setAutoresizingMask: CPViewWidthSizable | CPViewHeightSizable ];
[_text setLineBreakMode:CPLineBreakByTruncatingTail];
[_text setBezelStyle:CPTextFieldRoundedBezel];
[_text setFont:[CPFont boldSystemFontOfSize:11.0]];
[_text setVerticalAlignment:CPCenterVerticalTextAlignment];
[self unsetThemeState:CPThemeStateSelectedDataView];
[_text setValue:[CPColor colorWithCalibratedRed:71/255 green:90/255 blue:102/255 alpha:1] forThemeAttribute:"text-color" inState:CPThemeStateTableDataView];
[_text setValue:[CPColor colorWithCalibratedWhite:1 alpha:1] forThemeAttribute:"text-shadow-color" inState:CPThemeStateTableDataView];
[_text setValue:CGSizeMake(0,1) forThemeAttribute:"text-shadow-offset" inState:CPThemeStateTableDataView];
[_text setValue:[CPColor colorWithCalibratedWhite:1 alpha:1.0] forThemeAttribute:"text-color" inState:CPThemeStateTableDataView | CPThemeStateSelectedTableDataView];
[_text setValue:[CPColor colorWithCalibratedWhite:0 alpha:0.5] forThemeAttribute:"text-shadow-color" inState:CPThemeStateTableDataView | CPThemeStateSelectedTableDataView];
[_text setValue:CGSizeMake(0,-1) forThemeAttribute:"text-shadow-offset" inState:CPThemeStateTableDataView | CPThemeStateSelectedTableDataView];
[_text setValue:[CPFont boldSystemFontOfSize:12.0] forThemeAttribute:"font" inState:CPThemeStateTableDataView | CPThemeStateGroupRow];
[_text setValue:[CPColor colorWithCalibratedWhite:125 / 255 alpha:1.0] forThemeAttribute:"text-color" inState:CPThemeStateTableDataView | CPThemeStateGroupRow];
[_text setValue:[CPColor colorWithCalibratedWhite:1 alpha:1] forThemeAttribute:"text-shadow-color" inState:CPThemeStateTableDataView | CPThemeStateGroupRow];
[_text setValue:CGSizeMake(0,1) forThemeAttribute:"text-shadow-offset" inState:CPThemeStateTableDataView | CPThemeStateGroupRow];
[_text setValue:CGInsetMake(1.0, 0.0, 0.0, 2.0) forThemeAttribute:"content-inset" inState:CPThemeStateTableDataView | CPThemeStateGroupRow];
[_text setEditable:NO];
//[_text setFont:[CPFont systemFontOfSize:12.0]];
[self addSubview:_text];
_countView = [[CPBox alloc] initWithFrame:CGRectMakeZero()];
//[_countView setFillColor: [CPColor blueColor]];
[_countView setAutoresizingMask: CPViewMinXMargin]
[_countView setBorderType:CPLineBorder];
[_countView setBorderColor:[CPColor whiteColor]];
[_countView setBorderWidth:2.0];
[_countView setCornerRadius:8.0];
[self addSubview:_countView];
_countText = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[_countText setEditable:NO];
[_countText setAlignment:CPCenterTextAlignment];
[_countText setTextColor:[CPColor whiteColor]];
[_countText setFont:[CPFont boldSystemFontOfSize:10.0]];
[_countView addSubview:_countText];
}
return self;
}
- (void)setObjectValue:(id)aValue
{
var viewRect = [self frame];
var imageHeight = 20;
var imageTop = (CGRectGetHeight(viewRect) - imageHeight) / 2;
var top = (CGRectGetHeight(viewRect) - 24) / 2;
// Image Processing
var imageLoaded = false;
if ([aValue imagePath] != nil && [aValue imagePath] != "")
{
var image = [[CPImage alloc] initWithContentsOfFile:[aValue imagePath]];
[_imageView setFrame:CGRectMake(0, imageTop, imageHeight, imageHeight)];
[_imageView setImage:image];
//[self addSubview:_imageView];
imageLoaded = true;
}
else
{
[_imageView setImage:nil];
}
if ([aValue count] > 0)
{
//console.log(CGRectGetWidth([self frame]));
[_countText setIntegerValue:[aValue count]];
[_countText sizeToFit];
var countViewWidth = (CGRectGetWidth([_countText frame]) + 4) < 20 ? 20 : CGRectGetWidth([_countText frame]) + 4;
var roundViewLeft = CGRectGetWidth([self frame]) - countViewWidth - 5;
[_countView setFrame: CGRectMake(roundViewLeft, (CGRectGetHeight(viewRect) - 18) / 2, countViewWidth, 18 )];
[_countText setFrame: CGRectMake(0,1,CGRectGetWidth([_countView frame]),CGRectGetHeight([_countView frame]))];
// If th status contains the words ERROR or FAIL then make it red otherwise blue
if ([aValue title].indexOf("ERROR") != -1 || [aValue title].indexOf("FAIL") != -1)
{
[_countView setFillColor:[CPColor redColor]];
} else {
[_countView setFillColor:[CPColor blueColor]];
}
}
else
{
[_countView setFrame:CGRectMakeZero()];
}
// Text Processing
var textLeft = imageLoaded ? 18 : 0;
var textWidth = [aValue count] > 0 ? CGRectGetWidth([self frame]) - textLeft - CGRectGetWidth([_countView frame]) - 2 :
CGRectGetWidth(viewRect) - textLeft;
if ([aValue title] != "")
{
[_text setFrame:CGRectMake(textLeft, top, textWidth, 24)];
[_text setStringValue:[aValue title]];
}
}
- (void)setThemeState:(CPThemeState)aState
{
[super setThemeState:aState];
[_text setThemeState:aState];
}
- (void)unsetThemeState:(CPThemeState)aState
{
[super unsetThemeState:aState];
[_text unsetThemeState:aState];
}
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super initWithCoder:aCoder];
_imageView = [aCoder decodeObjectForKey:"imageView"];
_text = [aCoder decodeObjectForKey:"text"];
_countView = [aCoder decodeObjectForKey:"countView"];
_countText = [aCoder decodeObjectForKey:"countText"];
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeObject:_imageView forKey:"imageView"];
[aCoder encodeObject:_text forKey:"text"];
[aCoder encodeObject:_countView forKey:"countView"];
[aCoder encodeObject:_countText forKey:"countText"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment