Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 146 - (hide annotations) (download)
Mon Dec 14 13:13:21 2009 UTC (14 years, 4 months ago) by masaki
File size: 23751 byte(s)
[New] NSRuleEditor delegate完成!
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 143 [result setObject:criteria forKey:name];
185 masaki 124 }
186    
187     return result;
188     }
189 masaki 126 - (id)criteriaWithKeyPath:(NSString *)keypath
190     {
191     NSString *key = nil;
192     if([keypath isEqualToString:@"title"]) {
193     key = @"String";
194     } else if([keypath isEqualToString:@"rating"]) {
195     key = @"Rate";
196     }
197     if(key) {
198     id row = [rowTemplate valueForKey:key];
199 masaki 143 id c = [[[row objectAtIndex:0] mutableCopy] autorelease];
200     [c setValue:keypath forKey:XspfMREDValueKey];
201 masaki 126 return [NSArray arrayWithObject:c];
202     }
203    
204 masaki 143 if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:keypath]) {
205     id keys = [NSArray arrayWithObjects:@"AbDate", /*@"RlDate",*/ nil];
206     id result = [NSMutableArray array];
207     for(key in keys) {
208     id row = [rowTemplate valueForKey:key];
209     id c = [[[row objectAtIndex:0] mutableCopy] autorelease];
210     [c setValue:keypath forKey:XspfMREDValueKey];
211     [result addObject:c];
212     }
213    
214     return result;
215     }
216    
217 masaki 126 return nil;
218     }
219 masaki 120 - (void)awakeFromNib
220     {
221     if(!compound) {
222 masaki 143 compound = [[XspfMCompound alloc] init];
223 masaki 120 }
224 masaki 124
225     rowIDs = [[NSMutableArray array] retain];
226     rowFields = [[NSMutableDictionary dictionary] retain];
227    
228     NSBundle *m = [NSBundle mainBundle];
229     NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"];
230     NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path];
231     if(!rowsTemplate) {
232     exit(12345);
233 masaki 120 }
234 masaki 124
235 masaki 143 rowTemplate = [[self buildRows:rowsTemplate] retain];
236 masaki 124
237     // NSLog(@"rowTemplate =>\n%@", rowTemplate);
238    
239     NSMutableArray *newRows = [NSMutableArray array];
240    
241 masaki 126 id c = [self criteriaWithKeyPath:@"title"];
242     if(c) [newRows addObjectsFromArray:c];
243 masaki 124
244     for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) {
245 masaki 126 c = [self criteriaWithKeyPath:keyPath];
246     if(c) [newRows addObjectsFromArray:c];
247 masaki 124 }
248    
249 masaki 126 c = [self criteriaWithKeyPath:@"rating"];
250     if(c) [newRows addObjectsFromArray:c];
251 masaki 124
252     rows = [newRows retain];
253 masaki 125
254    
255 masaki 143
256 masaki 125 ////
257     predicateRows = [[NSMutableArray alloc] init];
258     [ruleEditor bind:XspfMREDRowsKey toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil];
259 masaki 120 }
260    
261 masaki 127 - (NSArray *)displayValuesWithPredicate:(NSComparisonPredicate *)predicate
262     {
263     id value02 = nil; id value03 = nil;
264     id leftKeyPath = [[predicate leftExpression] keyPath];
265    
266     switch([predicate predicateOperatorType]) {
267     case NSEqualToPredicateOperatorType:
268     value02 = XspfMStringPredicateIsEqualOperator;
269     break;
270     case NSNotEqualToPredicateOperatorType:
271     value02 = XspfMStringPredicateIsNotEqualOperator;
272     break;
273     case NSContainsPredicateOperatorType:
274     value02 = XspfMStringPredicateContainsOperator;
275     break;
276     case NSBeginsWithPredicateOperatorType:
277     value02 = XspfMStringPredicateBeginsWithOperator;
278     break;
279     case NSEndsWithPredicateOperatorType:
280     value02 = XspfMStringPredicateEndsWithOperator;
281     break;
282     }
283     id rightConstant = [[predicate rightExpression] constantValue];
284     value03 = [self textField];
285     [value03 setObjectValue:rightConstant];
286    
287     id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil];
288    
289     return disp;
290     }
291     - (NSArray *)ratingDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate
292     {
293     id value02 = nil; id value03 = nil;
294     id leftKeyPath = [[predicate leftExpression] keyPath];
295    
296     switch([predicate predicateOperatorType]) {
297     case NSEqualToPredicateOperatorType:
298     value02 = XspfMStringPredicateIsEqualOperator;
299     break;
300     case NSGreaterThanPredicateOperatorType:
301     value02 = @"is greater than";
302     break;
303     case NSLessThanPredicateOperatorType:
304     value02 = @"is less than";
305     break;
306     }
307     id rightConstant = [[predicate rightExpression] constantValue];
308     value03 = [self ratingIndicator];
309     [value03 setObjectValue:rightConstant];
310    
311     id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil];
312 masaki 143
313 masaki 127 return disp;
314     }
315 masaki 138 - (void)resolveVariable:(NSString *)variable value02:(id *)value02 value03:(id *)value03 value04:(id *)value04 value05:(id *)value05 value06:(id *)value06 value07:(id *)value07
316     {
317     NSArray *words = [variable componentsSeparatedByString:@"-"];
318    
319     switch([words count]) {
320     case 2:
321     *value02 = @"is in the last";
322     *value03 = [self numberField];
323     [*value03 setTag:2000];
324     [*value03 setStringValue:[words objectAtIndex:0]];
325     *value04 = [words objectAtIndex:1];
326     break;
327     case 3:
328     if([[words objectAtIndex:0] isEqualToString:@"not"]) {
329     *value02 = @"is not in the last";
330     *value03 = [self numberField];
331     [*value03 setTag:2000];
332     [*value03 setStringValue:[words objectAtIndex:1]];
333     *value04 = [words objectAtIndex:2];
334     } else {
335     *value02 = @"is exactly";
336     *value03 = [self numberField];
337     [*value03 setTag:2000];
338     [*value03 setStringValue:[words objectAtIndex:0]];
339     *value04 = [words objectAtIndex:1];
340     *value05 = @"ago";
341     }
342     break;
343     case 4:
344     *value02 = @"is between";
345     *value03 = [self numberField];
346     [*value03 setTag:2000];
347     [*value03 setStringValue:[words objectAtIndex:0]];
348     *value04 = @"and";
349     *value05 = [self numberField];
350     [*value05 setTag:2100];
351     [*value05 setStringValue:[words objectAtIndex:2]];
352     *value06 = [words objectAtIndex:3];
353     *value07 = @"ago";
354     break;
355     }
356     }
357 masaki 146 - (void)rangeDateDisplayValuesWithExpression:(NSExpression *)rightExp value02:(id *)value02 value03:(id *)value03 value04:(id *)value04 value05:(id *)value05
358     {
359     NSExpression *firstExp = [[rightExp collection] objectAtIndex:0];
360     NSExpression *secondExp = [[rightExp collection] objectAtIndex:1];
361    
362     *value02 = @"is in the range";
363     *value03 = [self datePicker];
364     [*value03 setObjectValue:[firstExp constantValue]];
365     *value04 = @"to";
366     *value05 = [self datePicker];
367     [*value05 setObjectValue:[secondExp constantValue]];
368     [*value05 setTag:1000];
369     }
370 masaki 138
371 masaki 132 - (NSArray *)dateRangeDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate
372     {
373     id leftKeyPath = [[predicate leftExpression] keyPath];
374 masaki 146 id rightVar = nil;
375     if([[predicate rightExpression] expressionType] == NSVariableExpressionType) {
376     rightVar = [[predicate rightExpression] variable];
377     }
378 masaki 132
379     id value02 = nil;
380     id value03 = nil;
381     id value04 = nil;
382     id value05 = nil;
383     id value06 = nil;
384     id value07 = nil;
385    
386 masaki 146 if(!rightVar) {
387     [self rangeDateDisplayValuesWithExpression:[predicate rightExpression] value02:&value02 value03:&value03 value04:&value04 value05:&value05];
388     }
389    
390 masaki 132 if([rightVar isEqualToString:@"TODAY"]) {
391     value02 = @"is today";
392     } else if([rightVar isEqualToString:@"YESTERDAY"]) {
393     value02 = @"is yesterday";
394     } else if([rightVar isEqualToString:@"THISWEEK"]) {
395     value02 = @"is this week";
396     } else if([rightVar isEqualToString:@"LASTWEEK"]) {
397     value02 = @"is last week";
398 masaki 138 } else {
399     [self resolveVariable:rightVar value02:&value02 value03:&value03 value04:&value04 value05:&value05 value06:&value06 value07:&value07];
400 masaki 132 }
401    
402     return [NSArray arrayWithObjects:leftKeyPath, value02, value03, value04, value05, value06, value07, nil];
403     }
404    
405 masaki 127 - (NSArray *)dateDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate
406     {
407     id value02 = nil; id value03 = nil;
408     id leftKeyPath = [[predicate leftExpression] keyPath];
409    
410     switch([predicate predicateOperatorType]) {
411     case NSEqualToPredicateOperatorType:
412     value02 = @"is the date";
413     break;
414     case NSGreaterThanPredicateOperatorType:
415     value02 = @"is after the date";
416     break;
417     case NSLessThanPredicateOperatorType:
418     value02 = @"is before the date";
419     break;
420     case NSBetweenPredicateOperatorType:
421     return [self dateRangeDisplayValuesWithPredicate:predicate];
422    
423     }
424     id rightConstant = [[predicate rightExpression] constantValue];
425     value03 = [self datePicker];
426     [value03 setObjectValue:rightConstant];
427    
428     id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil];
429    
430     return disp;
431     }
432    
433 masaki 143 - (id)criterionFromCriteria:(id)criteria withDisplayValues:(NSArray *)displayValues
434     {
435     NSMutableArray *result = [NSMutableArray array];
436    
437     NSInteger index = 0;
438    
439     do {
440     id displayValue = [displayValues objectAtIndex:index];
441     id hitCriterion = nil;
442     for(id criterion in criteria) {
443     id value = [criterion valueForKey:@"value"];
444     if([value isEqualToString:displayValue]) {
445     hitCriterion = criterion;
446     break;
447     }
448    
449     if(![displayValue isKindOfClass:[NSControl class]]) continue;
450    
451     Class fieldClass = Nil;
452     NSInteger tag = 0;
453     if([value hasPrefix:@"textField"]) {
454     fieldClass = [NSTextField class];
455     } else if([value hasPrefix:@"dateField"]) {
456     fieldClass = [NSDatePicker class];
457     if(![value isEqualToString:@"dateField"]) { // result == dateField02
458     tag = 1000;
459     }
460     } else if([value hasPrefix:@"rateField"]) {
461     fieldClass = [NSLevelIndicator class];
462     } else if([value hasPrefix:@"numberField"]) {
463     fieldClass = [NSTextField class];
464     if([value isEqualToString:@"numberField"]) {
465     tag = 2000;
466     } else { // result == numberField02
467     tag = 2100;
468     }
469     }
470     if(!fieldClass) continue;
471    
472     if([displayValue isKindOfClass:fieldClass] && [displayValue tag] == tag) {
473     hitCriterion = criterion;
474     break;
475     }
476     }
477    
478     if(hitCriterion) {
479     [result addObject:hitCriterion];
480     }
481    
482     criteria = [hitCriterion valueForKey:@"criteria"];
483     index++;
484     } while(criteria);
485    
486     return result;
487     }
488    
489 masaki 125 - (id)buildRowsFromPredicate:(id)predicate
490     {
491     if([predicate isKindOfClass:[NSCompoundPredicate class]]) {
492     id subrows = [NSMutableArray array];
493    
494     id value = nil;
495     switch([predicate compoundPredicateType]) {
496     case NSNotPredicateType:
497     // ?
498     break;
499     case NSAndPredicateType:
500     value = @"All";
501     break;
502     case NSOrPredicateType:
503     value = @"Any";
504     break;
505     }
506    
507     NSArray *sub = [predicate subpredicates];
508     for(id p in sub) {
509     [subrows addObject:[self buildRowsFromPredicate:p]];
510     }
511    
512 masaki 143 id criteria = [NSArray arrayWithObjects:value, @"of the following are true", nil];
513 masaki 127 id type = [NSNumber numberWithInt:NSRuleEditorRowTypeCompound];
514 masaki 125
515     id result = [NSMutableDictionary dictionaryWithObjectsAndKeys:
516     criteria, XspfMREDCriteriaKey,
517 masaki 143 criteria, XspfMREDDisplayValuesKey,
518 masaki 125 type, XspfMREDRowTypeKey,
519     subrows, XspfMREDSubrowsKey,
520     nil];
521    
522     return result;
523     } else if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
524     id leftKeyPath = [[predicate leftExpression] keyPath];
525     if(!leftKeyPath) return [NSArray array];
526 masaki 128
527     NSArray *disp = nil;
528 masaki 127 if([leftKeyPath isEqualToString:@"title"]) {
529 masaki 128 disp = [self displayValuesWithPredicate:predicate];
530 masaki 125 }
531 masaki 127 if([leftKeyPath isEqualToString:@"rating"]) {
532 masaki 128 disp = [self ratingDisplayValuesWithPredicate:predicate];
533 masaki 127 }
534 masaki 132 if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:leftKeyPath]) {
535 masaki 128 disp = [self dateDisplayValuesWithPredicate:predicate];
536 masaki 138 NSLog(@"dispalyValues -> %@", disp);
537 masaki 128 }
538    
539     if(disp) {
540 masaki 127 NSArray *row = [self criteriaWithKeyPath:leftKeyPath];
541 masaki 143 id c = [self criterionFromCriteria:row withDisplayValues:disp];
542 masaki 127 NSMutableDictionary *criterion = [NSMutableDictionary dictionaryWithObjectsAndKeys:
543 masaki 143 c, XspfMREDCriteriaKey,
544 masaki 127 disp, XspfMREDDisplayValuesKey,
545     [NSNumber numberWithInt:NSRuleEditorRowTypeSimple], XspfMREDRowTypeKey,
546     nil];
547     return criterion;
548     }
549 masaki 125
550     } else if([predicate isKindOfClass:[NSPredicate class]]) {
551     NSLog(@"--> %@", predicate);
552     } else {
553     NSLog(@"???predicate class is %@", NSStringFromClass([predicate class]));
554     }
555    
556     return [NSArray array];
557     }
558 masaki 123 - (void)setPredicate:(id)predicate
559     {
560 masaki 126 NSLog(@"predicate -> (%@) %@", NSStringFromClass([predicate class]), predicate);
561 masaki 123
562 masaki 125 id hoge = [self buildRowsFromPredicate:predicate];
563     id new = [NSArray arrayWithObject:hoge];
564    
565     [self willChangeValueForKey:XspfMREDPredicateRowsKey];
566     [predicateRows release];
567     predicateRows = [new retain];
568     [self didChangeValueForKey:XspfMREDPredicateRowsKey];
569 masaki 146 // [ruleEditor reloadCriteria];
570 masaki 123 }
571 masaki 125 - (void)setPredicateRows:(id)p
572     {
573     NSLog(@"new -> %@", p);
574     [predicateRows release];
575     predicateRows = [p retain];
576     }
577    
578     #pragma mark#### NSRleEditor Delegate ####
579 masaki 124 - (NSInteger)ruleEditor:(NSRuleEditor *)editor
580     numberOfChildrenForCriterion:(id)criterion
581     withRowType:(NSRuleEditorRowType)rowType
582     {
583     NSInteger result = 0;
584    
585 masaki 143 if(rowType == NSRuleEditorRowTypeCompound) {
586     return [compound numberOfChildrenForChild:criterion];
587     }
588    
589 masaki 124 if(!criterion) {
590 masaki 143 result = [rows count];
591     } else {
592     result = [[criterion valueForKey:XspfMREDCriteriaKey] count];
593 masaki 124 }
594    
595     // NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result);
596    
597     return result;
598     }
599    
600     - (id)ruleEditor:(NSRuleEditor *)editor
601     child:(NSInteger)index
602     forCriterion:(id)criterion
603     withRowType:(NSRuleEditorRowType)rowType
604     {
605     id result = nil;
606    
607 masaki 143 if(rowType == NSRuleEditorRowTypeCompound) {
608     return [compound childForChild:criterion atIndex:index];
609     }
610    
611 masaki 124 if(!criterion) {
612 masaki 143 result = [rows objectAtIndex:index];
613     } else {
614     result = [[criterion valueForKey:XspfMREDCriteriaKey] objectAtIndex:index];
615 masaki 124 }
616    
617     // NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result);
618    
619     return result;
620     }
621     - (id)ruleEditor:(NSRuleEditor *)editor
622     displayValueForCriterion:(id)criterion
623     inRow:(NSInteger)row
624     {
625     id result = nil;
626 masaki 143
627     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
628     if(rowType == NSRuleEditorRowTypeCompound) {
629     return [compound displayValueForChild:criterion];
630     }
631    
632     if(!criterion) {
633     //
634     } else {
635     result = [criterion valueForKey:XspfMREDValueKey];
636     }
637    
638     if([result isEqualToString:@"separator"]) {
639     return [NSMenuItem separatorItem];
640     }
641    
642     // create or find field object.
643     do {
644     Class searchClass = Nil;
645     SEL defaultSEL = Nil;
646     NSInteger tag = 0;
647    
648     if([result hasPrefix:@"textField"]) {
649     searchClass = [NSTextField class];
650     defaultSEL = @selector(textField);
651     } else if([result hasPrefix:@"dateField"]) {
652     searchClass = [NSDatePicker class];
653     defaultSEL = @selector(datePicker);
654     if(![result isEqualToString:@"dateField"]) { // result == dateField02
655     tag = 1000;
656     }
657     } else if([result hasPrefix:@"rateField"]) {
658     searchClass = [NSLevelIndicator class];
659     defaultSEL = @selector(ratingIndicator);
660     } else if([result hasPrefix:@"numberField"]) {
661     searchClass = [NSTextField class];
662     defaultSEL = @selector(numberField);
663     if([result isEqualToString:@"numberField"]) {
664     tag = 2000;
665     } else { // result == numberField02
666     tag = 2100;
667     }
668     }
669     if(!searchClass) break;
670    
671     id displayValues = [editor displayValuesForRow:row];
672     id field = nil;
673     for(id v in displayValues) {
674     if([v isKindOfClass:searchClass] && [v tag] == tag) {
675     field = v;
676     }
677     }
678     result = field ? field :[self performSelector:defaultSEL];
679     if(tag != 0) [result setTag:tag];
680     } while(NO);
681    
682 masaki 124 // NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result);
683    
684     return result;
685     }
686     - (NSDictionary *)ruleEditor:(NSRuleEditor *)editor
687     predicatePartsForCriterion:(id)criterion
688     withDisplayValue:(id)displayValue
689     inRow:(NSInteger)row
690     {
691     id result = nil;
692    
693 masaki 143 NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
694     if(rowType == NSRuleEditorRowTypeCompound) {
695     return [compound predicateForChild:criterion withDisplayValue:displayValue];
696     }
697    
698     if([criterion valueForKey:@"XspfMIgnoreExpression"]) return nil;
699    
700     result = [NSMutableDictionary dictionary];
701    
702     if([criterion valueForKey:@"NSRuleEditorPredicateOperatorType"]) {
703     [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOperatorType"] forKey:@"NSRuleEditorPredicateOperatorType"];
704     }
705     if([criterion valueForKey:@"NSRuleEditorPredicateOptions"]) {
706     [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOptions"] forKey:@"NSRuleEditorPredicateOptions"];
707     }
708     if([criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"]) {
709     id value = [criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"];
710     id exp = nil;
711     if([value isEqual:XspfMREDValueKey]) {
712     exp = [NSExpression expressionForKeyPath:displayValue];
713     } else {
714     exp = [NSExpression expressionForKeyPath:[criterion valueForKey:@"NSRuleEditorPredicateLeft"]];
715     }
716     if(exp) {
717     [result setValue:exp forKey:@"NSRuleEditorPredicateLeftExpression"];
718     }
719     }
720     if([criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]) {
721     id selector = [criterion valueForKey:@"NSRuleEditorPredicateRightExpression"];
722     id exp = nil;
723     if(NSSelectorFromString(selector)) {
724     exp = [NSExpression expressionForConstantValue:[displayValue performSelector:NSSelectorFromString(selector)]];
725     } else {
726     exp = [NSExpression expressionForConstantValue:[criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]];
727     }
728     if(exp) {
729     [result setValue:exp forKey:@"NSRuleEditorPredicateRightExpression"];
730     }
731     }
732     if([criterion valueForKey:@"XspfMPredicateRightExpression"]) {
733     SEL selector = NSSelectorFromString([criterion valueForKey:@"XspfMPredicateRightExpression"]);
734     id arg01 = [criterion valueForKey:@"XspfMRightExpressionArg01"];
735     id arg02 = [criterion valueForKey:@"XspfMRightExpressionArg02"];
736    
737    
738     if(arg02 && arg01) {
739     if([arg01 isEqual:XspfMREDDisplayValuesKey]) {
740     arg01 = [editor displayValuesForRow:row];
741     }
742     if([arg02 isEqual:XspfMREDDisplayValuesKey]) {
743     arg02 = [editor displayValuesForRow:row];
744     }
745     id r = [self performSelector:selector withObject:arg01 withObject:arg02];
746     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
747     } else if(arg01) {
748     if([arg01 isEqual:XspfMREDDisplayValuesKey]) {
749     arg01 = [editor displayValuesForRow:row];
750     }
751     id r = [self performSelector:selector withObject:arg01];
752     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
753     } else {
754     id r = [self performSelector:selector];
755     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
756     }
757     }
758    
759     // NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result);
760    
761 masaki 124 return result;
762     }
763     - (void)ruleEditorRowsDidChange:(NSNotification *)notification
764     {
765     //
766     }
767    
768 masaki 120 @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26