Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRuleEditorDelegate.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (hide annotations) (download)
Thu Dec 17 15:36:52 2009 UTC (14 years, 4 months ago) by masaki
File size: 10968 byte(s)
[New] NSPredicateからのNSRuleEditorのrowsの生成をXspfMRuleに委譲。
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 *XspfMREDPredicateRowsKey = @"predicateRows";
17     static NSString *XspfMREDNameKey = @"name";
18    
19    
20 masaki 124 - (NSDictionary *)buildRows:(NSArray *)template
21     {
22     NSMutableDictionary *result = [NSMutableDictionary dictionary];
23     for(id row in template) {
24 masaki 125 id name = [row valueForKey:XspfMREDNameKey];
25 masaki 152 id rule = [XspfMRule ruleWithPlist:row];
26     [result setObject:rule forKey:name];
27 masaki 124 }
28 masaki 152 rowTemplate = [result retain];
29 masaki 124 return result;
30     }
31 masaki 126 - (id)criteriaWithKeyPath:(NSString *)keypath
32     {
33     NSString *key = nil;
34     if([keypath isEqualToString:@"title"]) {
35     key = @"String";
36     } else if([keypath isEqualToString:@"rating"]) {
37     key = @"Rate";
38 masaki 152 } else if([[NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil] containsObject:keypath]) {
39     key = @"AbDate";
40 masaki 126 }
41     if(key) {
42     id row = [rowTemplate valueForKey:key];
43 masaki 152 id c = [[[row childAtIndex:0] copy] autorelease];
44     [c setValue:keypath];
45 masaki 126 return [NSArray arrayWithObject:c];
46     }
47    
48     return nil;
49     }
50 masaki 120 - (void)awakeFromNib
51     {
52     if(!compound) {
53 masaki 143 compound = [[XspfMCompound alloc] init];
54 masaki 120 }
55 masaki 152
56 masaki 124 NSBundle *m = [NSBundle mainBundle];
57     NSString *path = [m pathForResource:@"LibraryRowTemplate" ofType:@"plist"];
58     NSArray *rowsTemplate = [NSArray arrayWithContentsOfFile:path];
59     if(!rowsTemplate) {
60     exit(12345);
61 masaki 120 }
62 masaki 124
63 masaki 152 [self buildRows:rowsTemplate];
64    
65 masaki 124 NSMutableArray *newRows = [NSMutableArray array];
66    
67 masaki 126 id c = [self criteriaWithKeyPath:@"title"];
68     if(c) [newRows addObjectsFromArray:c];
69 masaki 124
70     for(id keyPath in [NSArray arrayWithObjects:@"lastPlayDate", @"modificationDate", @"creationDate", nil]) {
71 masaki 126 c = [self criteriaWithKeyPath:keyPath];
72     if(c) [newRows addObjectsFromArray:c];
73 masaki 124 }
74    
75 masaki 126 c = [self criteriaWithKeyPath:@"rating"];
76     if(c) [newRows addObjectsFromArray:c];
77 masaki 124
78 masaki 152 simples = [newRows retain];
79    
80 masaki 125 ////
81     predicateRows = [[NSMutableArray alloc] init];
82     [ruleEditor bind:XspfMREDRowsKey toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil];
83 masaki 120 }
84    
85 masaki 150 - (void)resolveExpression:(id)exp
86     {
87     NSString *message = nil;
88    
89     switch([exp expressionType]) {
90     case NSConstantValueExpressionType:
91     message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]];
92     break;
93     case NSEvaluatedObjectExpressionType:
94     message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]];
95     break;
96     case NSVariableExpressionType:
97     message = [NSString stringWithFormat:@"variable -> %@", [exp variable]];
98     break;
99     case NSKeyPathExpressionType:
100     message = [NSString stringWithFormat:@"keyPath -> %@", [exp keyPath]];
101     break;
102     case NSFunctionExpressionType:
103     message = [NSString stringWithFormat:@"oprand -> %@(%@)function -> %@, argumect -> %@",
104     [exp operand], NSStringFromClass([[exp operand] class]),
105     [exp function], [exp arguments]];
106     break;
107     case NSAggregateExpressionType:
108     message = [NSString stringWithFormat:@"collection -> %@", [exp collection]];
109     break;
110     }
111    
112     fprintf(stderr, "%s\n", [message UTF8String]);
113     }
114     - (void)resolvePredicate:(id)predicate
115     {
116     if([predicate isKindOfClass:[NSCompoundPredicate class]]) {
117     NSArray *sub = [predicate subpredicates];
118     for(id p in sub) {
119     [self resolvePredicate:p];
120     }
121     } else if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
122     id left = [predicate leftExpression];
123     id right = [predicate rightExpression];
124     SEL sel = Nil;
125     if([predicate predicateOperatorType] == NSCustomSelectorPredicateOperatorType) {
126     sel = [predicate customSelector];
127     }
128     fprintf(stderr, "left ->\t");
129     [self resolveExpression:left];
130     if(sel) {
131     fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"SEL -> %@", NSStringFromSelector(sel)] UTF8String]);
132     } else {
133     fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"type -> %d, opt -> %d, mod -> %d", [predicate predicateOperatorType], [predicate options], [predicate comparisonPredicateModifier]] UTF8String]);
134     }
135     fprintf(stderr, "right ->\t");
136     [self resolveExpression:right];
137     fprintf(stderr, "end resolve.\n");
138     }
139     }
140    
141 masaki 123 - (void)setPredicate:(id)predicate
142     {
143 masaki 150 NSLog(@"predicate -> (%@) %@", NSStringFromClass([predicate class]), predicate);
144     [self resolvePredicate:predicate];
145 masaki 123
146 masaki 150
147 masaki 153 // id hoge = [self buildRowsFromPredicate:predicate];
148     // id new = [NSArray arrayWithObject:hoge];
149     id new = [XspfMRule ruleEditorRowsFromPredicate:predicate withRowTemplate:rowTemplate];
150    
151 masaki 125
152     [self willChangeValueForKey:XspfMREDPredicateRowsKey];
153     [predicateRows release];
154     predicateRows = [new retain];
155     [self didChangeValueForKey:XspfMREDPredicateRowsKey];
156 masaki 146 // [ruleEditor reloadCriteria];
157 masaki 123 }
158 masaki 125 - (void)setPredicateRows:(id)p
159     {
160 masaki 147 // NSLog(@"new -> %@", p);
161 masaki 125 [predicateRows release];
162     predicateRows = [p retain];
163     }
164    
165     #pragma mark#### NSRleEditor Delegate ####
166 masaki 152
167 masaki 124 - (NSInteger)ruleEditor:(NSRuleEditor *)editor
168     numberOfChildrenForCriterion:(id)criterion
169     withRowType:(NSRuleEditorRowType)rowType
170     {
171     NSInteger result = 0;
172    
173 masaki 143 if(rowType == NSRuleEditorRowTypeCompound) {
174     return [compound numberOfChildrenForChild:criterion];
175     }
176    
177 masaki 124 if(!criterion) {
178 masaki 152 result = [simples count];
179 masaki 143 } else {
180 masaki 152 result = [criterion numberOfChildren];
181 masaki 124 }
182    
183 masaki 152 // NSLog(@"numner\tcriterion -> %@, type -> %d, result -> %d", criterion, rowType, result);
184 masaki 124
185     return result;
186     }
187    
188     - (id)ruleEditor:(NSRuleEditor *)editor
189     child:(NSInteger)index
190     forCriterion:(id)criterion
191     withRowType:(NSRuleEditorRowType)rowType
192     {
193     id result = nil;
194    
195 masaki 143 if(rowType == NSRuleEditorRowTypeCompound) {
196     return [compound childForChild:criterion atIndex:index];
197     }
198    
199 masaki 124 if(!criterion) {
200 masaki 152 result = [simples objectAtIndex:index];
201 masaki 143 } else {
202 masaki 152 result = [criterion childAtIndex:index];
203 masaki 124 }
204    
205 masaki 152 // NSLog(@"child\tindex -> %d, criterion -> %@, type -> %d, result -> %@", index, criterion, rowType, result);
206 masaki 124
207     return result;
208     }
209     - (id)ruleEditor:(NSRuleEditor *)editor
210     displayValueForCriterion:(id)criterion
211     inRow:(NSInteger)row
212     {
213     id result = nil;
214 masaki 143
215     NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
216     if(rowType == NSRuleEditorRowTypeCompound) {
217     return [compound displayValueForChild:criterion];
218     }
219    
220     if(!criterion) {
221     //
222     } else {
223 masaki 152 result = [criterion displayValueForRuleEditor:editor inRow:row];
224 masaki 143 }
225    
226    
227 masaki 152 // NSLog(@"display\tcriterion -> %@, row -> %d, result -> %@", criterion, row, result);
228 masaki 143
229 masaki 124 return result;
230     }
231     - (NSDictionary *)ruleEditor:(NSRuleEditor *)editor
232     predicatePartsForCriterion:(id)criterion
233     withDisplayValue:(id)displayValue
234     inRow:(NSInteger)row
235     {
236     id result = nil;
237    
238 masaki 143 NSRuleEditorRowType rowType = [editor rowTypeForRow:row];
239     if(rowType == NSRuleEditorRowTypeCompound) {
240     return [compound predicateForChild:criterion withDisplayValue:displayValue];
241     }
242    
243 masaki 152 result = [criterion predicatePartsWithDisplayValue:displayValue forRuleEditor:editor inRow:row];
244 masaki 153 // NSLog(@"predicate\tresult -> %@", result);
245 masaki 143
246 masaki 152 // NSLog(@"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", criterion, displayValue, row, result);
247 masaki 143
248 masaki 124 return result;
249     }
250     - (void)ruleEditorRowsDidChange:(NSNotification *)notification
251     {
252     //
253     }
254 masaki 150 @end
255 masaki 124
256 masaki 150 @implementation NSString (XspfMTest)
257     - (NSArray *)rangeOfToday
258     {
259     NSCalendar *aCalendar = [NSCalendar currentCalendar];
260     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
261     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
262     fromDate:now];
263     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
264    
265     id result = [NSArray arrayWithObjects:startOfToday, now, nil];
266     return result;
267     }
268     - (NSArray *)rangeOfYesterday
269     {
270    
271     NSCalendar *aCalendar = [NSCalendar currentCalendar];
272     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
273     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
274     fromDate:now];
275     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
276    
277     NSDateComponents *comp = [[NSDateComponents alloc] init];
278     [comp setDay:-1];
279     NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
280    
281     id result = [NSArray arrayWithObjects:startOfYesterday, startOfToday, nil];
282     return result;
283     }
284     - (NSArray *)rangeOfThisWeek
285     {
286     NSCalendar *aCalendar = [NSCalendar currentCalendar];
287     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
288     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit
289     fromDate:now];
290     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
291    
292     NSDateComponents *comp = [[NSDateComponents alloc] init];
293    
294     [comp setWeekday:-[nowComp weekday]+1];
295     NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
296    
297     id result = [NSArray arrayWithObjects:startOfThisWeek, now, nil];
298     return result;
299     }
300     - (NSArray *)rangeOfLastWeek
301     {
302     NSCalendar *aCalendar = [NSCalendar currentCalendar];
303     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
304     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit
305     fromDate:now];
306     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
307    
308     NSDateComponents *comp = [[NSDateComponents alloc] init];
309     [comp setWeekday:-[nowComp weekday]+1];
310     NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
311    
312     [comp setWeekday:-[nowComp weekday]+1];
313     [comp setWeek:-1];
314     NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
315    
316     id result = [NSArray arrayWithObjects:startOfLastWeek, startOfThisWeek, nil];
317     return result;
318     }
319     - (NSArray *)dateRangeFromVariable:(NSString *)date
320     {
321     NSLog(@"In function argument is %@", date);
322    
323     NSCalendar *aCalendar = [NSCalendar currentCalendar];
324     NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0.0];
325     NSDateComponents *nowComp = [aCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit
326     fromDate:now];
327     NSDate *startOfToday = [aCalendar dateFromComponents:nowComp];
328    
329     NSDateComponents *comp = [[NSDateComponents alloc] init];
330     [comp setDay:-1];
331     NSDate *startOfYesterday = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
332    
333     [comp setDay:0];
334     [comp setWeekday:-[nowComp weekday]+1];
335     NSDate *startOfThisWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
336    
337     [comp setWeekday:-[nowComp weekday]+1];
338     [comp setWeek:-1];
339     NSDate *startOfLastWeek = [aCalendar dateByAddingComponents:comp toDate:startOfToday options:0];
340    
341     NSLog(@"now -> %@\ntoday -> %@\nyesterday -> %@\nthisweek -> %@\nlastweek -> %@",
342     now, startOfToday, startOfYesterday, startOfThisWeek, startOfLastWeek);
343    
344     id result = [NSArray arrayWithObjects:now, startOfToday, nil];
345     return result;
346     }
347 masaki 120 @end

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