26 CPArray _allTemplates;
28 CPArray _rootHeaderTrees;
33 #pragma mark public methods
53 - (CPArray)rowTemplates
63 - (void)setRowTemplates:(
id)rowTemplates
65 if (_allTemplates == rowTemplates)
68 _allTemplates = rowTemplates;
70 [
self _updateItemsByCompoundTemplates];
71 [
self _updateItemsBySimpleTemplates];
73 if ([
self numberOfRows] > 0)
75 var predicate = [
super predicate];
76 [
self _reflectPredicate:predicate];
81 + (Class)_binderClassForBinding:(
CPString)aBinding
83 if (aBinding == CPValueBinding)
86 return [
super _binderClassForBinding:aBinding];
91 if (aBinding == CPValueBinding)
94 return [
super _replacementKeyPathForBinding:aBinding];
97 - (void)_initRuleEditorShared
99 [
super _initRuleEditorShared];
101 _rootTrees = [CPArray array];
102 _rootHeaderTrees = [CPArray array];
105 - (id)initWithFrame:(CGRect)aFrame
107 self = [
super initWithFrame:aFrame];
111 _allTemplates = [CPArray arrayWithObject:initialTemplate];
119 return [
super predicate];
122 - (void)_updateItemsBySimpleTemplates
125 count = [_allTemplates count],
130 var t = _allTemplates[count];
132 [templates insertObject:t atIndex:0];
135 var trees = [
self _constructTreesForTemplates:templates];
136 if ([trees count] > 0)
137 _rootTrees = [
self _mergeTree:trees];
140 - (void)_updateItemsByCompoundTemplates
143 count = [_allTemplates count],
148 var t = _allTemplates[count];
150 [templates insertObject:t atIndex:0];
153 var trees = [
self _constructTreesForTemplates:templates];
154 if ([trees count] > 0)
155 _rootHeaderTrees = [
self _mergeTree:trees];
158 - (CPArray)_constructTreesForTemplates:(
id)templates
161 count = [templates count];
163 for (var i = 0; i < count; i++)
165 var tree = [
self _constructTreeForTemplate:templates[i]];
166 [trees addObjectsFromArray:tree];
178 var icount = [aTree count];
179 for (var i = 0; i < icount; i++)
181 var anode = [aTree objectAtIndex:i],
182 jcount = [mergedTree count],
185 for (var j = 0; j < jcount; j++)
187 var mergednode = [mergedTree objectAtIndex:j];
189 if ([[mergednode title] isEqualToString:[anode title]])
191 var children1 = [mergednode children],
192 children2 = [anode children],
193 children12 = [children1 arrayByAddingObjectsFromArray:children2],
194 mergedChildren = [
self _mergeTree:children12];
196 [mergednode setChildren:mergedChildren];
202 [mergedTree addObject:anode];
210 var tree = [CPArray array],
211 templateViews = [aTemplate templateViews],
212 count = [templateViews count];
216 var children = [CPArray array],
221 templateView = [templateViews objectAtIndex:count],
222 isPopup = [templateView isKindOfClass:[CPPopUpButton class]];
226 itemArray = [[templateView itemArray] valueForKey:@"title"];
227 itemsCount = [itemArray count];
231 for (; menuIndex < itemsCount; menuIndex++)
233 var item = [_CPPredicateEditorTree new];
234 [item setIndexIntoTemplate:count];
235 [item setTemplate:aTemplate];
236 [item setMenuItemIndex:menuIndex];
238 [item setTitle:[itemArray objectAtIndex:menuIndex]];
240 [children addObject:item];
243 [children makeObjectsPerformSelector:@selector(setChildren:) withObject:tree];
250 #pragma mark Set the Predicate
252 - (void)setObjectValue:(
id)objectValue
254 var ov = [
self objectValue];
255 if ((ov == nil) != (objectValue == nil) || ![ov
isEqual:objectValue])
257 [
self _setPredicate:objectValue];
258 [
self _reflectPredicate:objectValue];
262 - (void)_reflectPredicate:(
id)predicate
264 var animation = _currentAnimation;
265 _currentAnimation = nil;
268 if (predicate != nil)
272 predicate = [[
CPCompoundPredicate alloc] initWithType:[
self _compoundPredicateTypeForRootRows] subpredicates:[CPArray arrayWithObject:predicate]];
274 var row = [
self _rowObjectFromPredicate:predicate];
276 [_boundArrayOwner setValue:[CPArray arrayWithObject:row] forKey:_boundArrayKeyPath];
279 [
self setAnimation:animation];
282 - (id)_rowObjectFromPredicate:(CPPredicate)predicate
288 if (matchedTemplate == nil)
291 var copyTemplate = [matchedTemplate copy],
292 subpredicates = [matchedTemplate displayableSubpredicatesOfPredicate:predicate];
294 if (subpredicates == nil)
296 [copyTemplate _setComparisonPredicate:predicate];
301 [copyTemplate _setCompoundPredicate:predicate];
305 var row = [
self _rowFromTemplate:copyTemplate originalTemplate:matchedTemplate withRowType:type];
307 if (subpredicates == nil)
310 var count = [subpredicates count],
313 for (var i = 0; i < count; i++)
315 var subrow = [
self _rowObjectFromPredicate:subpredicates[i]];
317 [subrows addObject:subrow];
320 [row setValue:subrows forKey:[
super subrowsKeyPath]];
327 var criteria = [CPArray array],
328 values = [CPArray array],
329 templateViews = [aTemplate templateViews],
335 while ((count = [rootItems count]) > 0)
338 for (var i = 0; i < count; i++)
340 treeChild = [rootItems objectAtIndex:i];
342 var currentView = [templateViews objectAtIndex:[treeChild indexIntoTemplate]],
343 title = [treeChild title];
345 if (title == nil || [title
isEqual:[currentView titleOfSelectedItem]])
347 var node = [_CPPredicateEditorRowNode rowNodeFromTree:treeChild];
348 [node applyTemplate:aTemplate withViews:templateViews forOriginalTemplate:originalTemplate];
350 [criteria addObject:node];
351 [values addObject:[node displayValue]];
356 rootItems = [treeChild children];
360 @"criteria": criteria,
361 @"displayValues": values,
368 #pragma mark Get the predicate
370 - (void)_updatePredicate
372 [
self _updatePredicateFromRows];
375 - (void)_updatePredicateFromRows
377 var rootRowsArray = [
super _rootRowsArray],
380 count2 = count = [rootRowsArray count],
385 var item = [rootRowsArray objectAtIndex:count],
386 subpredicate = [
self _predicateFromRowItem:item];
388 if (subpredicate != nil)
389 [subpredicates insertObject:subpredicate atIndex:0];
393 predicate = [subpredicates lastObject];
395 predicate = [[
CPCompoundPredicate alloc] initWithType:[
self _compoundPredicateTypeForRootRows] subpredicates:subpredicates];
397 [
self _setPredicate:predicate];
400 - (id)_predicateFromRowItem:(
id)rowItem
402 var subpredicates = [CPArray array],
403 rowType = [rowItem valueForKey:_typeKeyPath];
407 var subrows = [rowItem valueForKey:_subrowsArrayKeyPath],
408 count = [subrows count];
410 for (var i = 0; i < count; i++)
412 var subrow = [subrows objectAtIndex:i],
413 predicate = [
self _predicateFromRowItem:subrow];
415 [subpredicates addObject:predicate];
419 var criteria = [rowItem valueForKey:_itemsKeyPath],
420 displayValues = [rowItem valueForKey:_valuesKeyPath],
421 count = [criteria count],
422 lastItem = [criteria lastObject],
423 template = [lastItem templateForRow],
424 templateViews = [template templateViews];
426 for (var j = 0; j < count; j++)
428 var view = [templateViews objectAtIndex:j],
429 value = [displayValues objectAtIndex:j];
430 [[criteria objectAtIndex:j] setTemplateViews:templateViews];
433 [view selectItemWithTitle:value];
434 else if ([view respondsToSelector:
@selector(setObjectValue:)])
435 [view setObjectValue:[value objectValue]];
438 return [template predicateWithSubpredicates:subpredicates];
446 #pragma mark Control delegate
448 - (void)_sendRuleAction
450 [
super _sendRuleAction];
453 - (BOOL)_sendsActionOnIncompleteTextChange
481 #pragma mark RuleEditor delegate methods
483 - (int)_queryNumberOfChildrenOfItem:(
id)rowItem withRowType:(CPRuleEditorRowType)type
488 return [trees count];
490 return [[rowItem children] count];
493 - (id)_queryChild:(
int)childIndex ofItem:(
id)rowItem withRowType:(CPRuleEditorRowType)type
498 return [_CPPredicateEditorRowNode rowNodeFromTree:trees[childIndex]];
501 return [[rowItem children] objectAtIndex:childIndex];
504 - (id)_queryValueForItem:(
id)rowItem inRow:(CPInteger)rowIndex
506 return [rowItem displayValue];
511 var CPPredicateTemplatesKey =
@"CPPredicateTemplates";
515 - (id)initWithCoder:(
CPCoder)aCoder
517 self = [
super initWithCoder:aCoder];
521 var nibTemplates = [aCoder decodeObjectForKey:CPPredicateTemplatesKey];
523 if (nibTemplates != nil)
524 [
self setRowTemplates:nibTemplates];
530 - (void)encodeWithCoder:(
CPCoder)aCoder
532 [
super encodeWithCoder:aCoder];
533 [aCoder encodeObject:_allTemplates forKey:CPPredicateTemplatesKey];
542 - (void)setPlaceholderValue:(
id)aValue withMarker:(
CPString)aMarker forBinding:(
CPString)aBinding
544 [_source _reflectPredicate:nil];
547 - (void)setValue:(
id)aValue forBinding:(
CPString)aBinding
549 [_source _reflectPredicate:aValue];
562 return _predicateTarget;
568 - (void)setTarget:(
id)aValue
570 _predicateTarget = aValue;
578 return _predicateAction;
584 - (void)setAction:(
SEL)aValue
586 _predicateAction = aValue;