| 1 |
masaki |
120 |
// |
| 2 |
|
|
// XspfMRuleEditorDelegate.m |
| 3 |
|
|
// XspfManager |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 09/11/28. |
| 6 |
|
|
// Copyright 2009 masakih. All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
|
|
#import "XspfMRuleEditorDelegate.h" |
| 10 |
|
|
|
| 11 |
|
|
#import "XspfMRuleEditorRow.h" |
| 12 |
|
|
|
| 13 |
|
|
@implementation XspfMRuleEditorDelegate |
| 14 |
masaki |
125 |
|
| 15 |
|
|
static NSString *XspfMREDRowsKey = @"rows"; |
| 16 |
|
|
static NSString *XspfMREDCriteriaKey = @"criteria"; |
| 17 |
|
|
static NSString *XspfMREDDisplayValuesKey = @"displayValues"; |
| 18 |
|
|
static NSString *XspfMREDRowTypeKey = @"rowType"; |
| 19 |
|
|
static NSString *XspfMREDSubrowsKey = @"subrows"; |
| 20 |
|
|
static NSString *XspfMREDValueKey = @"value"; |
| 21 |
|
|
static NSString *XspfMREDPredicateRowsKey = @"predicateRows"; |
| 22 |
|
|
static NSString *XspfMREDNameKey = @"name"; |
| 23 |
|
|
|
| 24 |
|
|
static NSString *XspfMStringPredicateIsEqualOperator = @"is"; |
| 25 |
|
|
static NSString *XspfMStringPredicateIsNotEqualOperator = @"is not"; |
| 26 |
|
|
static NSString *XspfMStringPredicateContainsOperator = @"contains"; |
| 27 |
|
|
static NSString *XspfMStringPredicateBeginsWithOperator = @"begins with"; |
| 28 |
|
|
static NSString *XspfMStringPredicateEndsWithOperator = @"ends with"; |
| 29 |
|
|
|
| 30 |
|
|
//static NSString *XspfMAbDatePredicatePicker01 = @"date"; |
| 31 |
|
|
//static NSString *XspfMAbDatePredicatePicker02 = @"beginDate"; |
| 32 |
|
|
//static NSString *XspfMAbDatePredicatePicker03 = @"endDate"; |
| 33 |
|
|
//static NSString *XspfMAbDatePredicateIsEqualOperator = @"is the date"; |
| 34 |
|
|
//static NSString *XspfMAbDatePredicateLessThanOperator = @"is after the date"; |
| 35 |
|
|
//static NSString *XspfMAbDatePredicateGreaterThanOperator = @"is before the date"; |
| 36 |
|
|
//static NSString *XspfMAbDatePredicateBetweenOperator = @"is in the range"; |
| 37 |
|
|
//static NSString *XspfMAbDatePredicateAndField = @"andField"; |
| 38 |
|
|
|
| 39 |
|
|
|
| 40 |
masaki |
137 |
- (NSExpression *)rangeUnitFromDisplayValues:(NSArray *)displayValues option:(NSNumber *)optionValue |
| 41 |
|
|
{ |
| 42 |
|
|
NSInteger option = [optionValue integerValue]; |
| 43 |
|
|
|
| 44 |
|
|
NSString *variable = nil; |
| 45 |
|
|
id value02 = [displayValues objectAtIndex:2]; |
| 46 |
|
|
id value03 = [displayValues objectAtIndex:3]; |
| 47 |
|
|
id value04 = nil, value05 = nil; |
| 48 |
|
|
switch(option) { |
| 49 |
|
|
case 0: |
| 50 |
masaki |
138 |
variable = [NSString stringWithFormat:@"%d-%@-ago", [value02 intValue], value03]; |
| 51 |
masaki |
137 |
break; |
| 52 |
|
|
case 1: |
| 53 |
masaki |
138 |
variable = [NSString stringWithFormat:@"%d-%@", [value02 intValue], value03]; |
| 54 |
|
|
break; |
| 55 |
masaki |
137 |
case 2: |
| 56 |
masaki |
138 |
variable = [NSString stringWithFormat:@"not-%d-%@", [value02 intValue], value03]; |
| 57 |
masaki |
137 |
break; |
| 58 |
|
|
case 3: |
| 59 |
|
|
value04 = [displayValues objectAtIndex:4]; |
| 60 |
|
|
value05 = [displayValues objectAtIndex:5]; |
| 61 |
masaki |
138 |
variable = [NSString stringWithFormat:@"%d-%@-%d-%@", [value02 intValue], value03, [value04 intValue], value05]; |
| 62 |
masaki |
137 |
break; |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
|
|
return [NSExpression expressionForVariable:variable]; |
| 66 |
|
|
} |
| 67 |
masaki |
124 |
- (NSExpression *)rangeDateFromDisplayValues:(NSArray *)displayValues |
| 68 |
|
|
{ |
| 69 |
|
|
id field01 = nil; |
| 70 |
|
|
id field02 = nil; |
| 71 |
|
|
|
| 72 |
|
|
Class datepickerclass = [NSDatePicker class]; |
| 73 |
|
|
for(id v in displayValues) { |
| 74 |
|
|
if([v isKindOfClass:datepickerclass]) { |
| 75 |
|
|
if([v tag] == 0) { |
| 76 |
|
|
field01 = v; |
| 77 |
|
|
} else { |
| 78 |
|
|
field02 = v; |
| 79 |
|
|
} |
| 80 |
|
|
} |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
if(!field01 || !field02) return nil; |
| 84 |
|
|
|
| 85 |
|
|
id value01, value02; |
| 86 |
|
|
value01 = [field01 dateValue]; value02 = [field02 dateValue]; |
| 87 |
|
|
if([value01 compare:value02] == NSOrderedDescending) { |
| 88 |
|
|
id t = value02; |
| 89 |
|
|
value02 = value01; |
| 90 |
|
|
value01 = t; |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
id expression01, expression02; |
| 94 |
masaki |
133 |
expression01 = [NSExpression expressionForConstantValue:value01]; |
| 95 |
|
|
expression02 = [NSExpression expressionForConstantValue:value02]; |
| 96 |
masaki |
124 |
|
| 97 |
|
|
return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]]; |
| 98 |
|
|
} |
| 99 |
masaki |
132 |
- (NSExpression *)relatedDate:(NSNumber *)typeValue |
| 100 |
|
|
{ |
| 101 |
|
|
NSString *variable = nil; |
| 102 |
|
|
NSInteger type = [typeValue integerValue]; |
| 103 |
|
|
switch(type) { |
| 104 |
|
|
case 0: |
| 105 |
|
|
variable = @"TODAY"; |
| 106 |
|
|
break; |
| 107 |
|
|
case 1: |
| 108 |
|
|
variable = @"YESTERDAY"; |
| 109 |
|
|
break; |
| 110 |
|
|
case 2: |
| 111 |
|
|
variable = @"THISWEEK"; |
| 112 |
|
|
break; |
| 113 |
|
|
case 3: |
| 114 |
|
|
variable = @"LASTWEEK"; |
| 115 |
|
|
break; |
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
|
|
return [NSExpression expressionForVariable:variable]; |
| 119 |
|
|
} |
| 120 |
|
|
|
| 121 |
masaki |
124 |
- (NSView *)textField |
| 122 |
|
|
{ |
| 123 |
|
|
id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 124 |
|
|
[[text cell] setControlSize:NSSmallControlSize]; |
| 125 |
|
|
[text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 126 |
masaki |
126 |
[text setStringValue:@"1234567890"]; |
| 127 |
masaki |
124 |
[text sizeToFit]; |
| 128 |
|
|
[text setStringValue:@""]; |
| 129 |
|
|
[text setDelegate:self]; |
| 130 |
|
|
|
| 131 |
|
|
return text; |
| 132 |
|
|
} |
| 133 |
|
|
- (NSView *)datePicker |
| 134 |
|
|
{ |
| 135 |
|
|
id date = [[[NSDatePicker alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 136 |
|
|
[[date cell] setControlSize:NSSmallControlSize]; |
| 137 |
|
|
[date setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 138 |
|
|
[date setDatePickerElements:NSYearMonthDayDatePickerElementFlag]; |
| 139 |
|
|
[date setDrawsBackground:YES]; |
| 140 |
|
|
[date setDateValue:[NSDate dateWithTimeIntervalSinceNow:0.0]]; |
| 141 |
|
|
[date sizeToFit]; |
| 142 |
|
|
[date setDelegate:self]; |
| 143 |
|
|
|
| 144 |
|
|
return date; |
| 145 |
|
|
} |
| 146 |
|
|
- (NSView *)ratingIndicator |
| 147 |
|
|
{ |
| 148 |
|
|
id rate = [[[NSLevelIndicator alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 149 |
|
|
id cell = [rate cell]; |
| 150 |
|
|
[cell setControlSize:NSSmallControlSize]; |
| 151 |
|
|
[rate setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 152 |
|
|
[rate setMinValue:0]; |
| 153 |
|
|
[rate setMaxValue:5]; |
| 154 |
|
|
[cell setLevelIndicatorStyle:NSRatingLevelIndicatorStyle]; |
| 155 |
|
|
[cell setEditable:YES]; |
| 156 |
|
|
[rate sizeToFit]; |
| 157 |
|
|
|
| 158 |
|
|
return rate; |
| 159 |
|
|
} |
| 160 |
|
|
- (NSView *)numberField |
| 161 |
|
|
{ |
| 162 |
masaki |
134 |
id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease]; |
| 163 |
|
|
[[text cell] setControlSize:NSSmallControlSize]; |
| 164 |
|
|
[text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]]; |
| 165 |
|
|
[text setStringValue:@"123"]; |
| 166 |
|
|
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease]; |
| 167 |
|
|
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; |
| 168 |
|
|
[formatter setMinimum:[NSNumber numberWithInt:0]]; |
| 169 |
|
|
[text setFormatter:formatter]; |
| 170 |
|
|
[text sizeToFit]; |
| 171 |
|
|
[text setStringValue:@"1"]; |
| 172 |
|
|
[text setDelegate:self]; |
| 173 |
|
|
|
| 174 |
|
|
return text; |
| 175 |
masaki |
124 |
} |
| 176 |
|
|
|
| 177 |
masaki |
128 |
|
| 178 |
masaki |
124 |
- (NSDictionary *)buildRows:(NSArray *)template |
| 179 |
|
|
{ |
| 180 |
|
|
NSMutableDictionary *result = [NSMutableDictionary dictionary]; |
| 181 |
|
|
for(id row in template) { |
| 182 |
masaki |
125 |
id criteria = [row valueForKey:XspfMREDCriteriaKey]; |
| 183 |
|
|
id name = [row valueForKey:XspfMREDNameKey]; |
| 184 |
masaki |
141 |
// [result setObject:criteria forKey:name]; |
| 185 |
|
|
|
| 186 |
|
|
#warning Test |
| 187 |
|
|
id rule = [XspfMRule ruleWithPlist:row]; |
| 188 |
|
|
[result setObject:rule forKey:name]; |
| 189 |
masaki |
124 |
} |
| 190 |
masaki |
141 |
// id rules = [XspfMRule compoundRule]; |
| 191 |
|
|
// NSLog(@"Compound Rules->%@", rules); |
| 192 |
masaki |
124 |
|
| 193 |
masaki |
141 |
rowTemplate = [result retain]; |
| 194 |
masaki |
124 |
return result; |
| 195 |
|
|
} |
| 196 |
masaki |
126 |
- (id)criteriaWithKeyPath:(NSString *)keypath |
| 197 |
|
|
{ |
| 198 |
|
|
NSString *key = nil; |
| 199 |
|
|
if([keypath isEqualToString:@"title"]) { |
| 200 |
|
|
key = @"String"; |
| 201 |
|
|
} else if([keypath isEqualToString:@"rating"]) { |
| 202 |
|
|
key = @"Rate"; |
| 203 |
masaki |
141 |
} else if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:keypath]) { |
| 204 |
|
|
key = @"AbDate"; |
| 205 |
masaki |
126 |
} |
| 206 |
|
|
if(key) { |
| 207 |
|
|
id row = [rowTemplate valueForKey:key]; |
| 208 |
masaki |
141 |
// id c = [[[row objectAtIndex:0] mutableCopy] autorelease]; |
| 209 |
|
|
// [c setValue:keypath forKey:XspfMREDValueKey]; |
| 210 |
|
|
id c = [[[row childAtIndex:0] copy] autorelease]; |
| 211 |
|
|
[c setValue:keypath]; |
| 212 |
masaki |
126 |
return [NSArray arrayWithObject:c]; |
| 213 |
|
|
} |
| 214 |
|
|
|
| 215 |
|
|
return nil; |
| 216 |
|
|
} |
| 217 |
masaki |
120 |
- (void)awakeFromNib |
| 218 |
|
|
{ |
| 219 |
|
|
if(!compound) { |
| 220 |
masaki |
141 |
// compound = [[XspfMCompound alloc] init]; |
| 221 |
masaki |
120 |
} |
| 222 |
masaki |
124 |
|
| 223 |
|
|
rowIDs = [[NSMutableArray array] retain]; |
| 224 |
|
|
rowFields = [[NSMutableDictionary dictionary] retain]; |
| 225 |
|
|
|
| 226 |
|
|
NSBundle *m = [NSBundle mainBundle]; |
| 227 |
|
|
NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"]; |
| 228 |
|
|
NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path]; |
| 229 |
|
|
if(!rowsTemplate) { |
| 230 |
|
|
exit(12345); |
| 231 |
masaki |
120 |
} |
| 232 |
masaki |
124 |
|
| 233 |
masaki |
141 |
[self buildRows:rowsTemplate]; |
| 234 |
masaki |
124 |
|
| 235 |
|
|
// NSLog(@"rowTemplate =>\n%@", rowTemplate); |
| 236 |
|
|
|
| 237 |
|
|
NSMutableArray *newRows = [NSMutableArray array]; |
| 238 |
|
|
|
| 239 |
masaki |
126 |
id c = [self criteriaWithKeyPath:@"title"]; |
| 240 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 241 |
masaki |
124 |
|
| 242 |
|
|
for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) { |
| 243 |
masaki |
126 |
c = [self criteriaWithKeyPath:keyPath]; |
| 244 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 245 |
masaki |
124 |
} |
| 246 |
|
|
|
| 247 |
masaki |
126 |
c = [self criteriaWithKeyPath:@"rating"]; |
| 248 |
|
|
if(c) [newRows addObjectsFromArray:c]; |
| 249 |
masaki |
124 |
|
| 250 |
|
|
rows = [newRows retain]; |
| 251 |
masaki |
141 |
// NSLog(@"Rule->%@", rows); |
| 252 |
masaki |
125 |
|
| 253 |
masaki |
141 |
simples = [[XspfMRule compoundRule] retain]; |
| 254 |
masaki |
125 |
|
| 255 |
|
|
//// |
| 256 |
|
|
predicateRows = [[NSMutableArray alloc] init]; |
| 257 |
masaki |
141 |
// [ruleEditor setRowClass:[XspfMRule class]]; |
| 258 |
|
|
// [ruleEditor setCriteriaKeyPath:@"children"]; |
| 259 |
masaki |
125 |
[ruleEditor bind:XspfMREDRowsKey toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil]; |
| 260 |
masaki |
120 |
} |
| 261 |
|
|
|
| 262 |
masaki |
127 |
- (NSArray *)displayValuesWithPredicate:(NSComparisonPredicate *)predicate |
| 263 |
|
|
{ |
| 264 |
|
|
id value02 = nil; id value03 = nil; |
| 265 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 266 |
|
|
|
| 267 |
|
|
switch([predicate predicateOperatorType]) { |
| 268 |
|
|
case NSEqualToPredicateOperatorType: |
| 269 |
|
|
value02 = XspfMStringPredicateIsEqualOperator; |
| 270 |
|
|
break; |
| 271 |
|
|
case NSNotEqualToPredicateOperatorType: |
| 272 |
|
|
value02 = XspfMStringPredicateIsNotEqualOperator; |
| 273 |
|
|
break; |
| 274 |
|
|
case NSContainsPredicateOperatorType: |
| 275 |
|
|
value02 = XspfMStringPredicateContainsOperator; |
| 276 |
|
|
break; |
| 277 |
|
|
case NSBeginsWithPredicateOperatorType: |
| 278 |
|
|
value02 = XspfMStringPredicateBeginsWithOperator; |
| 279 |
|
|
break; |
| 280 |
|
|
case NSEndsWithPredicateOperatorType: |
| 281 |
|
|
value02 = XspfMStringPredicateEndsWithOperator; |
| 282 |
|
|
break; |
| 283 |
|
|
} |
| 284 |
|
|
id rightConstant = [[predicate rightExpression] constantValue]; |
| 285 |
|
|
value03 = [self textField]; |
| 286 |
|
|
[value03 setObjectValue:rightConstant]; |
| 287 |
|
|
|
| 288 |
|
|
id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil]; |
| 289 |
|
|
|
| 290 |
|
|
return disp; |
| 291 |
|
|
} |
| 292 |
|
|
- (NSArray *)ratingDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate |
| 293 |
|
|
{ |
| 294 |
|
|
id value02 = nil; id value03 = nil; |
| 295 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 296 |
|
|
|
| 297 |
|
|
switch([predicate predicateOperatorType]) { |
| 298 |
|
|
case NSEqualToPredicateOperatorType: |
| 299 |
|
|
value02 = XspfMStringPredicateIsEqualOperator; |
| 300 |
|
|
break; |
| 301 |
|
|
case NSGreaterThanPredicateOperatorType: |
| 302 |
|
|
value02 = @"is greater than"; |
| 303 |
|
|
break; |
| 304 |
|
|
case NSLessThanPredicateOperatorType: |
| 305 |
|
|
value02 = @"is less than"; |
| 306 |
|
|
break; |
| 307 |
|
|
} |
| 308 |
|
|
id rightConstant = [[predicate rightExpression] constantValue]; |
| 309 |
|
|
value03 = [self ratingIndicator]; |
| 310 |
|
|
[value03 setObjectValue:rightConstant]; |
| 311 |
|
|
|
| 312 |
|
|
id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil]; |
| 313 |
masaki |
141 |
|
| 314 |
masaki |
127 |
return disp; |
| 315 |
|
|
} |
| 316 |
masaki |
138 |
- (void)resolveVariable:(NSString *)variable value02:(id *)value02 value03:(id *)value03 value04:(id *)value04 value05:(id *)value05 value06:(id *)value06 value07:(id *)value07 |
| 317 |
|
|
{ |
| 318 |
|
|
NSArray *words = [variable componentsSeparatedByString:@"-"]; |
| 319 |
|
|
|
| 320 |
|
|
switch([words count]) { |
| 321 |
|
|
case 2: |
| 322 |
|
|
*value02 = @"is in the last"; |
| 323 |
|
|
*value03 = [self numberField]; |
| 324 |
|
|
[*value03 setTag:2000]; |
| 325 |
|
|
[*value03 setStringValue:[words objectAtIndex:0]]; |
| 326 |
|
|
*value04 = [words objectAtIndex:1]; |
| 327 |
|
|
break; |
| 328 |
|
|
case 3: |
| 329 |
|
|
if([[words objectAtIndex:0] isEqualToString:@"not"]) { |
| 330 |
|
|
*value02 = @"is not in the last"; |
| 331 |
|
|
*value03 = [self numberField]; |
| 332 |
|
|
[*value03 setTag:2000]; |
| 333 |
|
|
[*value03 setStringValue:[words objectAtIndex:1]]; |
| 334 |
|
|
*value04 = [words objectAtIndex:2]; |
| 335 |
|
|
} else { |
| 336 |
|
|
*value02 = @"is exactly"; |
| 337 |
|
|
*value03 = [self numberField]; |
| 338 |
|
|
[*value03 setTag:2000]; |
| 339 |
|
|
[*value03 setStringValue:[words objectAtIndex:0]]; |
| 340 |
|
|
*value04 = [words objectAtIndex:1]; |
| 341 |
|
|
*value05 = @"ago"; |
| 342 |
|
|
} |
| 343 |
|
|
break; |
| 344 |
|
|
case 4: |
| 345 |
|
|
*value02 = @"is between"; |
| 346 |
|
|
*value03 = [self numberField]; |
| 347 |
|
|
[*value03 setTag:2000]; |
| 348 |
|
|
[*value03 setStringValue:[words objectAtIndex:0]]; |
| 349 |
|
|
*value04 = @"and"; |
| 350 |
|
|
*value05 = [self numberField]; |
| 351 |
|
|
[*value05 setTag:2100]; |
| 352 |
|
|
[*value05 setStringValue:[words objectAtIndex:2]]; |
| 353 |
|
|
*value06 = [words objectAtIndex:3]; |
| 354 |
|
|
*value07 = @"ago"; |
| 355 |
|
|
break; |
| 356 |
|
|
} |
| 357 |
|
|
} |
| 358 |
|
|
|
| 359 |
masaki |
132 |
- (NSArray *)dateRangeDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate |
| 360 |
|
|
{ |
| 361 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 362 |
|
|
id rightVar = [[predicate rightExpression] variable]; |
| 363 |
|
|
|
| 364 |
|
|
id value02 = nil; |
| 365 |
|
|
id value03 = nil; |
| 366 |
|
|
id value04 = nil; |
| 367 |
|
|
id value05 = nil; |
| 368 |
|
|
id value06 = nil; |
| 369 |
|
|
id value07 = nil; |
| 370 |
|
|
|
| 371 |
|
|
if([rightVar isEqualToString:@"TODAY"]) { |
| 372 |
|
|
value02 = @"is today"; |
| 373 |
|
|
} else if([rightVar isEqualToString:@"YESTERDAY"]) { |
| 374 |
|
|
value02 = @"is yesterday"; |
| 375 |
|
|
} else if([rightVar isEqualToString:@"THISWEEK"]) { |
| 376 |
|
|
value02 = @"is this week"; |
| 377 |
|
|
} else if([rightVar isEqualToString:@"LASTWEEK"]) { |
| 378 |
|
|
value02 = @"is last week"; |
| 379 |
masaki |
138 |
} else { |
| 380 |
|
|
[self resolveVariable:rightVar value02:&value02 value03:&value03 value04:&value04 value05:&value05 value06:&value06 value07:&value07]; |
| 381 |
masaki |
132 |
} |
| 382 |
|
|
|
| 383 |
|
|
return [NSArray arrayWithObjects:leftKeyPath, value02, value03, value04, value05, value06, value07, nil]; |
| 384 |
|
|
} |
| 385 |
|
|
|
| 386 |
masaki |
127 |
- (NSArray *)dateDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate |
| 387 |
|
|
{ |
| 388 |
|
|
id value02 = nil; id value03 = nil; |
| 389 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 390 |
|
|
|
| 391 |
|
|
switch([predicate predicateOperatorType]) { |
| 392 |
|
|
case NSEqualToPredicateOperatorType: |
| 393 |
|
|
value02 = @"is the date"; |
| 394 |
|
|
break; |
| 395 |
|
|
case NSGreaterThanPredicateOperatorType: |
| 396 |
|
|
value02 = @"is after the date"; |
| 397 |
|
|
break; |
| 398 |
|
|
case NSLessThanPredicateOperatorType: |
| 399 |
|
|
value02 = @"is before the date"; |
| 400 |
|
|
break; |
| 401 |
|
|
case NSBetweenPredicateOperatorType: |
| 402 |
|
|
return [self dateRangeDisplayValuesWithPredicate:predicate]; |
| 403 |
|
|
|
| 404 |
|
|
} |
| 405 |
|
|
id rightConstant = [[predicate rightExpression] constantValue]; |
| 406 |
|
|
value03 = [self datePicker]; |
| 407 |
|
|
[value03 setObjectValue:rightConstant]; |
| 408 |
|
|
|
| 409 |
|
|
id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil]; |
| 410 |
|
|
|
| 411 |
|
|
return disp; |
| 412 |
|
|
} |
| 413 |
|
|
|
| 414 |
masaki |
125 |
- (id)buildRowsFromPredicate:(id)predicate |
| 415 |
|
|
{ |
| 416 |
|
|
if([predicate isKindOfClass:[NSCompoundPredicate class]]) { |
| 417 |
|
|
id subrows = [NSMutableArray array]; |
| 418 |
|
|
|
| 419 |
masaki |
141 |
NSInteger index = NSNotFound; |
| 420 |
masaki |
125 |
id value = nil; |
| 421 |
|
|
switch([predicate compoundPredicateType]) { |
| 422 |
|
|
case NSNotPredicateType: |
| 423 |
|
|
// ? |
| 424 |
|
|
break; |
| 425 |
|
|
case NSAndPredicateType: |
| 426 |
masaki |
141 |
index = 0; |
| 427 |
masaki |
125 |
value = @"All"; |
| 428 |
|
|
break; |
| 429 |
|
|
case NSOrPredicateType: |
| 430 |
masaki |
141 |
index = 1; |
| 431 |
masaki |
125 |
value = @"Any"; |
| 432 |
|
|
break; |
| 433 |
|
|
} |
| 434 |
masaki |
141 |
id criteria = [[XspfMRule compoundRule] objectAtIndex:index]; |
| 435 |
masaki |
125 |
|
| 436 |
|
|
NSArray *sub = [predicate subpredicates]; |
| 437 |
|
|
for(id p in sub) { |
| 438 |
|
|
[subrows addObject:[self buildRowsFromPredicate:p]]; |
| 439 |
|
|
} |
| 440 |
|
|
|
| 441 |
masaki |
141 |
id disp = [NSArray arrayWithObjects:value, @"of the following are true", nil]; |
| 442 |
masaki |
127 |
id type = [NSNumber numberWithInt:NSRuleEditorRowTypeCompound]; |
| 443 |
masaki |
125 |
|
| 444 |
|
|
id result = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
| 445 |
|
|
criteria, XspfMREDCriteriaKey, |
| 446 |
masaki |
141 |
disp, XspfMREDDisplayValuesKey, |
| 447 |
masaki |
125 |
type, XspfMREDRowTypeKey, |
| 448 |
|
|
subrows, XspfMREDSubrowsKey, |
| 449 |
|
|
nil]; |
| 450 |
|
|
|
| 451 |
|
|
return result; |
| 452 |
|
|
} else if([predicate isKindOfClass:[NSComparisonPredicate class]]) { |
| 453 |
|
|
id leftKeyPath = [[predicate leftExpression] keyPath]; |
| 454 |
|
|
if(!leftKeyPath) return [NSArray array]; |
| 455 |
masaki |
128 |
|
| 456 |
|
|
NSArray *disp = nil; |
| 457 |
masaki |
127 |
if([leftKeyPath isEqualToString:@"title"]) { |
| 458 |
masaki |
128 |
disp = [self displayValuesWithPredicate:predicate]; |
| 459 |
masaki |
125 |
} |
| 460 |
masaki |
127 |
if([leftKeyPath isEqualToString:@"rating"]) { |
| 461 |
masaki |
128 |
disp = [self ratingDisplayValuesWithPredicate:predicate]; |
| 462 |
masaki |
127 |
} |
| 463 |
masaki |
132 |
if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:leftKeyPath]) { |
| 464 |
masaki |
128 |
disp = [self dateDisplayValuesWithPredicate:predicate]; |
| 465 |
masaki |
138 |
NSLog(@"dispalyValues -> %@", disp); |
| 466 |
masaki |
128 |
} |
| 467 |
|
|
|
| 468 |
|
|
if(disp) { |
| 469 |
masaki |
127 |
NSArray *row = [self criteriaWithKeyPath:leftKeyPath]; |
| 470 |
|
|
NSMutableDictionary *criterion = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
| 471 |
|
|
row, XspfMREDCriteriaKey, |
| 472 |
|
|
disp, XspfMREDDisplayValuesKey, |
| 473 |
|
|
[NSNumber numberWithInt:NSRuleEditorRowTypeSimple], XspfMREDRowTypeKey, |
| 474 |
|
|
nil]; |
| 475 |
|
|
return criterion; |
| 476 |
|
|
} |
| 477 |
masaki |
125 |
|
| 478 |
|
|
} else if([predicate isKindOfClass:[NSPredicate class]]) { |
| 479 |
|
|
NSLog(@"--> %@", predicate); |
| 480 |
|
|
} else { |
| 481 |
|
|
NSLog(@"???predicate class is %@", NSStringFromClass([predicate class])); |
| 482 |
|
|
} |
| 483 |
|
|
|
| 484 |
|
|
return [NSArray array]; |
| 485 |
|
|
} |
| 486 |
masaki |
141 |
|
| 487 |
|
|
|
| 488 |
masaki |
123 |
- (void)setPredicate:(id)predicate |
| 489 |
|
|
{ |
| 490 |
masaki |
126 |
NSLog(@"predicate -> (%@) %@", NSStringFromClass([predicate class]), predicate); |
| 491 |
masaki |
123 |
|
| 492 |
masaki |
125 |
id hoge = [self buildRowsFromPredicate:predicate]; |
| 493 |
|
|
id new = [NSArray arrayWithObject:hoge]; |
| 494 |
masaki |
141 |
NSLog(@"new rows -> %@", new); |
| 495 |
masaki |
125 |
|
| 496 |
|
|
[self willChangeValueForKey:XspfMREDPredicateRowsKey]; |
| 497 |
|
|
[predicateRows release]; |
| 498 |
|
|
predicateRows = [new retain]; |
| 499 |
|
|
[self didChangeValueForKey:XspfMREDPredicateRowsKey]; |
| 500 |
|
|
[ruleEditor reloadCriteria]; |
| 501 |
masaki |
141 |
// [ruleEditor reloadPredicate]; |
| 502 |
|
|
// [ruleEditor reloadCriteria]; |
| 503 |
|
|
|
| 504 |
|
|
// NSLog(@"new display values -> %@", [ruleEditor displayValuesForRow:1]); |
| 505 |
masaki |
123 |
} |
| 506 |
masaki |
125 |
- (void)setPredicateRows:(id)p |
| 507 |
|
|
{ |
| 508 |
|
|
NSLog(@"new -> %@", p); |
| 509 |
|
|
[predicateRows release]; |
| 510 |
|
|
predicateRows = [p retain]; |
| 511 |
|
|
} |
| 512 |
|
|
|
| 513 |
|
|
#pragma mark#### NSRleEditor Delegate #### |
| 514 |
masaki |
124 |
- (NSInteger)ruleEditor:(NSRuleEditor *)editor |
| 515 |
|
|
numberOfChildrenForCriterion:(id)criterion |
| 516 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 517 |
|
|
{ |
| 518 |
|
|
NSInteger result = 0; |
| 519 |
|
|
|
| 520 |
|
|
if(!criterion) { |
| 521 |
masaki |
141 |
if(rowType == NSRuleEditorRowTypeCompound) |
| 522 |
|
|
return [simples count]; |
| 523 |
|
|
else |
| 524 |
|
|
return [rows count]; |
| 525 |
masaki |
124 |
} |
| 526 |
|
|
|
| 527 |
masaki |
141 |
result = [criterion numberOfChildren]; |
| 528 |
|
|
|
| 529 |
masaki |
124 |
// NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result); |
| 530 |
|
|
|
| 531 |
|
|
return result; |
| 532 |
|
|
} |
| 533 |
|
|
|
| 534 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 535 |
|
|
child:(NSInteger)index |
| 536 |
|
|
forCriterion:(id)criterion |
| 537 |
|
|
withRowType:(NSRuleEditorRowType)rowType |
| 538 |
|
|
{ |
| 539 |
|
|
id result = nil; |
| 540 |
|
|
|
| 541 |
|
|
if(!criterion) { |
| 542 |
masaki |
141 |
if(rowType == NSRuleEditorRowTypeCompound) |
| 543 |
|
|
return [simples objectAtIndex:index]; |
| 544 |
|
|
else |
| 545 |
|
|
return [rows objectAtIndex:index]; |
| 546 |
masaki |
124 |
} |
| 547 |
masaki |
141 |
|
| 548 |
masaki |
124 |
|
| 549 |
masaki |
141 |
result = [criterion childAtIndex:index]; |
| 550 |
|
|
|
| 551 |
|
|
// NSLog(@"child\tindex -> %d, criterion -> %%, type -> %d, result -> %@", index, rowType, result); |
| 552 |
masaki |
124 |
// NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result); |
| 553 |
|
|
|
| 554 |
|
|
return result; |
| 555 |
|
|
} |
| 556 |
|
|
- (id)ruleEditor:(NSRuleEditor *)editor |
| 557 |
|
|
displayValueForCriterion:(id)criterion |
| 558 |
|
|
inRow:(NSInteger)row |
| 559 |
|
|
{ |
| 560 |
|
|
id result = nil; |
| 561 |
masaki |
141 |
result = [criterion displayValueForRuleEditor:editor inRow:row]; |
| 562 |
|
|
// NSLog(@"display\tcriterion -> %%, row -> %d, result -> %@", row, result); |
| 563 |
masaki |
124 |
// NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result); |
| 564 |
|
|
|
| 565 |
|
|
return result; |
| 566 |
|
|
} |
| 567 |
|
|
- (NSDictionary *)ruleEditor:(NSRuleEditor *)editor |
| 568 |
|
|
predicatePartsForCriterion:(id)criterion |
| 569 |
|
|
withDisplayValue:(id)displayValue |
| 570 |
|
|
inRow:(NSInteger)row |
| 571 |
|
|
{ |
| 572 |
|
|
id result = nil; |
| 573 |
masaki |
141 |
result = [criterion predicatePartsWithDisplayValue:displayValue forRuleEditor:editor inRow:row]; |
| 574 |
|
|
// NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result); |
| 575 |
masaki |
124 |
|
| 576 |
|
|
return result; |
| 577 |
|
|
} |
| 578 |
|
|
- (void)ruleEditorRowsDidChange:(NSNotification *)notification |
| 579 |
|
|
{ |
| 580 |
|
|
// |
| 581 |
|
|
} |
| 582 |
|
|
|
| 583 |
masaki |
120 |
@end |