source: releases/Pester/1.1b4/Source/PSSnoozeUntilController.m@ 574

Last change on this file since 574 was 102, checked in by Nicholas Riley, 22 years ago

Pester 1.1b3

File size: 5.2 KB
Line 
1//
2// PSSnoozeUntilController.m
3// Pester
4//
5// Created by Nicholas Riley on Sun Feb 16 2003.
6// Copyright (c) 2003 Nicholas Riley. All rights reserved.
7//
8
9#import "PSAlarmNotifierController.h"
10#import "PSCalendarController.h"
11#import "PSSnoozeUntilController.h"
12#import "PSTimeDateEditor.h"
13#import "NSCalendarDate-NJRExtensions.h"
14
15@implementation PSSnoozeUntilController
16
17+ (PSSnoozeUntilController *)snoozeUntilControllerWithNotifierController:(PSAlarmNotifierController *)aController;
18{
19 return [[self alloc] initWithNotifierController: aController];
20}
21
22- (id)initWithNotifierController:(PSAlarmNotifierController *)aController;
23{
24 if ([self initWithWindowNibName: @"Snooze until"]) {
25 NSWindow *window = [self window];
26 alarm = [[PSAlarm alloc] init];
27 snoozeInterval = [aController snoozeInterval];
28 [alarm setInterval: snoozeInterval];
29 [PSTimeDateEditor setUpTimeField: timeOfDay dateField: timeDate completions: timeDateCompletions];
30 if ([alarm isValid]) {
31 [timeOfDay setObjectValue: [alarm time]];
32 [timeDate setObjectValue: [alarm date]];
33 }
34 [self update: self];
35
36 [NSApp beginSheet: window modalForWindow: [aController window] modalDelegate: self didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: aController];
37
38 // work around Cocoa confusion with our bizarre key loop
39 [timeOfDay setNextKeyView: timeDate];
40 }
41 return self;
42}
43
44- (void)dealloc;
45{
46 [alarm release];
47 [super dealloc];
48}
49
50- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(PSAlarmNotifierController *)aController;
51{
52 if (returnCode == NSRunAbortedResponse) {
53 [aController setSnoozeInterval: snoozeInterval];
54 [sheet close];
55 } else {
56 [aController snoozeUntilDate: [alarm date]];
57 }
58 // XXX according to Cocoa documentation, the sheet should hide after the didEnd method returns, but it doesn't.
59}
60
61// XXX yuck, should not be duplicating this method between PSAlarmSetController and PSSnoozeUntilController
62// XXX with -[NSControl currentEditor] don't need to compare? Also check -[NSControl validateEditing]
63- (id)objectValueForTextField:(NSTextField *)field whileEditing:(id)sender;
64{
65 if (sender == field) {
66 NSString *stringValue = [[[self window] fieldEditor: NO forObject: field] string];
67 id obj = nil;
68 [[field formatter] getObjectValue: &obj forString: stringValue errorDescription: NULL];
69 // NSLog(@"from field editor: %@", obj);
70 return obj;
71 } else {
72 // NSLog(@"from field: %@", [field objectValue]);
73 return [field objectValue];
74 }
75}
76
77#pragma mark date setting
78
79- (void)setAlarmDateAndInterval:(id)sender;
80{
81 [alarm setForDate: [self objectValueForTextField: timeDate whileEditing: sender]
82 atTime: [self objectValueForTextField: timeOfDay whileEditing: sender]];
83}
84
85// Be careful not to hook up any of the text fields' actions to update: because we handle them in controlTextDidChange: instead. If we could get the active text field somehow via public API (guess we could use controlTextDidBegin/controlTextDidEndEditing) then we'd not need to overload the update sender for this purpose. Or, I guess, we could use another method other than update. It should not be this hard to implement what is essentially standard behavior. Sigh.
86// Note: finding out whether a given control is editing is easier. See: <http://cocoa.mamasam.com/COCOADEV/2002/03/2/28501.php>.
87
88- (IBAction)update:(id)sender;
89{
90 BOOL isValid;
91 [self setAlarmDateAndInterval: sender];
92 isValid = [alarm isValid];
93 [snoozeButton setEnabled: isValid];
94 if (!isValid) {
95 [messageField setStringValue: [alarm invalidMessage]];
96 } else {
97 [messageField setStringValue: @""];
98 }
99}
100
101- (IBAction)dateCompleted:(NSPopUpButton *)sender;
102{
103 [timeDate setStringValue: [sender titleOfSelectedItem]];
104 [self update: sender];
105}
106
107#pragma mark calendar
108
109- (IBAction)showCalendar:(NSButton *)sender;
110{
111 [PSCalendarController controllerWithDate: [NSCalendarDate dateForDay: [timeDate objectValue]] delegate: self];
112}
113
114- (void)calendarController:(PSCalendarController *)calendar didSetDate:(NSCalendarDate *)date;
115{
116 [timeDate setObjectValue: date];
117 [self update: self];
118}
119
120- (NSView *)calendarControllerLaunchingView:(PSCalendarController *)controller;
121{
122 return timeCalendarButton;
123}
124
125#pragma mark actions
126
127- (IBAction)close:(id)sender;
128{
129 [NSApp endSheet: [self window] returnCode: NSRunAbortedResponse];
130}
131
132- (IBAction)snooze:(NSButton *)sender;
133{
134 if ([alarm isValid]) {
135 [NSApp endSheet: [self window] returnCode: NSRunStoppedResponse];
136 } else {
137 [messageField setStringValue: [@"Unable to snooze. " stringByAppendingString: [alarm invalidMessage]]];
138 [sender setEnabled: NO];
139 }
140}
141
142@end
143
144@implementation PSSnoozeUntilController (NSControlSubclassNotifications)
145
146// called because we're the delegate
147
148- (void)controlTextDidChange:(NSNotification *)notification;
149{
150 [self update: [notification object]];
151}
152
153@end
154
155@implementation PSSnoozeUntilController (NSWindowNotifications)
156
157- (void)windowWillClose:(NSNotification *)notification;
158{
159 [self autorelease];
160}
161
162@end
Note: See TracBrowser for help on using the repository browser.