-
Notifications
You must be signed in to change notification settings - Fork 333
/
AppController.j
205 lines (159 loc) · 4.76 KB
/
AppController.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* AppController.j
* CPFormatterTest
*
* Created by aparajita on June 30, 2011.
* Copyright 2011, Victory-Heart Productions All rights reserved.
*/
@import <Foundation/CPObject.j>
@import "DateFormatter.j"
var RecordData = [
{name:"Tom", age:34},
{name:"Dick", age:27},
{name:"Harry", age:50}
];
var randomFromTo = function(from, to)
{
return FLOOR(RAND() * (to - from + 1) + from);
};
@implementation AppController : CPObject
{
CPWindow theWindow;
CPTextField dateField1;
CPTextField dateField2;
CPTextField error1;
CPTextField textField;
CPTextField error2;
CPTextField recordField;
CPPopUpButton recordMenu;
}
- (void)awakeFromCib
{
[theWindow setInitialFirstResponder:dateField1];
[dateField1 setFormatter:[DateFormatter formatterWithDisplayFormat:@"D M jS, Y" editingFormat:@"m/j/Y" emptyIsValid:NO]];
[dateField2 setFormatter:[DateFormatter formatterWithDisplayFormat:@"m-d-Y" editingFormat:@"m-d-Y" emptyIsValid:YES]];
[dateField2 setDelegate:self];
[[CPNotificationCenter defaultCenter] addObserver:self
selector:@selector(resetErrorMessage:)
name:CPTextFieldDidFocusNotification
object:nil];
[textField setFormatter:[TextFormatter new]];
[textField setDelegate:self];
[error1 setStringValue:@""];
[error2 setStringValue:@""];
[recordField setFormatter:[ContactFormatter new]];
[recordField setObjectValue:RecordData[0]];
}
- (void)selectRecord:(id)sender
{
var record = RecordData[[sender selectedIndex]];
[recordField setObjectValue:record];
}
- (void)setDate1:(id)sender
{
[self setDate:dateField1];
}
- (void)setDate2:(id)sender
{
[self setDate:dateField2];
}
- (void)setDate:(CPTextField)field
{
var date = new Date(randomFromTo(1931, 2012), randomFromTo(0, 11), randomFromTo(1, 31));
[field setObjectValue:date];
}
- (void)objectValue1:(id)sender
{
CPLog.info("Date 1: %s", [[dateField1 objectValue] description]);
}
- (void)objectValue2:(id)sender
{
CPLog.info("Date 2: %s", [[dateField2 objectValue] description]);
}
- (void)setNil:(id)sender
{
[dateField1 setStringValue:nil]; // should log a warning and do nothing
}
- (void)setEmptyOK:(id)sender
{
[[dateField2 formatter] setEmptyIsValid:[sender state] === CPOnState];
}
- (void)controlTextDidChange:(CPNotification)aNotification
{
var field = [aNotification object],
error;
if (field === dateField2)
error = error1;
else if (field === textField)
error = error2;
[error setStringValue:@""];
}
- (BOOL)control:(CPControl)aControl didFailToFormatString:(CPString)aString errorDescription:(CPString)anError
{
CPLog.info("control:didFailToFormatString:%s errorDescription:%s", aString, anError);
if (anError)
{
var error;
if (aControl === dateField2)
error = error1;
else if (aControl === textField)
error = error2;
[error setStringValue:anError];
}
return NO;
}
- (void)resetErrorMessage:(CPNotification)aNotification
{
[error2 setStringValue:@""];
}
@end
@implementation TextFormatter : CPFormatter
- (CPString)stringForObjectValue:(id)anObject
{
var result;
if ([anObject isKindOfClass:[CPString class]])
result = [self errorDescriptionForString:anObject] === nil ? anObject : nil;
else
result = nil;
console.log("stringForObjectValue:%s ==> %s", [anObject description], result);
return result;
}
- (BOOL)getObjectValue:(CPStringRef)anObject forString:(CPString)aString errorDescription:(CPStringRef)anError
{
var error = [self errorDescriptionForString:aString];
if (error)
{
anObject(nil);
if (anError)
anError(error);
}
else
anObject(aString);
result = error === nil;
console.log("getObjectValue:forString:%s ==> %s", aString, result);
return result;
}
- (CPString)errorDescriptionForString:(CPString)aString
{
if (aString.length > 7)
return @"Maximum length is 7 characters.";
else if (!/^[a-zA-Z0-9]*$/.test(aString))
return @"Invalid characters in string.";
else
return nil;
}
@end
@implementation ContactFormatter : CPFormatter
- (CPString)stringForObjectValue:(id)anObject
{
if (anObject && typeof(anObject) === "object" && anObject.hasOwnProperty("name"))
return [CPString stringWithFormat:@"%s (age %d)", anObject.name, anObject.age];
else
return nil;
}
- (BOOL)getObjectValue:(CPStringRef)anObject forString:(CPString)aString errorDescription:(CPStringRef)anError
{
// We don't support reverse conversion
return NO;
}
@end