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