Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


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

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