Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRule.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 428 - (hide annotations) (download)
Sun Aug 8 07:36:08 2010 UTC (13 years, 8 months ago) by masakih
File size: 23261 byte(s)
[Fix] 日付に関係する条件が正しく反映しない問題を修正。
1 masaki 120 //
2 masakih 348 // XspfMRule.m
3 masaki 120 // XspfManager
4     //
5     // Created by Hori,Masaki on 09/11/29.
6     //
7    
8 masakih 350 /*
9 masakih 365 This source code is release under the New BSD License.
10 masakih 350 Copyright (c) 2009-2010, masakih
11     All rights reserved.
12    
13 masakih 365 ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に
14     限り、再頒布および使用が許可されます。
15    
16     1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含
17     めること。
18     2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表
19     示、本条件一覧、および下記免責条項を含めること。
20     3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、
21     コントリビューターの名前を使用してはならない。
22     本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、
23     明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証
24     も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター
25     も、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか
26     厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する
27     可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用
28     サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定
29     されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい
30     て、一切責任を負わないものとします。
31 masakih 350 -------------------------------------------------------------------
32     Copyright (c) 2009-2010, masakih
33     All rights reserved.
34    
35 masakih 365 Redistribution and use in source and binary forms, with or without
36     modification, are permitted provided that the following conditions
37     are met:
38 masakih 350
39 masakih 365 1, Redistributions of source code must retain the above copyright
40     notice, this list of conditions and the following disclaimer.
41     2, Redistributions in binary form must reproduce the above copyright
42     notice, this list of conditions and the following disclaimer in
43     the documentation and/or other materials provided with the
44     distribution.
45     3, The names of its contributors may be used to endorse or promote
46     products derived from this software without specific prior
47     written permission.
48     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53     INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59     POSSIBILITY OF SUCH DAMAGE.
60 masakih 350 */
61    
62 masakih 353
63 masakih 348 #import "XspfMRule.h"
64 masaki 170 #import "XspfMRule_private.h"
65 masaki 120
66 masaki 243 #import "XspfMLabelField.h"
67     #import "XspfMLabelCell.h"
68 masaki 120
69 masaki 243
70 masaki 152 @implementation XspfMRule (XspfMAccessor)
71     - (void)setChildren:(NSArray *)newChildren
72 masaki 120 {
73 masaki 158 if(!newChildren) newChildren = [NSMutableArray array];
74 masaki 152
75     [children autorelease];
76 masaki 158 children = [[NSMutableArray alloc] initWithArray:newChildren copyItems:YES];
77 masaki 120 }
78 masaki 152 - (void)addChild:(XspfMRule *)child
79     {
80     [children addObject:child];
81     }
82     - (void)setPredicateParts:(NSDictionary *)parts
83     {
84     [predicateHints autorelease];
85     predicateHints = [parts mutableCopy];
86     }
87     - (void)setExpression:(id)expression forKey:(id)key
88     {
89     [predicateHints setObject:expression forKey:key];
90     }
91     - (void)setValue:(NSString *)newValue
92     {
93 masaki 153 if([_value isEqualToString:newValue]) return;
94 masaki 152
95 masaki 153 [_value autorelease];
96     _value = [newValue copy];
97 masaki 152 }
98 masaki 153 - (NSString *)value { return _value; }
99 masaki 143 @end
100 masaki 152
101     @implementation XspfMRule
102     @dynamic value;
103    
104     - (NSInteger)numberOfChildren
105 masaki 120 {
106 masaki 152 return [children count];
107 masaki 120 }
108 masaki 152 - (id)childAtIndex:(NSInteger)index
109 masaki 120 {
110 masaki 152 return [children objectAtIndex:index];
111     }
112     - (id)displayValueForRuleEditor:(NSRuleEditor *)ruleEditor inRow:(NSInteger)row
113     {
114 masaki 153 return _value;
115 masaki 152 }
116     - (NSDictionary *)predicatePartsWithDisplayValue:(id)displayValue forRuleEditor:(NSRuleEditor *)ruleEditor inRow:(NSInteger)row
117     {
118     id result = [NSMutableDictionary dictionary];
119 masaki 142
120 masaki 152 NSRuleEditorRowType rowType = [ruleEditor rowTypeForRow:row];
121     if(rowType == NSRuleEditorRowTypeCompound) {
122     return predicateHints;
123     }
124    
125     if([predicateHints valueForKey:@"XspfMIgnoreExpression"]) return nil;
126    
127 masaki 166 id operatorType = [predicateHints valueForKey:NSRuleEditorPredicateOperatorType];
128     id option = [predicateHints valueForKey:NSRuleEditorPredicateOptions];
129     id leftExp = [predicateHints valueForKey:NSRuleEditorPredicateLeftExpression];
130     id rightExp = [predicateHints valueForKey:NSRuleEditorPredicateRightExpression];
131 masaki 152 id customRightExp = [predicateHints valueForKey:@"XspfMPredicateRightExpression"];
132    
133     if(operatorType) {
134     [result setValue:operatorType forKey:NSRuleEditorPredicateOperatorType];
135     }
136     if(option) {
137     [result setValue:option forKey:NSRuleEditorPredicateOptions];
138     }
139     if(leftExp) {
140     id exp = nil;
141     if([leftExp isEqual:@"value"]) {
142     exp = [NSExpression expressionForKeyPath:displayValue];
143     } else {
144     exp = [NSExpression expressionForKeyPath:leftExp];
145     }
146     if(exp) {
147     [result setValue:exp forKey:NSRuleEditorPredicateLeftExpression];
148     }
149     }
150     if(rightExp) {
151     SEL selector = NSSelectorFromString(rightExp);
152     id exp = nil;
153     if(selector) {
154     exp = [NSExpression expressionForConstantValue:[displayValue performSelector:selector]];
155     } else {
156     exp = [NSExpression expressionForConstantValue:rightExp];
157     }
158     if(exp) {
159     [result setValue:exp forKey:NSRuleEditorPredicateRightExpression];
160     }
161     }
162     if(customRightExp) {
163     SEL selector = NSSelectorFromString(customRightExp);
164     id arg01 = [predicateHints valueForKey:@"XspfMRightExpressionArg01"];
165     id arg02 = [predicateHints valueForKey:@"XspfMRightExpressionArg02"];
166 masakih 348 if([arg01 isEqual:@"displayValues"]) {
167     arg01 = [ruleEditor displayValuesForRow:row];
168     }
169     if([arg02 isEqual:@"displayValues"]) {
170     arg02 = [ruleEditor displayValuesForRow:row];
171     }
172 masaki 152
173 masakih 348 id rhs = nil;
174 masaki 152 if(arg02 && arg01) {
175 masakih 348 rhs = [self performSelector:selector withObject:arg01 withObject:arg02];
176 masaki 152 } else if(arg01) {
177 masakih 348 rhs = [self performSelector:selector withObject:arg01];
178 masaki 152 } else {
179 masakih 348 rhs = [self performSelector:selector];
180     }
181     if(rhs) {
182 masaki 163 [result setValue:rhs forKey:NSRuleEditorPredicateRightExpression];
183 masaki 152 }
184     }
185    
186 masaki 163 NSString *selName = [predicateHints valueForKey:@"XspfMCustomSelector"];
187     if(selName) {
188 masaki 154 id args = nil;
189     NSString *argSelName = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteator"];
190     if(argSelName) {
191     SEL argSel = NSSelectorFromString(argSelName);
192     id argSelArg01 = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteatorArg01"];
193     if([argSelArg01 isEqual:@"displayValues"]) {
194     argSelArg01 = [ruleEditor displayValuesForRow:row];
195     }
196     id argSelArg02 = [predicateHints valueForKey:@"XspfMCustomSelectorArgumentsCteatorArg02"];
197     if([argSelArg02 isEqual:@"displayValues"]) {
198     argSelArg02 = [ruleEditor displayValuesForRow:row];
199     }
200     if(argSelArg02) {
201     args = [self performSelector:argSel withObject:argSelArg01 withObject:argSelArg02];
202     } else if(argSelArg01) {
203     args = [self performSelector:argSel withObject:argSelArg01];
204     } else {
205     args = [self performSelector:argSel];
206     }
207     } else {
208     id arg01 = [predicateHints valueForKey:@"XspfMCustomSelectorArg01"];
209     args = [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01], nil];
210     }
211 masaki 152
212 masakih 348 id target = [NSExpression expressionForConstantValue:[[self class] functionHost]];
213 masaki 165 id rhs = [NSExpression expressionForFunction:target selectorName:selName arguments:args];
214 masaki 166 [result setValue:rhs forKey:NSRuleEditorPredicateRightExpression];
215 masaki 152 }
216    
217 masaki 187 // HMLog(HMLogLevelDebug, @"predicate\tcriterion -> %@, value -> %@, row -> %d, result -> %@", predicateHints, displayValue, row, result);
218 masaki 152
219     return result;
220 masaki 142 }
221 masaki 152
222 masaki 153 - (id)displayValue { return _value; }
223 masaki 120
224 masaki 338
225     #pragma mark#### Variables for add/change criteria of Library. ####
226 masakih 348 static NSArray *leftKeys = nil;
227     static NSArray *stringKeys = nil;
228     static NSArray *dateKeys = nil;
229     static NSArray *numberKeys = nil;
230 masakih 428 static NSArray *booleanKeys = nil;
231 masakih 356 static NSString *rateingKeyPath = @"rating";
232 masakih 353 static NSString *labelKeyPath = @"label";
233 masakih 348 static BOOL useRating = NO;
234     static BOOL useLabel = NO;
235    
236     + (void)registerStringTypeKeyPaths:(NSArray *)keyPaths
237     {
238     if(stringKeys) {
239     [stringKeys release];
240     [leftKeys release];
241     leftKeys = nil;
242     }
243     stringKeys = [[NSArray arrayWithArray:keyPaths] retain];
244     }
245     + (void)registerDateTypeKeyPaths:(NSArray *)keyPaths
246     {
247     if(dateKeys) {
248     [dateKeys release];
249     [leftKeys release];
250     leftKeys = nil;
251     }
252     dateKeys = [[NSArray arrayWithArray:keyPaths] retain];
253     }
254     + (void)registerNumberTypeKeyPaths:(NSArray *)keyPaths
255     {
256     if(numberKeys) {
257     [numberKeys release];
258     [leftKeys release];
259     leftKeys = nil;
260     }
261     numberKeys = [[NSArray arrayWithArray:keyPaths] retain];
262     }
263 masakih 428 + (void)registerBooleanTypeKeyPaths:(NSArray *)keyPaths
264     {
265     if(booleanKeys) {
266     [booleanKeys release];
267     [leftKeys release];
268     leftKeys = nil;
269     }
270     booleanKeys = [[NSArray arrayWithArray:keyPaths] retain];
271     }
272 masakih 348 + (void)setUseRating:(BOOL)flag
273     {
274     if(flag && useRating || !flag && !useRating) {
275     [leftKeys release];
276     leftKeys = nil;
277     }
278     useRating = flag;
279     }
280 masakih 367 + (void)setRatingKeyPath:(NSString *)keyPath
281     {
282     [rateingKeyPath release];
283     rateingKeyPath = [keyPath copy];
284     }
285 masakih 348 + (void)setUseLablel:(BOOL)flag
286     {
287     if(flag && useLabel || !flag && !useLabel) {
288     [leftKeys release];
289     leftKeys = nil;
290     }
291     useLabel = flag;
292     }
293 masakih 353 + (void)setLabelKeyPath:(NSString *)keyPath
294     {
295     [labelKeyPath release];
296     labelKeyPath = [keyPath copy];
297     }
298    
299 masaki 338 + (NSArray *)leftKeys
300     {
301     if(!leftKeys) {
302 masakih 348
303     id temp = [NSMutableArray array];
304     [temp addObjectsFromArray:stringKeys];
305     [temp addObjectsFromArray:dateKeys];
306     [temp addObjectsFromArray:numberKeys];
307 masakih 428 [temp addObjectsFromArray:booleanKeys];
308 masakih 348 if(useRating) {
309 masakih 353 [temp addObject:rateingKeyPath];
310 masakih 348 }
311     if(useLabel) {
312 masakih 353 [temp addObject:labelKeyPath];
313 masakih 348 }
314     leftKeys = [[NSArray arrayWithArray:temp] retain];
315 masaki 338 }
316     return leftKeys;
317     }
318 masakih 348 static inline BOOL isDateKeyPath(NSString *keyPath)
319 masaki 338 {
320 masakih 348 return [dateKeys containsObject:keyPath];
321 masaki 338 }
322 masakih 348 + (BOOL)isDateKeyPath:(NSString *)keyPath
323 masaki 338 {
324 masakih 348 return isDateKeyPath(keyPath);
325 masaki 338 }
326     - (BOOL)isDateKeyPath:(NSString *)keyPath
327     {
328     return isDateKeyPath(keyPath);
329     }
330 masakih 348 static inline BOOL isStringKeyPath(NSString *keyPath)
331     {
332     return [stringKeys containsObject:keyPath];
333     }
334     + (BOOL)isStringKeyPath:(NSString *)keyPath
335     {
336     return isStringKeyPath(keyPath);
337     }
338 masaki 338 - (BOOL)isStringKeyPath:(NSString *)keyPath
339     {
340 masakih 348 return isStringKeyPath(keyPath);
341 masaki 338 }
342 masakih 348 static inline BOOL isNumberKeyPath(NSString *keyPath)
343     {
344     return [numberKeys containsObject:keyPath];
345     }
346     + (BOOL)isNumberKeyPath:(NSString *)keyPath
347     {
348     return isNumberKeyPath(keyPath);
349     }
350     - (BOOL)isNumberKeyPath:(NSString *)keyPath
351     {
352     return isNumberKeyPath(keyPath);
353     }
354 masakih 428 static inline BOOL isBooleanKeyPath(NSString *keyPath)
355     {
356     return [booleanKeys containsObject:keyPath];
357     }
358     + (BOOL)isBooleanKeyPath:(NSString *)keyPath
359     {
360     return isBooleanKeyPath(keyPath);
361     }
362     - (BOOL)isBooleanKeyPath:(NSString *)keyPath
363     {
364     return isBooleanKeyPath(keyPath);
365     }
366 masakih 348 + (BOOL)isRateKeyPath:(NSString *)keyPath
367     {
368 masakih 353 return [keyPath isEqualToString:rateingKeyPath];
369 masakih 348 }
370 masaki 338 - (BOOL)isRateKeyPath:(NSString *)keyPath
371     {
372 masakih 353 return [keyPath isEqualToString:rateingKeyPath];
373 masaki 338 }
374 masakih 348 + (BOOL)isLabelKeyPath:(NSString *)keyPath
375 masaki 338 {
376 masakih 353 return [keyPath isEqualToString:labelKeyPath];
377 masaki 338 }
378 masakih 348 - (BOOL)isLabelKeyPath:(NSString *)keyPath
379 masaki 338 {
380 masakih 353 return [keyPath isEqualToString:labelKeyPath];
381 masaki 338 }
382    
383 masaki 164 #pragma mark == NSCopying Protocol ==
384 masaki 152 - (id)copyWithZone:(NSZone *)zone
385 masaki 142 {
386 masaki 152 XspfMRule *result = [[[self class] allocWithZone:zone] init];
387 masaki 158 [result setChildren:children];
388     [result setPredicateParts:predicateHints];
389     [result setValue:_value];
390 masaki 152
391     return result;
392 masaki 142 }
393 masaki 152
394 masaki 164 #pragma mark == NSCoding Protocol ==
395     static NSString *const XspfMRuleChildrenKey = @"XspfMRuleChildrenKey";
396     static NSString *const XspfMRulePredicateHintsKey = @"XspfMRulePredicateHintsKey";
397     static NSString *const XspfMRuleValueKey = @"XspfMRuleValueKey";
398     - (id)initWithCoder:(NSCoder *)decoder
399     {
400     self = [self init];
401    
402     [self setChildren:[decoder decodeObjectForKey:XspfMRuleChildrenKey]];
403     [self setPredicateParts:[decoder decodeObjectForKey:XspfMRulePredicateHintsKey]];
404     [self setValue:[decoder decodeObjectForKey:XspfMRuleValueKey]];
405    
406     return self;
407     }
408     - (void)encodeWithCoder:(NSCoder *)encoder
409     {
410     [encoder encodeObject:children forKey:XspfMRuleChildrenKey];
411     [encoder encodeObject:predicateHints forKey:XspfMRulePredicateHintsKey];
412     [encoder encodeObject:_value forKey:XspfMRuleValueKey];
413     }
414    
415 masaki 152 - (BOOL)isEqual:(id)other
416 masaki 120 {
417 masaki 152 if([super isEqual:other]) return YES;
418     if(![other isKindOfClass:[XspfMRule class]]) return NO;
419 masaki 120
420 masaki 152 XspfMRule *o = other;
421 masaki 153 if(![_value isEqualToString:o->_value]) return NO;
422 masaki 152 // if(![children isEqual:o->children]) return NO;
423     // if(![predicateHints isEqual:o->predicateHints]) return NO;
424    
425 masaki 120 return YES;
426     }
427 masaki 152 - (NSUInteger)hash
428     {
429 masaki 153 return _value ? [_value hash] : [super hash];
430 masaki 152 }
431    
432 masaki 120 - (id)description
433     {
434 masaki 152 return [NSString stringWithFormat:@"%@ {\n\t%@ = %@;\n\t%@ = %@;\n\t%@ = %@;}",
435     NSStringFromClass([self class]),
436 masaki 153 @"value", _value,
437 masaki 152 @"hints", predicateHints,
438     @"children", children,
439     nil];
440 masaki 120 }
441     @end
442    
443 masaki 152 @implementation XspfMRule (XspfMCreation)
444 masaki 120
445 masaki 152 - (id)init
446 masaki 120 {
447 masaki 152 [super init];
448    
449     children = [[NSMutableArray array] retain];
450     predicateHints = [[NSMutableDictionary dictionary] retain];
451    
452     return self;
453 masaki 120 }
454 masaki 152
455     - (id)initWithValue:(NSString *)newValue children:(NSArray *)newChildren predicateHints:(NSDictionary *)parts
456 masaki 120 {
457 masaki 152 [self init];
458 masaki 120
459 masaki 152 if([newValue isEqualToString:@"separator"]) {
460     [self release];
461     return [[XspfMSeparatorRule alloc] initSparetorRule];
462     }
463    
464 masaki 170 id fieldRule = [XspfMFieldRule fieldRuleWithValue:newValue];
465     if(fieldRule) {
466 masaki 152 [self release];
467 masaki 170 self = [fieldRule retain];
468 masaki 152 }
469    
470     [self setValue:newValue];
471     [self setChildren:newChildren];
472     [self setPredicateParts:parts];
473    
474 masaki 120 return self;
475     }
476 masaki 152 + (id)ruleWithValue:(NSString *)newValue children:(NSArray *)newChildren predicateHints:(NSDictionary *)parts
477 masaki 120 {
478 masaki 152 return [[[self alloc] initWithValue:newValue children:newChildren predicateHints:parts] autorelease];
479 masaki 120 }
480 masaki 152
481     + (NSArray *)compoundRule
482 masaki 120 {
483 masaki 152 id comp = [self ruleWithValue:@"of the following are true" children:nil predicateHints:[NSDictionary dictionary]];
484    
485     id allExp = [NSNumber numberWithUnsignedInt:NSAndPredicateType];
486     id all = [self ruleWithValue:@"All"
487     children:[NSArray arrayWithObject:comp]
488     predicateHints:[NSDictionary dictionaryWithObject:allExp forKey:NSRuleEditorPredicateCompoundType]];
489    
490     id anyExp = [NSNumber numberWithUnsignedInt:NSOrPredicateType];
491     id any = [self ruleWithValue:@"Any"
492     children:[NSArray arrayWithObject:comp]
493     predicateHints:[NSDictionary dictionaryWithObject:anyExp forKey:NSRuleEditorPredicateCompoundType]];
494 masakih 353
495 masaki 152 return [NSArray arrayWithObjects:all, any, nil];
496 masaki 120 }
497 masaki 152
498     - (NSDictionary *)predicateHintsWithPlist:(NSDictionary *)plist
499 masaki 120 {
500 masaki 152 NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:plist];
501     [result removeObjectForKey:@"criteria"];
502     [result removeObjectForKey:@"value"];
503    
504     return result;
505 masaki 120 }
506 masaki 152
507     + (id)ruleWithPlist:(id)plist
508 masaki 120 {
509 masaki 152 return [[[self alloc] initWithPlist:plist] autorelease];
510 masaki 120 }
511 masaki 152 - (id)initWithPlist:(id)plist
512 masaki 120 {
513 masaki 152 if(![plist isKindOfClass:[NSDictionary class]]) {
514     [self init];
515     [self release];
516     return nil;
517     }
518    
519     id pValue = [plist valueForKey:@"value"];
520     id criteria = [plist valueForKey:@"criteria"];
521     id pChildren = [NSMutableArray array];
522     for(id criterion in criteria) {
523     id c = [[self class] ruleWithPlist:criterion];
524     if(c) [pChildren addObject:c];
525     }
526     id hints = [self predicateHintsWithPlist:plist];
527    
528     return [self initWithValue:pValue children:pChildren predicateHints:hints];
529 masaki 120 }
530 masaki 152
531     - (void)dealloc
532 masaki 120 {
533 masaki 152 [children release];
534     [predicateHints release];
535 masaki 153 [_value release];
536 masaki 152
537     [super dealloc];
538 masaki 120 }
539 masaki 152
540 masaki 120 @end
541    
542 masaki 153 @implementation XspfMRule (XspfMPrivate)
543 masaki 142
544 masaki 153 - (NSView *)textField
545     {
546     id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
547     [[text cell] setControlSize:NSSmallControlSize];
548     [text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
549     [text setStringValue:@"1234567890"];
550     [text sizeToFit];
551     [text setStringValue:@""];
552    
553     return text;
554     }
555     - (NSView *)datePicker
556     {
557     id date = [[[NSDatePicker alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
558     [[date cell] setControlSize:NSSmallControlSize];
559     [date setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
560     [date setDatePickerElements:NSYearMonthDayDatePickerElementFlag];
561     [date setDrawsBackground:YES];
562     [date setDateValue:[NSDate dateWithTimeIntervalSinceNow:0.0]];
563     [date sizeToFit];
564    
565     return date;
566     }
567     - (NSView *)ratingIndicator
568     {
569     id rate = [[[NSLevelIndicator alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
570     id cell = [rate cell];
571     [cell setControlSize:NSSmallControlSize];
572     [rate setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
573     [rate setMinValue:0];
574     [rate setMaxValue:5];
575     [cell setLevelIndicatorStyle:NSRatingLevelIndicatorStyle];
576     [cell setEditable:YES];
577 masakih 346 [cell setHighlighted:YES];
578 masaki 153 [rate sizeToFit];
579    
580 masakih 346 [rate setAction:@selector(continuousHighlighted:)];
581 masakih 348 [rate setTarget:[[self class] functionHost]];
582 masakih 346
583 masaki 153 return rate;
584     }
585 masakih 346 - (void)setHighlightRate:(id)rate
586     {
587     [[rate cell] setHighlighted:YES];
588     }
589     - (IBAction)continuousHighlighted:(id)sender
590     {
591     [self performSelector:@selector(setHighlightRate:) withObject:sender afterDelay:0.0];
592     }
593 masaki 153 - (NSView *)numberField
594     {
595     id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
596     [[text cell] setControlSize:NSSmallControlSize];
597     [text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
598 masakih 348 [text setStringValue:@"1234"];
599 masaki 153 NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
600     [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
601     [formatter setMinimum:[NSNumber numberWithInt:0]];
602     [text setFormatter:formatter];
603     [text sizeToFit];
604     [text setStringValue:@"1"];
605    
606     return text;
607     }
608 masaki 243 - (NSView *)labelField
609     {
610 masakih 348 // HMLog(HMLogLevelDebug, @"Enter -> %@", NSStringFromSelector(_cmd));
611 masaki 243 id label = [[[XspfMLabelField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
612     [label sizeToFit];
613 masaki 251 [label setLabelStyle:XspfMSquareStyle];
614     [label setDrawX:YES];
615 masaki 243
616     return label;
617     }
618 masaki 153 @end
619 masaki 152 @implementation XspfMRule (XspfMExpressionBuilder)
620 masakih 348 + (id)functionHost
621     {
622     static id host = nil;
623     if(host) return host;
624     @synchronized(self) {
625     if(!host) {
626     host = [[self alloc] init];
627     }
628     }
629     return host;
630     }
631 masaki 156 - (NSArray *)twoNumberAndUnitArgs:(NSArray *)displayValues
632     {
633     id value03 = [displayValues objectAtIndex:2];
634     id arg01 = [NSNumber numberWithInt:[[value03 objectValue] intValue]];
635    
636     id value05 = [displayValues objectAtIndex:4];
637     id arg02 = [NSNumber numberWithInt:[[value05 objectValue] intValue]];
638    
639     id value06 = [displayValues objectAtIndex:5];
640     id arg03 = nil;
641 masakih 348 if([value06 isEqualToString:@"Hours"]) {
642     arg03 = [NSNumber numberWithInt:XspfMHoursUnitType];
643     } else if([value06 isEqualToString:@"Days"]) {
644 masaki 163 arg03 = [NSNumber numberWithInt:XspfMDaysUnitType];
645 masaki 156 } else if([value06 isEqualToString:@"Weeks"]) {
646 masaki 163 arg03 = [NSNumber numberWithInt:XpsfMWeeksUnitType];
647 masaki 156 } else if([value06 isEqualToString:@"Months"]) {
648 masaki 163 arg03 = [NSNumber numberWithInt:XspfMMonthsUnitType];
649 masaki 156 } else if([value06 isEqualToString:@"Years"]) {
650 masaki 163 arg03 = [NSNumber numberWithInt:XspfMYearsUnitType];
651 masaki 156 }
652    
653     if([arg01 compare:arg02] == NSOrderedDescending) {
654     id t = arg01;
655     arg01 = arg02;
656     arg02 = t;
657     }
658    
659     return [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01],
660     [NSExpression expressionForConstantValue:arg02],
661     [NSExpression expressionForConstantValue:arg03],
662     nil];
663     }
664 masaki 154 - (NSArray *)numberAndUnitArgs:(NSArray *)displayValues
665     {
666     id value03 = [displayValues objectAtIndex:2];
667     id arg01 = [NSNumber numberWithInt:[[value03 objectValue] intValue]];
668    
669     id value04 = [displayValues objectAtIndex:3];
670     id arg02 = nil;
671 masakih 348 if([value04 isEqualToString:@"Hours"]) {
672     arg02 = [NSNumber numberWithInt:XspfMHoursUnitType];
673     } else if([value04 isEqualToString:@"Days"]) {
674 masaki 163 arg02 = [NSNumber numberWithInt:XspfMDaysUnitType];
675 masaki 154 } else if([value04 isEqualToString:@"Weeks"]) {
676 masaki 163 arg02 = [NSNumber numberWithInt:XpsfMWeeksUnitType];
677 masaki 154 } else if([value04 isEqualToString:@"Months"]) {
678 masaki 163 arg02 = [NSNumber numberWithInt:XspfMMonthsUnitType];
679 masaki 154 } else if([value04 isEqualToString:@"Years"]) {
680 masaki 163 arg02 = [NSNumber numberWithInt:XspfMYearsUnitType];
681 masaki 154 }
682    
683     return [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01],
684     [NSExpression expressionForConstantValue:arg02], nil];
685     }
686 masaki 152 - (NSExpression *)rangeDateFromDisplayValues:(NSArray *)displayValues
687     {
688     id field01 = nil;
689     id field02 = nil;
690    
691     Class datepickerclass = [NSDatePicker class];
692     for(id v in displayValues) {
693     if([v isKindOfClass:datepickerclass]) {
694     if([v tag] == XspfMPrimaryDateFieldTag) {
695     field01 = v;
696     } else {
697     field02 = v;
698     }
699     }
700 masaki 142 }
701    
702 masaki 152 if(!field01 || !field02) return nil;
703    
704     id value01, value02;
705     value01 = [field01 dateValue]; value02 = [field02 dateValue];
706     if([value01 compare:value02] == NSOrderedDescending) {
707     id t = value02;
708     value02 = value01;
709     value01 = t;
710 masaki 142 }
711    
712 masaki 152 id expression01, expression02;
713     expression01 = [NSExpression expressionForConstantValue:value01];
714     expression02 = [NSExpression expressionForConstantValue:value02];
715    
716     return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]];
717 masaki 142 }
718 masakih 348 - (NSExpression *)rangeNumberFromDisplayValues:(NSArray *)displayValues
719     {
720     id field01 = nil;
721     id field02 = nil;
722    
723     Class numberFieldClass = [NSTextField class];
724     for(id v in displayValues) {
725     if([v isKindOfClass:numberFieldClass]) {
726     if([v tag] == XspfMPrimaryNumberFieldTag) {
727     field01 = v;
728     } else {
729     field02 = v;
730     }
731     }
732     }
733    
734     if(!field01 || !field02) return nil;
735    
736     NSInteger value01, value02;
737     value01 = [field01 integerValue]; value02 = [field02 integerValue];
738     if(value01 > value02) {
739     NSInteger t = value02;
740     value02 = value01;
741     value01 = t;
742     }
743    
744     id expression01, expression02;
745     expression01 = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:value01]];
746     expression02 = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:value02]];
747    
748     return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]];
749     }
750 masakih 428
751     - (NSExpression *)falseExpression
752     {
753     return [NSExpression expressionForConstantValue:[NSNumber numberWithBool:NO]];
754     }
755 masaki 120 @end
756 masaki 152

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