Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * AppController.j
- * desk_pad
- *
- * Created by You on May 18, 2011.
- * Copyright 2011, Your Company All rights reserved.
- */
- @import <Foundation/CPObject.j>
- @import <AppKit/CPTableView.j>
- @import <AppKit/CPTextField.j>
- @import <AppKit/CPPopUpButton.j>
- @import <AppKit/CPButtonBar.j>
- @import "Client.j"
- @import "ClientArrayController.j"
- @import "Project.j"
- @import "ProjectArrayController.j"
- @import "WorkTime.j"
- @import "WorkTimeArrayController.j"
- @import "Dns.j"
- @import "DnsArrayController.j"
- @import "DnsRecordsOutlineController.j"
- //@import "TabTableView.j"
- @implementation AppController : CPObject
- {
- CPWindow theWindow; //this "outlet" is connected automatically by the Cib
- CPMutableArray clients;
- CPMutableDictionary clientsLookup;
- IBOutlet ClientArrayController aClientArrayController;
- IBOutlet CPTableView aClientsTable;
- IBOutlet CPButton saveClientButton;
- CPMutableArray projects;
- IBOutlet ProjectArrayController aProjectArrayController;
- IBOutlet CPTableView aProjectsTable;
- IBOutlet CPButton saveProjectButton;
- IBOutlet CPPopUpButton clientsForProjectsPopUp;
- //CPPopUpButton projectPopUp;
- CPMutableArray workTimes;
- IBOutlet WorkTimeArrayController aWorkTimeArrayController;
- IBOutlet CPTableView aWorkTimesTable;
- CPMutableArray dns;
- IBOutlet DnsArrayController aDnsArrayController;
- IBOutlet CPTableView aDnsTable;
- IBOutlet CPButton saveDnsButton;
- IBOutlet CPPopUpButton clientsForDnsPopUp;
- IBOutlet CPOutlineView aDnsOutline;
- IBOutlet CPTabView myTabView;
- IBOutlet CPButton clientsSwitchButton;
- IBOutlet CPButton projectsSwitchButton;
- IBOutlet CPButton workTimesSwitchButton;
- IBOutlet CPButton dnsSwitchButton;
- }
- -(id)init
- {
- var clients = [Client all];
- [clients class];
- var projects = [Project all];
- [projects class];
- var workTimes = [WorkTime all];
- [workTimes class];
- var dns = [Dns all];
- [dns class];
- return self;
- }
- - (void)switchTabFromButton:(id)sender
- {
- switch([sender title]) {
- case @"Clients":
- [myTabView selectTabViewItemAtIndex:0];
- break;
- case @"Projects":
- [myTabView selectTabViewItemAtIndex:1];
- break;
- case @"WorkTimes":
- [myTabView selectTabViewItemAtIndex:2];
- break;
- case @"DNS":
- [myTabView selectTabViewItemAtIndex:3];
- break;
- }
- }
- - (void)saveClient:(id)sender
- {
- var aClient = [[aClientArrayController selectedObjects] lastObject];
- if (![aClient cdbId]) {
- [aClient setCdbId:[[aClient class] couchId:aClient]];
- }
- [aClient save];
- if ([clientsLookup valueForKey:[aClient cdbId]] == null) {
- [clientsForProjectsPopUp addItemWithTitle:[aClient name]];
- [clientsLookup setObject:item forKey:[aClient cdbId]];
- }
- }
- - (void)saveProject:(id)sender
- {
- var aProject = [[aProjectArrayController selectedObjects] lastObject];
- if (![aProject cdbId]) {
- [aProject setCdbId:[[aProject class] couchId:aProject]];
- }
- var selectedClientId = [[clients objectAtIndex:[clientsForProjectsPopUp indexOfSelectedItem]] cdbId];
- [aProject setClientId:selectedClientId];
- [aProject save];
- if ([[projectPopUp itemTitles] indexOfObject:[aProject name]] < 0) {
- [projectPopUp addItemWithTitle:[aProject name]];
- }
- if ([WTProjectsArray indexOfObject:[aProject cdbId]] < 0) {
- [WTProjectsArray addObject:[aProject cdbId]];
- }
- var projectColumn = [aWorkTimesTable tableColumnWithIdentifier:@"projectId"];
- var projectPopUp = [[CPPopUpButton alloc] initWithFrame:CGRectMake([projectColumn width], 12)];
- [projects enumerateObjectsUsingBlock:function(item) {
- [projectPopUp addItemWithTitle:[item name]];
- }];
- [projectColumn setDataView:projectPopUp];
- [aWorkTimesTable reloadData];
- }
- - (void)saveDns:(id)sender
- {
- var aDns = [[aDnsArrayController selectedObjects] lastObject];
- if (![aDns cdbId]) {
- [aDns setCdbId:[[aDns class] couchId:aProject]];
- }
- var selectedClientId = [[clients objectAtIndex:[clientsForDnsPopUp indexOfSelectedItem]] cdbId];
- [aDns setClientId:selectedClientId];
- [aDns save];
- }
- - (void)applicationDidFinishLaunching:(CPNotification)aNotification
- {
- // This is called when the application is done loading.
- [clientsSwitchButton setAction:@selector(switchTabFromButton:)];
- [projectsSwitchButton setAction:@selector(switchTabFromButton:)];
- [workTimesSwitchButton setAction:@selector(switchTabFromButton:)];
- [dnsSwitchButton setAction:@selector(switchTabFromButton:)];
- [saveClientButton setAction:@selector(saveClient:)];
- [saveProjectButton setAction:@selector(saveProject:)];
- [saveDnsButton setAction:@selector(saveDns:)];
- clientsLookup = [[CPMutableDictionary alloc] init];
- [clients enumerateObjectsUsingBlock:function(item) {
- [clientsForProjectsPopUp addItemWithTitle:[item name]];
- [clientsForDnsPopUp addItemWithTitle:[item name]];
- [clientsLookup setObject:item forKey:[item cdbId]];
- }];
- [myTabView selectTabViewItemAtIndex:0];
- }
- - (void)observeValueForKeyPath:(CPString)aKeyPath
- ofObject:(id)anObject
- change:(CPDictionary)aChange
- context:(id)aContext
- {
- var aItem = [[anObject arrangedObjects] objectAtIndex:[anObject selectionIndex]];
- var new_value = [aChange valueForKey:@"CPKeyValueChangeNewKey"];
- var old_value = [aChange valueForKey:@"CPKeyValueChangeOldKey"];
- //CPLog.debug(@"KVO %@ %@", aContext, aItem);
- //console.log(aItem);
- /* Project */
- if ([aItem class] == [Project class]) {
- var clientIndex = [clients indexOfObject:[clientsLookup objectForKey:[aItem clientId]]];
- [clientsForProjectsPopUp selectItemAtIndex:clientIndex];
- }
- /* Dns */
- if ([aItem class] == [Dns class]) {
- var clientIndex = [clients indexOfObject:[clientsLookup objectForKey:[aItem clientId]]];
- [clientsForDnsPopUp selectItemAtIndex:clientIndex];
- var dnsOutlineController = [[DnsRecordsOutlineController alloc] initWithDns:aItem];
- console.log("init dnsOutlineController", dnsOutlineController );
- [aDnsOutline setDataSource:dnsOutlineController];
- //[aDnsOutline setDelegate:dnsOutlineController];
- //[aDnsOutline expandItem:nil expandChildren:YES];
- //[scrollView setDocumentView:aDnsOutline];
- [aDnsOutline reloadData];
- console.log("reload item dnsOutlineController");
- //[aDnsOutline reloadItem:nil reloadChildren:NO];
- }
- /* WorkTime */
- if ([aItem class] == [WorkTime class]) {
- if (![new_value isEqual:old_value]) {
- if (aContext == "date" || aContext == "time") {
- var aNewWorkTime = [[WorkTime alloc] init];
- [aNewWorkTime setAttributes:[aWorkTime attributes]];
- [aNewWorkTime setIdentifier:null];
- [aNewWorkTime setCdbId:[[aNewWorkTime class] couchId:aNewWorkTime]];
- [aNewWorkTime save]; // TODO check if it really got saved
- [aItem destroy];
- } else {
- [aItem save];
- }
- }
- }
- }
- /*! Notification responder of TARemoveTableRow
- @param aNotification the received notification. This notification will contains as object the row
- */
- - (void)tableRowRemoved:(CPNotification)aNotification
- {
- var row = [[aNotification object] lastObject];
- if ([row cdbId]) {
- [row destroy]; // TODO remove itesm with space propogate error to interface
- }
- //CPLog.debug([[[aNotification object] lastObject] date]);
- }
- - (void)awakeFromCib
- {
- //CPLog.debug(self);
- [[CPNotificationCenter defaultCenter]
- addObserver:self
- selector:@selector(tableRowRemoved:)
- name:@"TARemoveTableRow" object:nil];
- [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.date" options:nil context:@"date"];
- [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.time" options:nil context:@"time"];
- [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.duration" options:nil context:@"duration"];
- [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.projectId" options:nil context:@"projectId"];
- [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.billable" options:nil context:@"billable"];
- [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.category" options:nil context:@"category"];
- [aWorkTimeArrayController addObserver:self forKeyPath:@"arrangedObjects.comment" options:nil context:@"comment"];
- [aProjectArrayController addObserver:self forKeyPath:@"selection.name" options:nil context:@"name"];
- [aDnsArrayController addObserver:self forKeyPath:@"selection.domain" options:nil context:@"domain"];
- //[aWorkTimeArrayController addObserver:self forKeyPath:@"selectedObjects" options:nil context:@"selected"];
- [theWindow setFullPlatformWindow:YES];
- // billable popup
- var billableColumn = [aWorkTimesTable tableColumnWithIdentifier:@"billable"];
- var billablePopUp = [[CPPopUpButton alloc] initWithFrame:CGRectMake([billableColumn width], 12)];
- [WTBillableArray enumerateObjectsUsingBlock:function(item) {
- [billablePopUp addItemWithTitle:item];
- }];
- [billableColumn setDataView:billablePopUp];
- // category popup
- var categoryColumn = [aWorkTimesTable tableColumnWithIdentifier:@"category"];
- var categoryPopUp = [[CPPopUpButton alloc] initWithFrame:CGRectMake([categoryColumn width], 12)];
- [WTCategoryArray enumerateObjectsUsingBlock:function(item) {
- [categoryPopUp addItemWithTitle:item];
- }];
- [categoryColumn setDataView:categoryPopUp];
- // projects popup
- var projectColumn = [aWorkTimesTable tableColumnWithIdentifier:@"projectId"];
- var projectPopUp = [[CPPopUpButton alloc] initWithFrame:CGRectMake([projectColumn width], 12)];
- [projects enumerateObjectsUsingBlock:function(item) {
- [projectPopUp addItemWithTitle:[item name]];
- }];
- CPLog.debug("old dataview %@", [projectColumn dataView]);
- [projectColumn setDataView:projectPopUp];
- [aWorkTimesTable reloadData];
- }
- /*- (void)keyDown:(CPEvent)event
- {
- console.log("key pressed");
- [super keyDown:event]; // let somebody else handle the event
- }*/
- @end
Add Comment
Please, Sign In to add comment