1#import "MyDocument.h" 2#import "LogItem.h" 3#import "GetEntry.h" 4 5@implementation MyDocument 6 7- (void)updateTotal { 8 [totalField setObjectValue: [NSNumber numberWithInt:[[self logItems] count]]]; 9} 10 11- (void)updateViews { 12 [self updateTotal]; 13 [tableview reloadData]; 14} 15 16- (void)createNewLogItem:(id)sender { 17 int row; 18 19 [GetEntry getEntry: [self logItems]]; 20 21 //Select and show thelast item added! 22 row = [[self logItems] count] - 1; 23 [self updateViews]; 24 [tableview selectRow: row byExtendingSelection: NO]; 25 [tableview scrollRowToVisible: row]; 26 [self updateChangeCount: NSChangeDone]; 27} 28 29- (void)deleteSelectedLogItem:(id)sender { 30 int row = [tableview selectedRow]; 31 if (row != -1) { 32 [[self logItems] removeObjectAtIndex: row]; 33 [tableview noteNumberOfRowsChanged]; 34 if (--row >= 0) { 35 [tableview selectRow: row byExtendingSelection: NO]; 36 [tableview scrollRowToVisible: row]; 37 } 38 [self updateTotal]; 39 [self updateChangeCount: NSChangeDone]; 40 } 41} 42 43- (int)numberOfRowsInTableView:(NSTableView *)tv { 44 [self updateTotal]; 45 return [[self logItems] count]; 46} 47 48- (id)tableView:(NSTableView *)tv 49 objectValueForTableColumn: (NSTableColumn *)tc 50 row: (int)row 51{ 52 /* 53 The table column identifier (NB not title, think of localisation) 54 should be the name of the instance variable it will dispplay. 55 We can therefore retrieve the variable using key-value coding 56 rather than having to have switch statements for each value. 57 */ 58 59 /* Compute and Format the delta in Seconds */ 60 if ([[tc identifier] isEqualTo: @"delta"] ){ 61 SInt32 delta; 62 63 if (row != 0){ 64 UInt32 current = [[[[self logItems] objectAtIndex: row] valueForKey: @"time"]unsignedIntValue]; 65 UInt32 previous = [[[[self logItems] objectAtIndex: row - 1] valueForKey: @"time"]unsignedIntValue]; 66 67 delta = current - previous; 68 } 69 else{ 70 delta = 0; 71 } 72 if (delta > 999999 || delta < 0) 73 delta = 999999; 74 return [NSNumber numberWithLong:delta]; 75 } 76 /* Format the time in Seconds */ 77 if ([[tc identifier] isEqualTo: @"time"] ){ 78 char newTime[255]; 79 UInt32 raw = [[[[self logItems] objectAtIndex: row] valueForKey: [tc identifier]]unsignedIntValue]; 80 UInt32 secs = raw / 1000000; 81 UInt32 usecs = raw % 1000000; 82 83 sprintf( newTime, "%4ld.%06ld", secs, usecs); 84 return [NSString stringWithCString:newTime]; 85 } 86 /* Format the numbers in Hex */ 87 if ([[tc identifier] isEqualTo: @"second"] | [[tc identifier] isEqualTo: @"first"]){ 88 char hexValue[255]; 89 90 sprintf(hexValue, "0x%04x", [[[[self logItems] objectAtIndex: row] valueForKey: [tc identifier]]unsignedIntValue]); 91 return [NSString stringWithCString:hexValue]; 92 } 93 /* Do the default for the type */ 94 return [[[self logItems] objectAtIndex: row] valueForKey: [tc identifier]]; 95} 96 97- (void)tableView:(NSTableView *)tv 98 setObjectValue:(id)object 99 forTableColumn:(NSTableColumn *)tc 100 row:(int)row 101{ 102} 103 104- (NSString *)windowNibName { 105 // Override returning the nib file name of the document 106 // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. 107 return @"MyDocument"; 108} 109 110- (void)windowControllerDidLoadNib:(NSWindowController *) aController{ 111 [super windowControllerDidLoadNib:aController]; 112 // Add any code here that need to be executed once the windowController has loaded the document's window. 113} 114 115- (NSData *)dataRepresentationOfType:(NSString *)aType { 116 // We need an archived representation of the model. 117 // The model is an array of logItems. An array knows how to archive 118 // itself; we should have written encoding method for LogItem. 119 return [NSArchiver archivedDataWithRootObject: [self logItems]]; 120} 121 122- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType { 123 // We should be passed an archived representation of the 124 // array of logItems. 125 [self setLogItems: [NSUnarchiver unarchiveObjectWithData: data]]; 126 return YES; 127} 128 129- (void)dealloc { 130 [self setLogItems:nil]; 131 [super dealloc]; 132} 133 134// accessor methods 135- (NSArray *)logItems { 136 if (logItems == nil) { 137 [self setLogItems: [NSMutableArray array]]; 138 } 139 return logItems; 140} 141- (void)setLogItems:(NSMutableArray *)value { 142 [value retain]; 143 [logItems release]; 144 logItems = value; 145} 146 147- (void) doTimer: (NSTimer *) myTimer{ 148// NSLog(@"doTimer"); 149 [self createNewLogItem: myTimer]; 150} 151 152- (void)startTimer:(id)sender{ 153 if (state){ 154 NSLog(@"Stop timer now!"); 155 [timer invalidate]; 156 // Display and keep track of state in button 157 [sender setTitle: @"Capture"]; 158 state = false; 159 } 160 else{ 161 NSLog(@"Start timer now!"); 162 timer = [NSTimer scheduledTimerWithTimeInterval: .5 target: self selector: @selector(doTimer:) userInfo: nil repeats: YES]; 163 164 // Display and keep track of state in button 165 [sender setTitle: @"Stop"]; 166 state = true; 167 } 168} 169 170@end 171