]> err.no Git - sope/blob - sope-appserver/WEExtensions/WETreeData.m
fix for SoProductResourceManager.m
[sope] / sope-appserver / WEExtensions / WETreeData.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
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 /*
24   WETreeDate
25
26   Take a look at WETreeView for more information.
27
28   WETreeData associations:
29     isTreeElement
30     icon
31     cornerIcon
32     title
33     string
34
35   Example:
36       TreeDataCell: WETreeData {
37         isTreeElement = YES; // this is a tree cell (that means, it has plus
38                              // and minus icons and all that stuff)
39       }
40       DataCell: WETreeData {
41         isTreeElement = NO;  // this is NOT a  tree cell, i.e. it does NOT
42                              // have any plus or minus icons. (This is just a
43                              // ordinary <td></td>!!!)
44       }
45 */
46
47 #include <NGObjWeb/WODynamicElement.h>
48
49 @interface WETreeData : WODynamicElement
50 {
51   // WODynamicElement: extraAttributes
52   // WODynamicElement: otherTagString
53 @protected
54   WOAssociation *isTreeElement;
55   WOAssociation *title;
56   WOAssociation *string;
57   WOAssociation *icon;
58   WOAssociation *cornerIcon;
59   
60   BOOL          doTable;
61   WOElement     *template;
62 }
63
64 @end
65
66 #include "WETreeContextKeys.h"
67 #include "WETreeMatrixElement.h"
68 #include "common.h"
69
70 @implementation WETreeData
71
72 static Class StrClass = Nil;
73
74 + (void)initialize {
75   StrClass = [NSString class];
76 }
77
78 - (id)initWithName:(NSString *)_name
79   associations:(NSDictionary *)_config
80   template:(WOElement *)_root
81 {
82   if ((self = [super initWithName:_name associations:_config template:_root])){
83     self->isTreeElement = WOExtGetProperty(_config, @"isTreeElement");
84     self->icon          = WOExtGetProperty(_config, @"icon");
85     self->cornerIcon    = WOExtGetProperty(_config, @"cornerIcon");
86     self->title         = WOExtGetProperty(_config, @"title");
87     self->string        = WOExtGetProperty(_config, @"string");
88
89     self->template = [_root retain];
90   }
91   return self;
92 }
93
94 - (void)dealloc {
95   [self->isTreeElement release];
96   [self->icon          release];
97   [self->cornerIcon    release];
98   [self->title         release];
99   [self->string        release];
100   [self->template      release];
101   [super dealloc];
102 }
103
104 /* HTML generation */
105
106 - (void)_appendIcon:(NSString *)_icon alt:(NSString *)_alt
107   toResponse:(WOResponse *)_response inContext:(WOContext *)_ctx
108 {
109   NSString *iconWidth;
110   
111   iconWidth = [_ctx objectForKey:WETreeView_IconWidth];
112   
113   [_response appendContentString:@"<img border=\"0\" src=\""];
114   [_response appendContentHTMLAttributeValue:_icon];
115   [_response appendContentString:@"\""];
116   
117   if ([iconWidth length] > 0) {
118     [_response appendContentString:@" width=\""];
119     [_response appendContentString:iconWidth];
120     [_response appendContentString:@"\""];
121   }
122   if ([_alt length] > 0) {
123     [_response appendContentString:@" alt=\""];
124     [_response appendContentHTMLAttributeValue:_alt];
125     [_response appendContentString:@"\""];
126   }
127   
128   [_response appendContentString:@" />"];
129 }
130
131 - (void)_appendLink:(NSString *)_icon resp:(WOResponse *)_response
132   ctx:(WOContext *)_ctx
133 {
134   BOOL doForm = [_ctx isInForm];
135
136   doForm = NO;
137   
138   if (doForm) {
139     [_response appendContentString:@"<input type=\"image\" border=\"0\""];
140     [_response appendContentString:@" align=\"top\" name=\""];
141     [_response appendContentString:[_ctx elementID]];
142     [_response appendContentString:@"\" src=\""];
143     [_response appendContentString:_icon];
144     [_response appendContentString:@"\" />"];
145   }
146   else {
147     [_ctx appendElementIDComponent:WETreeView_ZOOM_ACTION_ID];
148     [_response appendContentString:@"<a href=\""];
149     [_response appendContentString:[_ctx componentActionURL]];
150     [_response appendContentString:@"\">"];
151     [self _appendIcon:_icon alt:@"z" toResponse:_response inContext:_ctx];
152     [_response appendContentString:@"</a>"];
153     [_ctx deleteLastElementIDComponent];
154   }
155 }
156
157 - (void)_appendTreeElement:(NSString *)_key
158   toResponse:(WOResponse *)_response
159   inContext:(WOContext *)_ctx
160 {
161   BOOL     doLink;
162   NSString *img;
163   
164   doLink = (_key == WETreeView_Plus   || _key == WETreeView_CornerPlus ||
165             _key == WETreeView_Minus  || _key == WETreeView_CornerMinus);
166   
167   img    = [_ctx objectForKey:_key];
168   img    = WEUriOfResource(img, _ctx);
169
170   if (_key == WETreeView_Leaf) {
171     NSString *tmp = [self->icon stringValueInComponent:[_ctx component]];
172     
173     tmp = WEUriOfResource(tmp, _ctx);
174     img = (tmp) ? tmp : img;    
175   }
176   else if (_key == WETreeView_CornerLeaf) {
177     NSString *tmp = [self->cornerIcon stringValueInComponent:[_ctx component]];
178     tmp = WEUriOfResource(tmp, _ctx);
179     img = (tmp) ? tmp : img;
180   }
181   
182   if (img == nil) {
183     if (doLink) {
184       [_ctx appendElementIDComponent:WETreeView_ZOOM_ACTION_ID];
185       [_response appendContentString:@"<a href=\""];
186       [_response appendContentString:[_ctx componentActionURL]];
187       [_response appendContentString:@"\">"];
188
189       if (_key == WETreeView_Plus || _key == WETreeView_CornerPlus)
190         [_response appendContentString:@"<tt>[+]</tt>"];
191       else if (_key == WETreeView_Minus || _key == WETreeView_CornerMinus)
192         [_response appendContentString:@"<tt>[-]</tt>"];
193     
194       [_response appendContentString:@"</a>"];
195       [_ctx deleteLastElementIDComponent];
196     }
197     else if (_key == WETreeView_Leaf)
198       [_response appendContentString:@"<tt>--&nbsp;</tt>"];
199     else if (_key == WETreeView_CornerLeaf)
200       [_response appendContentString:@"<tt>-|&nbsp;</tt>"];
201     else if (_key == WETreeView_Line)
202       [_response appendContentString:@"<tt>&nbsp;|&nbsp;</tt>"];
203     else if (_key == WETreeView_CornerLeaf)
204       [_response appendContentString:@"<tt>&nbsp;--</tt>"];
205     else if (_key == WETreeView_Junction || _key == WETreeView_Corner)
206       [_response appendContentString:@"<tt>&nbsp;|-</tt>"];
207     else if (_key == WETreeView_Space)
208       [_response appendContentString:@"<tt>&nbsp;&nbsp;&nbsp;</tt>"];
209   }
210   else {
211     if (doLink)
212       [self _appendLink:img resp:_response ctx:_ctx];
213     else
214       [self _appendIcon:img alt:@"z" toResponse:_response inContext:_ctx];
215   }
216 }
217
218 - (void)appendHeader:(WOResponse *)_response inContext:(WOContext *)_ctx {
219   WOComponent *cmp   = [_ctx component];
220   NSString    *tmp   = [self->title stringValueInComponent:cmp];
221   BOOL        isTree = [self->isTreeElement boolValueInComponent:cmp];;
222
223   if (tmp == nil)
224     return;
225   
226   if (self->doTable) {
227     [_response appendContentString:@"<td"];
228     [self appendExtraAttributesToResponse:_response inContext:_ctx];
229     if (self->otherTagString) {
230         [_response appendContentString:
231                    [self->otherTagString stringValueInComponent:
232                         [_ctx component]]];
233     }
234     if (isTree) {
235         [_response appendContentString:@" colspan=\""];
236         [_response appendContentString:
237                    [[_ctx objectForKey:WETreeView_HEADER_MODE] stringValue]];
238         [_response appendContentString:@"\"><nobr>"];
239     }
240     else
241       [_response appendContentString:@"><nobr>"];
242   }
243   
244   [_response appendContentString:@"<b>"];
245   [_response appendContentHTMLString:tmp];
246   [_response appendContentString:@"</b>"];
247   
248   if (doTable)
249     [_response appendContentString:@"</nobr></td>"];
250 }
251
252 - (void)appendData:(WOResponse *)_response inContext:(WOContext *)_ctx {
253   /* TODO: split up this method */
254   _WETreeMatrixElement *treeElement;
255   WOComponent *cmp       = nil;
256   BOOL        isTree     = NO;
257   NSString    *content   = nil;
258   int         i;
259
260   cmp         = [_ctx component];
261   isTree      = [self->isTreeElement boolValueInComponent:cmp];
262   content     = [self->string stringValueInComponent:cmp];
263   treeElement = [_ctx objectForKey:WETreeView_TreeElement];
264
265   if (isTree) {
266     for (i = 0; i < [treeElement depth]+1; i++) {
267       NSString *iconWidth;
268       NSString *treeElm;
269
270       iconWidth = [_ctx objectForKey:WETreeView_IconWidth];
271       
272       treeElm = (i < [treeElement depth])
273         ? [treeElement elementAtIndex:i]
274         : [treeElement leaf];
275
276       if (self->doTable) {
277         [_response appendContentString:@"<td"];
278         [self appendExtraAttributesToResponse:_response inContext:_ctx];
279         [_response appendContentString:@" valign=\"middle\" align=\"center\""];
280         
281         if (iconWidth) {
282           NSString *s;
283           s = [[StrClass alloc] initWithFormat:@" width=\"%@\"", iconWidth];
284           [_response appendContentString:s];
285           [s release];
286         }
287         
288         if (treeElm == WETreeView_Plus     || treeElm == WETreeView_Minus ||
289             treeElm == WETreeView_Junction || treeElm == WETreeView_Line) {
290           NSString *img;
291           
292           img = [_ctx objectForKey:WETreeView_Line];
293           img = WEUriOfResource(img, _ctx);
294           if (img) {
295             [_response appendContentString:@" background=\""];
296             [_response appendContentString:img];
297             [_response appendContentCharacter:'"'];
298           }
299         }
300         [_response appendContentString:@"><nobr>"];
301       }
302         
303       [self _appendTreeElement:treeElm toResponse:_response inContext:_ctx];
304       if (self->doTable)
305         [_response appendContentString:@"</nobr></td>"];
306     }
307
308     if (self->doTable) {
309       [_response appendContentString:@"<td"];
310       [self appendExtraAttributesToResponse:_response inContext:_ctx];
311       [_response appendContentString:@" width='97%'"];
312       [_response appendContentString:@" valign='middle' align='left'"];
313       [_response appendContentString:@" colspan=\""];
314       [_response appendContentString:[treeElement colspanAsString]];
315       [_response appendContentString:@"\">"];
316     }
317     
318     /* add cell content */
319     [self->template appendToResponse:_response inContext:_ctx];
320     if (content)
321       [_response appendContentHTMLString:content];
322     
323     if (self->doTable)
324       [_response appendContentString:@"</td>"];
325   }
326   else { // ! isTree
327     if (self->doTable) {
328       [_response appendContentString:@"<td"];
329       [self appendExtraAttributesToResponse:_response inContext:_ctx];
330       if (self->otherTagString) {
331         [_response appendContentString:
332                    [self->otherTagString stringValueInComponent:
333                         [_ctx component]]];
334       }
335       [_response appendContentCharacter:'>'];
336     }
337     
338     /* add cell content */
339     [self->template appendToResponse:_response inContext:_ctx];
340     if (content) [_response appendContentHTMLString:content];
341
342     if (self->doTable)
343       [_response appendContentString:@"</td>"];
344   }
345 }
346
347 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
348   [self->template takeValuesFromRequest:_rq inContext:_ctx];
349 }
350
351 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
352   return [self->template invokeActionForRequest:_rq inContext:_ctx];
353 }
354
355 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
356   self->doTable = ([_ctx objectForKey:WETreeView_RenderNoTable] == nil);
357   
358   if ([_ctx objectForKey:WETreeView_HEADER_MODE])
359     [self appendHeader:_response inContext:_ctx];
360   else
361     [self appendData:_response inContext:_ctx];
362 }
363
364 @end /* WETreeData */