]> err.no Git - sope/blob - sope-appserver/WOExtensions/WOTable.m
minor improvement to WOHttpAdaptor, bumped framework revisions
[sope] / sope-appserver / WOExtensions / WOTable.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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
9   later version.
10
11   SOPE 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.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include <NGObjWeb/WODynamicElement.h>
23
24 @interface WOTable : WODynamicElement
25 {
26   // WODynamicElement: extraAttributes
27   // WODynamicElement: otherTagString
28 @protected
29   WOAssociation *list;        // array of objects to iterate through
30   WOAssociation *item;        // current item in the array
31   WOAssociation *maxColumns;  // number of columns
32   WOAssociation *index;       // current index
33   WOAssociation *col;         // current column (is updated with each iteration)
34   WOAssociation *row;         // current row    (is updated with each iteration)
35
36   WOAssociation *cellAlign;   // current cell align
37   WOAssociation *cellVAlign;  // current cell valign
38   WOAssociation *rowBackgroundColor;  // current row background color;
39   WOAssociation *cellBackgroundColor; // current cell background color;
40
41   /*
42     table attributes are implemented as extraAttributes:
43      
44          tableBackgroundColor, --> bgColor
45          border,               --> border
46          cellpadding,          --> cellpadding
47          cellspacing,          --> cellspacing
48   */
49   
50   /* non WO attribute */
51   WOAssociation *horizontal;   // order items horizontal (default = NO)
52   WOAssociation *hasOwnTDs;    // don't draw TDs
53
54   WOElement     *template;
55 }
56 @end
57
58 #include "common.h"
59
60 static NSString *_WOTableHeaderString_  = @"_WOTableHeaderString_";
61 static NSString *_WOTableContentString_ = @"_WOTableContentString_";
62 static NSString *_WOTableContextMode_   = @"_WOTableContextMode_";
63
64 @implementation WOTable
65
66 - (id)initWithName:(NSString *)_name
67   associations:(NSDictionary *)_config
68   template:(WOElement *)_c
69 {
70   if ((self = [super initWithName:_name associations:_config template:_c])) {
71     self->list       = WOExtGetProperty(_config, @"list");
72     self->item       = WOExtGetProperty(_config, @"item");
73     self->maxColumns = WOExtGetProperty(_config, @"maxColumns");
74     self->index      = WOExtGetProperty(_config, @"index");
75     self->col        = WOExtGetProperty(_config, @"col");
76     self->row        = WOExtGetProperty(_config, @"row");
77     self->horizontal = WOExtGetProperty(_config, @"horizontal");
78     self->hasOwnTDs  = WOExtGetProperty(_config, @"hasOwnTDs");
79
80     self->cellAlign  = WOExtGetProperty(_config, @"cellAlign");
81     self->cellVAlign = WOExtGetProperty(_config, @"cellVAlign");
82     
83     self->rowBackgroundColor  = 
84       WOExtGetProperty(_config, @"rowBackgroundColor");
85     self->cellBackgroundColor = 
86       WOExtGetProperty(_config, @"cellBackgroundColor");
87
88     self->template = RETAIN(_c);
89   }
90   return self;
91 }
92
93 - (void)dealloc {
94   [self->template release];
95   
96   [self->list       release];
97   [self->item       release];
98   [self->maxColumns release];
99   [self->index      release];
100   [self->col        release];
101   [self->row        release];
102   [self->horizontal release];
103   [self->hasOwnTDs  release];
104
105   [self->cellAlign           release];
106   [self->cellVAlign          release];
107   [self->rowBackgroundColor  release];
108   [self->cellBackgroundColor release];
109   [super dealloc];
110 }
111
112 static inline void _applyIndex(WOTable *self, WOComponent *cmp, unsigned _idx)
113 {
114   NSArray  *array;
115   BOOL     isHor;
116   unsigned r, c, cnt, cols;
117
118   isHor = [self->horizontal boolValueInComponent:cmp];
119   cols  = [self->maxColumns unsignedIntValueInComponent:cmp];
120   array = [self->list valueInComponent:cmp];
121   cnt   = [array count];
122   cols  = (cols) ? cols : 1;
123   r     = (isHor) ? (_idx / cols) + 1 : _idx % ((cnt / cols)+1) + 1;
124   c     = (isHor) ? (_idx % cols) + 1 : _idx / ((cnt / cols)+1) + 1;
125     
126   if ([self->index isValueSettable])
127     [self->index setUnsignedIntValue:_idx inComponent:cmp];
128
129   if ([self->row isValueSettable])
130     [self->row setUnsignedIntValue:r inComponent:cmp];
131
132   if ([self->col isValueSettable])
133     [self->col setUnsignedIntValue:c inComponent:cmp];
134
135   if ([self->item isValueSettable]) {
136     if (_idx < cnt)
137       [self->item setValue:[array objectAtIndex:_idx] inComponent:cmp];
138     else {
139       [cmp logWithFormat:@"WOTable: array did change, index is invalid."];
140       [self->item setValue:nil inComponent:cmp];
141     }
142   }
143 }
144
145
146 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
147   WOComponent *cmp;
148   NSArray     *array;
149   unsigned    cnt, i;
150
151   cmp   = [_ctx component];
152   array = [self->list valueInComponent:cmp];
153   cnt   = [array count];
154
155   [_ctx appendZeroElementIDComponent];
156
157   [_ctx setObject:_WOTableContentString_ forKey:_WOTableContextMode_];
158
159   for (i=0; i<cnt; i++) {
160     _applyIndex(self, cmp, i);
161     [self->template takeValuesFromRequest:_req inContext:_ctx];
162     [_ctx incrementLastElementIDComponent];
163   }
164
165   [_ctx removeObjectForKey:_WOTableContextMode_];
166   [_ctx deleteLastElementIDComponent];
167 }
168
169 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
170   WOComponent *cmp;
171   id          result = nil;
172
173   cmp = [_ctx component];
174   
175   if ([self->list valueInComponent:cmp] != nil) {
176     unsigned idx = [[_ctx currentElementID] intValue];
177     NSString *s;
178     
179     [_ctx consumeElementID]; // consume index
180     s = [[NSString alloc] initWithFormat:@"%i", idx];
181     [_ctx appendElementIDComponent:s];
182     [s release];
183     _applyIndex(self, cmp, idx);
184     result = [self->template invokeActionForRequest:_req inContext:_ctx];
185     [_ctx deleteLastElementIDComponent];
186   }
187   return result;
188 }
189
190 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
191   WOComponent *cmp   = nil;
192   NSArray     *array = nil;
193   BOOL        isHor  = NO; // is horizontal
194   BOOL        drawTD = YES;
195   unsigned    c, colCount; // column index
196   unsigned    r, rowCount; // row    index
197   unsigned    cnt;
198   unsigned    ownTDCount = 0;
199
200   cmp      = [_ctx component];
201   colCount = [self->maxColumns unsignedIntValueInComponent:cmp];
202   array    = [self->list valueInComponent:cmp];
203   cnt      = [array count];
204   isHor    = [self->horizontal boolValueInComponent:cmp];
205   drawTD   = ![self->hasOwnTDs boolValueInComponent:cmp];
206   if (!drawTD)
207     ownTDCount = [self->hasOwnTDs intValueInComponent:cmp];
208   
209   colCount = (colCount < cnt) ? colCount : cnt;
210   colCount = (colCount) ? colCount : 1;
211   rowCount = ((cnt % colCount) > 0) ? (cnt / colCount) + 1 : (cnt / colCount);
212
213   [_response appendContentString:@"<table "];
214   [self appendExtraAttributesToResponse:_response inContext:_ctx];
215   [_response appendContentCharacter:'>'];
216
217   if (!drawTD) {
218     NSString *rowColor = [self->rowBackgroundColor stringValueInComponent:cmp];
219     unsigned i;
220
221     [_ctx setObject:_WOTableHeaderString_ forKey:_WOTableContextMode_];
222     [_response appendContentString:@"<tr"];
223     if (rowColor) {
224       [_response appendContentString:@" bgcolor=\""];
225       [_response appendContentString:rowColor];
226       [_response appendContentCharacter:'"'];
227     }
228     [_response appendContentCharacter:'>'];
229     for (i = 0; i < colCount; i++) {
230       [self->template appendToResponse:_response inContext:_ctx];
231     }
232     [_response appendContentString:@"</tr>"];
233     [_ctx removeObjectForKey:_WOTableContextMode_];
234   }
235
236   if (!drawTD)
237     [_ctx setObject:_WOTableContentString_ forKey:_WOTableContextMode_];
238
239   for (r=0; r<rowCount; r++) {
240     NSString *rowColor = [self->rowBackgroundColor stringValueInComponent:cmp];
241     [_response appendContentString:@"<tr"];
242     if (rowColor) {
243       [_response appendContentString:@" bgcolor=\""];
244       [_response appendContentString:rowColor];
245       [_response appendContentCharacter:'"'];
246     }
247     [_response appendContentCharacter:'>'];
248     
249     for (c=0; c<colCount; c++) {
250       NSString *cColor = [self->cellBackgroundColor stringValueInComponent:cmp];
251       NSString *align  = [self->cellAlign  stringValueInComponent:cmp];
252       NSString *valign = [self->cellVAlign stringValueInComponent:cmp];
253       NSString *width  = nil;
254       unsigned i = (isHor) ? r*colCount+c : c*rowCount+r;
255       
256       if (drawTD) {
257         [_response appendContentString:@"<td"];
258         width = [[NSString alloc] initWithFormat:@"%d%%", (int)(100 / colCount)];
259         if (width) {
260           [_response appendContentString:@" width=\""];
261           [_response appendContentString:width];
262           [_response appendContentCharacter:'"'];
263           [width release];
264         }
265         if (cColor) {
266           [_response appendContentString:@" bgcolor=\""];
267           [_response appendContentString:cColor];
268           [_response appendContentCharacter:'"'];
269         }
270         if (align) {
271           [_response appendContentString:@" align=\""];
272           [_response appendContentString:align];
273           [_response appendContentCharacter:'"'];
274         }
275         if (valign) {
276           [_response appendContentString:@" valign=\""];
277           [_response appendContentString:valign];
278           [_response appendContentCharacter:'"'];
279         }
280         [_response appendContentCharacter:'>'];
281       }
282       if (i < cnt) {
283         NSString *s;
284
285         s = [[NSString alloc] initWithFormat:@"%i", i];
286         [_ctx appendElementIDComponent:s];
287         [s release];
288         _applyIndex(self, cmp, i);
289         [self->template appendToResponse:_response inContext:_ctx];
290         [_ctx deleteLastElementIDComponent];
291       }
292       else if (ownTDCount > 0) {
293         unsigned j;
294         
295         for (j = 0; j < ownTDCount; j++)
296           [_response appendContentString:@"<td>&nbsp;</td>"];
297       }
298       else
299         [_response appendContentString:@"&nbsp;"];
300
301       if (drawTD)
302         [_response appendContentString:@"</td>"];
303     }
304     [_response appendContentString:@"</tr>"];
305   }
306   if (!drawTD)
307     [_ctx removeObjectForKey:_WOTableContextMode_];
308
309   [_response appendContentString:@"</table>"];
310 }
311
312 /* description */
313
314 - (NSString *)associationDescription {
315   NSMutableString *str;
316
317   str = [NSMutableString stringWithCapacity:128];
318   if (self->list)       [str appendFormat:@" list=%@",       self->list];
319   if (self->item)       [str appendFormat:@" item=%@",       self->item];
320   if (self->maxColumns) [str appendFormat:@" maxColumns=%@", self->maxColumns];
321   if (self->index)      [str appendFormat:@" index=%@",      self->index];
322   if (self->col)        [str appendFormat:@" col=%@",        self->col];
323   if (self->row)        [str appendFormat:@" row=%@",        self->row];
324   if (self->horizontal) [str appendFormat:@" horizontal=%@", self->horizontal];
325   if (self->hasOwnTDs)  [str appendFormat:@" hasOwnTDs=%@",  self->hasOwnTDs];
326   if (self->template)   [str appendFormat:@" template=%@",   self->template];
327   return str;
328 }
329
330 @end /* WOTable */
331
332 @interface WOTableContextKey : WODynamicElement
333 {
334   WOElement *template;
335 }
336 @end
337
338 @implementation WOTableContextKey
339
340 - (NSString *)_contextValue {
341   return nil;
342 }
343
344 - (id)initWithName:(NSString *)_name
345   associations:(NSDictionary *)_config
346   template:(WOElement *)_c
347 {
348   if ((self = [super initWithName:_name associations:_config template:_c])) {
349     self->template  = [_c retain];
350   }
351   return self;
352 }
353
354 - (void)dealloc {
355   [self->template release];
356   [super dealloc];
357 }
358
359 - (BOOL)doShow:(WOContext *)_ctx {
360   NSString *key = [_ctx objectForKey:_WOTableContextMode_];
361
362   if (key == nil)
363     return NO;
364
365   return [key isEqualToString:[self _contextValue]];
366 }
367
368 // ******************** responder ********************
369
370 - (void)takeValuesFromRequest:(WORequest *)_request
371   inContext:(WOContext *)_ctx
372 {
373   if (![self doShow:_ctx])
374     return;
375
376   [_ctx appendElementIDComponent:@"1"];
377   [self->template takeValuesFromRequest:_request inContext:_ctx];
378   [_ctx deleteLastElementIDComponent];
379 }
380
381 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
382   NSString *state;
383   id result;
384
385   if ((state = [[_ctx currentElementID] stringValue]) == nil)
386     return nil;
387   
388   [_ctx consumeElementID]; // consume state-id (on or off)
389   
390   if (![state isEqualToString:@"1"])
391     return nil;
392   
393   [_ctx appendElementIDComponent:state];
394   result = [self->template invokeActionForRequest:_request inContext:_ctx];
395   [_ctx deleteLastElementIDComponent];
396   return result;
397 }
398
399 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
400   if (![self doShow:_ctx]) 
401     return;
402
403   [_ctx appendElementIDComponent:@"1"];
404   [self->template appendToResponse:_response inContext:_ctx];
405   [_ctx deleteLastElementIDComponent];
406 }
407
408 @end /* WOTableContextKey */
409
410 @interface WOTableHeader : WOTableContextKey
411 @end /* WOTableHeader */
412
413 @implementation WOTableHeader
414
415 - (NSString *)_contextValue {
416   return _WOTableHeaderString_;
417 }
418
419 @end /* WOTableHeader */
420
421
422 @interface WOTableContent : WOTableContextKey
423 @end /* WOTableContent */
424
425 @implementation WOTableContent
426
427 - (NSString *)_contextValue {
428   return _WOTableContentString_;
429 }
430
431 @end /* WOTableContent */