Skip to content

Instantly share code, notes, and snippets.

@purem
Created December 31, 2010 15:44
Show Gist options
  • Save purem/761091 to your computer and use it in GitHub Desktop.
Save purem/761091 to your computer and use it in GitHub Desktop.
@import <AppKit/CPWindowController.j>
@import "JTSearchBuilderRowView.j"
var searchBuilderSharedInstance = nil;
@implementation JTSearchBuilderWindowController : CPWindowController
{
var border;
var elementPadding;
var buttonHeight;
var rowHeight
var rowCount;
var initialHeight;
var matchSelector;
var rowsView;
var rowSet;
var addButton;
}
+ (JTSearchBuilderViewController)sharedSearchBuilder
{
if (!searchBuilderSharedInstance)
{
searchBuilderSharedInstance = [[JTSearchBuilderWindowController alloc] init];
}
return searchBuilderSharedInstance;
}
- (id)init
{
var frame = CGRectMake(0, 0, 800, 500);
var theWindow = [[CPPanel alloc] initWithContentRect:frame
styleMask:CPHUDBackgroundWindowMask | CPClosableWindowMask | CPTitledWindowMask];
if (self = [super initWithWindow:theWindow])
{
//Get fields
//Get operations
[theWindow setTitle:@"Search Builder"];
[theWindow setLevel:CPFloatingWindowLevel];
[theWindow center];
[theWindow setDelegate:self];
var contentView = [theWindow contentView];
buttonHeight = 24;
border = 10;
rowHeight = 24;
rowCount = 0;
elementPadding = 3;
initialHeight = buttonHeight + 2 * border;
rowSet = [CPSet set];
var matchLabel = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[matchLabel setObjectValue:@"Match"];
[matchLabel setFont:[CPFont systemFontOfSize:16.0]];
[matchLabel setTextColor:[CPColor whiteColor]];
[matchLabel setAlignment:CPLeftTextAlignment];
[matchLabel sizeToFit];
[matchLabel setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
var tempFrame = [matchLabel frame];
tempFrame.origin = CPPointMake(border, border);
[matchLabel setFrame:tempFrame];
[contentView addSubview:matchLabel];
matchSelector = [[CPPopUpButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(tempFrame) + elementPadding, border, 70, buttonHeight)];
[matchSelector addItemWithTitle:@"Any"];
[matchSelector addItemWithTitle:@"All"];
[matchSelector setAutoresizingMask:CPViewMinYMargin];
[contentView addSubview:matchSelector];
var matchLabel2 = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[matchLabel2 setObjectValue:@" of the following rules:"];
[matchLabel2 setFont:[CPFont systemFontOfSize:16.0]];
[matchLabel2 setTextColor:[CPColor whiteColor]];
[matchLabel2 setAlignment:CPLeftTextAlignment];
[matchLabel2 sizeToFit];
[matchLabel2 setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
var tempFrame = [matchLabel2 frame];
tempFrame.origin = CPPointMake(CGRectGetMaxX([matchSelector frame]), border);
[matchLabel2 setFrame:tempFrame];
[contentView addSubview:matchLabel2];
rowsView = [[CPView alloc] initWithFrame:CGRectMake(border, CGRectGetMaxY([matchLabel2 frame]) + elementPadding, frame.size.width, initialHeight)];
[contentView addSubview:rowsView];
var buttonWidth = CGRectGetWidth(frame) / 8;
addButton = [[CPButton alloc] initWithFrame:CGRectMake((CGRectGetWidth(frame) - buttonWidth) / 2, border, buttonWidth, buttonHeight)];
[addButton setTitle:"Add Field"];
[addButton setTarget:self];
[addButton setAction:@selector(addRow)];
[addButton setAutoresizingMask:CPViewMinYMargin];
[rowsView addSubview:addButton];
searchButton = [[CPButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame) - buttonWidth - 2 * border, border, buttonWidth, buttonHeight)];
[searchButton setTitle:"Search"];
[searchButton setTarget:self];
[searchButton setAction:@selector(search)];
[searchButton setAutoresizingMask:CPViewMinYMargin];
[rowsView addSubview:searchButton];
[self addRow];
}
return self;
}
- (void)showSearchBuilder:(id)sender
{
[searchBuilderSharedInstance showWindow:sender];
}
- (void)hideSearchBuilder:(id)sender
{
[searchBuilderSharedInstance orderOut:sender];
}
- (void)toggleSearchBuilder:(id)sender
{
var searchBuilderWindow = [searchBuilderSharedInstance window];
if ([searchBuilderWindow isVisible])
{
[searchBuilderSharedInstance orderOut:sender];
}
else
{
[searchBuilderSharedInstance showWindow:sender];
}
}
- (void)addRow {
var newRow = [[JTSearchBuilderRowView alloc] initWithFrame:CGRectMake(0, rowCount * (24 + elementPadding), CGRectGetWidth([rowsView frame]) - 2 * border, rowHeight)
index:rowCount
delegate:self];
[rowsView addSubview:newRow];
var frame = [rowsView frame];
frame.size.height += buttonHeight + elementPadding;
[rowsView setFrame:frame];
[rowSet addObject:newRow];
rowCount++;
}
- (void)removeRow:(id)sender {
var deleteRowView = [sender superview];
if ([deleteRowView isKindOfClass:[JTSearchBuilderRowView class]])
{
var removedRowIndex = [deleteRowView index];
if (removedRowIndex != 0)
{
[deleteRowView removeFromSuperview];
[rowSet removeObject:deleteRowView];
deleteRowView = nil;
rowCount--;
//Enumerate rows and move rows that are above index down
var rowEnumerator = [rowSet objectEnumerator];
while ((currentRow = [rowEnumerator nextObject]) != nil)
{
if ([currentRow index] > removedRowIndex)
{
var rowFrame = [currentRow frame];
rowFrame.origin.y -= buttonHeight + elementPadding;
[currentRow setFrame:rowFrame];
}
}
//Reduce size of rowView
var rowViewFrame = [rowsView frame];
rowViewFrame.size.height -= buttonHeight + elementPadding;
[rowsView setFrame:rowViewFrame];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment