[26] | 1 | //
|
---|
| 2 | // PSAlarmsController.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Fri Oct 11 2002.
|
---|
| 6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "PSAlarmsController.h"
|
---|
| 10 | #import "PSAlarm.h"
|
---|
[53] | 11 | #import "PSAlerts.h"
|
---|
[26] | 12 | #import "NSTableView-NJRExtensions.h"
|
---|
[51] | 13 | #import "NJRTableView.h"
|
---|
| 14 | #import "NJRTableDelegate.h"
|
---|
[26] | 15 |
|
---|
| 16 | @implementation PSAlarmsController
|
---|
| 17 |
|
---|
[51] | 18 | - (void)alarmsChanged;
|
---|
| 19 | {
|
---|
| 20 | reorderedAlarms = [[tableView delegate] reorderedDataForData: [alarms alarms]];
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[26] | 23 | - (id)init;
|
---|
| 24 | {
|
---|
| 25 | if ( (self = [super initWithWindowNibName: @"Alarms"]) != nil) {
|
---|
| 26 | alarms = [PSAlarms allAlarms];
|
---|
[28] | 27 | // XXX workaround for bug in 10.2.1, 10.1.5: autosave name set in IB doesn't show up
|
---|
| 28 | [self setWindowFrameAutosaveName: @"Pester alarm list"];
|
---|
| 29 | // Apple documents the NSUserDefaults key, so we can rely on it hopefully.
|
---|
| 30 | if (nil == [[NSUserDefaults standardUserDefaults] objectForKey:
|
---|
| 31 | [@"NSWindow Frame " stringByAppendingString: [[self window] frameAutosaveName]]])
|
---|
[51] | 32 | {
|
---|
[28] | 33 | [[self window] center];
|
---|
[51] | 34 | }
|
---|
[26] | 35 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(alarmsChanged) name: PSAlarmsDidChangeNotification object: alarms];
|
---|
[51] | 36 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(tableViewSelectionDidChange:) name: NSTableViewSelectionDidChangeNotification object: tableView];
|
---|
[26] | 37 | [tableView setAutosaveName: @"Alarm list"];
|
---|
| 38 | [tableView setAutosaveTableColumns: YES];
|
---|
[51] | 39 | [self alarmsChanged];
|
---|
[26] | 40 | [[self window] makeFirstResponder: tableView];
|
---|
[51] | 41 | [[self window] setResizeIncrements: NSMakeSize(1, [tableView cellHeight])];
|
---|
[26] | 42 | }
|
---|
| 43 | return self;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | - (IBAction)remove:(id)sender;
|
---|
| 48 | {
|
---|
[51] | 49 | [alarms removeAlarms: [[tableView delegate] selectedItems]];
|
---|
[26] | 50 | }
|
---|
| 51 |
|
---|
| 52 | @end
|
---|
| 53 |
|
---|
| 54 | @implementation PSAlarmsController (NSTableDataSource)
|
---|
| 55 |
|
---|
| 56 | - (int)numberOfRowsInTableView:(NSTableView *)tableView;
|
---|
| 57 | {
|
---|
| 58 | return [alarms alarmCount];
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
|
---|
| 62 | {
|
---|
[51] | 63 | PSAlarm *alarm = [reorderedAlarms objectAtIndex: row];
|
---|
[26] | 64 |
|
---|
| 65 | if ([[tableColumn identifier] isEqualToString: @"message"]) return [alarm message];
|
---|
| 66 | else {
|
---|
| 67 | NSCalendarDate *date = [alarm date];
|
---|
[28] | 68 | if ([[tableColumn identifier] isEqualToString: @"date"]) return [alarm shortDateString];
|
---|
[26] | 69 | if ([[tableColumn identifier] isEqualToString: @"time"]) {
|
---|
| 70 | if (date == nil) return @"ÇexpiredÈ";
|
---|
[28] | 71 | return [alarm timeString];
|
---|
[26] | 72 | }
|
---|
| 73 | }
|
---|
| 74 | return nil;
|
---|
| 75 | }
|
---|
| 76 | @end
|
---|
| 77 |
|
---|
[51] | 78 | @implementation PSAlarmsController (NJRTableViewDataSource)
|
---|
| 79 |
|
---|
| 80 | - (void)removeSelectedRowsFromTableView:(NSTableView *)aTableView;
|
---|
| 81 | {
|
---|
| 82 | [self remove: aTableView];
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[53] | 85 | - (NSString *)tableView:(NSTableView *)aTableView toolTipForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
|
---|
| 86 | {
|
---|
| 87 | PSAlarm *alarm = [reorderedAlarms objectAtIndex: rowIndex];
|
---|
| 88 |
|
---|
| 89 | return [[alarm prettyDescription] string];
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[51] | 92 | @end
|
---|
| 93 |
|
---|
[26] | 94 | @implementation PSAlarmsController (NSTableViewNotifications)
|
---|
| 95 |
|
---|
| 96 | - (void)tableViewSelectionDidChange:(NSNotification *)aNotification;
|
---|
| 97 | {
|
---|
| 98 | [removeButton setEnabled: ([tableView numberOfSelectedRows] != 0)];
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | @end
|
---|
| 102 |
|
---|
| 103 | @implementation PSAlarmsController (NSWindowDelegate)
|
---|
| 104 |
|
---|
[28] | 105 | // XXX workaround for bug in 10.1.5, 10.2.1 (and earlier?): no autosave on window move
|
---|
| 106 | - (void)windowDidMove:(NSNotification *)aNotification
|
---|
| 107 | {
|
---|
| 108 | NSString *autosaveName = [[self window] frameAutosaveName];
|
---|
| 109 | // on initial display, we get a notification inside -[NSWindow setFrameAutosaveName]!
|
---|
| 110 | if (autosaveName != nil) {
|
---|
| 111 | [[self window] saveFrameUsingName: autosaveName];
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[26] | 115 | - (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame;
|
---|
| 116 | {
|
---|
| 117 | NSWindow *window = [tableView window];
|
---|
| 118 | NSRect frame = [window frame];
|
---|
| 119 | NSScrollView *scrollView = [tableView enclosingScrollView];
|
---|
| 120 | float displayedHeight = [[scrollView contentView] bounds].size.height;
|
---|
| 121 | float heightChange = [[scrollView documentView] bounds].size.height - displayedHeight;
|
---|
| 122 | float heightExcess;
|
---|
| 123 |
|
---|
| 124 | if (heightChange >= 0 && heightChange <= 1) {
|
---|
| 125 | // either the window is already optimal size, or it's too big
|
---|
| 126 | float rowHeight = [tableView cellHeight];
|
---|
| 127 | heightChange = (rowHeight * [tableView numberOfRows]) - displayedHeight;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | frame.size.height += heightChange;
|
---|
| 131 |
|
---|
| 132 | if ( (heightExcess = [window minSize].height - frame.size.height) > 1 ||
|
---|
| 133 | (heightExcess = [window maxSize].height - frame.size.height) < 1) {
|
---|
| 134 | heightChange += heightExcess;
|
---|
| 135 | frame.size.height += heightExcess;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | frame.origin.y -= heightChange;
|
---|
| 139 |
|
---|
| 140 | return frame;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[34] | 143 | @end |
---|