From 2c7f2098004f5fe90189c536affc73dfe67393e9 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Mon, 10 Aug 2009 16:38:54 +0000 Subject: =?UTF-8?q?=E2=80=A2=20added=20to=20the=20FieldEditorSheet=20a=20m?= =?UTF-8?q?ax=20text=20length=20validation=20=E2=80=A2=20added=20the=20max?= =?UTF-8?q?=20text=20length=20validation=20for=20the=20TableContent=20whil?= =?UTF-8?q?e=20editing=20in=20the=20FieldEditorSheet=20=E2=80=A2=20reinvok?= =?UTF-8?q?ed=20the=20validation=20of=20the=20max=20text=20length=20for=20?= =?UTF-8?q?TableContent=20editing=20while=20editing=20in=20=20a=20cell=20?= =?UTF-8?q?=E2=80=A2=20fixed=20in=20TableContent:=20prevent=20the=20editin?= =?UTF-8?q?g=20of=20data=20cells=20in=20gray=20if=20the=20cell=20displays?= =?UTF-8?q?=20the=20gray=20NULL=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPFieldEditorController.h | 3 +++ Source/SPFieldEditorController.m | 48 +++++++++++++++++++++++++++++++++++++++- Source/TableContent.m | 18 ++++++++++----- 3 files changed, 63 insertions(+), 6 deletions(-) (limited to 'Source') diff --git a/Source/SPFieldEditorController.h b/Source/SPFieldEditorController.h index 76e8a86e..f7b669f4 100644 --- a/Source/SPFieldEditorController.h +++ b/Source/SPFieldEditorController.h @@ -49,6 +49,7 @@ NSString *tmpFileName; int counter; + unsigned long long maxTextLength; NSUserDefaults *prefs; } @@ -65,6 +66,8 @@ - (id)editWithObject:(id)data fieldName:(NSString*)fieldName usingEncoding:(NSStringEncoding)anEncoding isObjectBlob:(BOOL)isFieldBlob isEditable:(BOOL)isEditable withWindow:(NSWindow *)tableWindow; +- (void)setTextMaxLength:(unsigned long long)length; + - (void)processPasteImageData; - (void)processUpdatedImageData:(NSData *)data; diff --git a/Source/SPFieldEditorController.m b/Source/SPFieldEditorController.m index 7b05c5fa..230a3861 100644 --- a/Source/SPFieldEditorController.m +++ b/Source/SPFieldEditorController.m @@ -28,6 +28,7 @@ #import "SPTextViewAdditions.h" #import "SPDataAdditions.h" #import "QLPreviewPanel.h" +#import "SPDataCellFormatter.h" @implementation SPFieldEditorController @@ -37,6 +38,7 @@ // force the nib to be loaded (void) [self window]; counter = 0; + maxTextLength = 0; // Allow the user to enter cmd+return to close the edit sheet in addition to fn+return [editSheetOkButton setKeyEquivalentModifierMask:NSCommandKeyMask]; @@ -51,6 +53,11 @@ [super dealloc]; } +- (void)setTextMaxLength:(unsigned long long)length +{ + maxTextLength = length; +} + - (id)editWithObject:(id)data fieldName:(NSString*)fieldName usingEncoding:(NSStringEncoding)anEncoding isObjectBlob:(BOOL)isFieldBlob isEditable:(BOOL)isEditable withWindow:(NSWindow *)tableWindow { @@ -594,12 +601,51 @@ } } +#pragma mark - +#pragma mark Delegates + +/* + Validate editTextView for max text length + */ +- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)r replacementString:(NSString *)replacementString +{ + if(textView == editTextView && maxTextLength > 0) { + + int newLength; + + // Pure attribute changes are ok. + if (!replacementString) return YES; + + // The exact change isn't known. Disallow the change to be safe. + if (r.location==NSNotFound) return NO; + + // Calculate the length of the text after the change. + newLength=[[textView textStorage] length]+[replacementString length]-r.length; + + // If it's too long, disallow the change. + // If the user pastes something into insert it partially to maxTextLength. + if (newLength>maxTextLength) { + + if(maxTextLength-[[textView textStorage] length] < [replacementString length]) { + [textView insertText:[replacementString substringToIndex:maxTextLength-[[textView textStorage] length]]]; + } + NSBeep(); + return NO; + } + + // Otherwise, allow it. + return YES; + + } + return YES; +} + /* invoked when the user changes the string in the editSheet */ - (void)textViewDidChangeSelection:(NSNotification *)notification { - + // Do nothing if user really didn't changed text (e.g. for font size changing return) if(editSheetWillBeInitialized || ([[[notification object] textStorage] changeInLength]==0)) return; diff --git a/Source/TableContent.m b/Source/TableContent.m index c0d77959..e94037f3 100644 --- a/Source/TableContent.m +++ b/Source/TableContent.m @@ -237,7 +237,7 @@ // Set the line break mode and an NSFormatter subclass which truncates long strings for display [dataCell setLineBreakMode:NSLineBreakByTruncatingTail]; - //[dataCell setFormatter:[[SPDataCellFormatter new] autorelease]]; + [dataCell setFormatter:[[SPDataCellFormatter new] autorelease]]; // Set field length limit if field is a varchar to match varchar length if ([[columnDefinition objectForKey:@"typegrouping"] isEqualToString:@"string"]) { @@ -1876,9 +1876,17 @@ * This function changes the text color of text/blob fields which are not yet loaded to gray */ - (void)tableView:(CMCopyTable *)aTableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)aTableColumn row:(int)row -{ +{ + + // If user wants to edit 'cell' set text color to black and return to avoid + // writing in gray if value was NULL + if ( [aTableView editedColumn] == [[aTableColumn identifier] intValue] && [aTableView editedRow] == row) { + [cell setTextColor:[NSColor blackColor]]; + return; + } + NSDictionary *column = NSArrayObjectAtIndex(dataColumns, [[aTableColumn identifier] intValue]); - + // For NULL cell's display the user's NULL value placeholder in grey to easily distinguish it from other values if ([cell respondsToSelector:@selector(setTextColor:)]) { @@ -2139,7 +2147,7 @@ if ([multipleLineEditingButton state] == NSOnState || isBlob) { SPFieldEditorController *fieldEditor = [[SPFieldEditorController alloc] init]; - + [fieldEditor setTextMaxLength:[[[aTableColumn dataCellForRow:rowIndex] formatter] textLimit]]; id editData = [[fieldEditor editWithObject:[[filteredResult objectAtIndex:rowIndex] objectAtIndex:[[aTableColumn identifier] intValue]] fieldName:[[aTableColumn headerCell] stringValue] usingEncoding:[mySQLConnection encoding] @@ -2152,7 +2160,7 @@ isEditingRow = YES; currentlyEditingRow = rowIndex; } - + [[filteredResult objectAtIndex:rowIndex] replaceObjectAtIndex:[[aTableColumn identifier] intValue] withObject:[editData copy]]; } -- cgit v1.2.3