Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 124 - (hide annotations) (download)
Sat Dec 5 13:49:53 2009 UTC (14 years, 4 months ago) by masaki
File size: 15037 byte(s)
[Mod] 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 124 - (id)uniqueObject
15     {
16     id object = nil;
17     unsigned i = 0;
18     do {
19     object = [NSNumber numberWithUnsignedInt:i++];
20     } while([rowIDs containsObject:object]);
21    
22     return object;
23     }
24     - (NSView *)fieldForName:(NSString *)name inRow:(NSInteger)row
25     {
26     // throw exception.
27     id rowID = [rowIDs objectAtIndex:row];
28    
29     id fields= [rowFields objectForKey:rowID];
30     if(!fields) return nil;
31    
32     return [fields objectForKey:name];
33     }
34     - (void)setField:(NSView *)field forName:(NSString *)name inRow:(NSInteger)row
35     {
36     id rowID = nil;
37     if([rowID count] < row) {
38     rowID = [self uniqueObject];
39     } else {
40     rowID = [rowIDs objectAtIndex:row];
41     }
42     id fields = [rowFields objectForKey:rowID];
43     if(!fields) {
44     fields = [NSMutableDictionary dictionary];
45     }
46     [fields setObject:field forKey:name];
47     }
48 masaki 120
49 masaki 124 - (NSInteger)tagAForType:(UInt16)type inRow:(UInt16)row
50     {
51     return type + row << 16;
52     }
53     - (UInt16) typeForTag:(NSInteger)tag
54     {
55     return 0x00FF & tag;
56     }
57     - (UInt16)rowForTag:(NSInteger)tag
58     {
59     return tag >> 16;
60     }
61     - (NSExpression *)rangeDateFromDisplayValues:(NSArray *)displayValues
62     {
63     id field01 = nil;
64     id field02 = nil;
65    
66     Class datepickerclass = [NSDatePicker class];
67     for(id v in displayValues) {
68     if([v isKindOfClass:datepickerclass]) {
69     if([v tag] == 0) {
70     field01 = v;
71     } else {
72     field02 = v;
73     }
74     }
75     }
76    
77     if(!field01 || !field02) return nil;
78    
79     id value01, value02;
80     value01 = [field01 dateValue]; value02 = [field02 dateValue];
81     if([value01 compare:value02] == NSOrderedDescending) {
82     id t = value02;
83     value02 = value01;
84     value01 = t;
85     }
86    
87     id expression01, expression02;
88     expression01 = [NSExpression expressionForConstantValue:[field01 dateValue]];
89     expression02 = [NSExpression expressionForConstantValue:[field02 dateValue]];
90    
91     return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]];
92     }
93    
94     - (NSView *)textField
95     {
96     id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
97     [[text cell] setControlSize:NSSmallControlSize];
98     [text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
99     [text setStringValue:@"0123456"];
100     [text sizeToFit];
101     [text setStringValue:@""];
102     [text setDelegate:self];
103    
104     return text;
105     }
106     - (NSView *)datePicker
107     {
108     id date = [[[NSDatePicker alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
109     [[date cell] setControlSize:NSSmallControlSize];
110     [date setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
111     [date setDatePickerElements:NSYearMonthDayDatePickerElementFlag];
112     [date setDrawsBackground:YES];
113     [date setDateValue:[NSDate dateWithTimeIntervalSinceNow:0.0]];
114     [date sizeToFit];
115     [date setDelegate:self];
116    
117     return date;
118     }
119     - (NSView *)ratingIndicator
120     {
121     id rate = [[[NSLevelIndicator alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
122     id cell = [rate cell];
123     [cell setControlSize:NSSmallControlSize];
124     [rate setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
125     [rate setMinValue:0];
126     [rate setMaxValue:5];
127     [cell setLevelIndicatorStyle:NSRatingLevelIndicatorStyle];
128     [cell setEditable:YES];
129     [rate sizeToFit];
130    
131     return rate;
132     }
133     - (NSView *)numberField
134     {
135     [self doesNotRecognizeSelector:_cmd];
136     return nil;
137     }
138    
139     - (NSArray *)buildRowsFromTemplate:(NSArray *)template
140     {
141     NSMutableArray *result = [NSMutableArray array];
142     // for(id row in template) {
143     // NSMutableDictionary *dict = [NSMutableDictionary dictionary];
144     //
145     // id criteria = [row valueForKey:@"criteria"];
146     // if(criteria && ![criteria isEqual:[NSNull null]]) {
147     // criteria = [self buildRowsFromTemplate:criteria];
148     // }
149     // [dict setValue:criteria forKey:@"criteria"];
150     //
151     // id value = [row valueForKey:@"value"];
152     // if(value) {
153     // [dict setValue:value forKey:@"value"];
154     // }
155     //
156     // [result addObject:dict];
157     // }
158     return template;
159    
160     return result;
161     }
162     - (NSDictionary *)buildRows:(NSArray *)template
163     {
164     NSMutableDictionary *result = [NSMutableDictionary dictionary];
165     for(id row in template) {
166     id criteria = [row valueForKey:@"criteria"];
167     // if(criteria) {
168     // criteria = [self buildRowsFromTemplate:criteria];
169     // }
170     id name = [row valueForKey:@"name"];
171     [result setObject:criteria forKey:name];
172     }
173    
174     return result;
175     }
176 masaki 120 - (void)awakeFromNib
177     {
178     if(!compound) {
179     compound = [[XspfMCompound alloc] init];
180     }
181 masaki 124 // if(!simples) {
182     // simples = [NSMutableArray array];
183     // [simples retain];
184     //
185     // XspfMStringPredicate *pre;
186     // pre = [XspfMStringPredicate simpleWithKeyPath:@"title" rightType:0 operator:0];
187     // [simples addObject:pre];
188     // pre = [XspfMAbsoluteDatePredicate simpleWithKeyPath:@"lastPlayDate" rightType:0 operator:0];
189     // [simples addObject:pre];
190     // pre = [XspfMAbsoluteDatePredicate simpleWithKeyPath:@"modificationDate" rightType:0 operator:0];
191     // [simples addObject:pre];
192     // pre = [XspfMAbsoluteDatePredicate simpleWithKeyPath:@"creationDate" rightType:0 operator:0];
193     // [simples addObject:pre];
194     // }
195    
196     rowIDs = [[NSMutableArray array] retain];
197     rowFields = [[NSMutableDictionary dictionary] retain];
198    
199     NSBundle *m = [NSBundle mainBundle];
200     NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"];
201     NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path];
202     if(!rowsTemplate) {
203     exit(12345);
204 masaki 120 }
205 masaki 124
206     rowTemplate = [[self buildRows:rowsTemplate] retain];
207    
208     // NSLog(@"rowTemplate =>\n%@", rowTemplate);
209    
210     NSMutableArray *newRows = [NSMutableArray array];
211    
212     id row = [rowTemplate valueForKey:@"String"];
213     row = [row mutableCopy];
214     id c = [row objectAtIndex:0];
215     [c setValue:@"title" forKey:@"value"];
216     [newRows addObject:c];
217     [row release];
218    
219     row = [rowTemplate valueForKey:@"AbDate"];
220     row = [row mutableCopy];
221     c = [row objectAtIndex:0];
222     for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) {
223     c = [c mutableCopy];
224     [c setValue:keyPath forKey:@"value"];
225     [newRows addObject:c];
226     [c release];
227     }
228     [row release];
229    
230     row = [rowTemplate valueForKey:@"Rate"];
231     row = [row mutableCopy];
232     c = [row objectAtIndex:0];
233     [c setValue:@"rating" forKey:@"value"];
234     [newRows addObject:c];
235     [row release];
236    
237     rows = [newRows retain];
238 masaki 120 }
239    
240 masaki 123 - (void)showPredicate:(id)predicate
241     {
242     if([predicate isKindOfClass:[NSCompoundPredicate class]]) {
243     NSArray *sub = [predicate subpredicates];
244     NSLog(@"-> %d(%d)\n|\n-->",[predicate compoundPredicateType], [sub count]);
245     for(id p in sub) {
246 masaki 124 [self showPredicate:p];
247 masaki 123 }
248 masaki 124 } else if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
249     NSLog(@"--> (Comparision) ope->%d, mod->%d, left->%@, right->%@, SEL->%s, opt->%u",
250     [predicate predicateOperatorType], [predicate comparisonPredicateModifier],
251     [predicate leftExpression], [predicate rightExpression],
252     [predicate customSelector], [predicate options]);
253     } else if([predicate isKindOfClass:[NSPredicate class]]) {
254     NSLog(@"--> %@", predicate);
255 masaki 123 } else {
256     NSLog(@"???predicate class is %@", NSStringFromClass([predicate class]));
257     }
258     }
259     - (void)setPredicate:(id)predicate
260     {
261     NSLog(@"predicate -> %@", predicate);
262     // NSLog(@"Predicate class is %@", [predicate class]);
263    
264     [self showPredicate:predicate];
265     }
266 masaki 124 #if 1
267     - (NSInteger)ruleEditor:(NSRuleEditor *)editor
268     numberOfChildrenForCriterion:(id)criterion
269     withRowType:(NSRuleEditorRowType)rowType
270     {
271     NSInteger result = 0;
272    
273     if(rowType == NSRuleEditorRowTypeCompound) {
274     return [compound numberOfChildrenForChild:criterion];
275     }
276    
277     if(!criterion) {
278     result = [rows count];
279     } else {
280     result = [[criterion valueForKey:@"criteria"] count];
281     }
282    
283     // NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result);
284    
285     return result;
286     }
287    
288     - (id)ruleEditor:(NSRuleEditor *)editor
289     child:(NSInteger)index
290     forCriterion:(id)criterion
291     withRowType:(NSRuleEditorRowType)rowType
292     {
293     id result = nil;
294    
295     if(rowType == NSRuleEditorRowTypeCompound) {
296     return [compound childForChild:criterion atIndex:index];
297     }
298    
299     if(!criterion) {
300     result = [rows objectAtIndex:index];
301     } else {
302     result = [[criterion valueForKey:@"criteria"] objectAtIndex:index];
303     }
304    
305     // NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result);
306    
307     return result;
308     }
309     - (id)ruleEditor:(NSRuleEditor *)editor
310     displayValueForCriterion:(id)criterion
311     inRow:(NSInteger)row
312     {
313     id result = nil;
314    
315     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
316     if(rowType == NSRuleEditorRowTypeCompound) {
317     return [compound displayValueForChild:criterion];
318     }
319    
320     if(!criterion) {
321     //
322     } else {
323     result = [criterion valueForKey:@"value"];
324     }
325    
326     // create or find field object.
327     do {
328     Class searchClass = Nil;
329     SEL defaultSEL = Nil;
330     NSInteger tag = 0;
331    
332     if([result hasPrefix:@"text"]) {
333     searchClass = [NSTextField class];
334     defaultSEL = @selector(textField);
335     } else if([result hasPrefix:@"dateField"]) {
336     searchClass = [NSDatePicker class];
337     defaultSEL = @selector(datePicker);
338     if(![result isEqualToString:@"dateField"]) { // result == dateField02
339     tag = 1000;
340     }
341     } else if([result hasPrefix:@"rateField"]) {
342     searchClass = [NSLevelIndicator class];
343     defaultSEL = @selector(ratingIndicator);
344     }
345     if(!searchClass) break;
346    
347     id displayValues = [editor displayValuesForRow:row];
348     id field = nil;
349     for(id v in displayValues) {
350     if([v isKindOfClass:searchClass] && [v tag] == tag) {
351     field = v;
352     }
353     }
354     result = field ? field :[self performSelector:defaultSEL];
355     if(tag != 0) [result setTag:tag];
356     } while(NO);
357    
358     // NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result);
359    
360     return result;
361     }
362     - (NSDictionary *)ruleEditor:(NSRuleEditor *)editor
363     predicatePartsForCriterion:(id)criterion
364     withDisplayValue:(id)displayValue
365     inRow:(NSInteger)row
366     {
367     id result = nil;
368    
369     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
370     if(rowType == NSRuleEditorRowTypeCompound) {
371     return [compound predicateForChild:criterion withDisplayValue:displayValue];
372     }
373    
374     result = [NSMutableDictionary dictionary];
375     if([criterion valueForKey:@"NSRuleEditorPredicateOperatorType"]) {
376     [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOperatorType"] forKey:@"NSRuleEditorPredicateOperatorType"];
377     }
378     if([criterion valueForKey:@"NSRuleEditorPredicateOptions"]) {
379     [result setValue:[criterion valueForKey:@"NSRuleEditorPredicateOptions"] forKey:@"NSRuleEditorPredicateOptions"];
380     }
381     if([criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"]) {
382     id value = [criterion valueForKey:@"NSRuleEditorPredicateLeftExpression"];
383     id exp = nil;
384     if([value isEqual:@"value"]) {
385     exp = [NSExpression expressionForKeyPath:displayValue];
386     } else {
387     exp = [NSExpression expressionForKeyPath:[criterion valueForKey:@"NSRuleEditorPredicateLeft"]];
388     }
389     if(exp) {
390     [result setValue:exp forKey:@"NSRuleEditorPredicateLeftExpression"];
391     }
392     }
393     if([criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]) {
394     id selector = [criterion valueForKey:@"NSRuleEditorPredicateRightExpression"];
395     id exp = nil;
396     if(NSSelectorFromString(selector)) {
397     exp = [NSExpression expressionForConstantValue:[displayValue performSelector:NSSelectorFromString(selector)]];
398     } else {
399     exp = [NSExpression expressionForConstantValue:[criterion valueForKey:@"NSRuleEditorPredicateRightExpression"]];
400     }
401     if(exp) {
402     [result setValue:exp forKey:@"NSRuleEditorPredicateRightExpression"];
403     }
404     }
405     if([criterion valueForKey:@"XspfMPredicateRightExpression"]) {
406     SEL selector = NSSelectorFromString([criterion valueForKey:@"XspfMPredicateRightExpression"]);
407     id arg01 = [criterion valueForKey:@"XspfMRightExpressionArg01"];
408     // id arg02 = [criterion valueForKey:@"XspfMRightExpressionArg02"];
409    
410     if(arg01) {
411     if([arg01 isEqualToString:@"displayValues"]) {
412     arg01 = [editor displayValuesForRow:row];
413     }
414     id r = [self performSelector:selector withObject:arg01];
415     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
416     } else {
417     id r = [self performSelector:selector];
418     [result setValue:r forKey:@"NSRuleEditorPredicateRightExpression"];
419     }
420     }
421    
422     NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result);
423    
424     return result;
425     }
426     - (void)ruleEditorRowsDidChange:(NSNotification *)notification
427     {
428     //
429     }
430    
431     #else
432 masaki 120 #pragma mark#### NSRuleEditor Delegate ####
433     - (NSInteger)ruleEditor:(NSRuleEditor *)editor
434     numberOfChildrenForCriterion:(id)criterion
435     withRowType:(NSRuleEditorRowType)rowType
436     {
437     NSInteger result = 0;
438    
439     if(rowType == NSRuleEditorRowTypeCompound) {
440     result = [compound numberOfChildrenForChild:criterion];
441     goto end;
442     }
443    
444     if(!criterion) {
445     result = [simples count];
446     goto end;
447     return result;
448     }
449    
450     for(id s in simples) {
451     if([s isMyChild:criterion]) result += [s numberOfChildrenForChild:criterion];
452     }
453    
454     end:
455     // NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result);
456    
457     return result;
458     }
459     - (id)ruleEditor:(NSRuleEditor *)editor
460     child:(NSInteger)index
461     forCriterion:(id)criterion
462     withRowType:(NSRuleEditorRowType)rowType
463     {
464     id result = nil;
465    
466     if(rowType == NSRuleEditorRowTypeCompound) {
467     result = [compound childForChild:criterion atIndex:index];
468     goto end;
469     }
470    
471     if(!criterion) {
472     id s = [simples objectAtIndex:index];
473     result = [s childForChild:criterion atIndex:index];
474     goto end;
475     }
476    
477     for(id s in simples) {
478     if([s isMyChild:criterion]) {
479     result = [s childForChild:criterion atIndex:index];
480     goto end;
481     }
482     }
483    
484     end:
485     // NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result);
486    
487     return result;
488     }
489     - (id)ruleEditor:(NSRuleEditor *)editor
490     displayValueForCriterion:(id)criterion
491     inRow:(NSInteger)row
492     {
493     id result = nil;
494    
495     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
496     if(rowType == NSRuleEditorRowTypeCompound) {
497     result = [compound displayValueForChild:criterion];
498     goto end;
499     }
500    
501     for(id s in simples) {
502     if([s isMyChild:criterion]) {
503     result = [s displayValueForChild:criterion];
504     goto end;
505     }
506     }
507    
508     end:
509     // NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result);
510     return result;
511     }
512    
513 masaki 121 - (NSDictionary *)ruleEditor:(NSRuleEditor *)editor
514     predicatePartsForCriterion:(id)criterion
515     withDisplayValue:(id)value
516     inRow:(NSInteger)row
517     {
518     id result = nil;
519    
520     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
521     if(rowType == NSRuleEditorRowTypeCompound) {
522     result = [compound predicateForChild:criterion withDisplayValue:value];
523     goto end;
524     }
525    
526     for(id s in simples) {
527     if([s isMyChild:criterion]) {
528     result = [s predicateForChild:criterion withDisplayValue:value];
529     goto end;
530     }
531     }
532    
533     end:
534     // NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, value, row, result);
535    
536     return result;
537     }
538 masaki 124 #endif
539 masaki 120 @end

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