Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 143 - (hide annotations) (download)
Sun Dec 13 14:41:42 2009 UTC (14 years, 4 months ago) by masaki
File size: 22939 byte(s)
[???] NSRuleEditorテスト中
criterion用クラスを破棄。
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    
358 masaki 132 - (NSArray *)dateRangeDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate
359     {
360     id leftKeyPath = [[predicate leftExpression] keyPath];
361     id rightVar = [[predicate rightExpression] variable];
362    
363     id value02 = nil;
364     id value03 = nil;
365     id value04 = nil;
366     id value05 = nil;
367     id value06 = nil;
368     id value07 = nil;
369    
370     if([rightVar isEqualToString:@"TODAY"]) {
371     value02 = @"is today";
372     } else if([rightVar isEqualToString:@"YESTERDAY"]) {
373     value02 = @"is yesterday";
374     } else if([rightVar isEqualToString:@"THISWEEK"]) {
375     value02 = @"is this week";
376     } else if([rightVar isEqualToString:@"LASTWEEK"]) {
377     value02 = @"is last week";
378 masaki 138 } else {
379     [self resolveVariable:rightVar value02:&value02 value03:&value03 value04:&value04 value05:&value05 value06:&value06 value07:&value07];
380 masaki 132 }
381    
382     return [NSArray arrayWithObjects:leftKeyPath, value02, value03, value04, value05, value06, value07, nil];
383     }
384    
385 masaki 127 - (NSArray *)dateDisplayValuesWithPredicate:(NSComparisonPredicate *)predicate
386     {
387     id value02 = nil; id value03 = nil;
388     id leftKeyPath = [[predicate leftExpression] keyPath];
389    
390     switch([predicate predicateOperatorType]) {
391     case NSEqualToPredicateOperatorType:
392     value02 = @"is the date";
393     break;
394     case NSGreaterThanPredicateOperatorType:
395     value02 = @"is after the date";
396     break;
397     case NSLessThanPredicateOperatorType:
398     value02 = @"is before the date";
399     break;
400     case NSBetweenPredicateOperatorType:
401     return [self dateRangeDisplayValuesWithPredicate:predicate];
402    
403     }
404     id rightConstant = [[predicate rightExpression] constantValue];
405     value03 = [self datePicker];
406     [value03 setObjectValue:rightConstant];
407    
408     id disp = [NSArray arrayWithObjects:leftKeyPath, value02, value03, nil];
409    
410     return disp;
411     }
412    
413 masaki 143 - (id)criterionFromCriteria:(id)criteria withDisplayValues:(NSArray *)displayValues
414     {
415     NSMutableArray *result = [NSMutableArray array];
416    
417     NSInteger index = 0;
418    
419     do {
420     id displayValue = [displayValues objectAtIndex:index];
421     id hitCriterion = nil;
422     for(id criterion in criteria) {
423     id value = [criterion valueForKey:@"value"];
424     if([value isEqualToString:displayValue]) {
425     hitCriterion = criterion;
426     break;
427     }
428    
429     if(![displayValue isKindOfClass:[NSControl class]]) continue;
430    
431     Class fieldClass = Nil;
432     NSInteger tag = 0;
433     if([value hasPrefix:@"textField"]) {
434     fieldClass = [NSTextField class];
435     } else if([value hasPrefix:@"dateField"]) {
436     fieldClass = [NSDatePicker class];
437     if(![value isEqualToString:@"dateField"]) { // result == dateField02
438     tag = 1000;
439     }
440     } else if([value hasPrefix:@"rateField"]) {
441     fieldClass = [NSLevelIndicator class];
442     } else if([value hasPrefix:@"numberField"]) {
443     fieldClass = [NSTextField class];
444     if([value isEqualToString:@"numberField"]) {
445     tag = 2000;
446     } else { // result == numberField02
447     tag = 2100;
448     }
449     }
450     if(!fieldClass) continue;
451    
452     if([displayValue isKindOfClass:fieldClass] && [displayValue tag] == tag) {
453     hitCriterion = criterion;
454     break;
455     }
456     }
457    
458     if(hitCriterion) {
459     [result addObject:hitCriterion];
460     }
461    
462     criteria = [hitCriterion valueForKey:@"criteria"];
463     index++;
464     } while(criteria);
465    
466     return result;
467     }
468    
469 masaki 125 - (id)buildRowsFromPredicate:(id)predicate
470     {
471     if([predicate isKindOfClass:[NSCompoundPredicate class]]) {
472     id subrows = [NSMutableArray array];
473    
474     id value = nil;
475     switch([predicate compoundPredicateType]) {
476     case NSNotPredicateType:
477     // ?
478     break;
479     case NSAndPredicateType:
480     value = @"All";
481     break;
482     case NSOrPredicateType:
483     value = @"Any";
484     break;
485     }
486    
487     NSArray *sub = [predicate subpredicates];
488     for(id p in sub) {
489     [subrows addObject:[self buildRowsFromPredicate:p]];
490     }
491    
492 masaki 143 id criteria = [NSArray arrayWithObjects:value, @"of the following are true", nil];
493 masaki 127 id type = [NSNumber numberWithInt:NSRuleEditorRowTypeCompound];
494 masaki 125
495     id result = [NSMutableDictionary dictionaryWithObjectsAndKeys:
496     criteria, XspfMREDCriteriaKey,
497 masaki 143 criteria, XspfMREDDisplayValuesKey,
498 masaki 125 type, XspfMREDRowTypeKey,
499     subrows, XspfMREDSubrowsKey,
500     nil];
501    
502     return result;
503     } else if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
504     id leftKeyPath = [[predicate leftExpression] keyPath];
505     if(!leftKeyPath) return [NSArray array];
506 masaki 128
507     NSArray *disp = nil;
508 masaki 127 if([leftKeyPath isEqualToString:@"title"]) {
509 masaki 128 disp = [self displayValuesWithPredicate:predicate];
510 masaki 125 }
511 masaki 127 if([leftKeyPath isEqualToString:@"rating"]) {
512 masaki 128 disp = [self ratingDisplayValuesWithPredicate:predicate];
513 masaki 127 }
514 masaki 132 if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:leftKeyPath]) {
515 masaki 128 disp = [self dateDisplayValuesWithPredicate:predicate];
516 masaki 138 NSLog(@"dispalyValues -> %@", disp);
517 masaki 128 }
518    
519     if(disp) {
520 masaki 127 NSArray *row = [self criteriaWithKeyPath:leftKeyPath];
521 masaki 143 id c = [self criterionFromCriteria:row withDisplayValues:disp];
522 masaki 127 NSMutableDictionary *criterion = [NSMutableDictionary dictionaryWithObjectsAndKeys:
523 masaki 143 c, XspfMREDCriteriaKey,
524 masaki 127 disp, XspfMREDDisplayValuesKey,
525     [NSNumber numberWithInt:NSRuleEditorRowTypeSimple], XspfMREDRowTypeKey,
526     nil];
527     return criterion;
528     }
529 masaki 125
530     } else if([predicate isKindOfClass:[NSPredicate class]]) {
531     NSLog(@"--> %@", predicate);
532     } else {
533     NSLog(@"???predicate class is %@", NSStringFromClass([predicate class]));
534     }
535    
536     return [NSArray array];
537     }
538 masaki 123 - (void)setPredicate:(id)predicate
539     {
540 masaki 126 NSLog(@"predicate -> (%@) %@", NSStringFromClass([predicate class]), predicate);
541 masaki 123
542 masaki 125 id hoge = [self buildRowsFromPredicate:predicate];
543     id new = [NSArray arrayWithObject:hoge];
544    
545     [self willChangeValueForKey:XspfMREDPredicateRowsKey];
546     [predicateRows release];
547     predicateRows = [new retain];
548     [self didChangeValueForKey:XspfMREDPredicateRowsKey];
549     [ruleEditor reloadCriteria];
550 masaki 123 }
551 masaki 125 - (void)setPredicateRows:(id)p
552     {
553     NSLog(@"new -> %@", p);
554     [predicateRows release];
555     predicateRows = [p retain];
556     }
557    
558     #pragma mark#### NSRleEditor Delegate ####
559 masaki 124 - (NSInteger)ruleEditor:(NSRuleEditor *)editor
560     numberOfChildrenForCriterion:(id)criterion
561     withRowType:(NSRuleEditorRowType)rowType
562     {
563     NSInteger result = 0;
564    
565 masaki 143 if(rowType == NSRuleEditorRowTypeCompound) {
566     return [compound numberOfChildrenForChild:criterion];
567     }
568    
569 masaki 124 if(!criterion) {
570 masaki 143 result = [rows count];
571     } else {
572     result = [[criterion valueForKey:XspfMREDCriteriaKey] count];
573 masaki 124 }
574    
575     // NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result);
576    
577     return result;
578     }
579    
580     - (id)ruleEditor:(NSRuleEditor *)editor
581     child:(NSInteger)index
582     forCriterion:(id)criterion
583     withRowType:(NSRuleEditorRowType)rowType
584     {
585     id result = nil;
586    
587 masaki 143 if(rowType == NSRuleEditorRowTypeCompound) {
588     return [compound childForChild:criterion atIndex:index];
589     }
590    
591 masaki 124 if(!criterion) {
592 masaki 143 result = [rows objectAtIndex:index];
593     } else {
594     result = [[criterion valueForKey:XspfMREDCriteriaKey] objectAtIndex:index];
595 masaki 124 }
596    
597     // NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result);
598    
599     return result;
600     }
601     - (id)ruleEditor:(NSRuleEditor *)editor
602     displayValueForCriterion:(id)criterion
603     inRow:(NSInteger)row
604     {
605     id result = nil;
606 masaki 143
607     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
608     if(rowType == NSRuleEditorRowTypeCompound) {
609     return [compound displayValueForChild:criterion];
610     }
611    
612     if(!criterion) {
613     //
614     } else {
615     result = [criterion valueForKey:XspfMREDValueKey];
616     }
617    
618     if([result isEqualToString:@"separator"]) {
619     return [NSMenuItem separatorItem];
620     }
621    
622     // create or find field object.
623     do {
624     Class searchClass = Nil;
625     SEL defaultSEL = Nil;
626     NSInteger tag = 0;
627    
628     if([result hasPrefix:@"textField"]) {
629     searchClass = [NSTextField class];
630     defaultSEL = @selector(textField);
631     } else if([result hasPrefix:@"dateField"]) {
632     searchClass = [NSDatePicker class];
633     defaultSEL = @selector(datePicker);
634     if(![result isEqualToString:@"dateField"]) { // result == dateField02
635     tag = 1000;
636     }
637     } else if([result hasPrefix:@"rateField"]) {
638     searchClass = [NSLevelIndicator class];
639     defaultSEL = @selector(ratingIndicator);
640     } else if([result hasPrefix:@"numberField"]) {
641     searchClass = [NSTextField class];
642     defaultSEL = @selector(numberField);
643     if([result isEqualToString:@"numberField"]) {
644     tag = 2000;
645     } else { // result == numberField02
646     tag = 2100;
647     }
648     }
649     if(!searchClass) break;
650    
651     id displayValues = [editor displayValuesForRow:row];
652     id field = nil;
653     for(id v in displayValues) {
654     if([v isKindOfClass:searchClass] && [v tag] == tag) {
655     field = v;
656     }
657     }
658     result = field ? field :[self performSelector:defaultSEL];
659     if(tag != 0) [result setTag:tag];
660     } while(NO);
661    
662 masaki 124 // NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result);
663    
664     return result;
665     }
666     - (NSDictionary *)ruleEditor:(NSRuleEditor *)editor
667     predicatePartsForCriterion:(id)criterion
668     withDisplayValue:(id)displayValue
669     inRow:(NSInteger)row
670     {
671     id result = nil;
672    
673 masaki 143 NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
674     if(rowType == NSRuleEditorRowTypeCompound) {
675     return [compound predicateForChild:criterion withDisplayValue:displayValue];
676     }
677    
678     if([criterion valueForKey:@"XspfMIgnoreExpression"]) return nil;
679    
680     result = [NSMutableDictionary dictionary];
681    
682     if([criterion valueForKey:@"NSRuleEditorPredicateOperatorType"]) {
683     [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOperatorType"] forKey:@"NSRuleEditorPredicateOperatorType"];
684     }
685     if([criterion valueForKey:@"NSRuleEditorPredicateOptions"]) {
686     [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOptions"] forKey:@"NSRuleEditorPredicateOptions"];
687     }
688     if([criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"]) {
689     id value = [criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"];
690     id exp = nil;
691     if([value isEqual:XspfMREDValueKey]) {
692     exp = [NSExpression expressionForKeyPath:displayValue];
693     } else {
694     exp = [NSExpression expressionForKeyPath:[criterion valueForKey:@"NSRuleEditorPredicateLeft"]];
695     }
696     if(exp) {
697     [result setValue:exp forKey:@"NSRuleEditorPredicateLeftExpression"];
698     }
699     }
700     if([criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]) {
701     id selector = [criterion valueForKey:@"NSRuleEditorPredicateRightExpression"];
702     id exp = nil;
703     if(NSSelectorFromString(selector)) {
704     exp = [NSExpression expressionForConstantValue:[displayValue performSelector:NSSelectorFromString(selector)]];
705     } else {
706     exp = [NSExpression expressionForConstantValue:[criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]];
707     }
708     if(exp) {
709     [result setValue:exp forKey:@"NSRuleEditorPredicateRightExpression"];
710     }
711     }
712     if([criterion valueForKey:@"XspfMPredicateRightExpression"]) {
713     SEL selector = NSSelectorFromString([criterion valueForKey:@"XspfMPredicateRightExpression"]);
714     id arg01 = [criterion valueForKey:@"XspfMRightExpressionArg01"];
715     id arg02 = [criterion valueForKey:@"XspfMRightExpressionArg02"];
716    
717    
718     if(arg02 && arg01) {
719     if([arg01 isEqual:XspfMREDDisplayValuesKey]) {
720     arg01 = [editor displayValuesForRow:row];
721     }
722     if([arg02 isEqual:XspfMREDDisplayValuesKey]) {
723     arg02 = [editor displayValuesForRow:row];
724     }
725     id r = [self performSelector:selector withObject:arg01 withObject:arg02];
726     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
727     } else if(arg01) {
728     if([arg01 isEqual:XspfMREDDisplayValuesKey]) {
729     arg01 = [editor displayValuesForRow:row];
730     }
731     id r = [self performSelector:selector withObject:arg01];
732     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
733     } else {
734     id r = [self performSelector:selector];
735     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
736     }
737     }
738    
739     // NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result);
740    
741 masaki 124 return result;
742     }
743     - (void)ruleEditorRowsDidChange:(NSNotification *)notification
744     {
745     //
746     }
747    
748 masaki 120 @end

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