Skip to content

Commit 9b3e362

Browse files
committed
• StarkPredicateEditor now has a BOOL replaceIsRegex method which I shall use tomorrow to do regex replacements.
1 parent b4ea926 commit 9b3e362

2 files changed

+42
-4
lines changed

SSYReplacePredicateEditorRowTemplate.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ extern NSString* SSYReplacePredicateCheckboxWillGoAwayNotification ;
3434
@interface SSYReplacePredicateCheckbox : NSButton
3535

3636
@property (assign) SSYReplacePredicateEditorRowTemplate* predicateEditorRowTemplate ;
37-
@property (copy) NSString* attributeKey ;
37+
@property (readonly) NSString* attributeKey ;
38+
@property (readonly) BOOL isRegex ;
3839

3940
@end
4041

@@ -48,5 +49,6 @@ extern NSString* SSYReplacePredicateCheckboxWillGoAwayNotification ;
4849
@property (copy) NSString* attributeKey ;
4950

5051
+ (NSString*)anyEditableStringValueInView:(NSView*)view ;
52+
+ (NSString*)localizedRegexMenuItemTitle ;
5153

5254
@end

SSYReplacePredicateEditorRowTemplate.m

+39-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@interface SSYReplacePredicateCheckbox ()
66

77
@property (assign) CGFloat lastSuperWidth ;
8+
@property (copy) NSString* attributeKey ;
89

910
@end
1011

@@ -67,18 +68,49 @@ - (void)trackSuperview {
6768
}
6869
#endif
6970

71+
/*!
72+
@details This method is a kludge. I tried to do it the proper way, which is
73+
to look at the -representedObject of the menu items. Unfortunately, that is
74+
an undocumented NSDictionary. Then I tried to add tags of value equal to the
75+
to NSPredicateOperatorType value, in -[StarkPredicateEditor templates]. That
76+
didn't work because the popup menu in the view is a
77+
NSRuleEditorPopupButton which is apparently copied by Cocoa from the
78+
NSPopUpButton seen in the -templates method, but when Cocoa copies it, it
79+
apparently does not copy the tag because the tags in these copies are all 0.
80+
So, finally I settled on this kludge which is to compare the, eek, menu titles.
81+
It is reliable because I defined localizedRegexMenuItemTitle and use it to both
82+
make the menu and, then in here, to do the comparison and, or course, it would
83+
be a programming error if two menu items had the same title :) But it's still
84+
a code-smelly way to identify a menu item. */
85+
- (BOOL)isRegex {
86+
NSPopUpButton* operatorPopup = nil ;
87+
for (NSView* sibling in self.superview.subviews) {
88+
if ([sibling isKindOfClass:[NSPopUpButton class]]) {
89+
if (sibling.frame.origin.x >= operatorPopup.frame.origin.x) {
90+
operatorPopup = (NSPopUpButton*)sibling ;
91+
}
92+
}
93+
}
94+
95+
NSString* selectedTitle = operatorPopup.selectedItem.title ;
96+
BOOL isRegex = ([selectedTitle isEqualToString:[SSYReplacePredicateEditorRowTemplate localizedRegexMenuItemTitle]]) ;
97+
98+
return isRegex ;
99+
}
100+
101+
70102
@end
71103

72104
@implementation SSYReplacePredicateEditorRowTemplate
73105

74106
/* Superclass NSPredicateEditorRowTemplate must conform to NSCopying because,
75-
as the name implies, this is a *template*. Cocoa makes a copy whenever
76-
it needs another one. */
107+
as the name implies, this is a *template*. For some reason, Cocoa makes a copy
108+
whenever it needs another one, and then of course, I guess, it makes a view
109+
(NSView) from that. */
77110
- (SSYReplacePredicateEditorRowTemplate*)copyWithZone:(NSZone*)zone {
78111
SSYReplacePredicateEditorRowTemplate* copy = [super copyWithZone:zone] ;
79112
copy.replacer = self.replacer ;
80113
copy.attributeKey = self.attributeKey ;
81-
/*SSYDBL*/ NSLog(@"Made copy: %@ with ak=%@", copy, copy.attributeKey ) ;
82114
return copy ;
83115
}
84116

@@ -138,4 +170,8 @@ + (NSString*)anyEditableStringValueInView:(NSView*)view {
138170
return answer ;
139171
}
140172

173+
+ (NSString*)localizedRegexMenuItemTitle {
174+
return NSLocalizedString(@"Regular Expression (grep)", nil) ;
175+
}
176+
141177
@end

0 commit comments

Comments
 (0)