Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


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

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