| 1 |
// |
| 2 |
// XspfMRuleEditorDelegate.m |
| 3 |
// XspfManager |
| 4 |
// |
| 5 |
// Created by Hori,Masaki on 09/11/28. |
| 6 |
// |
| 7 |
|
| 8 |
/* |
| 9 |
This source code is release under the New BSD License. |
| 10 |
Copyright (c) 2009-2010, masakih |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に |
| 14 |
限り、再頒布および使用が許可されます。 |
| 15 |
|
| 16 |
1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含 |
| 17 |
めること。 |
| 18 |
2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表 |
| 19 |
示、本条件一覧、および下記免責条項を含めること。 |
| 20 |
3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、 |
| 21 |
コントリビューターの名前を使用してはならない。 |
| 22 |
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、 |
| 23 |
明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証 |
| 24 |
も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター |
| 25 |
も、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか |
| 26 |
厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する |
| 27 |
可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用 |
| 28 |
サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定 |
| 29 |
されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい |
| 30 |
て、一切責任を負わないものとします。 |
| 31 |
------------------------------------------------------------------- |
| 32 |
Copyright (c) 2009-2010, masakih |
| 33 |
All rights reserved. |
| 34 |
|
| 35 |
Redistribution and use in source and binary forms, with or without |
| 36 |
modification, are permitted provided that the following conditions |
| 37 |
are met: |
| 38 |
|
| 39 |
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 |
*/ |
| 61 |
|
| 62 |
#import "XspfMRuleEditorDelegate.h" |
| 63 |
|
| 64 |
#import "XspfMRule.h" |
| 65 |
#import "XspfMRuleRowsBuilder.h" |
| 66 |
#import "XspfMRuleRowTemplate.h" |
| 67 |
|
| 68 |
@implementation XspfMRuleEditorDelegate |
| 69 |
|
| 70 |
static NSString *XspfMREDPredicateRowsKey = @"predicateRows"; |
| 71 |
|
| 72 |
+ (void)registerStringTypeKeyPaths:(NSArray *)keyPaths |
| 73 |
{ |
| 74 |
[XspfMRule registerStringTypeKeyPaths:keyPaths]; |
| 75 |
} |
| 76 |
+ (void)registerDateTypeKeyPaths:(NSArray *)keyPaths |
| 77 |
{ |
| 78 |
[XspfMRule registerDateTypeKeyPaths:keyPaths]; |
| 79 |
} |
| 80 |
+ (void)registerNumberTypeKeyPaths:(NSArray *)keyPaths |
| 81 |
{ |
| 82 |
[XspfMRule registerNumberTypeKeyPaths:keyPaths]; |
| 83 |
} |
| 84 |
+ (void)registerBooleanTypeKeyPaths:(NSArray *)keyPaths |
| 85 |
{ |
| 86 |
[XspfMRule registerBooleanTypeKeyPaths:keyPaths]; |
| 87 |
} |
| 88 |
+ (void)setUseRating:(BOOL)flag |
| 89 |
{ |
| 90 |
[XspfMRule setUseRating:flag]; |
| 91 |
} |
| 92 |
+ (void)setRatingKeyPath:(NSString *)keyPath |
| 93 |
{ |
| 94 |
[XspfMRule setRatingKeyPath:keyPath]; |
| 95 |
} |
| 96 |
+ (void)setUseLablel:(BOOL)flag |
| 97 |
{ |
| 98 |
[XspfMRule setUseLablel:flag]; |
| 99 |
} |
| 100 |
+ (void)setLabelKeyPath:(NSString *)keyPath |
| 101 |
{ |
| 102 |
[XspfMRule setLabelKeyPath:keyPath]; |
| 103 |
} |
| 104 |
|
| 105 |
- (void)awakeFromNib |
| 106 |
{ |
| 107 |
NSBundle *mainBundle = [NSBundle mainBundle]; |
| 108 |
NSString *templatePath = [mainBundle pathForResource:@"LibraryRowTemplate" ofType:@"plist"]; |
| 109 |
rowTemplate = [[XspfMRuleRowTemplate rowTemplateWithPath:templatePath] retain]; |
| 110 |
compounds = [[XspfMRule compoundRule] retain]; |
| 111 |
|
| 112 |
predicateRows = [[NSMutableArray alloc] init]; |
| 113 |
[ruleEditor bind:@"rows" toObject:self withKeyPath:XspfMREDPredicateRowsKey options:nil]; |
| 114 |
} |
| 115 |
- (void)dealloc |
| 116 |
{ |
| 117 |
[ruleEditor unbind:@"rows"]; |
| 118 |
[simples release]; |
| 119 |
[compounds release]; |
| 120 |
[predicateRows release]; |
| 121 |
[rowTemplate release]; |
| 122 |
|
| 123 |
[super dealloc]; |
| 124 |
} |
| 125 |
|
| 126 |
- (NSArray *)simples |
| 127 |
{ |
| 128 |
if(simples) return simples; |
| 129 |
|
| 130 |
NSMutableArray *newRows = [NSMutableArray array]; |
| 131 |
for(id keyPath in [XspfMRule leftKeys]) { |
| 132 |
id c = [rowTemplate criteriaForKeyPath:keyPath]; |
| 133 |
if(c) [newRows addObjectsFromArray:c]; |
| 134 |
} |
| 135 |
|
| 136 |
simples = [[NSArray alloc] initWithArray:newRows]; |
| 137 |
|
| 138 |
return simples; |
| 139 |
} |
| 140 |
- (void)setPredicateRows:(id)p |
| 141 |
{ |
| 142 |
if([predicateRows isEqual:p]) return; |
| 143 |
|
| 144 |
[predicateRows release]; |
| 145 |
predicateRows = [p retain]; |
| 146 |
} |
| 147 |
- (void)setPredicate:(id)predicate |
| 148 |
{ |
| 149 |
XspfMRuleRowsBuilder *builder = [XspfMRuleRowsBuilder builderWithPredicate:predicate]; |
| 150 |
builder.rowTemplate = rowTemplate; |
| 151 |
[builder build]; |
| 152 |
id new = [NSMutableArray arrayWithObject:[builder row]]; |
| 153 |
|
| 154 |
[self setPredicateRows:new]; |
| 155 |
} |
| 156 |
|
| 157 |
#pragma mark#### NSRleEditor Delegate #### |
| 158 |
|
| 159 |
- (NSInteger)ruleEditor:(NSRuleEditor *)editor |
| 160 |
numberOfChildrenForCriterion:(id)criterion |
| 161 |
withRowType:(NSRuleEditorRowType)rowType |
| 162 |
{ |
| 163 |
NSInteger result = 0; |
| 164 |
|
| 165 |
if(!criterion) { |
| 166 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 167 |
result = [compounds count]; |
| 168 |
} else { |
| 169 |
result = [[self simples] count]; |
| 170 |
} |
| 171 |
} else { |
| 172 |
result = [criterion numberOfChildren]; |
| 173 |
} |
| 174 |
|
| 175 |
return result; |
| 176 |
} |
| 177 |
|
| 178 |
- (id)ruleEditor:(NSRuleEditor *)editor |
| 179 |
child:(NSInteger)index |
| 180 |
forCriterion:(id)criterion |
| 181 |
withRowType:(NSRuleEditorRowType)rowType |
| 182 |
{ |
| 183 |
id result = nil; |
| 184 |
|
| 185 |
if(!criterion) { |
| 186 |
if(rowType == NSRuleEditorRowTypeCompound) { |
| 187 |
result = [compounds objectAtIndex:index]; |
| 188 |
} else { |
| 189 |
result = [[self simples] objectAtIndex:index]; |
| 190 |
} |
| 191 |
} else { |
| 192 |
result = [criterion childAtIndex:index]; |
| 193 |
} |
| 194 |
|
| 195 |
return result; |
| 196 |
} |
| 197 |
- (id)ruleEditor:(NSRuleEditor *)editor |
| 198 |
displayValueForCriterion:(id)criterion |
| 199 |
inRow:(NSInteger)row |
| 200 |
{ |
| 201 |
id result = [criterion displayValueForRuleEditor:editor inRow:row]; |
| 202 |
|
| 203 |
return result; |
| 204 |
} |
| 205 |
- (NSDictionary *)ruleEditor:(NSRuleEditor *)editor |
| 206 |
predicatePartsForCriterion:(id)criterion |
| 207 |
withDisplayValue:(id)displayValue |
| 208 |
inRow:(NSInteger)row |
| 209 |
{ |
| 210 |
id result = [criterion predicatePartsWithDisplayValue:displayValue forRuleEditor:editor inRow:row]; |
| 211 |
|
| 212 |
return result; |
| 213 |
} |
| 214 |
- (void)ruleEditorRowsDidChange:(NSNotification *)notification |
| 215 |
{ |
| 216 |
// |
| 217 |
} |
| 218 |
|
| 219 |
#pragma mark---- Debugging ---- |
| 220 |
- (void)resolveExpression:(id)exp |
| 221 |
{ |
| 222 |
NSString *message = nil; |
| 223 |
|
| 224 |
switch([exp expressionType]) { |
| 225 |
case NSConstantValueExpressionType: |
| 226 |
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 227 |
break; |
| 228 |
case NSEvaluatedObjectExpressionType: |
| 229 |
message = [NSString stringWithFormat:@"constant -> %@", [exp constantValue]]; |
| 230 |
break; |
| 231 |
case NSVariableExpressionType: |
| 232 |
message = [NSString stringWithFormat:@"variable -> %@", [exp variable]]; |
| 233 |
break; |
| 234 |
case NSKeyPathExpressionType: |
| 235 |
message = [NSString stringWithFormat:@"keyPath -> %@", [exp keyPath]]; |
| 236 |
break; |
| 237 |
case NSFunctionExpressionType: |
| 238 |
message = [NSString stringWithFormat:@"oprand -> %@(%@), function -> %@, arguments -> %@", |
| 239 |
[exp operand], NSStringFromClass([[exp operand] class]), |
| 240 |
[exp function], [exp arguments]]; |
| 241 |
break; |
| 242 |
case NSAggregateExpressionType: |
| 243 |
message = [NSString stringWithFormat:@"collection -> %@", [exp collection]]; |
| 244 |
break; |
| 245 |
} |
| 246 |
|
| 247 |
fprintf(stderr, "%s\n", [message UTF8String]); |
| 248 |
} |
| 249 |
- (void)resolvePredicate:(id)predicate |
| 250 |
{ |
| 251 |
if([predicate isKindOfClass:[NSCompoundPredicate class]]) { |
| 252 |
NSArray *sub = [predicate subpredicates]; |
| 253 |
for(id p in sub) { |
| 254 |
[self resolvePredicate:p]; |
| 255 |
} |
| 256 |
} else if([predicate isKindOfClass:[NSComparisonPredicate class]]) { |
| 257 |
NSComparisonPredicate *compPredicate = predicate; |
| 258 |
id left = [compPredicate leftExpression]; |
| 259 |
id right = [compPredicate rightExpression]; |
| 260 |
SEL sel = Nil; |
| 261 |
if([compPredicate predicateOperatorType] == NSCustomSelectorPredicateOperatorType) { |
| 262 |
sel = [compPredicate customSelector]; |
| 263 |
} |
| 264 |
fprintf(stderr, "left ->\t"); |
| 265 |
[self resolveExpression:left]; |
| 266 |
if(sel) { |
| 267 |
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"SEL -> %@", NSStringFromSelector(sel)] UTF8String]); |
| 268 |
} else { |
| 269 |
fprintf(stderr, "%s\n", [[NSString stringWithFormat:@"type -> %d, opt -> %d, mod -> %d", [compPredicate predicateOperatorType], [compPredicate options], [compPredicate comparisonPredicateModifier]] UTF8String]); |
| 270 |
} |
| 271 |
fprintf(stderr, "right ->\t"); |
| 272 |
[self resolveExpression:right]; |
| 273 |
fprintf(stderr, "end resolve.\n"); |
| 274 |
} |
| 275 |
} |
| 276 |
@end |