Created
June 8, 2009 13:47
-
-
Save RonchettiAssociati/125821 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* AppController.j | |
* WktTest04 | |
* | |
* Created by Bruno Ronchetti on May 31, 2009. | |
* Copyright 2009, Ronchetti & Associati All rights reserved. | |
*/ | |
@import <Foundation/CPObject.j> | |
@import <AppKit/CPCollectionView.j> | |
@implementation AppController : CPObject | |
{ | |
CPWindow theWindow; //this "outlet" is connected automatically by the Cib | |
CPView id horizontalSplitView; | |
CPView id topView; | |
CPView id bottomView; | |
CPView id verticalSplitView; | |
CPView id leftView; | |
CPView id rightView; | |
CPButton id firstMenuButton; | |
CPButton id secondMenuButton; | |
CPButton id thirdMenuButton; | |
CPButton id fourthMenuButton; | |
CPCollectionView id listCollectionView; | |
CPView id listItem; | |
} | |
- (void)applicationDidFinishLaunching:(CPNotification)aNotification | |
{ | |
// This is called when the application is done loading. | |
CPLogRegister(CPLogPopup); | |
/* | |
dataArray contains the nodes as elements of an Array; | |
for each element we need: | |
level of the node: | |
level 0 is the level immediately below the root | |
an indication od the type of node: | |
"+" the node has children | |
"|" the node is a leaf | |
the name of the node | |
the name of the node's parent; | |
you may have to pre-process your graph to bring it in this form; | |
*/ | |
dataArray = new Array; | |
dataArray.push([0,@"+",@"pippo","root"]); | |
dataArray.push([0,@"|",@"pippo2","root"]); | |
dataArray.push([0,@"|",@"pippo3","root"]); | |
dataArray.push([0,@"+",@"pippo4","root"]); | |
dataArray.push([1,@"+",@"pippo5","pippo4"]); | |
dataArray.push([2,@"|",@"pippo6","pippo5"]); | |
dataArray.push([1,@"|",@"pluto", "pippo"]); | |
dataArray.push([1,@"+",@"paperino","pippo"]); | |
dataArray.push([2,@"|",@"gambadilegno", "paperino"]); | |
dataArray.push([2,@"|",@"minnie","paperino"]); | |
displayedArray = new Array; | |
initializeDisplayedArray(); | |
var bounds = [leftView bounds]; | |
folderScrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 450)]; | |
[folderScrollView setAutohidesScrollers:YES]; | |
[folderScrollView setBackgroundColor:[CPColor blueColor]]; | |
folderCollectionViewItem = [[CPCollectionViewItem alloc] init]; | |
[folderCollectionViewItem setView:[[FolderListCell alloc] initWithFrame:CGRectMakeZero()]]; | |
folderCollectionView = [[CPCollectionView alloc] initWithFrame:CGRectMake(0, 0, 200, 450)]; | |
[folderCollectionView setItemPrototype:folderCollectionViewItem]; | |
[folderCollectionView setMinItemSize:CGSizeMake(20, 18)]; | |
[folderCollectionView setMaxItemSize:CGSizeMake(1000, 72)]; | |
[folderCollectionView setMaxNumberOfColumns:1]; | |
[folderCollectionView setVerticalMargin:0]; | |
[folderCollectionView setAutoresizingMask:CPViewWidthSizable]; | |
[folderCollectionView setContent:displayedArray]; | |
[folderScrollView setDocumentView:folderCollectionView]; | |
[[folderScrollView contentView] setBackgroundColor:[CPColor colorWithCalibratedRed:213/255 green:221/255 blue:230/255 alpha:1.0]]; | |
[leftView addSubview:folderScrollView]; | |
} | |
- (void)awakeFromCib | |
{ | |
// This is called when the cib is done loading. | |
// You can implement this method on any object instantiated from a Cib. | |
// It's a useful hook for setting up current UI values, and other things. | |
imgNumberOne = [[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"Number_1.png"] size:CPSizeMake(24, 24)]; | |
imgNumberTwo = [[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"Number_2.png"] size:CPSizeMake(24, 24)]; | |
imgNumberThree = [[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"Number_3.png"] size:CPSizeMake(24, 24)]; | |
imgNumberFour = [[CPImage alloc] initWithContentsOfFile:[[CPBundle mainBundle] pathForResource:@"Number_4.png"] size:CPSizeMake(24, 24)]; | |
// In this case, we want the window from Cib to become our full browser window | |
[theWindow setFullBridge:YES]; | |
[horizontalSplitView setPosition:36 ofDividerAtIndex:0]; | |
[verticalSplitView setPosition:150 ofDividerAtIndex:0]; | |
[topView setBackgroundColor:[CPColor colorWithCalibratedRed:66/255 green:72/255 blue: 78/255 alpha:1.0]]; | |
[leftView setBackgroundColor:[CPColor colorWithCalibratedRed:223/255 green:225/255 blue: 227/255 alpha:1.0]]; | |
[firstMenuButton setImage:imgNumberOne]; | |
[secondMenuButton setImage:imgNumberTwo]; | |
[thirdMenuButton setImage:imgNumberThree]; | |
[fourthMenuButton setImage:imgNumberFour]; | |
} | |
- (void)reloadList:(id)sender | |
{ | |
[folderCollectionView setContent:[displayedArray copy]]; | |
} | |
@end | |
@implementation FolderListCell : CPView | |
{ | |
CPLabel label1; | |
CPButton button1; | |
} | |
- (void)setRepresentedObject:(JSObject)anObject | |
{ | |
self = [super initWithFrame:CGRectMake(0, 0, 140, 20)]; | |
indent = anObject[0]*10; | |
type = anObject[1]; | |
description = anObject[2]; | |
button1 = [[CPButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; | |
[button1 setFrameOrigin: CGPointMake(indent, 0)]; | |
[button1 setBordered:NO]; | |
[button1 setTitle:type]; | |
[button1 setFont:[CPFont systemFontOfSize:18]]; | |
[button1 setTarget:self]; | |
[button1 setAction:@selector(button1Pressed:)]; | |
[self addSubview:button1]; | |
label1 = [[CPTextField alloc] initWithFrame:CGRectMake(0, 0, 120, 18)]; | |
[label1 setStringValue:description]; | |
[label1 setFont:[CPFont systemFontOfSize:12]]; | |
[label1 setTextColor:[CPColor blackColor]]; | |
[label1 setFrameOrigin: CGPointMake(indent+20, 0)]; | |
[self addSubview:label1]; | |
} | |
-(IBAction)button1Pressed:(id)sender | |
{ | |
if ([button1 title] == @"+") | |
{ | |
addChildrenToDisplayedArray([label1 stringValue]); | |
[button1 setTitle:@"-"]; | |
[label1 setTextColor:[CPColor grayColor]]; | |
[CPApp sendAction:@selector(reloadList:) to:nil from:sender]; | |
} | |
else if ([button1 title] == @"-") | |
{ | |
removeChildrenFromDisplayedArray([label1 stringValue]); | |
[button1 setTitle:@"+"]; | |
[label1 setTextColor:[CPColor blackColor]]; | |
[CPApp sendAction:@selector(reloadList:) to:nil from:sender]; | |
} | |
else | |
{ | |
} | |
} | |
@end | |
function initializeDisplayedArray() { | |
for (var i =0; i< dataArray.length; i++) { | |
if (dataArray[i][0] < 1) { | |
displayedArray.push(dataArray[i]); | |
} | |
} | |
return displayedArray; | |
} | |
function addChildrenToDisplayedArray(key) { | |
for (var i =0; i< displayedArray.length; i++) { | |
if (displayedArray[i][2] == key) { | |
var leadingArray = displayedArray.slice(0,i+1); | |
var trailingArray = displayedArray.slice(i+1); | |
break; | |
} | |
} | |
for (var j =0; j< dataArray.length; j++) { | |
if (dataArray[j][3] == key) { | |
leadingArray.push(dataArray[j]); | |
CPLog(leadingArray.length); | |
} | |
} | |
displayedArray = leadingArray.concat(trailingArray); | |
return displayedArray; | |
} | |
function removeChildrenFromDisplayedArray(key) { | |
for (var i =0; i< displayedArray.length; i++) { | |
if (displayedArray[i][2] == key) { | |
var keyLevel = displayedArray[i][0]; | |
break; | |
} | |
} | |
for (var j =i+1; j< displayedArray.length; j++) { | |
CPLog(j + " " +displayedArray[j]); | |
if (displayedArray[j][0] > keyLevel) { | |
displayedArray.splice(j,1); | |
j--; | |
} | |
else | |
{ | |
break; | |
} | |
} | |
return displayedArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment