Revision: 27468 http://sourceforge.net/p/bibdesk/svn/27468 Author: hofman Date: 2022-05-23 15:00:23 +0000 (Mon, 23 May 2022) Log Message: ----------- Combine methods to reset fields and reload the table
Modified Paths: -------------- trunk/bibdesk/BDSKEditor.m Modified: trunk/bibdesk/BDSKEditor.m =================================================================== --- trunk/bibdesk/BDSKEditor.m 2022-05-23 14:38:54 UTC (rev 27467) +++ trunk/bibdesk/BDSKEditor.m 2022-05-23 15:00:23 UTC (rev 27468) @@ -116,10 +116,7 @@ - (void)setupCollectionView; - (void)collectionViewFrameDidChange:(NSNotification *)notification; - (void)setupTypePopUp; -- (NSArray *)currentFields; -- (void)resetFields; -- (void)resetFieldsIfNeeded; -- (void)reloadTable; +- (void)resetFields:(BOOL)alwaysReload; - (void)reloadTableWithFields:(NSArray *)newFields; - (void)registerForNotifications; - (void)breakTextStorageConnections; @@ -220,7 +217,7 @@ citeKeyConstraint = [[NSLayoutConstraint constraintWithWidth:[citeKeyTitle intrinsicContentSize].width forItem:citeKeyTitle] retain]; [citeKeyTitle addConstraint:citeKeyConstraint]; - [self resetFields]; + [self resetFields:YES]; [self setupCollectionView]; if (editorFlags.isEditable) [tableView registerForDraggedTypes:[NSArray arrayWithObjects:BDSKPasteboardTypePublications, NSPasteboardTypeString, (NSString *)kUTTypeURL, (NSString *)kUTTypeFileURL, NSURLPboardType, NSFilenamesPboardType, nil]]; @@ -794,7 +791,7 @@ addedFields = [[NSMutableSet alloc] init]; [addedFields addObject:newField]; [tabView selectFirstTabViewItem:nil]; - [self resetFields]; + [self resetFields:YES]; [self setKeyField:newField]; } }]; @@ -842,7 +839,7 @@ [addedFields removeObject:oldField]; [tabView selectFirstTabViewItem:nil]; if ([NSString isEmptyString:oldValue]) { - [self resetFields]; + [self resetFields:YES]; } else { [publication setField:oldField toValue:nil]; [[self undoManager] setActionName:NSLocalizedString(@"Remove Field", @"Undo action name")]; @@ -2429,7 +2426,7 @@ [self updatePreviewing]; } else if([changeKey isEqualToString:BDSKPubTypeString]){ - [self resetFieldsIfNeeded]; + [self resetFields:NO]; [self updateTypePopup]; [self synchronizeWindowTitleWithDocumentName]; } @@ -2441,10 +2438,10 @@ [[[self textFieldAtRow:editedRow] currentEditor] setString:newValue ?: @""]; if ([changeKey isEqualToString:BDSKCrossrefString] && [NSString isEmptyString:newValue] != [NSString isEmptyString:oldValue]) { // Crossref field was added or removed - [self resetFields]; + [self resetFields:YES]; } else { // every field value could change, but not the displayed field names - [self reloadTable]; + [self reloadTableWithFields:nil]; } [authorTableView reloadData]; [self synchronizeWindowTitleWithDocumentName]; @@ -2495,7 +2492,7 @@ NSTextField *textField = [self textFieldAtRow:editedRow]; [[textField currentEditor] setString:[textField formatter] ? [[textField formatter] editingStringForObjectValue:tmpValue] : tmpValue]; } - [self resetFields]; + [self resetFields:YES]; [self setupCollectionView]; [self synchronizeWindowTitleWithDocumentName]; [authorTableView reloadData]; @@ -2534,7 +2531,7 @@ BDSKEditorTextField *textField = [self textFieldAtRow:editedRow]; [[textField currentEditor] setString:[textField formatter] ? [[textField formatter] editingStringForObjectValue:tmpValue] : tmpValue]; } - [self resetFields]; + [self resetFields:YES]; } else { // a field value changed NSUInteger row = [fields indexOfObject:changeKey]; @@ -2565,7 +2562,7 @@ for (id pub in pubs) { if ([crossref isCaseInsensitiveEqual:[pub citeKey]]) { // changes in the parent cannot change the field names, as custom fields are never inherited - [self reloadTable]; + [self reloadTableWithFields:nil]; break; } } @@ -2575,7 +2572,7 @@ - (void)typeInfoDidChange:(NSNotification *)aNotification{ if ([self commitEditing]) { [self setupTypePopUp]; - [self resetFieldsIfNeeded]; + [self resetFields:NO]; } } @@ -2583,7 +2580,7 @@ // ensure that the pub updates first, since it observes this notification also [publication customFieldsDidChange:aNotification]; if ([self commitEditing]) { - [self resetFieldsIfNeeded]; + [self resetFields:NO]; [self setupCollectionView]; [authorTableView reloadData]; } @@ -2594,7 +2591,7 @@ if(changedOwner == nil || changedOwner == [publication owner]) { for (NSString *field in fields) { if ([[publication valueOfField:field] isComplex] && [self commitEditing]) { - [self reloadTable]; + [self reloadTableWithFields:nil]; break; } } @@ -3349,46 +3346,6 @@ @implementation BDSKEditor (Private) -- (NSArray *)currentFields { - // build the new set of fields - NSMutableArray *currentFields = [NSMutableArray array]; - NSMutableArray *allFields = [[NSMutableArray alloc] init]; - NSString *field; - BDSKTypeManager *tm = [BDSKTypeManager sharedManager]; - NSString *type = [publication pubType]; - NSMutableSet *ignoredKeys = [[NSMutableSet alloc] initWithObjects:BDSKDateAddedString, BDSKDateModifiedString, BDSKColorString, nil]; - - [ignoredKeys unionSet:[tm noteFieldsSet]]; - [ignoredKeys unionSet:[tm integerFieldsSet]]; - - [allFields addObjectsFromArray:[tm standardFieldsForType:type]]; - - for (field in allFields) { - if ([ignoredKeys containsObject:field] == NO) { - [ignoredKeys addObject:field]; - [currentFields addObject:field]; - } - } - - [allFields release]; - allFields = [[publication allFieldNames] mutableCopy]; - [allFields addNonDuplicateObjectsFromArray:[addedFields allObjects]]; - [allFields sortUsingSelector:@selector(caseInsensitiveNumericCompare:)]; - - for (field in allFields) { - if ([ignoredKeys containsObject:field] == NO) { - [ignoredKeys addObject:field]; - if ([addedFields containsObject:field] || NO == [[publication valueOfField:field inherit:NO] isEqualAsComplexString:@""]) - [currentFields addObject:field]; - } - } - - [allFields release]; - [ignoredKeys release]; - - return currentFields; -} - - (void)reloadTableWithFields:(NSArray *)newFields{ // if we were editing in the tableView, we will restore the selected cell and the selection NSString *editedTitle = nil; @@ -3401,10 +3358,13 @@ [NSException raise:NSInternalInconsistencyException format:@"Failed to commit edits in %s, trouble ahead", __func__]; } - if (newFields && [fields isEqualToArray:newFields] == NO) { + // newFields should always be different from fields or nil + if (newFields) { [fields setArray:newFields]; + editorFlags.didSetupFields = YES; + // align the cite key field with the form cells if ([fields count] > 0) { NSTableColumn *tableColumn = [tableView tableColumnWithIdentifier:@"field"]; @@ -3441,23 +3401,48 @@ } } -- (void)reloadTable { - [self reloadTableWithFields:nil]; -} - -- (void)resetFields{ - [self reloadTableWithFields:[self currentFields]]; +- (void)resetFields:(BOOL)alwaysReload{ + // build the new set of fields + NSMutableArray *currentFields = [NSMutableArray array]; + NSMutableArray *allFields = [[NSMutableArray alloc] init]; + NSString *field; + BDSKTypeManager *tm = [BDSKTypeManager sharedManager]; + NSString *type = [publication pubType]; + NSMutableSet *ignoredKeys = [[NSMutableSet alloc] initWithObjects:BDSKDateAddedString, BDSKDateModifiedString, BDSKColorString, nil]; - editorFlags.didSetupFields = YES; -} - -- (void)resetFieldsIfNeeded{ - NSArray *currentFields = [self currentFields]; + [ignoredKeys unionSet:[tm noteFieldsSet]]; + [ignoredKeys unionSet:[tm integerFieldsSet]]; - if ([fields isEqualToArray:currentFields] == NO) { + [allFields addObjectsFromArray:[tm standardFieldsForType:type]]; + + for (field in allFields) { + if ([ignoredKeys containsObject:field] == NO) { + [ignoredKeys addObject:field]; + [currentFields addObject:field]; + } + } + + [allFields release]; + allFields = [[publication allFieldNames] mutableCopy]; + [allFields addNonDuplicateObjectsFromArray:[addedFields allObjects]]; + [allFields sortUsingSelector:@selector(caseInsensitiveNumericCompare:)]; + + for (field in allFields) { + if ([ignoredKeys containsObject:field] == NO) { + [ignoredKeys addObject:field]; + if ([addedFields containsObject:field] || NO == [[publication valueOfField:field inherit:NO] isEqualAsComplexString:@""]) + [currentFields addObject:field]; + } + } + + [allFields release]; + [ignoredKeys release]; + + if ([fields isEqualToArray:currentFields] == NO) + currentFields = nil; + + if (alwaysReload || currentFields) [self reloadTableWithFields:currentFields]; - editorFlags.didSetupFields = YES; - } } - (CGFloat)addButtonToCollectionviewItem:(BDSKEditorCollectionViewItem *)item { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit