[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"
|
---|
[102] | 12 | #import "NSString-NJRExtensions.h"
|
---|
[26] | 13 | #import "NSTableView-NJRExtensions.h"
|
---|
[51] | 14 | #import "NJRTableView.h"
|
---|
| 15 | #import "NJRTableDelegate.h"
|
---|
[26] | 16 |
|
---|
[113] | 17 | @interface PSAlarmsController (PrivateUndoSupport)
|
---|
| 18 |
|
---|
| 19 | - (void)_restoreAlarms:(NSSet *)selectedAlarms;
|
---|
| 20 | - (void)_removeAlarms:(NSSet *)selectedAlarms;
|
---|
| 21 |
|
---|
| 22 | @end
|
---|
| 23 |
|
---|
[26] | 24 | @implementation PSAlarmsController
|
---|
| 25 |
|
---|
[51] | 26 | - (void)alarmsChanged;
|
---|
| 27 | {
|
---|
[102] | 28 | reorderedAlarms = [[alarmList delegate] reorderedDataForData: [alarms alarms]];
|
---|
[51] | 29 | }
|
---|
| 30 |
|
---|
[26] | 31 | - (id)init;
|
---|
| 32 | {
|
---|
| 33 | if ( (self = [super initWithWindowNibName: @"Alarms"]) != nil) {
|
---|
| 34 | alarms = [PSAlarms allAlarms];
|
---|
[28] | 35 | // XXX workaround for bug in 10.2.1, 10.1.5: autosave name set in IB doesn't show up
|
---|
| 36 | [self setWindowFrameAutosaveName: @"Pester alarm list"];
|
---|
| 37 | // Apple documents the NSUserDefaults key, so we can rely on it hopefully.
|
---|
| 38 | if (nil == [[NSUserDefaults standardUserDefaults] objectForKey:
|
---|
| 39 | [@"NSWindow Frame " stringByAppendingString: [[self window] frameAutosaveName]]])
|
---|
[51] | 40 | {
|
---|
[28] | 41 | [[self window] center];
|
---|
[51] | 42 | }
|
---|
[26] | 43 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(alarmsChanged) name: PSAlarmsDidChangeNotification object: alarms];
|
---|
[102] | 44 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(tableViewSelectionDidChange:) name: NSTableViewSelectionDidChangeNotification object: alarmList];
|
---|
| 45 | messageAttributes = [[NSDictionary alloc] initWithObjectsAndKeys: [[[alarmList tableColumnWithIdentifier: @"message"] dataCell] font], NSFontAttributeName, nil];
|
---|
| 46 | [alarmList setAutosaveName: @"Alarm list"];
|
---|
| 47 | [alarmList setAutosaveTableColumns: YES];
|
---|
[51] | 48 | [self alarmsChanged];
|
---|
[102] | 49 | [[self window] makeFirstResponder: alarmList];
|
---|
| 50 | [[self window] setResizeIncrements: NSMakeSize(1, [alarmList cellHeight])];
|
---|
[26] | 51 | }
|
---|
| 52 | return self;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[102] | 55 | - (void)dealloc;
|
---|
| 56 | {
|
---|
| 57 | [reorderedAlarms release];
|
---|
| 58 | [messageAttributes release];
|
---|
| 59 | [super dealloc];
|
---|
| 60 | }
|
---|
[26] | 61 |
|
---|
[113] | 62 | - (NSUndoManager *)undoManager;
|
---|
| 63 | {
|
---|
| 64 | return [[self window] undoManager];
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | - (void)_restoreAlarms:(NSSet *)selectedAlarms;
|
---|
| 68 | {
|
---|
| 69 | [alarms restoreAlarms: selectedAlarms];
|
---|
| 70 | [[alarmList delegate] selectItems: selectedAlarms];
|
---|
| 71 | [[self undoManager] setActionName: NSLocalizedString(@"Alarm Removal", "Undo action")];
|
---|
| 72 | [[[self undoManager] prepareWithInvocationTarget: self] _removeAlarms: selectedAlarms];
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | - (void)_removeAlarms:(NSSet *)selectedAlarms;
|
---|
| 76 | {
|
---|
| 77 | [alarms removeAlarms: selectedAlarms];
|
---|
| 78 | [[self undoManager] setActionName: NSLocalizedString(@"Alarm Removal", "Undo action")];
|
---|
| 79 | [[[self undoManager] prepareWithInvocationTarget: self] _restoreAlarms: selectedAlarms];
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[26] | 82 | - (IBAction)remove:(id)sender;
|
---|
| 83 | {
|
---|
[113] | 84 | [self _removeAlarms: [[alarmList delegate] selectedItems]];
|
---|
[26] | 85 | }
|
---|
| 86 |
|
---|
| 87 | @end
|
---|
| 88 |
|
---|
| 89 | @implementation PSAlarmsController (NSTableDataSource)
|
---|
| 90 |
|
---|
| 91 | - (int)numberOfRowsInTableView:(NSTableView *)tableView;
|
---|
| 92 | {
|
---|
| 93 | return [alarms alarmCount];
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
|
---|
| 97 | {
|
---|
[51] | 98 | PSAlarm *alarm = [reorderedAlarms objectAtIndex: row];
|
---|
[26] | 99 |
|
---|
[102] | 100 | if ([[tableColumn identifier] isEqualToString: @"message"]) {
|
---|
| 101 | NSMutableString *message = [[alarm message] mutableCopy];
|
---|
| 102 | [message truncateToWidth: [tableView frameOfCellAtColumn: 0 row: row].size.width by: NSLineBreakByTruncatingTail withAttributes: messageAttributes];
|
---|
| 103 | return [message autorelease];
|
---|
| 104 | } else {
|
---|
[26] | 105 | NSCalendarDate *date = [alarm date];
|
---|
[28] | 106 | if ([[tableColumn identifier] isEqualToString: @"date"]) return [alarm shortDateString];
|
---|
[26] | 107 | if ([[tableColumn identifier] isEqualToString: @"time"]) {
|
---|
[355] | 108 | if (date == nil) return @"«expired»";
|
---|
[28] | 109 | return [alarm timeString];
|
---|
[26] | 110 | }
|
---|
| 111 | }
|
---|
| 112 | return nil;
|
---|
| 113 | }
|
---|
| 114 | @end
|
---|
| 115 |
|
---|
[51] | 116 | @implementation PSAlarmsController (NJRTableViewDataSource)
|
---|
| 117 |
|
---|
| 118 | - (void)removeSelectedRowsFromTableView:(NSTableView *)aTableView;
|
---|
| 119 | {
|
---|
| 120 | [self remove: aTableView];
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[53] | 123 | - (NSString *)tableView:(NSTableView *)aTableView toolTipForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex;
|
---|
| 124 | {
|
---|
| 125 | PSAlarm *alarm = [reorderedAlarms objectAtIndex: rowIndex];
|
---|
| 126 |
|
---|
| 127 | return [[alarm prettyDescription] string];
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[51] | 130 | @end
|
---|
| 131 |
|
---|
[26] | 132 | @implementation PSAlarmsController (NSTableViewNotifications)
|
---|
| 133 |
|
---|
| 134 | - (void)tableViewSelectionDidChange:(NSNotification *)aNotification;
|
---|
| 135 | {
|
---|
[102] | 136 | [removeButton setEnabled: ([alarmList numberOfSelectedRows] != 0)];
|
---|
[26] | 137 | }
|
---|
| 138 |
|
---|
| 139 | @end
|
---|
| 140 |
|
---|
| 141 | @implementation PSAlarmsController (NSWindowDelegate)
|
---|
| 142 |
|
---|
[28] | 143 | // XXX workaround for bug in 10.1.5, 10.2.1 (and earlier?): no autosave on window move
|
---|
| 144 | - (void)windowDidMove:(NSNotification *)aNotification
|
---|
| 145 | {
|
---|
| 146 | NSString *autosaveName = [[self window] frameAutosaveName];
|
---|
| 147 | // on initial display, we get a notification inside -[NSWindow setFrameAutosaveName]!
|
---|
| 148 | if (autosaveName != nil) {
|
---|
| 149 | [[self window] saveFrameUsingName: autosaveName];
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[26] | 153 | - (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame;
|
---|
| 154 | {
|
---|
[102] | 155 | NSWindow *window = [alarmList window];
|
---|
[26] | 156 | NSRect frame = [window frame];
|
---|
[102] | 157 | NSScrollView *scrollView = [alarmList enclosingScrollView];
|
---|
[26] | 158 | float displayedHeight = [[scrollView contentView] bounds].size.height;
|
---|
| 159 | float heightChange = [[scrollView documentView] bounds].size.height - displayedHeight;
|
---|
| 160 | float heightExcess;
|
---|
| 161 |
|
---|
| 162 | if (heightChange >= 0 && heightChange <= 1) {
|
---|
| 163 | // either the window is already optimal size, or it's too big
|
---|
[102] | 164 | float rowHeight = [alarmList cellHeight];
|
---|
| 165 | heightChange = (rowHeight * [alarmList numberOfRows]) - displayedHeight;
|
---|
[26] | 166 | }
|
---|
| 167 |
|
---|
| 168 | frame.size.height += heightChange;
|
---|
| 169 |
|
---|
| 170 | if ( (heightExcess = [window minSize].height - frame.size.height) > 1 ||
|
---|
| 171 | (heightExcess = [window maxSize].height - frame.size.height) < 1) {
|
---|
| 172 | heightChange += heightExcess;
|
---|
| 173 | frame.size.height += heightExcess;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | frame.origin.y -= heightChange;
|
---|
| 177 |
|
---|
| 178 | return frame;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[34] | 181 | @end |
---|