Forcing the display of a compound string using NSPredicateEditor

I am creating an NSPredicateEditor in code and trying to achieve what I was hoping would be a fairly simple task. Basically, I would like to display a composite version of NSPredicateEditorRowTemplate (giving the user the ability to match when "All / Any / None of the following values ​​is true") and a series of fairly simple dungeons beneath it. To do this, I create my NSPredicateEditor and bind its value to save the changes when the user edits his predicate.

The problem I am facing is that, in spite of everything, I cannot make the complex NSPredicateEditorRowTemplate show by default as the parent row with one subframe. When I first create a predicate, I pass a dummy empty predicate, for example:

filename BEGINSWITH[cd] ''

To make the composite line display, I need to create two signatures:

filename BEGINSWITH[cd] '' && path ==[cd] ''

For reference, here is how I create an NSPredicateEditor:

NSArray *keyPaths = [NSArray arrayWithObjects:[NSExpression expressionForKeyPath:@"filename"], [NSExpression expressionForKeyPath:@"path"], nil];
NSArray *operators = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSEqualToPredicateOperatorType],
                                                                            [NSNumber numberWithInteger:NSNotEqualToPredicateOperatorType],
                                                                            [NSNumber numberWithInteger:NSBeginsWithPredicateOperatorType],
                                                                            [NSNumber numberWithInteger:NSEndsWithPredicateOperatorType],
                                                                            [NSNumber numberWithInteger:NSContainsPredicateOperatorType],
                                                                            nil];

NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions:keyPaths
                                                                          rightExpressionAttributeType:NSStringAttributeType
                                                                                              modifier:NSDirectPredicateModifier
                                                                                             operators:operators
                                                                                               options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];

NSArray *compoundTypes = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSNotPredicateType],
                                                        [NSNumber numberWithInteger:NSAndPredicateType],
                                                        [NSNumber numberWithInteger:NSOrPredicateType],
                                                        nil];

NSPredicateEditorRowTemplate *compound = [[NSPredicateEditorRowTemplate alloc] initWithCompoundTypes:compoundTypes];
NSArray *rowTemplates = [NSArray arrayWithObjects:compound, template, nil];
[template release];
[compound release];

predicateEditor = [[NSPredicateEditor alloc] init];
[predicateEditor setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[predicateEditor setRowTemplates:rowTemplates];
[predicateEditor setCanRemoveAllRows:NO];
[predicateEditor setContinuous:YES];
[predicateEditor setFrame:[[scrollView contentView] bounds]];

// Create a custom binding for our NSPredicateEditor
SIPredicateStringValueTransformer *transformer = [[[SIPredicateStringValueTransformer alloc] init] autorelease];
NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionaryWithObjectsAndKeys:transformer, NSValueTransformerBindingOption, [NSNumber numberWithBool:YES], NSValidatesImmediatelyBindingOption, nil];
[predicateEditor bind:@"value" toObject:self withKeyPath:@"self.filter.predicate" options:bindingOptions];

// Add our NSPredicateEditor to our NSScrollView
[scrollView setDocumentView:predicateEditor];
+5
source share
1 answer

The problem is that you give it a predicate of comparison. You need to give it a tricky one.

I assume your object SIPredicateStringValueTransformertakes a string and turns it into a predicate, right? And this probably does something like this:

- (id) transformedValue:(id)value {
  return [NSPredicate predicateWithFormat:value];
}

, - , (.. NSCompoundPredicate NSComparisonPredicate). :

- (id) transformedValue:(id)value {
  NSPredicate *p = [NSPredicate predicateWithFormat:value];
  if ([p isKindOfClass:[NSComparisonPredicate class]]) {
    p = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObject:p]];
  }
  return p;
}

NSCompoundPredicate , . , .

+5

All Articles