]> err.no Git - sope/blob - sope-appserver/WOExtensions/WOThresholdColoredNumber.m
PCH support
[sope] / sope-appserver / WOExtensions / WOThresholdColoredNumber.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/NGObjWeb.h>
23
24 @interface WOThresholdColoredNumber : WODynamicElement
25 {
26   WOAssociation *lowColor;
27   WOAssociation *highColor;
28   WOAssociation *threshold;
29   WOAssociation *value;
30   WOAssociation *numberFormat;
31 }
32 @end
33
34 #import <Foundation/NSNumberFormatter.h>
35 #include "common.h"
36
37 @implementation WOThresholdColoredNumber
38
39 - (id)initWithName:(NSString *)_name
40   associations:(NSDictionary *)_config
41   template:(WOElement *)_temp
42 {
43   if ((self = [super initWithName:_name associations:_config template:_temp])) {
44     self->lowColor     = WOExtGetProperty(_config, @"lowColor");
45     self->highColor    = WOExtGetProperty(_config, @"highColor");
46     self->threshold    = WOExtGetProperty(_config, @"threshold");
47     self->value        = WOExtGetProperty(_config, @"value");
48     self->numberFormat = WOExtGetProperty(_config, @"numberformat");
49   }
50   return self;
51 }
52
53 - (void)dealloc {
54   RELEASE(self->lowColor);
55   RELEASE(self->highColor);
56   RELEASE(self->threshold);
57   RELEASE(self->value);
58   RELEASE(self->numberFormat);
59   
60   [super dealloc];
61 }
62
63
64 // *** responder ***
65
66 - (void)appendToResponse:(WOResponse *)_resp inContext:(WOContext *)_ctx {
67   WOComponent *comp;
68   NSString    *low;     // lowColor     association
69   NSString    *high;    // highColor    association
70   NSNumber    *t;       // threshold    association
71   NSNumber    *v;       // value        association
72   NSString    *nFormat; // numberFormat association
73   NSString    *color;
74   NSString    *result = nil;
75
76   comp    = [_ctx component];
77   low     = [self->lowColor     stringValueInComponent:comp];
78   high    = [self->highColor    stringValueInComponent:comp];
79   t       = [self->threshold    valueInComponent:comp];
80   v       = [self->value        valueInComponent:comp];
81   nFormat = [self->numberFormat stringValueInComponent:comp];
82
83   if (![v isKindOfClass:[NSNumber class]]) {
84 #if DEBUG
85     NSLog(@"WARNING! WOThresholdColoredNumber 'value' is not a NSNumber");
86     result = @"[WARNING! WOThresholdColoredNumber:'value' must be a NSNumber]";
87 #else
88     result = @"";
89 #endif
90   }
91   else if (nFormat) {
92     NSNumberFormatter *formatter;
93
94     formatter = AUTORELEASE([[NSNumberFormatter alloc] init]);
95     [formatter setFormat:nFormat];
96     result = [formatter stringForObjectValue:v];
97   }
98   else
99     result = [v stringValue];
100
101   color = ([v compare:t] == NSOrderedAscending) ? low : high;
102
103   if (color) {
104     [_resp appendContentString:@"<FONT COLOR=\""];
105     [_resp appendContentString:color];
106     [_resp appendContentString:@"\">"];
107   }
108   [_resp appendContentString:result];
109   if (color)
110     [_resp appendContentString:@"</FONT>"];
111 }
112
113 @end /* WOThresholdColoredNumber */