Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 125 - (hide annotations) (download)
Sun Dec 6 04:09:28 2009 UTC (14 years, 4 months ago) by masaki
File size: 17325 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 - (id)uniqueObject
42     {
43     id object = nil;
44     unsigned i = 0;
45     do {
46     object = [NSNumber numberWithUnsignedInt:i++];
47     } while([rowIDs containsObject:object]);
48    
49     return object;
50     }
51     - (NSView *)fieldForName:(NSString *)name inRow:(NSInteger)row
52     {
53     // throw exception.
54     id rowID = [rowIDs objectAtIndex:row];
55    
56     id fields= [rowFields objectForKey:rowID];
57     if(!fields) return nil;
58    
59     return [fields objectForKey:name];
60     }
61     - (void)setField:(NSView *)field forName:(NSString *)name inRow:(NSInteger)row
62     {
63     id rowID = nil;
64     if([rowID count] < row) {
65     rowID = [self uniqueObject];
66     } else {
67     rowID = [rowIDs objectAtIndex:row];
68     }
69     id fields = [rowFields objectForKey:rowID];
70     if(!fields) {
71     fields = [NSMutableDictionary dictionary];
72     }
73     [fields setObject:field forKey:name];
74     }
75 masaki 120
76 masaki 124 - (NSInteger)tagAForType:(UInt16)type inRow:(UInt16)row
77     {
78     return type + row << 16;
79     }
80     - (UInt16) typeForTag:(NSInteger)tag
81     {
82     return 0x00FF & tag;
83     }
84     - (UInt16)rowForTag:(NSInteger)tag
85     {
86     return tag >> 16;
87     }
88     - (NSExpression *)rangeDateFromDisplayValues:(NSArray *)displayValues
89     {
90     id field01 = nil;
91     id field02 = nil;
92    
93     Class datepickerclass = [NSDatePicker class];
94     for(id v in displayValues) {
95     if([v isKindOfClass:datepickerclass]) {
96     if([v tag] == 0) {
97     field01 = v;
98     } else {
99     field02 = v;
100     }
101     }
102     }
103    
104     if(!field01 || !field02) return nil;
105    
106     id value01, value02;
107     value01 = [field01 dateValue]; value02 = [field02 dateValue];
108     if([value01 compare:value02] == NSOrderedDescending) {
109     id t = value02;
110     value02 = value01;
111     value01 = t;
112     }
113    
114     id expression01, expression02;
115     expression01 = [NSExpression expressionForConstantValue:[field01 dateValue]];
116     expression02 = [NSExpression expressionForConstantValue:[field02 dateValue]];
117    
118     return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]];
119     }
120    
121     - (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     [text setStringValue:@"0123456"];
127     [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     [self doesNotRecognizeSelector:_cmd];
163     return nil;
164     }
165    
166     - (NSArray *)buildRowsFromTemplate:(NSArray *)template
167     {
168     NSMutableArray *result = [NSMutableArray array];
169     // for(id row in template) {
170     // NSMutableDictionary *dict = [NSMutableDictionary dictionary];
171     //
172     // id criteria = [row valueForKey:@"criteria"];
173     // if(criteria && ![criteria isEqual:[NSNull null]]) {
174     // criteria = [self buildRowsFromTemplate:criteria];
175     // }
176     // [dict setValue:criteria forKey:@"criteria"];
177     //
178 masaki 125 // id value = [row valueForKey:XspfMREDValueKey];
179 masaki 124 // if(value) {
180 masaki 125 // [dict setValue:value forKey:XspfMREDValueKey];
181 masaki 124 // }
182     //
183     // [result addObject:dict];
184     // }
185     return template;
186    
187     return result;
188     }
189     - (NSDictionary *)buildRows:(NSArray *)template
190     {
191     NSMutableDictionary *result = [NSMutableDictionary dictionary];
192     for(id row in template) {
193 masaki 125 id criteria = [row valueForKey:XspfMREDCriteriaKey];
194 masaki 124 // if(criteria) {
195     // criteria = [self buildRowsFromTemplate:criteria];
196     // }
197 masaki 125 id name = [row valueForKey:XspfMREDNameKey];
198 masaki 124 [result setObject:criteria forKey:name];
199     }
200    
201     return result;
202     }
203 masaki 120 - (void)awakeFromNib
204     {
205     if(!compound) {
206     compound = [[XspfMCompound alloc] init];
207     }
208 masaki 124 // if(!simples) {
209     // simples = [NSMutableArray array];
210     // [simples retain];
211     //
212     // XspfMStringPredicate *pre;
213     // pre = [XspfMStringPredicate simpleWithKeyPath:@"title" rightType:0 operator:0];
214     // [simples addObject:pre];
215     // pre = [XspfMAbsoluteDatePredicate simpleWithKeyPath:@"lastPlayDate" rightType:0 operator:0];
216     // [simples addObject:pre];
217     // pre = [XspfMAbsoluteDatePredicate simpleWithKeyPath:@"modificationDate" rightType:0 operator:0];
218     // [simples addObject:pre];
219     // pre = [XspfMAbsoluteDatePredicate simpleWithKeyPath:@"creationDate" rightType:0 operator:0];
220     // [simples addObject:pre];
221     // }
222    
223     rowIDs = [[NSMutableArray array] retain];
224     rowFields = [[NSMutableDictionary dictionary] retain];
225    
226     NSBundle *m = [NSBundle mainBundle];
227     NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"];
228     NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path];
229     if(!rowsTemplate) {
230     exit(12345);
231 masaki 120 }
232 masaki 124
233     rowTemplate = [[self buildRows:rowsTemplate] retain];
234    
235     // NSLog(@"rowTemplate =>\n%@", rowTemplate);
236    
237     NSMutableArray *newRows = [NSMutableArray array];
238    
239     id row = [rowTemplate valueForKey:@"String"];
240     row = [row mutableCopy];
241     id c = [row objectAtIndex:0];
242 masaki 125 [c setValue:@"title" forKey:XspfMREDValueKey];
243 masaki 124 [newRows addObject:c];
244     [row release];
245    
246     row = [rowTemplate valueForKey:@"AbDate"];
247     row = [row mutableCopy];
248     c = [row objectAtIndex:0];
249     for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) {
250     c = [c mutableCopy];
251 masaki 125 [c setValue:keyPath forKey:XspfMREDValueKey];
252 masaki 124 [newRows addObject:c];
253     [c release];
254     }
255     [row release];
256    
257     row = [rowTemplate valueForKey:@"Rate"];
258     row = [row mutableCopy];
259     c = [row objectAtIndex:0];
260 masaki 125 [c setValue:@"rating" forKey:XspfMREDValueKey];
261 masaki 124 [newRows addObject:c];
262     [row release];
263    
264     rows = [newRows retain];
265 masaki 125
266    
267    
268     ////
269     predicateRows = [[NSMutableArray alloc] init];
270     [ruleEditor bind:XspfMREDRowsKey toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil];
271 masaki 120 }
272    
273 masaki 123 - (void)showPredicate:(id)predicate
274     {
275     if([predicate isKindOfClass:[NSCompoundPredicate class]]) {
276     NSArray *sub = [predicate subpredicates];
277     NSLog(@"-> %d(%d)\n|\n-->",[predicate compoundPredicateType], [sub count]);
278     for(id p in sub) {
279 masaki 124 [self showPredicate:p];
280 masaki 123 }
281 masaki 124 } else if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
282     NSLog(@"--> (Comparision) ope->%d, mod->%d, left->%@, right->%@, SEL->%s, opt->%u",
283     [predicate predicateOperatorType], [predicate comparisonPredicateModifier],
284     [predicate leftExpression], [predicate rightExpression],
285     [predicate customSelector], [predicate options]);
286 masaki 125
287 masaki 124 } else if([predicate isKindOfClass:[NSPredicate class]]) {
288     NSLog(@"--> %@", predicate);
289 masaki 123 } else {
290     NSLog(@"???predicate class is %@", NSStringFromClass([predicate class]));
291     }
292     }
293 masaki 125 - (id)buildRowsFromPredicate:(id)predicate
294     {
295     if([predicate isKindOfClass:[NSCompoundPredicate class]]) {
296     id subrows = [NSMutableArray array];
297    
298     id value = nil;
299     switch([predicate compoundPredicateType]) {
300     case NSNotPredicateType:
301     // ?
302     break;
303     case NSAndPredicateType:
304     value = @"All";
305     break;
306     case NSOrPredicateType:
307     value = @"Any";
308     break;
309     }
310    
311     #warning MUST IMPLEMENT!!
312     NSArray *sub = [predicate subpredicates];
313     for(id p in sub) {
314     [subrows addObject:[self buildRowsFromPredicate:p]];
315     }
316    
317     id criteria = [NSArray arrayWithObjects:value, @"of the following are true", nil];
318     id type = [NSNumber numberWithInt:1];
319    
320     id result = [NSMutableDictionary dictionaryWithObjectsAndKeys:
321     criteria, XspfMREDCriteriaKey,
322     criteria, XspfMREDDisplayValuesKey,
323     type, XspfMREDRowTypeKey,
324     subrows, XspfMREDSubrowsKey,
325     nil];
326    
327     return result;
328     } else if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
329     id leftKeyPath = [[predicate leftExpression] keyPath];
330     if(!leftKeyPath) return [NSArray array];
331     if(![leftKeyPath isEqualToString:@"title"]) return [NSArray array];
332    
333     id value02 = nil; id value03 = nil; id criteria01;
334    
335     switch([predicate predicateOperatorType]) {
336     case NSEqualToPredicateOperatorType:
337     value02 = XspfMStringPredicateIsEqualOperator;
338     break;
339     case NSNotEqualToPredicateOperatorType:
340     value02 = XspfMStringPredicateIsNotEqualOperator;
341     break;
342     case NSContainsPredicateOperatorType:
343     value02 = XspfMStringPredicateContainsOperator;
344     break;
345     case NSBeginsWithPredicateOperatorType:
346     value02 = XspfMStringPredicateBeginsWithOperator;
347     break;
348     case NSEndsWithPredicateOperatorType:
349     value02 = XspfMStringPredicateEndsWithOperator;
350     break;
351     }
352     id rightConstant = [[predicate rightExpression] constantValue];
353     value03 = [self textField];
354     [value03 setObjectValue:rightConstant];
355    
356     id disp = [NSArray arrayWithObjects:@"title", value02, value03, nil];
357     id type = [NSNumber numberWithInt:0];
358     // id subs = [NSArray array];
359     id row = [rowTemplate valueForKey:@"String"];
360     row = [[row mutableCopy] autorelease];
361     id c = [row objectAtIndex:0];
362     [c setValue:@"title" forKey:XspfMREDValueKey];
363    
364     criteria01 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
365     row, XspfMREDCriteriaKey,
366     disp, XspfMREDDisplayValuesKey,
367     type, XspfMREDRowTypeKey,
368     // subs, XspfMREDSubrowsKey,
369     nil];
370    
371     return criteria01;
372    
373     } else if([predicate isKindOfClass:[NSPredicate class]]) {
374     NSLog(@"--> %@", predicate);
375     } else {
376     NSLog(@"???predicate class is %@", NSStringFromClass([predicate class]));
377     }
378    
379     return [NSArray array];
380     }
381 masaki 123 - (void)setPredicate:(id)predicate
382     {
383     NSLog(@"predicate -> %@", predicate);
384     // NSLog(@"Predicate class is %@", [predicate class]);
385    
386 masaki 125 // NSLog(@"old rows -> %@", predicateRows);
387     id hoge = [self buildRowsFromPredicate:predicate];
388     id new = [NSArray arrayWithObject:hoge];
389     // NSLog(@"new rows -> %@", new);
390    
391     [self willChangeValueForKey:XspfMREDPredicateRowsKey];
392     [predicateRows release];
393     predicateRows = [new retain];
394     [self didChangeValueForKey:XspfMREDPredicateRowsKey];
395     [ruleEditor reloadCriteria];
396    
397     // [self showPredicate:predicate];
398 masaki 123 }
399 masaki 125 - (void)setPredicateRows:(id)p
400     {
401     NSLog(@"new -> %@", p);
402     [predicateRows release];
403     predicateRows = [p retain];
404     }
405    
406     #pragma mark#### NSRleEditor Delegate ####
407 masaki 124 - (NSInteger)ruleEditor:(NSRuleEditor *)editor
408     numberOfChildrenForCriterion:(id)criterion
409     withRowType:(NSRuleEditorRowType)rowType
410     {
411     NSInteger result = 0;
412    
413     if(rowType == NSRuleEditorRowTypeCompound) {
414     return [compound numberOfChildrenForChild:criterion];
415     }
416    
417     if(!criterion) {
418     result = [rows count];
419     } else {
420 masaki 125 result = [[criterion valueForKey:XspfMREDCriteriaKey] count];
421 masaki 124 }
422    
423     // NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result);
424    
425     return result;
426     }
427    
428     - (id)ruleEditor:(NSRuleEditor *)editor
429     child:(NSInteger)index
430     forCriterion:(id)criterion
431     withRowType:(NSRuleEditorRowType)rowType
432     {
433     id result = nil;
434    
435     if(rowType == NSRuleEditorRowTypeCompound) {
436     return [compound childForChild:criterion atIndex:index];
437     }
438    
439     if(!criterion) {
440     result = [rows objectAtIndex:index];
441     } else {
442 masaki 125 result = [[criterion valueForKey:XspfMREDCriteriaKey] objectAtIndex:index];
443 masaki 124 }
444    
445     // NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result);
446    
447     return result;
448     }
449     - (id)ruleEditor:(NSRuleEditor *)editor
450     displayValueForCriterion:(id)criterion
451     inRow:(NSInteger)row
452     {
453     id result = nil;
454    
455     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
456     if(rowType == NSRuleEditorRowTypeCompound) {
457     return [compound displayValueForChild:criterion];
458     }
459    
460     if(!criterion) {
461     //
462     } else {
463 masaki 125 result = [criterion valueForKey:XspfMREDValueKey];
464 masaki 124 }
465    
466     // create or find field object.
467     do {
468     Class searchClass = Nil;
469     SEL defaultSEL = Nil;
470     NSInteger tag = 0;
471    
472 masaki 125 if([result hasPrefix:@"textField"]) {
473 masaki 124 searchClass = [NSTextField class];
474     defaultSEL = @selector(textField);
475     } else if([result hasPrefix:@"dateField"]) {
476     searchClass = [NSDatePicker class];
477     defaultSEL = @selector(datePicker);
478     if(![result isEqualToString:@"dateField"]) { // result == dateField02
479     tag = 1000;
480     }
481     } else if([result hasPrefix:@"rateField"]) {
482     searchClass = [NSLevelIndicator class];
483     defaultSEL = @selector(ratingIndicator);
484     }
485     if(!searchClass) break;
486    
487     id displayValues = [editor displayValuesForRow:row];
488     id field = nil;
489     for(id v in displayValues) {
490     if([v isKindOfClass:searchClass] && [v tag] == tag) {
491     field = v;
492     }
493     }
494     result = field ? field :[self performSelector:defaultSEL];
495     if(tag != 0) [result setTag:tag];
496     } while(NO);
497    
498     // NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result);
499    
500     return result;
501     }
502     - (NSDictionary *)ruleEditor:(NSRuleEditor *)editor
503     predicatePartsForCriterion:(id)criterion
504     withDisplayValue:(id)displayValue
505     inRow:(NSInteger)row
506     {
507     id result = nil;
508    
509     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
510     if(rowType == NSRuleEditorRowTypeCompound) {
511     return [compound predicateForChild:criterion withDisplayValue:displayValue];
512     }
513    
514     result = [NSMutableDictionary dictionary];
515     if([criterion valueForKey:@"NSRuleEditorPredicateOperatorType"]) {
516     [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOperatorType"] forKey:@"NSRuleEditorPredicateOperatorType"];
517     }
518     if([criterion valueForKey:@"NSRuleEditorPredicateOptions"]) {
519     [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOptions"] forKey:@"NSRuleEditorPredicateOptions"];
520     }
521     if([criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"]) {
522     id value = [criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"];
523     id exp = nil;
524 masaki 125 if([value isEqual:XspfMREDValueKey]) {
525 masaki 124 exp = [NSExpression expressionForKeyPath:displayValue];
526     } else {
527     exp = [NSExpression expressionForKeyPath:[criterion valueForKey:@"NSRuleEditorPredicateLeft"]];
528     }
529     if(exp) {
530     [result setValue:exp forKey:@"NSRuleEditorPredicateLeftExpression"];
531     }
532     }
533     if([criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]) {
534     id selector = [criterion valueForKey:@"NSRuleEditorPredicateRightExpression"];
535     id exp = nil;
536     if(NSSelectorFromString(selector)) {
537     exp = [NSExpression expressionForConstantValue:[displayValue performSelector:NSSelectorFromString(selector)]];
538     } else {
539     exp = [NSExpression expressionForConstantValue:[criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]];
540     }
541     if(exp) {
542     [result setValue:exp forKey:@"NSRuleEditorPredicateRightExpression"];
543     }
544     }
545     if([criterion valueForKey:@"XspfMPredicateRightExpression"]) {
546     SEL selector = NSSelectorFromString([criterion valueForKey:@"XspfMPredicateRightExpression"]);
547     id arg01 = [criterion valueForKey:@"XspfMRightExpressionArg01"];
548     // id arg02 = [criterion valueForKey:@"XspfMRightExpressionArg02"];
549    
550     if(arg01) {
551 masaki 125 if([arg01 isEqualToString:XspfMREDDisplayValuesKey]) {
552 masaki 124 arg01 = [editor displayValuesForRow:row];
553     }
554     id r = [self performSelector:selector withObject:arg01];
555     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
556     } else {
557     id r = [self performSelector:selector];
558     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
559     }
560     }
561    
562 masaki 125 // NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result);
563 masaki 124
564     return result;
565     }
566     - (void)ruleEditorRowsDidChange:(NSNotification *)notification
567     {
568     //
569     }
570    
571 masaki 120 @end

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