~ubuntu-branches/ubuntu/edgy/sope/edgy

« back to all changes in this revision

Viewing changes to sope-appserver/WOExtensions/WOTabPanel.m

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Ley
  • Date: 2005-08-19 16:53:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050819165331-hs683wz1osm708pw
Tags: upstream-4.4rc.2
ImportĀ upstreamĀ versionĀ 4.4rc.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    }
 
223
    else {
 
224
      /* gen link */
 
225
      [_response appendContentString:@"<a href=\""];
 
226
      [_response appendContentHTMLAttributeValue:[_ctx componentActionURL]];
 
227
      [_response appendContentString:@"\" title=\""];
 
228
      [_response appendContentHTMLAttributeValue:title];
 
229
      [_response appendContentString:@"\">"];
 
230
      
 
231
      if (self->textColor) {
 
232
        [_response appendContentString:@"<font color=\""];
 
233
        [_response appendContentHTMLAttributeValue:
 
234
                     [self->textColor stringValueInComponent:sComponent]];
 
235
        [_response appendContentString:@"\">"];
 
236
      }
 
237
      
 
238
      [_response appendContentHTMLString:title];
 
239
 
 
240
      if (self->textColor)
 
241
        [_response appendContentString:@"</font>"];
 
242
      [_response appendContentString:@"</a>"];
 
243
    }
 
244
    
 
245
    [_response appendContentString:@"</td>"];
 
246
    
 
247
    [_ctx incrementLastElementIDComponent]; /* increment index */
 
248
  }
 
249
  [_ctx deleteLastElementIDComponent]; /* del index */
 
250
  [_ctx deleteLastElementIDComponent]; /* del 'tab' */
 
251
 
 
252
  [self->selectedTab setValue:[ttabs objectAtIndex:selIdx]
 
253
                     inComponent:sComponent];
 
254
  
 
255
  [_response appendContentString:@"</tr><tr><td colspan=\""];
 
256
  s = [[NSString alloc] initWithFormat:@"%d",[ttabs count]];
 
257
  [_response appendContentString:s];
 
258
  [s release];
 
259
  [_response appendContentString:@"\" bgcolor=\""];
 
260
  [_response appendContentString:selColor];
 
261
  [_response appendContentString:@"\">"];
 
262
  
 
263
  [_ctx appendElementIDComponent:@"content"];
 
264
  [self->template appendToResponse:_response inContext:_ctx];
 
265
  [_ctx deleteLastElementIDComponent];
 
266
  
 
267
  [_response appendContentString:@"</td></tr></table>"];
 
268
}
 
269
 
 
270
@end /* WOTabPanel */