Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfManager/XspfMRule.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 365 - (hide annotations) (download)
Fri May 21 13:38:42 2010 UTC (13 years, 11 months ago) by masakih
File size: 22476 byte(s)
ライセンス表示の行が長過ぎるので適当に行を分けた。
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 356 static NSString *rateingKeyPath = @"rating";
231 masakih 353 static NSString *labelKeyPath = @"label";
232 masakih 348 static BOOL useRating = NO;
233     static BOOL useLabel = NO;
234    
235     + (void)registerStringTypeKeyPaths:(NSArray *)keyPaths
236     {
237     if(stringKeys) {
238     [stringKeys release];
239     [leftKeys release];
240     leftKeys = nil;
241     }
242     stringKeys = [[NSArray arrayWithArray:keyPaths] retain];
243     }
244     + (void)registerDateTypeKeyPaths:(NSArray *)keyPaths
245     {
246     if(dateKeys) {
247     [dateKeys release];
248     [leftKeys release];
249     leftKeys = nil;
250     }
251     dateKeys = [[NSArray arrayWithArray:keyPaths] retain];
252     }
253     + (void)registerNumberTypeKeyPaths:(NSArray *)keyPaths
254     {
255     if(numberKeys) {
256     [numberKeys release];
257     [leftKeys release];
258     leftKeys = nil;
259     }
260     numberKeys = [[NSArray arrayWithArray:keyPaths] retain];
261     }
262     + (void)setUseRating:(BOOL)flag
263     {
264     if(flag && useRating || !flag && !useRating) {
265     [leftKeys release];
266     leftKeys = nil;
267     }
268     useRating = flag;
269     }
270     + (void)setUseLablel:(BOOL)flag
271     {
272     if(flag && useLabel || !flag && !useLabel) {
273     [leftKeys release];
274     leftKeys = nil;
275     }
276     useLabel = flag;
277     }
278 masakih 353 + (void)setLabelKeyPath:(NSString *)keyPath
279     {
280     [labelKeyPath release];
281     labelKeyPath = [keyPath copy];
282     }
283    
284 masaki 338 + (NSArray *)leftKeys
285     {
286     if(!leftKeys) {
287 masakih 348
288     id temp = [NSMutableArray array];
289     [temp addObjectsFromArray:stringKeys];
290     [temp addObjectsFromArray:dateKeys];
291     [temp addObjectsFromArray:numberKeys];
292     if(useRating) {
293 masakih 353 [temp addObject:rateingKeyPath];
294 masakih 348 }
295     if(useLabel) {
296 masakih 353 [temp addObject:labelKeyPath];
297 masakih 348 }
298     leftKeys = [[NSArray arrayWithArray:temp] retain];
299 masaki 338 }
300     return leftKeys;
301     }
302 masakih 348 static inline BOOL isDateKeyPath(NSString *keyPath)
303 masaki 338 {
304 masakih 348 return [dateKeys containsObject:keyPath];
305 masaki 338 }
306 masakih 348 + (BOOL)isDateKeyPath:(NSString *)keyPath
307 masaki 338 {
308 masakih 348 return isDateKeyPath(keyPath);
309 masaki 338 }
310     - (BOOL)isDateKeyPath:(NSString *)keyPath
311     {
312     return isDateKeyPath(keyPath);
313     }
314 masakih 348 static inline BOOL isStringKeyPath(NSString *keyPath)
315     {
316     return [stringKeys containsObject:keyPath];
317     }
318     + (BOOL)isStringKeyPath:(NSString *)keyPath
319     {
320     return isStringKeyPath(keyPath);
321     }
322 masaki 338 - (BOOL)isStringKeyPath:(NSString *)keyPath
323     {
324 masakih 348 return isStringKeyPath(keyPath);
325 masaki 338 }
326 masakih 348 static inline BOOL isNumberKeyPath(NSString *keyPath)
327     {
328     return [numberKeys containsObject:keyPath];
329     }
330     + (BOOL)isNumberKeyPath:(NSString *)keyPath
331     {
332     return isNumberKeyPath(keyPath);
333     }
334     - (BOOL)isNumberKeyPath:(NSString *)keyPath
335     {
336     return isNumberKeyPath(keyPath);
337     }
338     + (BOOL)isRateKeyPath:(NSString *)keyPath
339     {
340 masakih 353 return [keyPath isEqualToString:rateingKeyPath];
341 masakih 348 }
342 masaki 338 - (BOOL)isRateKeyPath:(NSString *)keyPath
343     {
344 masakih 353 return [keyPath isEqualToString:rateingKeyPath];
345 masaki 338 }
346 masakih 348 + (BOOL)isLabelKeyPath:(NSString *)keyPath
347 masaki 338 {
348 masakih 353 return [keyPath isEqualToString:labelKeyPath];
349 masaki 338 }
350 masakih 348 - (BOOL)isLabelKeyPath:(NSString *)keyPath
351 masaki 338 {
352 masakih 353 return [keyPath isEqualToString:labelKeyPath];
353 masaki 338 }
354    
355 masaki 164 #pragma mark == NSCopying Protocol ==
356 masaki 152 - (id)copyWithZone:(NSZone *)zone
357 masaki 142 {
358 masaki 152 XspfMRule *result = [[[self class] allocWithZone:zone] init];
359 masaki 158 [result setChildren:children];
360     [result setPredicateParts:predicateHints];
361     [result setValue:_value];
362 masaki 152
363     return result;
364 masaki 142 }
365 masaki 152
366 masaki 164 #pragma mark == NSCoding Protocol ==
367     static NSString *const XspfMRuleChildrenKey = @"XspfMRuleChildrenKey";
368     static NSString *const XspfMRulePredicateHintsKey = @"XspfMRulePredicateHintsKey";
369     static NSString *const XspfMRuleValueKey = @"XspfMRuleValueKey";
370     - (id)initWithCoder:(NSCoder *)decoder
371     {
372     self = [self init];
373    
374     [self setChildren:[decoder decodeObjectForKey:XspfMRuleChildrenKey]];
375     [self setPredicateParts:[decoder decodeObjectForKey:XspfMRulePredicateHintsKey]];
376     [self setValue:[decoder decodeObjectForKey:XspfMRuleValueKey]];
377    
378     return self;
379     }
380     - (void)encodeWithCoder:(NSCoder *)encoder
381     {
382     [encoder encodeObject:children forKey:XspfMRuleChildrenKey];
383     [encoder encodeObject:predicateHints forKey:XspfMRulePredicateHintsKey];
384     [encoder encodeObject:_value forKey:XspfMRuleValueKey];
385     }
386    
387 masaki 152 - (BOOL)isEqual:(id)other
388 masaki 120 {
389 masaki 152 if([super isEqual:other]) return YES;
390     if(![other isKindOfClass:[XspfMRule class]]) return NO;
391 masaki 120
392 masaki 152 XspfMRule *o = other;
393 masaki 153 if(![_value isEqualToString:o->_value]) return NO;
394 masaki 152 // if(![children isEqual:o->children]) return NO;
395     // if(![predicateHints isEqual:o->predicateHints]) return NO;
396    
397 masaki 120 return YES;
398     }
399 masaki 152 - (NSUInteger)hash
400     {
401 masaki 153 return _value ? [_value hash] : [super hash];
402 masaki 152 }
403    
404 masaki 120 - (id)description
405     {
406 masaki 152 return [NSString stringWithFormat:@"%@ {\n\t%@ = %@;\n\t%@ = %@;\n\t%@ = %@;}",
407     NSStringFromClass([self class]),
408 masaki 153 @"value", _value,
409 masaki 152 @"hints", predicateHints,
410     @"children", children,
411     nil];
412 masaki 120 }
413     @end
414    
415 masaki 152 @implementation XspfMRule (XspfMCreation)
416 masaki 120
417 masaki 152 - (id)init
418 masaki 120 {
419 masaki 152 [super init];
420    
421     children = [[NSMutableArray array] retain];
422     predicateHints = [[NSMutableDictionary dictionary] retain];
423    
424     return self;
425 masaki 120 }
426 masaki 152
427     - (id)initWithValue:(NSString *)newValue children:(NSArray *)newChildren predicateHints:(NSDictionary *)parts
428 masaki 120 {
429 masaki 152 [self init];
430 masaki 120
431 masaki 152 if([newValue isEqualToString:@"separator"]) {
432     [self release];
433     return [[XspfMSeparatorRule alloc] initSparetorRule];
434     }
435    
436 masaki 170 id fieldRule = [XspfMFieldRule fieldRuleWithValue:newValue];
437     if(fieldRule) {
438 masaki 152 [self release];
439 masaki 170 self = [fieldRule retain];
440 masaki 152 }
441    
442     [self setValue:newValue];
443     [self setChildren:newChildren];
444     [self setPredicateParts:parts];
445    
446 masaki 120 return self;
447     }
448 masaki 152 + (id)ruleWithValue:(NSString *)newValue children:(NSArray *)newChildren predicateHints:(NSDictionary *)parts
449 masaki 120 {
450 masaki 152 return [[[self alloc] initWithValue:newValue children:newChildren predicateHints:parts] autorelease];
451 masaki 120 }
452 masaki 152
453     + (NSArray *)compoundRule
454 masaki 120 {
455 masaki 152 id comp = [self ruleWithValue:@"of the following are true" children:nil predicateHints:[NSDictionary dictionary]];
456    
457     id allExp = [NSNumber numberWithUnsignedInt:NSAndPredicateType];
458     id all = [self ruleWithValue:@"All"
459     children:[NSArray arrayWithObject:comp]
460     predicateHints:[NSDictionary dictionaryWithObject:allExp forKey:NSRuleEditorPredicateCompoundType]];
461    
462     id anyExp = [NSNumber numberWithUnsignedInt:NSOrPredicateType];
463     id any = [self ruleWithValue:@"Any"
464     children:[NSArray arrayWithObject:comp]
465     predicateHints:[NSDictionary dictionaryWithObject:anyExp forKey:NSRuleEditorPredicateCompoundType]];
466 masakih 353
467 masaki 152 return [NSArray arrayWithObjects:all, any, nil];
468 masaki 120 }
469 masaki 152
470     - (NSDictionary *)predicateHintsWithPlist:(NSDictionary *)plist
471 masaki 120 {
472 masaki 152 NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:plist];
473     [result removeObjectForKey:@"criteria"];
474     [result removeObjectForKey:@"value"];
475    
476     return result;
477 masaki 120 }
478 masaki 152
479     + (id)ruleWithPlist:(id)plist
480 masaki 120 {
481 masaki 152 return [[[self alloc] initWithPlist:plist] autorelease];
482 masaki 120 }
483 masaki 152 - (id)initWithPlist:(id)plist
484 masaki 120 {
485 masaki 152 if(![plist isKindOfClass:[NSDictionary class]]) {
486     [self init];
487     [self release];
488     return nil;
489     }
490    
491     id pValue = [plist valueForKey:@"value"];
492     id criteria = [plist valueForKey:@"criteria"];
493     id pChildren = [NSMutableArray array];
494     for(id criterion in criteria) {
495     id c = [[self class] ruleWithPlist:criterion];
496     if(c) [pChildren addObject:c];
497     }
498     id hints = [self predicateHintsWithPlist:plist];
499    
500     return [self initWithValue:pValue children:pChildren predicateHints:hints];
501 masaki 120 }
502 masaki 152
503     - (void)dealloc
504 masaki 120 {
505 masaki 152 [children release];
506     [predicateHints release];
507 masaki 153 [_value release];
508 masaki 152
509     [super dealloc];
510 masaki 120 }
511 masaki 152
512 masaki 120 @end
513    
514 masaki 153 @implementation XspfMRule (XspfMPrivate)
515 masaki 142
516 masaki 153 - (NSView *)textField
517     {
518     id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
519     [[text cell] setControlSize:NSSmallControlSize];
520     [text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
521     [text setStringValue:@"1234567890"];
522     [text sizeToFit];
523     [text setStringValue:@""];
524    
525     return text;
526     }
527     - (NSView *)datePicker
528     {
529     id date = [[[NSDatePicker alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
530     [[date cell] setControlSize:NSSmallControlSize];
531     [date setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
532     [date setDatePickerElements:NSYearMonthDayDatePickerElementFlag];
533     [date setDrawsBackground:YES];
534     [date setDateValue:[NSDate dateWithTimeIntervalSinceNow:0.0]];
535     [date sizeToFit];
536    
537     return date;
538     }
539     - (NSView *)ratingIndicator
540     {
541     id rate = [[[NSLevelIndicator alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
542     id cell = [rate cell];
543     [cell setControlSize:NSSmallControlSize];
544     [rate setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
545     [rate setMinValue:0];
546     [rate setMaxValue:5];
547     [cell setLevelIndicatorStyle:NSRatingLevelIndicatorStyle];
548     [cell setEditable:YES];
549 masakih 346 [cell setHighlighted:YES];
550 masaki 153 [rate sizeToFit];
551    
552 masakih 346 [rate setAction:@selector(continuousHighlighted:)];
553 masakih 348 [rate setTarget:[[self class] functionHost]];
554 masakih 346
555 masaki 153 return rate;
556     }
557 masakih 346 - (void)setHighlightRate:(id)rate
558     {
559     [[rate cell] setHighlighted:YES];
560     }
561     - (IBAction)continuousHighlighted:(id)sender
562     {
563     [self performSelector:@selector(setHighlightRate:) withObject:sender afterDelay:0.0];
564     }
565 masaki 153 - (NSView *)numberField
566     {
567     id text = [[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
568     [[text cell] setControlSize:NSSmallControlSize];
569     [text setFont:[NSFont controlContentFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
570 masakih 348 [text setStringValue:@"1234"];
571 masaki 153 NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
572     [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
573     [formatter setMinimum:[NSNumber numberWithInt:0]];
574     [text setFormatter:formatter];
575     [text sizeToFit];
576     [text setStringValue:@"1"];
577    
578     return text;
579     }
580 masaki 243 - (NSView *)labelField
581     {
582 masakih 348 // HMLog(HMLogLevelDebug, @"Enter -> %@", NSStringFromSelector(_cmd));
583 masaki 243 id label = [[[XspfMLabelField alloc] initWithFrame:NSMakeRect(0,0,100,19)] autorelease];
584     [label sizeToFit];
585 masaki 251 [label setLabelStyle:XspfMSquareStyle];
586     [label setDrawX:YES];
587 masaki 243
588     return label;
589     }
590 masaki 153 @end
591 masaki 152 @implementation XspfMRule (XspfMExpressionBuilder)
592 masakih 348 + (id)functionHost
593     {
594     static id host = nil;
595     if(host) return host;
596     @synchronized(self) {
597     if(!host) {
598     host = [[self alloc] init];
599     }
600     }
601     return host;
602     }
603 masaki 156 - (NSArray *)twoNumberAndUnitArgs:(NSArray *)displayValues
604     {
605     id value03 = [displayValues objectAtIndex:2];
606     id arg01 = [NSNumber numberWithInt:[[value03 objectValue] intValue]];
607    
608     id value05 = [displayValues objectAtIndex:4];
609     id arg02 = [NSNumber numberWithInt:[[value05 objectValue] intValue]];
610    
611     id value06 = [displayValues objectAtIndex:5];
612     id arg03 = nil;
613 masakih 348 if([value06 isEqualToString:@"Hours"]) {
614     arg03 = [NSNumber numberWithInt:XspfMHoursUnitType];
615     } else if([value06 isEqualToString:@"Days"]) {
616 masaki 163 arg03 = [NSNumber numberWithInt:XspfMDaysUnitType];
617 masaki 156 } else if([value06 isEqualToString:@"Weeks"]) {
618 masaki 163 arg03 = [NSNumber numberWithInt:XpsfMWeeksUnitType];
619 masaki 156 } else if([value06 isEqualToString:@"Months"]) {
620 masaki 163 arg03 = [NSNumber numberWithInt:XspfMMonthsUnitType];
621 masaki 156 } else if([value06 isEqualToString:@"Years"]) {
622 masaki 163 arg03 = [NSNumber numberWithInt:XspfMYearsUnitType];
623 masaki 156 }
624    
625     if([arg01 compare:arg02] == NSOrderedDescending) {
626     id t = arg01;
627     arg01 = arg02;
628     arg02 = t;
629     }
630    
631     return [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01],
632     [NSExpression expressionForConstantValue:arg02],
633     [NSExpression expressionForConstantValue:arg03],
634     nil];
635     }
636 masaki 154 - (NSArray *)numberAndUnitArgs:(NSArray *)displayValues
637     {
638     id value03 = [displayValues objectAtIndex:2];
639     id arg01 = [NSNumber numberWithInt:[[value03 objectValue] intValue]];
640    
641     id value04 = [displayValues objectAtIndex:3];
642     id arg02 = nil;
643 masakih 348 if([value04 isEqualToString:@"Hours"]) {
644     arg02 = [NSNumber numberWithInt:XspfMHoursUnitType];
645     } else if([value04 isEqualToString:@"Days"]) {
646 masaki 163 arg02 = [NSNumber numberWithInt:XspfMDaysUnitType];
647 masaki 154 } else if([value04 isEqualToString:@"Weeks"]) {
648 masaki 163 arg02 = [NSNumber numberWithInt:XpsfMWeeksUnitType];
649 masaki 154 } else if([value04 isEqualToString:@"Months"]) {
650 masaki 163 arg02 = [NSNumber numberWithInt:XspfMMonthsUnitType];
651 masaki 154 } else if([value04 isEqualToString:@"Years"]) {
652 masaki 163 arg02 = [NSNumber numberWithInt:XspfMYearsUnitType];
653 masaki 154 }
654    
655     return [NSArray arrayWithObjects:[NSExpression expressionForConstantValue:arg01],
656     [NSExpression expressionForConstantValue:arg02], nil];
657     }
658 masaki 152 - (NSExpression *)rangeDateFromDisplayValues:(NSArray *)displayValues
659     {
660     id field01 = nil;
661     id field02 = nil;
662    
663     Class datepickerclass = [NSDatePicker class];
664     for(id v in displayValues) {
665     if([v isKindOfClass:datepickerclass]) {
666     if([v tag] == XspfMPrimaryDateFieldTag) {
667     field01 = v;
668     } else {
669     field02 = v;
670     }
671     }
672 masaki 142 }
673    
674 masaki 152 if(!field01 || !field02) return nil;
675    
676     id value01, value02;
677     value01 = [field01 dateValue]; value02 = [field02 dateValue];
678     if([value01 compare:value02] == NSOrderedDescending) {
679     id t = value02;
680     value02 = value01;
681     value01 = t;
682 masaki 142 }
683    
684 masaki 152 id expression01, expression02;
685     expression01 = [NSExpression expressionForConstantValue:value01];
686     expression02 = [NSExpression expressionForConstantValue:value02];
687    
688     return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]];
689 masaki 142 }
690 masakih 348 - (NSExpression *)rangeNumberFromDisplayValues:(NSArray *)displayValues
691     {
692     id field01 = nil;
693     id field02 = nil;
694    
695     Class numberFieldClass = [NSTextField class];
696     for(id v in displayValues) {
697     if([v isKindOfClass:numberFieldClass]) {
698     if([v tag] == XspfMPrimaryNumberFieldTag) {
699     field01 = v;
700     } else {
701     field02 = v;
702     }
703     }
704     }
705    
706     if(!field01 || !field02) return nil;
707    
708     NSInteger value01, value02;
709     value01 = [field01 integerValue]; value02 = [field02 integerValue];
710     if(value01 > value02) {
711     NSInteger t = value02;
712     value02 = value01;
713     value01 = t;
714     }
715    
716     id expression01, expression02;
717     expression01 = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:value01]];
718     expression02 = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:value02]];
719    
720     return [NSExpression expressionForAggregate:[NSArray arrayWithObjects:expression01, expression02, nil]];
721     }
722 masaki 120 @end
723 masaki 152

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