2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #import <Foundation/NSNumberFormatter.h>
26 #import <Foundation/NSDateFormatter.h>
28 @interface WOText : WOInput
32 // inherited: disabled
37 WOAssociation *numberformat; // string
38 WOAssociation *dateformat; // string
39 WOAssociation *formatter;
44 @interface NSObject(UsedKeyPath)
45 - (NSString *)keyPath;
48 @implementation WOText
50 - (id)initWithName:(NSString *)_name
51 associations:(NSDictionary *)_config
52 template:(WOElement *)_root {
54 if ((self = [super initWithName:_name associations:_config template:_root])) {
55 self->rows = OWGetProperty(_config, @"rows");
56 self->cols = OWGetProperty(_config, @"cols");
57 self->formatter = OWGetProperty(_config, @"formatter");
58 self->numberformat = OWGetProperty(_config, @"numberformat");
59 self->dateformat = OWGetProperty(_config, @"dateformat");
61 if (self->formatter == nil) {
62 if ([_config objectForKey:@"formatterClass"]) {
65 className = OWGetProperty(_config, @"formatterClass");
66 className = [className autorelease];
68 className = [className valueInComponent:nil];
69 className = NSClassFromString(className);
70 className = [[className alloc] init];
72 self->formatter = [WOAssociation associationWithValue:className];
73 self->formatter = [self->formatter retain];
81 if (self->formatter) num++;
82 if (self->numberformat) num++;
83 if (self->dateformat) num++;
85 NSLog(@"WARNING: more than one formats specified in element %@", self);
92 [self->numberformat release];
93 [self->dateformat release];
94 [self->formatter release];
102 static inline NSFormatter *_getFormatter(WOText *self, WOContext *_ctx) {
103 NSFormatter *fmt = nil;
105 if (self->numberformat) {
106 fmt = [[[NSNumberFormatter alloc] init] autorelease];
107 [(NSNumberFormatter *)fmt setFormat:
108 [self->numberformat valueInComponent:[_ctx component]]];
110 else if (self->dateformat) {
111 fmt = [[NSDateFormatter alloc]
113 [self->dateformat valueInComponent:
115 allowNaturalLanguage:NO];
116 fmt = [fmt autorelease];
118 else if (self->formatter) {
119 fmt = [self->formatter valueInComponent:[_ctx component]];
125 // ******************** responder ********************
127 - (id)parseFormValue:(id)_value inContext:(WOContext *)_ctx {
128 NSFormatter *fmt = nil;
130 fmt = _getFormatter(self, _ctx);
133 NSString *errorText = nil;
136 //fmt = [self->formatter valueInComponent:[_ctx component]];
138 if ([fmt getObjectValue:&object forString:[_value stringValue]
139 errorDescription:&errorText]) {
144 NSException *formatException = nil;
145 NSString *keyPath = nil;
147 if ([self->value respondsToSelector:@selector(keyPath)])
148 keyPath = [(id)self->value keyPath];
150 formatException = [NSException exceptionWithName:@"WOValidationException"
154 [[_ctx component] validationFailedWithException:formatException
161 return [super parseFormValue:_value inContext:_ctx];
164 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
165 WOComponent *sComponent;
171 sComponent = [_ctx component];
172 v = [self->value valueInComponent:sComponent];
173 r = [self->rows unsignedIntValueInComponent:sComponent];
174 c = [self->cols unsignedIntValueInComponent:sComponent];
176 fmt = _getFormatter(self, _ctx);
178 NSString *formattedObj = nil;
180 formattedObj = [fmt editingStringForObjectValue:v];
186 WOResponse_AddCString(_response, "<textarea name=\"");
187 [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
188 WOResponse_AddChar(_response, '"');
190 WOResponse_AddCString(_response, " rows=\"");
191 WOResponse_AddUInt(_response, r);
192 WOResponse_AddChar(_response, '"');
195 WOResponse_AddCString(_response, " cols=\"");
196 WOResponse_AddUInt(_response, c);
197 WOResponse_AddChar(_response, '"');
199 [self appendExtraAttributesToResponse:_response inContext:_ctx];
200 if (self->otherTagString) {
201 WOResponse_AddString(_response,
202 [self->otherTagString stringValueInComponent:
205 WOResponse_AddChar(_response, '>');
207 if ([v length] > 0) {
211 ua = [[_ctx request] headerForKey:@"user-agent"];
213 if ([ua rangeOfString:@"Opera"].length > 0)
217 v = [v stringByReplacingString:@"\r" withString:@""];
219 [_response appendContentHTMLString:v];
222 WOResponse_AddCString(_response, "</textarea>");
227 - (NSString *)associationDescription {
228 NSMutableString *str = nil;
230 str = [NSMutableString stringWithCapacity:64];
231 [str appendString:[super associationDescription]];
233 if (self->rows) [str appendFormat:@" rows=%@", self->rows];
234 if (self->cols) [str appendFormat:@" cols=%@", self->cols];
235 if (self->formatter) [str appendFormat:@" formatter=%@", self->formatter];
236 if (self->dateformat) [str appendFormat:@" dateformat=%@", self->dateformat];
237 if (self->numberformat)
238 [str appendFormat:@" numberformat=%@", self->numberformat];