| 1 |
// |
| 2 |
// XspfMRule.m |
| 3 |
// XspfManager |
| 4 |
// |
| 5 |
// Created by Hori,Masaki on 09/11/29. |
| 6 |
// |
| 7 |
|
| 8 |
/* |
| 9 |
This source code is release under the New BSD License. |
| 10 |
Copyright (c) 2009-2010, masakih |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に |
| 14 |
限り、再頒布および使用が許可されます。 |
| 15 |
|
| 16 |
1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含 |
| 17 |
めること。 |
| 18 |
2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表 |
| 19 |
示、本条件一覧、および下記免責条項を含めること。 |
| 20 |
3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、 |
| 21 |
コントリビューターの名前を使用してはならない。 |
| 22 |
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、 |
| 23 |
明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証 |
| 24 |
も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター |
| 25 |
も、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか |
| 26 |
厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する |
| 27 |
可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用 |
| 28 |
サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定 |
| 29 |
されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい |
| 30 |
て、一切責任を負わないものとします。 |
| 31 |
------------------------------------------------------------------- |
| 32 |
Copyright (c) 2009-2010, masakih |
| 33 |
All rights reserved. |
| 34 |
|
| 35 |
Redistribution and use in source and binary forms, with or without |
| 36 |
modification, are permitted provided that the following conditions |
| 37 |
are met: |
| 38 |
|
| 39 |
1, Redistributions of source code must retain the above copyright |
| 40 |
notice, this list of conditions and the following disclaimer. |
| 41 |
2, Redistributions in binary form must reproduce the above copyright |
| 42 |
notice, this list of conditions and the following disclaimer in |
| 43 |
the documentation and/or other materials provided with the |
| 44 |
distribution. |
| 45 |
3, The names of its contributors may be used to endorse or promote |
| 46 |
products derived from this software without specific prior |
| 47 |
written permission. |
| 48 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 49 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 50 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 51 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 52 |
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 53 |
INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 54 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 55 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 56 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 57 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 58 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 59 |
POSSIBILITY OF SUCH DAMAGE. |
| 60 |
*/ |
| 61 |
|
| 62 |
|
| 63 |
#import "XspfMRule.h" |
| 64 |
#import "XspfMRule_private.h" |
| 65 |
|
| 66 |
#import "HMLabelField.h" |
| 67 |
#import "HMLabelCell.h" |
| 68 |
|
| 69 |
|
| 70 |
@implementation XspfMRule (XspfMAccessor) |
| 71 |
- (void)setChildren:(NSArray *)newChildren |
| 72 |
{ |
| 73 |
if(!newChildren) newChildren = [NSMutableArray array]; |
| 74 |
|
| 75 |
[children autorelease]; |
| 76 |
children = [[NSMutableArray alloc] initWithArray:newChildren copyItems:YES]; |
| 77 |
} |
| 78 |
- (void)addChild:(XspfMRule *)child |
| 79 |
{ |
| 80 |
[children addObject:child]; |
| 81 |
} |
| 82 |
- (void)setPredicateParts:(NSDictionary *)parts |
| 83 |
{ |
| 84 |
[predicateHints autorelease]; |
| 85 |
predicateHints = [parts mutableCopy]; |
| 86 |
} |
| 87 |
- (void)setExpression:(id)expression forKey:(id)key |
| 88 |
{ |
| 89 |
[predicateHints setObject:expression forKey:key]; |
| 90 |
} |
| 91 |
- (void)setValue:(NSString *)newValue |
| 92 |
{ |
| 93 |
if([_value isEqualToString:newValue]) return; |
| 94 |
|
| 95 |
[_value autorelease]; |
| 96 |
_value = [newValue copy]; |
| 97 |
} |
| 98 |
- (NSString *)value { return _value; } |
| 99 |
@end |
| 100 |
|
| 101 |
@implementation XspfMRule |
| 102 |
@dynamic value; |
| 103 |
|
| 104 |
- (NSInteger)numberOfChildren |
| 105 |
{ |
| 106 |
return [children count]; |
| 107 |
} |
| 108 |
- (id)childAtIndex:(NSInteger)index |
| 109 |
{ |
| 110 |
return [children objectAtIndex:index]; |
| 111 |
} |
| 112 |
- (id)displayValueForRuleEditor:(NSRuleEditor *)ruleEditor inRow:(NSInteger)row |
| 113 |
{ |
| 114 |
return _value; |
| 115 |
} |
| 116 |
- (NSDictionary *)predicatePartsWithDisplayValue:(id)displayValue forRuleEditor:(NSRuleEditor *)ruleEditor inRow:(NSInteger)row |
| 117 |
{ |
| 118 |
id result = [NSMutableDictionary dictionary]; |
| 119 |
|
| 120 |
NSRuleEditorRowType rowType = [ruleEditor rowTypeForRow:row]; |
| 121 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 122 |
return predicateHints; |
| 123 |
} |
| 124 |
|
| 125 |
if([predicateHints valueForKey:@"XspfMIgnoreExpression"]) return nil; |
| 126 |
|
| 127 |
id operatorType = [predicateHints valueForKey:NSRuleEditorPredicateOperatorType]; |
| 128 |
id option = [predicateHints valueForKey:NSRuleEditorPredicateOptions]; |
| 129 |
id leftExp = [predicateHints valueForKey:NSRuleEditorPredicateLeftExpression]; |
| 130 |
id rightExp = [predicateHints valueForKey:NSRuleEditorPredicateRightExpression]; |
| 131 |
id customRightExp = [predicateHints valueForKey:@"XspfMPredicateRightExpression"]; |
| 132 |
|
| 133 |
if(operatorType) { |
| 134 |
[result setValue:operatorType forKey:NSRuleEditorPredicateOperatorType]; |
| 135 |
} |
| 136 |
if(option) { |
| 137 |
[result setValue:option forKey:NSRuleEditorPredicateOptions]; |
| 138 |
} |
| 139 |
if(leftExp) { |
| 140 |
id exp = nil; |
| 141 |
if([leftExp isEqual:@"value"]) { |
| 142 |
exp = [NSExpression expressionForKeyPath:displayValue]; |
| 143 |
} else { |
| 144 |
exp = [NSExpression expressionForKeyPath:leftExp]; |
| 145 |
} |
| 146 |
if(exp) { |
| 147 |
[result setValue:exp forKey:NSRuleEditorPredicateLeftExpression]; |
| 148 |
} |
| 149 |
} |
| 150 |
if(rightExp) { |
| 151 |
SEL selector = NSSelectorFromString(rightExp); |
| 152 |
id exp = nil; |
| 153 |
if(selector) { |
| 154 |
exp = [NSExpression expressionForConstantValue:[displayValue performSelector:selector]]; |
| 155 |
} else { |
| 156 |
exp = [NSExpression expressionForConstantValue:rightExp]; |
| 157 |
} |
| 158 |
if(exp) { |
| 159 |
[result setValue:exp forKey:NSRuleEditorPredicateRightExpression]; |
| 160 |
} |
| 161 |
} |
| 162 |
if(customRightExp) { |
| 163 |
SEL selector = NSSelectorFromString(customRightExp); |
| 164 |
id arg01 = [predicateHints valueForKey:@"XspfMRightExpressionArg01"]; |
| 165 |
id arg02 = [predicateHints valueForKey:@"XspfMRightExpressionArg02"]; |
| 166 |
if([arg01 isEqual:@"displayValues"]) { |
| 167 |
arg01 = [ruleEditor displayValuesForRow:row]; |
| 168 |
} |
| 169 |
if([arg02 isEqual:@"displayValues"]) { |
| 170 |
arg02 = [ruleEditor displayValuesForRow:row]; |
| 171 |
} |
| 172 |
|
| 173 |
id rhs = nil; |
| 174 |
if(arg02 && arg01) { |
| 175 |
rhs = [self performSelector:selector withObject:arg01 withObject:arg02]; |
| 176 |
} else if(arg01) { |
| 177 |
rhs = [self performSelector:selector withObject:arg01]; |
| 178 |
} else { |
| 179 |
rhs = [self performSelector:selector]; |
| 180 |
} |
| 181 |
if(rhs) { |
| 182 |
[result setValue:rhs forKey:NSRuleEditorPredicateRightExpression]; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
NSString *selName = [predicateHints valueForKey:@"XspfMCustomSelector"]; |
| 187 |
if(selName) { |
| 188 |
id args = nil; |
| 189 |
NSString *argSelName = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteator"]; |
| 190 |
if(argSelName) { |
| 191 |
SEL argSel = NSSelectorFromString(argSelName); |
| 192 |
id argSelArg01 = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteatorArg01"]; |
| 193 |
if([argSelArg01 isEqual:@"displayValues"]) { |
| 194 |
argSelArg01 = [ruleEditor displayValuesForRow:row]; |
| 195 |
} |
| 196 |
id argSelArg02 = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteatorArg02"]; |
| 197 |
if([argSelArg02 isEqual:@"displayValues"]) { |
| 198 |
argSelArg02 = [ruleEditor displayValuesForRow:row]; |
| 199 |
} |
| 200 |
if(argSelArg02) { |
| 201 |
args = [self performSelector:argSel withObject:argSelArg01 withObject:argSelArg02]; |
| 202 |
} else if(argSelArg01) { |
| 203 |
args = [self performSelector:argSel withObject:argSelArg01]; |
| 204 |
} else { |
| 205 |
args = [self performSelector:argSel]; |
| 206 |
} |
| 207 |
} else { |
| 208 |
id arg01 = [predicateHints valueForKey:@"XspfMCustomSelectorArg01"]; |
| 209 |
args = [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01], nil]; |
| 210 |
} |
| 211 |
|
| 212 |
id target = [NSExpression expressionForConstantValue:[[self class] functionHost]]; |
| 213 |
id rhs = [NSExpression expressionForFunction:target selectorName:selName arguments:args]; |
| 214 |
[result setValue:rhs forKey:NSRuleEditorPredicateRightExpression]; |
| 215 |
} |
| 216 |
|
| 217 |
// HMLog(HMLogLevelDebug, @"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", predicateHints, displayValue, row, result); |
| 218 |
|
| 219 |
return result; |
| 220 |
} |
| 221 |
|
| 222 |
- (id)displayValue { return _value; } |
| 223 |
|
| 224 |
|
| 225 |
#pragma mark#### Variables for add/change criteria of Library. #### |
| 226 |
static NSArray *leftKeys = nil; |
| 227 |
static NSArray *stringKeys = nil; |
| 228 |
static NSArray *dateKeys = nil; |
| 229 |
static NSArray *numberKeys = nil; |
| 230 |
static NSArray *booleanKeys = nil; |
| 231 |
static NSString *ratingKeyPath = @"rating"; |
| 232 |
static NSString *labelKeyPath = @"label"; |
| 233 |
static BOOL useRating = NO; |
| 234 |
static BOOL useLabel = NO; |
| 235 |
|
| 236 |
+ (void)registerStringTypeKeyPaths:(NSArray *)keyPaths |
| 237 |
{ |
| 238 |
if(stringKeys) { |
| 239 |
[stringKeys release]; |
| 240 |
[leftKeys release]; |
| 241 |
leftKeys = nil; |
| 242 |
} |
| 243 |
stringKeys = [[NSArray arrayWithArray:keyPaths] retain]; |
| 244 |
} |
| 245 |
+ (void)registerDateTypeKeyPaths:(NSArray *)keyPaths |
| 246 |
{ |
| 247 |
if(dateKeys) { |
| 248 |
[dateKeys release]; |
| 249 |
[leftKeys release]; |
| 250 |
leftKeys = nil; |
| 251 |
} |
| 252 |
dateKeys = [[NSArray arrayWithArray:keyPaths] retain]; |
| 253 |
} |
| 254 |
+ (void)registerNumberTypeKeyPaths:(NSArray *)keyPaths |
| 255 |
{ |
| 256 |
if(numberKeys) { |
| 257 |
[numberKeys release]; |
| 258 |
[leftKeys release]; |
| 259 |
leftKeys = nil; |
| 260 |
} |
| 261 |
numberKeys = [[NSArray arrayWithArray:keyPaths] retain]; |
| 262 |
} |
| 263 |
+ (void)registerBooleanTypeKeyPaths:(NSArray *)keyPaths |
| 264 |
{ |
| 265 |
if(booleanKeys) { |
| 266 |
[booleanKeys release]; |
| 267 |
[leftKeys release]; |
| 268 |
leftKeys = nil; |
| 269 |
} |
| 270 |
booleanKeys = [[NSArray arrayWithArray:keyPaths] retain]; |
| 271 |
} |
| 272 |
+ (void)setUseRating:(BOOL)flag |
| 273 |
{ |
| 274 |
if((flag && useRating) || (!flag && !useRating)) { |
| 275 |
[leftKeys release]; |
| 276 |
leftKeys = nil; |
| 277 |
} |
| 278 |
useRating = flag; |
| 279 |
} |
| 280 |
+ (void)setRatingKeyPath:(NSString *)keyPath |
| 281 |
{ |
| 282 |
[ratingKeyPath release]; |
| 283 |
ratingKeyPath = [keyPath copy]; |
| 284 |
} |
| 285 |
+ (void)setUseLablel:(BOOL)flag |
| 286 |
{ |
| 287 |
if((flag && useLabel) || (!flag && !useLabel)) { |
| 288 |
[leftKeys release]; |
| 289 |
leftKeys = nil; |
| 290 |
} |
| 291 |
useLabel = flag; |
| 292 |
} |
| 293 |
+ (void)setLabelKeyPath:(NSString *)keyPath |
| 294 |
{ |
| 295 |
[labelKeyPath release]; |
| 296 |
labelKeyPath = [keyPath copy]; |
| 297 |
} |
| 298 |
|
| 299 |
+ (NSArray *)leftKeys |
| 300 |
{ |
| 301 |
if(!leftKeys) { |
| 302 |
|
| 303 |
id temp = [NSMutableArray array]; |
| 304 |
[temp addObjectsFromArray:stringKeys]; |
| 305 |
[temp addObjectsFromArray:dateKeys]; |
| 306 |
[temp addObjectsFromArray:numberKeys]; |
| 307 |
[temp addObjectsFromArray:booleanKeys]; |
| 308 |
if(useRating) { |
| 309 |
[temp addObject:ratingKeyPath]; |
| 310 |
} |
| 311 |
if(useLabel) { |
| 312 |
[temp addObject:labelKeyPath]; |
| 313 |
} |
| 314 |
leftKeys = [[NSArray arrayWithArray:temp] retain]; |
| 315 |
} |
| 316 |
return leftKeys; |
| 317 |
} |
| 318 |
static inline BOOL isDateKeyPath(NSString *keyPath) |
| 319 |
{ |
| 320 |
return [dateKeys containsObject:keyPath]; |
| 321 |
} |
| 322 |
+ (BOOL)isDateKeyPath:(NSString *)keyPath |
| 323 |
{ |
| 324 |
return isDateKeyPath(keyPath); |
| 325 |
} |
| 326 |
- (BOOL)isDateKeyPath:(NSString *)keyPath |
| 327 |
{ |
| 328 |
return isDateKeyPath(keyPath); |
| 329 |
} |
| 330 |
static inline BOOL isStringKeyPath(NSString *keyPath) |
| 331 |
{ |
| 332 |
return [stringKeys containsObject:keyPath]; |
| 333 |
} |
| 334 |
+ (BOOL)isStringKeyPath:(NSString *)keyPath |
| 335 |
{ |
| 336 |
return isStringKeyPath(keyPath); |
| 337 |
} |
| 338 |
- (BOOL)isStringKeyPath:(NSString *)keyPath |
| 339 |
{ |
| 340 |
return isStringKeyPath(keyPath); |
| 341 |
} |
| 342 |
static inline BOOL isNumberKeyPath(NSString *keyPath) |
| 343 |
{ |
| 344 |
return [numberKeys containsObject:keyPath]; |
| 345 |
} |
| 346 |
+ (BOOL)isNumberKeyPath:(NSString *)keyPath |
| 347 |
{ |
| 348 |
return isNumberKeyPath(keyPath); |
| 349 |
} |
| 350 |
- (BOOL)isNumberKeyPath:(NSString *)keyPath |
| 351 |
{ |
| 352 |
return isNumberKeyPath(keyPath); |
| 353 |
} |
| 354 |
static inline BOOL isBooleanKeyPath(NSString *keyPath) |
| 355 |
{ |
| 356 |
return [booleanKeys containsObject:keyPath]; |
| 357 |
} |
| 358 |
+ (BOOL)isBooleanKeyPath:(NSString *)keyPath |
| 359 |
{ |
| 360 |
return isBooleanKeyPath(keyPath); |
| 361 |
} |
| 362 |
- (BOOL)isBooleanKeyPath:(NSString *)keyPath |
| 363 |
{ |
| 364 |
return isBooleanKeyPath(keyPath); |
| 365 |
} |
| 366 |
+ (BOOL)isRateKeyPath:(NSString *)keyPath |
| 367 |
{ |
| 368 |
return [keyPath isEqualToString:ratingKeyPath]; |
| 369 |
} |
| 370 |
- (BOOL)isRateKeyPath:(NSString *)keyPath |
| 371 |
{ |
| 372 |
return [keyPath isEqualToString:ratingKeyPath]; |
| 373 |
} |
| 374 |
+ (BOOL)isLabelKeyPath:(NSString *)keyPath |
| 375 |
{ |
| 376 |
return [keyPath isEqualToString:labelKeyPath]; |
| 377 |
} |
| 378 |
- (BOOL)isLabelKeyPath:(NSString *)keyPath |
| 379 |
{ |
| 380 |
return [keyPath isEqualToString:labelKeyPath]; |
| 381 |
} |
| 382 |
|
| 383 |
#pragma mark == NSCopying Protocol == |
| 384 |
- (id)copyWithZone:(NSZone *)zone |
| 385 |
{ |
| 386 |
XspfMRule *result = [[[self class] allocWithZone:zone] init]; |
| 387 |
[result setChildren:children]; |
| 388 |
[result setPredicateParts:predicateHints]; |
| 389 |
[result setValue:_value]; |
| 390 |
|
| 391 |
return result; |
| 392 |
} |
| 393 |
|
| 394 |
#pragma mark == NSCoding Protocol == |
| 395 |
static NSString *const XspfMRuleChildrenKey = @"XspfMRuleChildrenKey"; |
| 396 |
static NSString *const XspfMRulePredicateHintsKey = @"XspfMRulePredicateHintsKey"; |
| 397 |
static NSString *const XspfMRuleValueKey = @"XspfMRuleValueKey"; |
| 398 |
- (id)initWithCoder:(NSCoder *)decoder |
| 399 |
{ |
| 400 |
self = [self init]; |
| 401 |
|
| 402 |
[self setChildren:[decoder decodeObjectForKey:XspfMRuleChildrenKey]]; |
| 403 |
[self setPredicateParts:[decoder decodeObjectForKey:XspfMRulePredicateHintsKey]]; |
| 404 |
[self setValue:[decoder decodeObjectForKey:XspfMRuleValueKey]]; |
| 405 |
|
| 406 |
return self; |
| 407 |
} |
| 408 |
- (void)encodeWithCoder:(NSCoder *)encoder |
| 409 |
{ |
| 410 |
[encoder encodeObject:children forKey:XspfMRuleChildrenKey]; |
| 411 |
[encoder encodeObject:predicateHints forKey:XspfMRulePredicateHintsKey]; |
| 412 |
[encoder encodeObject:_value forKey:XspfMRuleValueKey]; |
| 413 |
} |
| 414 |
|
| 415 |
- (BOOL)isEqual:(id)other |
| 416 |
{ |
| 417 |
if([super isEqual:other]) return YES; |
| 418 |
if(![other isKindOfClass:[XspfMRule class]]) return NO; |
| 419 |
|
| 420 |
XspfMRule *o = other; |
| 421 |
if(![_value isEqualToString:o->_value]) return NO; |
| 422 |
// if(![children isEqual:o->children]) return NO; |
| 423 |
// if(![predicateHints isEqual:o->predicateHints]) return NO; |
| 424 |
|
| 425 |
return YES; |
| 426 |
} |
| 427 |
- (NSUInteger)hash |
| 428 |
{ |
| 429 |
return _value ? [_value hash] : [super hash]; |
| 430 |
} |
| 431 |
|
| 432 |
- (id)description |
| 433 |
{ |
| 434 |
return [NSString stringWithFormat:@"%@ {\n\t%@ = %@;\n\t%@ = %@;\n\t%@ = %@;}", |
| 435 |
NSStringFromClass([self class]), |
| 436 |
@"value", _value, |
| 437 |
@"hints", predicateHints, |
| 438 |
@"children", children, |
| 439 |
nil]; |
| 440 |
} |
| 441 |
@end |
| 442 |
|
| 443 |
@implementation XspfMRule (XspfMCreation) |
| 444 |
|
| 445 |
- (id)init |
| 446 |
{ |
| 447 |
[super init]; |
| 448 |
|
| 449 |
children = [[NSMutableArray array] retain]; |
| 450 |
predicateHints = [[NSMutableDictionary dictionary] retain]; |
| 451 |
|
| 452 |
return self; |
| 453 |
} |
| 454 |
|
| 455 |
- (id)initWithValue:(NSString *)newValue children:(NSArray *)newChildren predicateHints:(NSDictionary *)parts |
| 456 |
{ |
| 457 |
[self init]; |
| 458 |
|
| 459 |
if([newValue isEqualToString:@"separator"]) { |
| 460 |
[self release]; |
| 461 |
return [[XspfMSeparatorRule alloc] initSparetorRule]; |
| 462 |
} |
| 463 |
|
| 464 |
id fieldRule = [XspfMFieldRule fieldRuleWithValue:newValue]; |
| 465 |
if(fieldRule) { |
| 466 |
[self release]; |
| 467 |
self = [fieldRule retain]; |
| 468 |
} |
| 469 |
|
| 470 |
[self setValue:newValue]; |
| 471 |
[self setChildren:newChildren]; |
| 472 |
[self setPredicateParts:parts]; |
| 473 |
|
| 474 |
return self; |
| 475 |
} |
| 476 |
+ (id)ruleWithValue:(NSString *)newValue children:(NSArray *)newChildren predicateHints:(NSDictionary *)parts |
| 477 |
{ |
| 478 |
return [[[self alloc] initWithValue:newValue children:newChildren predicateHints:parts] autorelease]; |
| 479 |
} |
| 480 |
|
| 481 |
+ (NSArray *)compoundRule |
| 482 |
{ |
| 483 |
id comp = [self ruleWithValue:@"of the following are true" children:nil predicateHints:[NSDictionary dictionary]]; |
| 484 |
|
| 485 |
id allExp = [NSNumber numberWithUnsignedInt:NSAndPredicateType]; |
| 486 |
id all = [self ruleWithValue:@"All" |
| 487 |
children:[NSArray arrayWithObject:comp] |
| 488 |
predicateHints:[NSDictionary dictionaryWithObject:allExp forKey:NSRuleEditorPredicateCompoundType]]; |
| 489 |
|
| 490 |
id anyExp = [NSNumber numberWithUnsignedInt:NSOrPredicateType]; |
| 491 |
id any = [self ruleWithValue:@"Any" |
| 492 |
children:[NSArray arrayWithObject:comp] |
| 493 |
predicateHints:[NSDictionary dictionaryWithObject:anyExp forKey:NSRuleEditorPredicateCompoundType]]; |
| 494 |
|
| 495 |
return [NSArray arrayWithObjects:all, any, nil]; |
| 496 |
} |
| 497 |
|
| 498 |
- (NSDictionary *)predicateHintsWithPlist:(NSDictionary *)plist |
| 499 |
{ |
| 500 |
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:plist]; |
| 501 |
[result removeObjectForKey:@"criteria"]; |
| 502 |
[result removeObjectForKey:@"value"]; |
| 503 |
|
| 504 |
return result; |
| 505 |
} |
| 506 |
|
| 507 |
+ (id)ruleWithPlist:(id)plist |
| 508 |
{ |
| 509 |
return [[[self alloc] initWithPlist:plist] autorelease]; |
| 510 |
} |
| 511 |
- (id)initWithPlist:(id)plist |
| 512 |
{ |
| 513 |
if(![plist isKindOfClass:[NSDictionary class]]) { |
| 514 |
[self init]; |
| 515 |
[self release]; |
| 516 |
return nil; |
| 517 |
} |
| 518 |
|
| 519 |
id pValue = [plist valueForKey:@"value"]; |
| 520 |
id criteria = [plist valueForKey:@"criteria"]; |
| 521 |
id pChildren = [NSMutableArray array]; |
| 522 |
for(id criterion in criteria) { |
| 523 |
id c = [[self class] ruleWithPlist:criterion]; |
| 524 |
if(c) [pChildren addObject:c]; |
| 525 |
} |
| 526 |
id hints = [self predicateHintsWithPlist:plist]; |
| 527 |
|
| 528 |
return [self initWithValue:pValue children:pChildren predicateHints:hints]; |
| 529 |
} |
| 530 |
|
| 531 |
- (void)dealloc |
| 532 |
{ |
| 533 |
[children release]; |
| 534 |
[predicateHints release]; |
| 535 |
[_value release]; |
| 536 |
|
| 537 |
[super dealloc]; |
| 538 |
} |
| 539 |
|
| 540 |
@end |
| 541 |
|
| 542 |
@implementation XspfMRule (XspfMPrivate) |
| 543 |
|
| 544 |
- (NSView *)textField |
| 545 |
{ |
| 546 |
id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 547 |
[[text cell] setControlSize:NSSmallControlSize]; |
| 548 |
[text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 549 |
[text setStringValue:@"1234567890"]; |
| 550 |
[text sizeToFit]; |
| 551 |
[text setStringValue:@""]; |
| 552 |
|
| 553 |
return text; |
| 554 |
} |
| 555 |
- (NSView *)datePicker |
| 556 |
{ |
| 557 |
id date = [[[NSDatePicker alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 558 |
[[date cell] setControlSize:NSSmallControlSize]; |
| 559 |
[date setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 560 |
[date setDatePickerElements:NSYearMonthDayDatePickerElementFlag]; |
| 561 |
[date setDrawsBackground:YES]; |
| 562 |
[date setDateValue:[NSDate dateWithTimeIntervalSinceNow:0.0]]; |
| 563 |
[date sizeToFit]; |
| 564 |
|
| 565 |
return date; |
| 566 |
} |
| 567 |
- (NSView *)ratingIndicator |
| 568 |
{ |
| 569 |
id rate = [[[NSLevelIndicator alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 570 |
id cell = [rate cell]; |
| 571 |
[cell setControlSize:NSSmallControlSize]; |
| 572 |
[rate setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 573 |
[rate setMinValue:0]; |
| 574 |
[rate setMaxValue:5]; |
| 575 |
[cell setLevelIndicatorStyle:NSRatingLevelIndicatorStyle]; |
| 576 |
[cell setEditable:YES]; |
| 577 |
[cell setHighlighted:YES]; |
| 578 |
[rate sizeToFit]; |
| 579 |
|
| 580 |
[rate setAction:@selector(continuousHighlighted:)]; |
| 581 |
[rate setTarget:[[self class] functionHost]]; |
| 582 |
|
| 583 |
return rate; |
| 584 |
} |
| 585 |
- (void)setHighlightRate:(id)rate |
| 586 |
{ |
| 587 |
[[rate cell] setHighlighted:YES]; |
| 588 |
} |
| 589 |
- (IBAction)continuousHighlighted:(id)sender |
| 590 |
{ |
| 591 |
[self performSelector:@selector(setHighlightRate:) withObject:sender afterDelay:0.0]; |
| 592 |
} |
| 593 |
- (NSView *)numberField |
| 594 |
{ |
| 595 |
id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 596 |
[[text cell] setControlSize:NSSmallControlSize]; |
| 597 |
[text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 598 |
[text setStringValue:@"1234"]; |
| 599 |
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; |
| 600 |
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; |
| 601 |
[formatter setMinimum:[NSNumber numberWithInt:0]]; |
| 602 |
[text setFormatter:formatter]; |
| 603 |
[text sizeToFit]; |
| 604 |
[text setStringValue:@"1"]; |
| 605 |
|
| 606 |
return text; |
| 607 |
} |
| 608 |
- (NSView *)labelField |
| 609 |
{ |
| 610 |
// HMLog(HMLogLevelDebug, @"Enter -> %@", NSStringFromSelector(_cmd)); |
| 611 |
id label = [[[HMLabelField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 612 |
[label sizeToFit]; |
| 613 |
[label setLabelStyle:HMSquareStyle]; |
| 614 |
[label setDrawX:YES]; |
| 615 |
|
| 616 |
return label; |
| 617 |
} |
| 618 |
@end |
| 619 |
@implementation XspfMRule (XspfMExpressionBuilder) |
| 620 |
+ (id)functionHost |
| 621 |
{ |
| 622 |
static id host = nil; |
| 623 |
if(host) return host; |
| 624 |
@synchronized(self) { |
| 625 |
if(!host) { |
| 626 |
host = [[self alloc] init]; |
| 627 |
} |
| 628 |
} |
| 629 |
return host; |
| 630 |
} |
| 631 |
- (NSArray *)twoNumberAndUnitArgs:(NSArray *)displayValues |
| 632 |
{ |
| 633 |
id value03 = [displayValues objectAtIndex:2]; |
| 634 |
id arg01 = [NSNumber numberWithInt:[[value03 objectValue] intValue]]; |
| 635 |
|
| 636 |
id value05 = [displayValues objectAtIndex:4]; |
| 637 |
id arg02 = [NSNumber numberWithInt:[[value05 objectValue] intValue]]; |
| 638 |
|
| 639 |
id value06 = [displayValues objectAtIndex:5]; |
| 640 |
id arg03 = nil; |
| 641 |
if([value06 isEqualToString:@"Hours"]) { |
| 642 |
arg03 = [NSNumber numberWithInt:XspfMHoursUnitType]; |
| 643 |
} else if([value06 isEqualToString:@"Days"]) { |
| 644 |
arg03 = [NSNumber numberWithInt:XspfMDaysUnitType]; |
| 645 |
} else if([value06 isEqualToString:@"Weeks"]) { |
| 646 |
arg03 = [NSNumber numberWithInt:XpsfMWeeksUnitType]; |
| 647 |
} else if([value06 isEqualToString:@"Months"]) { |
| 648 |
arg03 = [NSNumber numberWithInt:XspfMMonthsUnitType]; |
| 649 |
} else if([value06 isEqualToString:@"Years"]) { |
| 650 |
arg03 = [NSNumber numberWithInt:XspfMYearsUnitType]; |
| 651 |
} |
| 652 |
|
| 653 |
if([arg01 compare:arg02] == NSOrderedDescending) { |
| 654 |
id t = arg01; |
| 655 |
arg01 = arg02; |
| 656 |
arg02 = t; |
| 657 |
} |
| 658 |
|
| 659 |
return [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01], |
| 660 |
[NSExpression expressionForConstantValue:arg02], |
| 661 |
[NSExpression expressionForConstantValue:arg03], |
| 662 |
nil]; |
| 663 |
} |
| 664 |
- (NSArray *)numberAndUnitArgs:(NSArray *)displayValues |
| 665 |
{ |
| 666 |
id value03 = [displayValues objectAtIndex:2]; |
| 667 |
id arg01 = [NSNumber numberWithInt:[[value03 objectValue] intValue]]; |
| 668 |
|
| 669 |
id value04 = [displayValues objectAtIndex:3]; |
| 670 |
id arg02 = nil; |
| 671 |
if([value04 isEqualToString:@"Hours"]) { |
| 672 |
arg02 = [NSNumber numberWithInt:XspfMHoursUnitType]; |
| 673 |
} else if([value04 isEqualToString:@"Days"]) { |
| 674 |
arg02 = [NSNumber numberWithInt:XspfMDaysUnitType]; |
| 675 |
} else if([value04 isEqualToString:@"Weeks"]) { |
| 676 |
arg02 = [NSNumber numberWithInt:XpsfMWeeksUnitType]; |
| 677 |
} else if([value04 isEqualToString:@"Months"]) { |
| 678 |
arg02 = [NSNumber numberWithInt:XspfMMonthsUnitType]; |
| 679 |
} else if([value04 isEqualToString:@"Years"]) { |
| 680 |
arg02 = [NSNumber numberWithInt:XspfMYearsUnitType]; |
| 681 |
} |
| 682 |
|
| 683 |
return [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01], |
| 684 |
[NSExpression expressionForConstantValue:arg02], nil]; |
| 685 |
} |
| 686 |
- (NSExpression *)rangeDateFromDisplayValues:(NSArray *)displayValues |
| 687 |
{ |
| 688 |
id field01 = nil; |
| 689 |
id field02 = nil; |
| 690 |
|
| 691 |
Class datepickerclass = [NSDatePicker class]; |
| 692 |
for(id v in displayValues) { |
| 693 |
if([v isKindOfClass:datepickerclass]) { |
| 694 |
if([v tag] == XspfMPrimaryDateFieldTag) { |
| 695 |
field01 = v; |
| 696 |
} else { |
| 697 |
field02 = v; |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
if(!field01 || !field02) return nil; |
| 703 |
|
| 704 |
id value01, value02; |
| 705 |
value01 = [field01 dateValue]; value02 = [field02 dateValue]; |
| 706 |
if([value01 compare:value02] == NSOrderedDescending) { |
| 707 |
id t = value02; |
| 708 |
value02 = value01; |
| 709 |
value01 = t; |
| 710 |
} |
| 711 |
|
| 712 |
id expression01, expression02; |
| 713 |
expression01 = [NSExpression expressionForConstantValue:value01]; |
| 714 |
expression02 = [NSExpression expressionForConstantValue:value02]; |
| 715 |
|
| 716 |
return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]]; |
| 717 |
} |
| 718 |
- (NSExpression *)rangeNumberFromDisplayValues:(NSArray *)displayValues |
| 719 |
{ |
| 720 |
id field01 = nil; |
| 721 |
id field02 = nil; |
| 722 |
|
| 723 |
Class numberFieldClass = [NSTextField class]; |
| 724 |
for(id v in displayValues) { |
| 725 |
if([v isKindOfClass:numberFieldClass]) { |
| 726 |
if([v tag] == XspfMPrimaryNumberFieldTag) { |
| 727 |
field01 = v; |
| 728 |
} else { |
| 729 |
field02 = v; |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
if(!field01 || !field02) return nil; |
| 735 |
|
| 736 |
NSInteger value01, value02; |
| 737 |
value01 = [field01 integerValue]; value02 = [field02 integerValue]; |
| 738 |
if(value01 > value02) { |
| 739 |
NSInteger t = value02; |
| 740 |
value02 = value01; |
| 741 |
value01 = t; |
| 742 |
} |
| 743 |
|
| 744 |
id expression01, expression02; |
| 745 |
expression01 = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:value01]]; |
| 746 |
expression02 = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:value02]]; |
| 747 |
|
| 748 |
return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]]; |
| 749 |
} |
| 750 |
|
| 751 |
- (NSExpression *)falseExpression |
| 752 |
{ |
| 753 |
return [NSExpression expressionForConstantValue:[NSNumber numberWithBool:NO]]; |
| 754 |
} |
| 755 |
@end |
| 756 |
|