From e3b2ea1aa4b868db760d465e2a4319f86c475131 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 2 Nov 2010 20:32:49 +0000 Subject: =?UTF-8?q?=E2=80=A2=20minor=20code=20cleaning=20and=20comment=20s?= =?UTF-8?q?tuff?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPFieldEditorController.m | 421 ++++++++++++++++++++------------------- 1 file changed, 214 insertions(+), 207 deletions(-) (limited to 'Source/SPFieldEditorController.m') diff --git a/Source/SPFieldEditorController.m b/Source/SPFieldEditorController.m index a5ec347d..17595947 100644 --- a/Source/SPFieldEditorController.m +++ b/Source/SPFieldEditorController.m @@ -113,14 +113,14 @@ [qlTypesItems addObject:type]; } } - + qlTypes = [[NSDictionary dictionaryWithObject:qlTypesItems forKey:SPQuickLookTypes] retain]; [qlTypesItems release]; fieldType = @""; fieldEncoding = @""; } - + return self; } @@ -144,68 +144,30 @@ [super dealloc]; } -/** - * Set the maximum text length of the underlying table field for input validation. - * - * @param length The maximum text length - */ -- (void)setTextMaxLength:(NSUInteger)length -{ - maxTextLength = length; -} - -/** - * Set the field type of the underlying table field for input validation. - * - * @param aType The field type which will be used for dispatching which sheet will be shown. If type == BIT the bitSheet will be used otherwise the editSheet. - */ -- (void)setFieldType:(NSString*)aType -{ - fieldType = aType; -} - -/** - * Set the field encoding of the underlying table field for displaying it to the user. - * - * @param aEncoding encoding - */ -- (void)setFieldEncoding:(NSString*)aEncoding -{ - fieldEncoding = aEncoding; -} - -/** - * Set if underlying table field allows NULL for several validations. - * - * @param allowNULL If allowNULL is YES NULL value is allowed for the underlying table field - */ -- (void)setAllowNULL:(BOOL)allowNULL -{ - _allowNULL = allowNULL; -} +#pragma mark - /** * Main method for editing data. It will validate several settings and display a modal sheet for theWindow whioch waits until the user closes the sheet. * * @param data The to be edited table field data. - * + * * @param fieldName The name of the currently edited table field. - * + * * @param anEncoding The used encoding while editing. - * + * * @param isFieldBlob If YES the underlying table field is a TEXT/BLOB field. This setting handles several controls which are offered in the sheet to the user. - * + * * @param isEditable If YES the underlying table field is editable, if NO the field is not editable and the SPFieldEditorController sheet do not show a "OK" button for saving. - * + * * @param theWindow The window for displaying the sheet. - * + * * @param sender The calling instance. - * + * * @param contextInfo context info for processing the edited data in sender. - * + * */ - (void)editWithObject:(id)data fieldName:(NSString*)fieldName usingEncoding:(NSStringEncoding)anEncoding - isObjectBlob:(BOOL)isFieldBlob isEditable:(BOOL)isEditable withWindow:(NSWindow *)theWindow + isObjectBlob:(BOOL)isFieldBlob isEditable:(BOOL)isEditable withWindow:(NSWindow *)theWindow sender:(id)sender contextInfo:(NSDictionary*)theContextInfo { @@ -251,10 +213,10 @@ NSInteger i = 0; NSInteger maxBit = (maxTextLength > 64) ? 64 : maxTextLength; if([bitSheetNULLButton state] == NSOffState) - for(i=0; i 0 && [[editTextView textStorage] length] > maxTextLength && ![[[editTextView textStorage] string] isEqualToString:[prefs objectForKey:SPNullValue]]) { - [editTextView setSelectedRange:NSMakeRange(maxTextLength, [[editTextView textStorage] length] - maxTextLength)]; - [editTextView scrollRangeToVisible:NSMakeRange([editTextView selectedRange].location,0)]; - [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Text is too long. Maximum text length is set to %llu.", @"Text is too long. Maximum text length is set to %llu."), maxTextLength]]; - return; - } - - editSheetReturnCode = 1; - } - else if(sender == bitSheetOkButton && _isEditable) { - editSheetReturnCode = 1; - } - - // Delete all QuickLook temp files if it was invoked - if(tmpFileName != nil) { - NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirPath error:nil]; - for (NSString *file in dirContents) { - if ([file hasPrefix:@"SequelProQuickLook"]) { - if(![[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", tmpDirPath, file] error:NULL]) { - NSLog(@"QL: Couldn't delete temporary file '%@/%@'.", tmpDirPath, file); - } - } - } - } - - [NSApp endSheet:usedSheet returnCode:1]; - [usedSheet orderOut:self]; - - if(callerInstance) { - id returnData = ( editSheetReturnCode && _isEditable ) ? (_isGeometry) ? [editTextView string] : sheetEditData : nil; - [callerInstance processFieldEditorResult:returnData contextInfo:contextInfo]; - } - +/** + * Set the field encoding of the underlying table field for displaying it to the user. + * + * @param aEncoding encoding + */ +- (void)setFieldEncoding:(NSString*)aEncoding +{ + fieldEncoding = aEncoding; } /** - * Open the open file panel to load a file (text/image) into the editSheet + * Set if underlying table field allows NULL for several validations. + * + * @param allowNULL If allowNULL is YES NULL value is allowed for the underlying table field */ -- (IBAction)openEditSheet:(id)sender +- (void)setAllowNULL:(BOOL)allowNULL { - [[NSOpenPanel openPanel] beginSheetForDirectory:nil - file:@"" - modalForWindow:usedSheet - modalDelegate:self didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) - contextInfo:NULL]; + _allowNULL = allowNULL; } /** @@ -549,6 +483,18 @@ } } +/** + * Open the open file panel to load a file (text/image) into the editSheet + */ +- (IBAction)openEditSheet:(id)sender +{ + [[NSOpenPanel openPanel] beginSheetForDirectory:nil + file:@"" + modalForWindow:usedSheet + modalDelegate:self didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) + contextInfo:NULL]; +} + /** * Open the save file panel to save the content of the editSheet according to its type as NSData or NSString atomically into the past file. */ @@ -575,50 +521,59 @@ contextInfo:NULL]; } +- (void)sheetDidEnd:(id)sheet returnCode:(NSInteger)returnCode contextInfo:(NSString *)contextInfo +{ + // Remember spell cheecker status + [prefs setBool:[editTextView isContinuousSpellCheckingEnabled] forKey:SPBlobTextEditorSpellCheckingEnabled]; +} + /** - * Save file panel didEndSelector. If the returnCode == NSOKButton it writes the current content of editSheet according to its type as NSData or NSString atomically into the past file. + * Close the editSheet. Before closing it validates the editSheet data against maximum text size. + * If data size is too long select the part which is to long for better editing and keep the sheet opened. + * If any temporary Quicklook files were created delete them before clsoing the sheet. */ -- (void)savePanelDidEnd:(NSSavePanel *)panel returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo +- (IBAction)closeEditSheet:(id)sender { - if (returnCode == NSOKButton) { - [editSheetProgressBar startAnimation:self]; - - NSString *fileName = [panel filename]; - - // Write binary field types directly to the file - if ( [sheetEditData isKindOfClass:[NSData class]] ) { - [sheetEditData writeToFile:fileName atomically:YES]; + editSheetReturnCode = 0; + // Validate the sheet data before saving them. + // - for max text length (except for NULL value string) select the part which won't be saved + // and suppress closing the sheet + if(sender == editSheetOkButton) { + if (maxTextLength > 0 && [[editTextView textStorage] length] > maxTextLength && ![[[editTextView textStorage] string] isEqualToString:[prefs objectForKey:SPNullValue]]) { + [editTextView setSelectedRange:NSMakeRange(maxTextLength, [[editTextView textStorage] length] - maxTextLength)]; + [editTextView scrollRangeToVisible:NSMakeRange([editTextView selectedRange].location,0)]; + [SPTooltip showWithObject:[NSString stringWithFormat:NSLocalizedString(@"Text is too long. Maximum text length is set to %llu.", @"Text is too long. Maximum text length is set to %llu."), maxTextLength]]; + return; } - else if ( [sheetEditData isKindOfClass:[MCPGeometryData class]] ) { - - if ( [editSheetSegmentControl selectedSegment] == 0 || editImage == nil ) { - - [[editTextView string] writeToFile:fileName - atomically:YES - encoding:encoding - error:NULL]; - } else if (editImage != nil){ - - SPGeometryDataView *v = [[[SPGeometryDataView alloc] initWithCoordinates:[sheetEditData coordinates] targetDimension:2000.0] autorelease]; - NSData *pdf = [v pdfData]; - if(pdf) - [pdf writeToFile:fileName atomically:YES]; + editSheetReturnCode = 1; + } + else if(sender == bitSheetOkButton && _isEditable) { + editSheetReturnCode = 1; + } + // Delete all QuickLook temp files if it was invoked + if(tmpFileName != nil) { + NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirPath error:nil]; + for (NSString *file in dirContents) { + if ([file hasPrefix:@"SequelProQuickLook"]) { + if(![[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", tmpDirPath, file] error:NULL]) { + NSLog(@"QL: Couldn't delete temporary file '%@/%@'.", tmpDirPath, file); + } } } - // Write other field types' representations to the file via the current encoding - else { - [[sheetEditData description] writeToFile:fileName - atomically:YES - encoding:encoding - error:NULL]; - } + } - [editSheetProgressBar stopAnimation:self]; + [NSApp endSheet:usedSheet returnCode:1]; + [usedSheet orderOut:self]; + + if(callerInstance) { + id returnData = ( editSheetReturnCode && _isEditable ) ? (_isGeometry) ? [editTextView string] : sheetEditData : nil; + [callerInstance processFieldEditorResult:returnData contextInfo:contextInfo]; } + } /** @@ -650,7 +605,6 @@ // set the image preview, string contents and hex representation [editImage setImage:image]; - if(contents) [editTextView setString:contents]; else @@ -687,6 +641,70 @@ } } +/** + * Save file panel didEndSelector. If the returnCode == NSOKButton it writes the current content of editSheet according to its type as NSData or NSString atomically into the past file. + */ +- (void)savePanelDidEnd:(NSSavePanel *)panel returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo +{ + if (returnCode == NSOKButton) { + + [editSheetProgressBar startAnimation:self]; + + NSString *fileName = [panel filename]; + + // Write binary field types directly to the file + if ( [sheetEditData isKindOfClass:[NSData class]] ) { + [sheetEditData writeToFile:fileName atomically:YES]; + + } + else if ( [sheetEditData isKindOfClass:[MCPGeometryData class]] ) { + + if ( [editSheetSegmentControl selectedSegment] == 0 || editImage == nil ) { + + [[editTextView string] writeToFile:fileName + atomically:YES + encoding:encoding + error:NULL]; + + } else if (editImage != nil){ + + SPGeometryDataView *v = [[[SPGeometryDataView alloc] initWithCoordinates:[sheetEditData coordinates] targetDimension:2000.0] autorelease]; + NSData *pdf = [v pdfData]; + if(pdf) + [pdf writeToFile:fileName atomically:YES]; + + } + } + // Write other field types' representations to the file via the current encoding + else { + [[sheetEditData description] writeToFile:fileName + atomically:YES + encoding:encoding + error:NULL]; + } + + [editSheetProgressBar stopAnimation:self]; + } +} + +#pragma mark - +#pragma mark Drop methods + +/** + * If the image was deleted reset all views in editSheet. + * The actual dropped image process is handled by (processUpdatedImageData:). + */ +- (IBAction)dropImage:(id)sender +{ + if ( [editImage image] == nil ) { + if (nil != sheetEditData) [sheetEditData release]; + sheetEditData = [[NSData alloc] init]; + [editTextView setString:@""]; + [hexTextView setString:@""]; + return; + } +} + #pragma mark - #pragma mark QuickLook @@ -703,7 +721,7 @@ /** * Create a temporary file in NSTemporaryDirectory() with the chosen extension type which will be called by Apple's Quicklook generator - * + * * @param type The type as file extension for Apple's default Quicklook generator. * * @param isText If YES the content of editSheet will be treates as pure text. @@ -753,7 +771,7 @@ /** * Opens QuickLook for current data if QuickLook is available - * + * * @param type The type as file extension for Apple's default Quicklook generator. * * @param isText If YES the content of editSheet will be treates as pure text. @@ -842,7 +860,6 @@ } - /** * QuickLook delegate for SDK 10.6. Set the Quicklook delegate to self and suppress setShowsAddToiPhotoButton since the format is unknow. */ @@ -932,9 +949,9 @@ // Return the App's middle point NSRect mwf = [[NSApp mainWindow] frame]; return NSMakeRect( - mwf.origin.x+mwf.size.width/2, - mwf.origin.y+mwf.size.height/2, - 5, 5); + mwf.origin.x+mwf.size.width/2, + mwf.origin.y+mwf.size.height/2, + 5, 5); } // QuickLook delegates for SDK 10.6 @@ -982,7 +999,7 @@ /** * Invoked if the imageView was changed or a file dragged and dropped onto it. - * + * * @param data The image data. If data == nil the reset all views in editSheet. */ - (void)processUpdatedImageData:(NSData *)data @@ -1017,21 +1034,6 @@ editSheetWillBeInitialized = NO; } -/** - * If the image was deleted reset all views in editSheet. - * The actual dropped image process is handled by (processUpdatedImageData:). - */ -- (IBAction)dropImage:(id)sender -{ - if ( [editImage image] == nil ) { - if (nil != sheetEditData) [sheetEditData release]; - sheetEditData = [[NSData alloc] init]; - [editTextView setString:@""]; - [hexTextView setString:@""]; - return; - } -} - #pragma mark - #pragma mark BIT Field Sheet @@ -1043,7 +1045,6 @@ NSInteger i = 0; NSInteger maxBit = (maxTextLength > 64) ? 64 : maxTextLength; - if([bitSheetNULLButton state] == NSOnState) { if ( sheetEditData != nil ) { [sheetEditData release]; @@ -1059,7 +1060,8 @@ NSMutableString *bitString = [NSMutableString string]; [bitString setString:@""]; - for(i=0; i 64) ? 64 : maxTextLength; if([sender state] == NSOnState) { - for(i=0; i