]> err.no Git - sope/blob - sope-appserver/WEExtensions/WETableMatrixLabel.m
fixed copyrights for 2005
[sope] / sope-appserver / WEExtensions / WETableMatrixLabel.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 WETableMatrixLabel : WODynamicElement
25 {
26   WOAssociation *position;    /* top, bottom, left, right, topleft */
27   WOAssociation *elementName;
28   WOAssociation *span;
29   WOAssociation *string;
30   
31   WOElement     *template;
32 }
33 @end
34
35 #include <NGObjWeb/NGObjWeb.h>
36 #include "common.h"
37
38 @implementation WETableMatrixLabel
39
40 static Class    StrClass  = Nil;
41
42 + (void)initialize {
43   static BOOL didInit = NO;
44   if (didInit) return;
45   didInit = YES;
46   
47   StrClass = [NSString class];
48 }
49
50 static NSString *retStrForInt(int i) {
51   switch(i) {
52   case 0:  return @"0";
53   case 1:  return @"1";
54   case 2:  return @"2";
55   case 3:  return @"3";
56   case 4:  return @"4";
57   case 5:  return @"5";
58   case 6:  return @"6";
59   case 7:  return @"7";
60   case 8:  return @"8";
61   case 9:  return @"9";
62   case 10: return @"10";
63     // TODO: find useful count!
64   default:
65     return [[StrClass alloc] initWithFormat:@"%i", i];
66   }
67 }
68
69 - (id)initWithName:(NSString *)_name
70   associations:(NSDictionary *)_config
71   template:(WOElement *)_subs
72 {
73   if ((self = [super initWithName:_name associations:_config template:_subs])) {
74     self->position = WOExtGetProperty(_config, @"position");
75     if (self->position == nil)
76       self->position = [[WOAssociation associationWithValue:@"top"] retain];
77
78     self->elementName = WOExtGetProperty(_config, @"elementName");
79     self->span        = WOExtGetProperty(_config, @"span");
80     self->string      = WOExtGetProperty(_config, @"string");
81     
82     self->template = [_subs retain];
83   }
84   return self;
85 }
86
87 - (void)dealloc {
88   [self->string      release];
89   [self->span        release];
90   [self->elementName release];
91   [self->position    release];
92   [self->template    release];
93   [super dealloc];
94 }
95
96 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
97   NSString *pos;
98   id       tmp;
99   NSString *tag;
100   int      ispan;
101   
102   pos = [self->position stringValueInComponent:[_ctx component]];
103   
104   if ((tmp = [_ctx valueForKey:@"WETableMatrix_Query"])) {
105     [tmp addObject:pos];
106     return;
107   }
108
109   if ((tmp = [_ctx valueForKey:@"WETableMatrix_Mode"]) == nil)
110     return;
111
112   if (![tmp isEqualToString:pos])
113     return;
114
115   /* check span (some kind of condition) */
116   
117   ispan = [self->span intValueInComponent:[_ctx component]];
118   if (ispan < 1) ispan = 1;
119   
120   if (ispan > 1) {
121     int idx;
122
123     idx = [[_ctx objectForKey:@"WETableMatrix_Index"] intValue];
124     if (idx % ispan != 0)
125       /* the label is not active in that column/row */
126       return;
127   }
128   else if (ispan < 1)
129     ispan = 1;
130   
131   tag = [self->elementName stringValueInComponent:[_ctx component]];
132   
133   if (tag) {
134     NSString *s;
135     int rspan, cspan;
136     
137     [_response appendContentString:@"<"];
138     [_response appendContentString:tag];
139     
140     rspan = [[_ctx objectForKey:@"WETableMatrix_RowSpan"] intValue];
141     cspan = [[_ctx objectForKey:@"WETableMatrix_ColSpan"] intValue];
142     
143     if (rspan > 1) {
144       [_response appendContentString:@" rowspan=\""];
145       s = retStrForInt(rspan);
146       [_response appendContentString:s];
147       [s release];
148       [_response appendContentString:@"\""];
149     }
150     if (cspan > 1) {
151       [_response appendContentString:@" colspan=\""];
152       s = retStrForInt(cspan);
153       [_response appendContentString:s];
154       [s release];
155       [_response appendContentString:@"\""];
156     }
157     
158     if (ispan > 1) {
159       if ([tmp isEqualToString:@"bottom"] || [tmp isEqualToString:@"top"]) {
160         [_response appendContentString:@" colspan=\""];
161         NSAssert(cspan <= 1, @"double row-span !!");
162       }
163       else {
164         [_response appendContentString:@" rowspan=\""];
165         NSAssert(rspan <= 1, @"double row-span !!");
166       }
167       s = retStrForInt(ispan);
168       [_response appendContentString:s];
169       [s release];
170       [_response appendContentString:@"\""];
171     }
172
173     if ([tmp isEqualToString:@"top"]) {
174       int    count;
175       double width;
176       char   buf[64];
177       NSString *s;
178
179       count = [[_ctx objectForKey:@"WETableMatrix_Count"] intValue];
180       width = 100.0 / ((double)count / (double)ispan);
181       
182       sprintf(buf, "%.0f", width);
183       s = [[StrClass alloc] initWithCString:buf];
184       
185       [_response appendContentString:@" width=\""];
186       [_response appendContentString:s];
187       [_response appendContentString:@"%\""];
188       
189       [s release];
190     }
191     
192     [self appendExtraAttributesToResponse:_response inContext:_ctx];
193     [_response appendContentString:@">"];
194   }
195   
196   if (self->string) {
197     NSString *s;
198     s = [self->string stringValueInComponent:[_ctx component]];
199     [_response appendContentHTMLString:s];
200   }
201   [self->template appendToResponse:_response inContext:_ctx];
202   
203   if (tag) {
204     [_response appendContentString:@"</"];
205     [_response appendContentString:tag];
206     [_response appendContentString:@">"];
207   }
208 }
209
210 @end /* WETableMatrixLabel */