Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


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

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