]> err.no Git - sope/blob - sope-appserver/WOExtensions/WOTabPanel.m
minor improvement to WOHttpAdaptor, bumped framework revisions
[sope] / sope-appserver / WOExtensions / WOTabPanel.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 WOTabPanel : WODynamicElement
25 {
26   WOAssociation *tabs;
27   WOAssociation *selectedTab;
28   WOAssociation *tabNameKey;
29   WOAssociation *nonSelectedBgColor;
30   WOAssociation *bgcolor;
31   WOAssociation *textColor;
32   WOAssociation *submitActionName;
33   
34   WOElement *template;
35 }
36 @end
37
38 #include "common.h"
39
40 @interface WOContext(NGPrivates)
41 - (void)addActiveFormElement:(WOElement *)_element;
42 @end
43
44 @implementation WOTabPanel
45
46 - (id)initWithName:(NSString *)_name
47   associations:(NSDictionary *)_config
48   template:(WOElement *)_c
49 {
50   if ((self = [super initWithName:_name associations:_config template:_c])) {
51     self->tabs               = WOExtGetProperty(_config, @"tabs");
52     self->selectedTab        = WOExtGetProperty(_config, @"selectedTab");
53     self->tabNameKey         = WOExtGetProperty(_config, @"tabNameKey");
54     
55     self->nonSelectedBgColor = 
56       WOExtGetProperty(_config, @"nonSelectedBgColor");
57     self->bgcolor            = WOExtGetProperty(_config, @"bgcolor");
58     self->textColor          = WOExtGetProperty(_config, @"textColor");
59     
60     self->submitActionName   = WOExtGetProperty(_config, @"submitActionName");
61
62     self->template = [_c retain];
63   }
64   return self;
65 }
66
67 - (void)dealloc {
68   [self->submitActionName release];
69   [self->textColor   release];
70   [self->bgcolor     release];
71   [self->nonSelectedBgColor release];
72   [self->tabNameKey  release];
73   [self->selectedTab release];
74   [self->tabs        release];
75   [self->template    release];
76   [super dealloc];
77 }
78
79 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
80   /* check whether a (form) tab was clicked */
81   [_ctx appendElementIDComponent:@"tab"];
82   {
83     WOComponent *sComponent;
84     NSArray *ttabs;
85     unsigned i;
86     
87     sComponent = [_ctx component];
88     ttabs = [self->tabs valueInComponent:sComponent];
89     
90     [_ctx appendZeroElementIDComponent];
91     
92     for (i = 0; i < [ttabs count]; i++) {
93       if ([_req formValueForKey:[_ctx elementID]]) {
94         /* found active tab */
95         [self->selectedTab setValue:[ttabs objectAtIndex:i]
96                            inComponent:sComponent];
97         [_ctx addActiveFormElement:self];
98         break;
99       }
100       [_ctx incrementLastElementIDComponent];
101     }
102     
103     [_ctx deleteLastElementIDComponent];
104   }
105   [_ctx deleteLastElementIDComponent];
106
107   /* let content take values */
108   [_ctx appendElementIDComponent:@"content"];
109   [self->template takeValuesFromRequest:_req inContext:_ctx];
110   [_ctx deleteLastElementIDComponent];
111 }
112
113 - (id)invokeActionForRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
114   id result;
115   NSString *section;
116
117   section = [_ctx currentElementID];
118   if ([section isEqualToString:@"tab"]) {
119     WOComponent *sComponent;
120     NSArray *ttabs;
121     int idx;
122     
123     [_ctx consumeElementID]; // consume 'tab'
124     
125     sComponent = [_ctx component];
126     ttabs = [self->tabs valueInComponent:sComponent];
127
128     idx = [[_ctx currentElementID] intValue];
129     [_ctx consumeElementID]; // consume index
130
131     if (idx >= (int)[ttabs count]) {
132       /* index out of range */
133       idx = 0;
134     }
135     
136     [self->selectedTab setValue:[ttabs objectAtIndex:idx]
137                        inComponent:sComponent];
138     
139     result = [_ctx page];
140   }
141   else if ([section isEqualToString:@"content"]) { 
142     [_ctx consumeElementID]; // consume 'content'
143     
144     [_ctx appendElementIDComponent:@"content"];
145     result = [self->template invokeActionForRequest:_req inContext:_ctx];
146     [_ctx deleteLastElementIDComponent];
147   }
148   else {
149     NSLog(@"%s: missing section id !", __PRETTY_FUNCTION__);
150     result = [_ctx page];
151   }
152   return result;
153 }
154
155 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
156   WOComponent *sComponent;
157   NSArray     *ttabs;
158   BOOL        isInForm;
159   unsigned    i, selIdx;
160   NSString    *selColor, *unselColor, *s;
161   
162   sComponent = [_ctx component];
163   ttabs = [self->tabs valueInComponent:sComponent];
164
165   selColor = self->bgcolor
166     ? [self->bgcolor stringValueInComponent:sComponent]
167     : @"#CCCCCC";
168   
169   unselColor = self->nonSelectedBgColor
170     ? [self->nonSelectedBgColor stringValueInComponent:sComponent]
171     : @"#AAAAAA";
172   
173   if ([ttabs count] < 1) {
174     /* no tabs configured .. */
175     [self->template appendToResponse:_response inContext:_ctx];
176     return;
177   }
178   
179   [_response appendContentString:@"<table border='0'>"];
180   [_response appendContentString:@"<tr>"];
181
182   isInForm = [_ctx isInForm];
183   
184   /* cannot use -indexOfObjectIdenticalTo:, since this doesn't work
185      with language bridges (base types crossing a bridge aren't
186      enforced to be identical ... */
187   selIdx = [ttabs indexOfObject:
188                     [self->selectedTab valueInComponent:sComponent]];
189   if (selIdx == NSNotFound)
190     selIdx = 0;
191   
192   [_ctx appendElementIDComponent:@"tab"];
193   [_ctx appendZeroElementIDComponent];
194   
195   for (i = 0; i < [ttabs count]; i++) {
196     id       tab;
197     BOOL     isCurrentSelected;
198     NSString *title;
199     
200     tab = [ttabs objectAtIndex:i];
201     isCurrentSelected = i == selIdx;
202     
203     [self->selectedTab setValue:tab inComponent:sComponent];
204
205     title = (self->tabNameKey)
206       ? [self->tabNameKey stringValueInComponent:sComponent]
207       : [tab stringValue];
208     
209     [_response appendContentString:@"<td"];
210     [_response appendContentString:@" bgcolor=\""];
211     [_response appendContentString:isCurrentSelected ? selColor : unselColor];
212     [_response appendContentString:@"\""];
213     [_response appendContentString:@">"];
214     
215     if (isInForm) {
216       /* gen submit button */
217       [_response appendContentString:@"<input type='submit' name=\""];
218       [_response appendContentHTMLAttributeValue:[_ctx elementID]];
219       [_response appendContentString:@"\" value=\""];
220       [_response appendContentString:title];
221       [_response appendContentString:
222                    (_ctx->wcFlags.xmlStyleEmptyElements ? @" />" : @">")];
223     }
224     else {
225       /* gen link */
226       [_response appendContentString:@"<a href=\""];
227       [_response appendContentHTMLAttributeValue:[_ctx componentActionURL]];
228       [_response appendContentString:@"\" title=\""];
229       [_response appendContentHTMLAttributeValue:title];
230       [_response appendContentString:@"\">"];
231       
232       if (self->textColor != nil) {
233         [_response appendContentString:@"<font color=\""];
234         [_response appendContentHTMLAttributeValue:
235                      [self->textColor stringValueInComponent:sComponent]];
236         [_response appendContentString:@"\">"];
237       }
238       
239       [_response appendContentHTMLString:title];
240
241       if (self->textColor != nil)
242         [_response appendContentString:@"</font>"];
243       [_response appendContentString:@"</a>"];
244     }
245     
246     [_response appendContentString:@"</td>"];
247     
248     [_ctx incrementLastElementIDComponent]; /* increment index */
249   }
250   [_ctx deleteLastElementIDComponent]; /* del index */
251   [_ctx deleteLastElementIDComponent]; /* del 'tab' */
252
253   [self->selectedTab setValue:[ttabs objectAtIndex:selIdx]
254                      inComponent:sComponent];
255   
256   [_response appendContentString:@"</tr><tr><td colspan=\""];
257   s = [[NSString alloc] initWithFormat:@"%d",[ttabs count]];
258   [_response appendContentString:s];
259   [s release];
260   [_response appendContentString:@"\" bgcolor=\""];
261   [_response appendContentString:selColor];
262   [_response appendContentString:@"\">"];
263   
264   [_ctx appendElementIDComponent:@"content"];
265   [self->template appendToResponse:_response inContext:_ctx];
266   [_ctx deleteLastElementIDComponent];
267   
268   [_response appendContentString:@"</td></tr></table>"];
269 }
270
271 @end /* WOTabPanel */