00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "LLBar.h"
00010
00011
00012 @implementation LLBar
00013
00014 static NSBezierPath *line = nil;
00015
00016 - (id) init {
00017 self = [super init];
00018 if (self != nil) {
00019 min = 0.0;
00020 max = 1.0;
00021 value = max;
00022 tempMax = max;
00023 discrete = NO;
00024 line = [[NSBezierPath alloc] init];
00025 name = [[NSString stringWithString:@"not set"] retain];
00026 }
00027 return self;
00028 }
00029
00030 - (NSString *)name {
00031 return name;
00032 }
00033
00034 - (float) min {
00035 return min;
00036 }
00037
00038 - (float) max {
00039 return max;
00040 }
00041
00042 - (float) value {
00043 return value;
00044 }
00045
00046 - (float) tempMax {
00047 return tempMax;
00048 }
00049
00050 - (float) warning {
00051 return warning;
00052 }
00053
00054 - (bool) discrete {
00055 return discrete;
00056 }
00057
00058 - (float) critical {
00059 return critical;
00060 }
00061
00062 - (void) setName:(NSString *) newValue {
00063 [name release];
00064 name = newValue;
00065 [name retain];
00066 }
00067
00068 - (void) setDiscrete:(bool)newValue {
00069 discrete = newValue;
00070 }
00071
00072 - (void) setMin:(float)newValue {
00073 min = newValue;
00074 }
00075
00076 - (void) setMax:(float)newValue {
00077 max = newValue;
00078 tempMax = newValue;
00079 }
00080
00081 - (void) setValue:(float)newValue {
00082 if ((value > max) || (value > tempMax)) {
00083
00084
00085 newValue = max;
00086 }
00087 value = newValue;
00088 }
00089
00090 - (void) setTempMax:(float)newValue {
00091 if (newValue > max) {
00092 NSLog(@"LLBar.setTempMax tempmax <= max");
00093 return;
00094 }
00095 tempMax = newValue;
00096 }
00097
00098 - (void) setWarning:(float)newValue {
00099 if (newValue > max) {
00100 NSLog(@"LLBar.setWarning <= max");
00101 return;
00102 }
00103 warning = newValue;
00104 }
00105
00106 - (void) setCritical:(float)newValue {
00107 if (newValue > max) {
00108 NSLog(@"LLBar.setCritical <= max");
00109 return;
00110 }
00111 critical = newValue;
00112 }
00113
00114 - (void) drawRect:(NSRect)aRect {
00115
00116 NSColor *barColor = [NSColor greenColor];
00117 NSRect barRect = NSInsetRect(aRect, 2, 2);
00118 NSRect disabledBar = barRect;
00119 float maxBarLength = barRect.size.width;
00120
00121 int nrOfDescreteRects = 0;
00122
00123
00124 [[NSColor whiteColor] set];
00125 NSRectFill(aRect);
00126
00127
00128 [[NSColor blackColor] set];
00129 [line removeAllPoints];
00130 [line appendBezierPathWithRect:aRect];
00131 [line stroke];
00132
00133
00134 if (min < max) {
00135
00136 barRect.size.width = maxBarLength * value / max;
00137 nrOfDescreteRects = value;
00138 if (warning < critical) {
00139 if (value < min) {
00140 barRect.size.width = 0;
00141 } else {
00142 if (value < warning) {
00143
00144 } else {
00145 if (value < critical) {
00146 barColor = [NSColor orangeColor];
00147 } else {
00148 barColor = [NSColor redColor];
00149 }
00150 }
00151 }
00152 } else {
00153 if (value < min) {
00154 barRect.size.width = 0;
00155 } else {
00156 if (value > warning) {
00157
00158 } else {
00159 if (value > critical) {
00160 barColor = [NSColor orangeColor];
00161 } else {
00162 barColor = [NSColor redColor];
00163 }
00164 }
00165 }
00166 }
00167 } else {
00168
00169 }
00170
00171
00172 if (!discrete) {
00173
00174 [barColor set];
00175 NSRectFill(barRect);
00176
00177 [[NSColor blackColor] set];
00178 NSFrameRect(barRect);
00179 } else {
00180
00181
00182 float discreteRectWidth = barRect.size.width;
00183 discreteRectWidth -= LLBAR_SEPERATION_BETWEEN_RECTS;
00184 discreteRectWidth /= nrOfDescreteRects;
00185 discreteRectWidth -= LLBAR_SEPERATION_BETWEEN_RECTS;
00186 barRect.size.width = discreteRectWidth;
00187
00188 barRect.origin.x += LLBAR_SEPERATION_BETWEEN_RECTS;
00189 for (int i = 0; i < nrOfDescreteRects; i++) {
00190
00191 [barColor set];
00192 NSRectFill(barRect);
00193
00194 [[NSColor blackColor] set];
00195 NSFrameRect(barRect);
00196
00197 barRect.origin.x += (discreteRectWidth + LLBAR_SEPERATION_BETWEEN_RECTS);
00198 }
00199 }
00200
00201
00202 if (tempMax < max) {
00203
00204
00205 NSPoint end = barRect.origin;
00206 end.x += maxBarLength;
00207
00208
00209 NSPoint start = barRect.origin;
00210 start.x += maxBarLength * tempMax / max;
00211
00212
00213
00214 start.y -= barRect.size.height / 2;
00215 end.y = start.y;
00216
00217
00218 disabledBar.origin.x = start.x;
00219 disabledBar.size.width = end.x - start.x;
00220 [[NSColor grayColor] set];
00221 NSRectFill(disabledBar);
00222
00223
00224
00225 [[NSColor blackColor] set];
00226 [line removeAllPoints];
00227 [line moveToPoint:start];
00228 [line lineToPoint:end];
00229 [line stroke];
00230 }
00231
00232 }
00233
00234 @end