[53] | 1 | //
|
---|
| 2 | // NJRIntervalField.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Wed Dec 25 2002.
|
---|
| 6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "NJRIntervalField.h"
|
---|
| 10 |
|
---|
| 11 | // XXX much implementation borrowed from DockCam, then factored; should replace DockCam interval selector with a NJRIntervalField at some point
|
---|
| 12 |
|
---|
| 13 | @implementation NJRIntervalField
|
---|
| 14 |
|
---|
| 15 | - (NSTimeInterval)interval;
|
---|
| 16 | {
|
---|
| 17 | NSText *editor = [self currentEditor];
|
---|
| 18 | id obj = nil;
|
---|
| 19 |
|
---|
| 20 | if (editor != nil) {
|
---|
| 21 | NSString *stringValue = [editor string];
|
---|
| 22 | if (![[self formatter] getObjectValue: &obj forString: stringValue errorDescription: NULL])
|
---|
| 23 | return 0;
|
---|
| 24 | } else {
|
---|
| 25 | obj = self;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | return [obj intValue] * [[intervalUnits selectedItem] tag];
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | - (BOOL)setInterval:(NSTimeInterval)interval;
|
---|
| 32 | {
|
---|
| 33 | // we assume that the tags are in ascending order in the array
|
---|
| 34 | NSEnumerator *e = [[intervalUnits itemArray] reverseObjectEnumerator];
|
---|
| 35 | NSMenuItem *i;
|
---|
| 36 | int multiplierTag;
|
---|
| 37 |
|
---|
| 38 | while ( (i = [e nextObject]) != nil) {
|
---|
| 39 | multiplierTag = [i tag];
|
---|
[102] | 40 | if (multiplierTag <= 0) continue;
|
---|
[53] | 41 | if (((int)interval % multiplierTag) == 0) {
|
---|
| 42 | NSFormatter *formatter = [self formatter];
|
---|
[601] | 43 | int intervalValue = (int)interval / multiplierTag;
|
---|
[53] | 44 | if (formatter != nil) {
|
---|
| 45 | id ignored;
|
---|
| 46 | if (![formatter getObjectValue: &ignored forString: [formatter stringForObjectValue: [NSNumber numberWithInt: intervalValue]] errorDescription: NULL]) return NO;
|
---|
| 47 | }
|
---|
| 48 | [self setIntValue: intervalValue];
|
---|
| 49 | [intervalUnits selectItem: i];
|
---|
| 50 | return YES;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | return NO;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | - (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)range replacementString:(NSString *)string;
|
---|
| 57 | {
|
---|
| 58 | unsigned length = [string length];
|
---|
| 59 | if (length != 0) {
|
---|
| 60 | unichar c;
|
---|
| 61 | int tag = -1;
|
---|
| 62 | c = [string characterAtIndex: length - 1];
|
---|
| 63 | switch (c) {
|
---|
| 64 | case 's': case 'S': tag = 1; break;
|
---|
| 65 | case 'm': case 'M': tag = 60; break;
|
---|
| 66 | case 'h': case 'H': tag = 60 * 60; break;
|
---|
[513] | 67 | case 'd': case 'D': tag = 60 * 60 * 24; break;
|
---|
| 68 | case 'w': case 'W': tag = 60 * 60 * 24 * 7; break;
|
---|
[102] | 69 | case 'u': case 'U': tag = -2; break;
|
---|
[53] | 70 | default: break;
|
---|
| 71 | }
|
---|
[102] | 72 | if (tag != -1) {
|
---|
| 73 | int itemIndex = [intervalUnits indexOfItemWithTag: tag];
|
---|
| 74 | if (itemIndex != -1) {
|
---|
| 75 | [intervalUnits selectItemAtIndex: itemIndex];
|
---|
| 76 | [[intervalUnits menu] performActionForItemAtIndex: itemIndex];
|
---|
| 77 | }
|
---|
| 78 | if (tag < 0) return NO; // don't send update
|
---|
| 79 | }
|
---|
[53] | 80 | }
|
---|
| 81 | return [super textView: textView shouldChangeTextInRange: range replacementString: string];
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | @end
|
---|