Revision: 28851
          http://sourceforge.net/p/bibdesk/svn/28851
Author:   hofman
Date:     2024-04-06 16:11:05 +0000 (Sat, 06 Apr 2024)
Log Message:
-----------
Update UI when publication's type changes, this can also happen with undo

Modified Paths:
--------------
    trunk/bibdesk/BDSKTextImportController.m

Modified: trunk/bibdesk/BDSKTextImportController.m
===================================================================
--- trunk/bibdesk/BDSKTextImportController.m    2024-04-06 15:27:13 UTC (rev 
28850)
+++ trunk/bibdesk/BDSKTextImportController.m    2024-04-06 16:11:05 UTC (rev 
28851)
@@ -91,6 +91,7 @@
 - (void)loadFromFileURL:(NSURL *)url;
 - (void)setShowingWebView:(BOOL)showWebView;
 - (void)setupTypeUI;
+- (void)updateType;
 - (void)updateColumnWidths;
 - (void)setType:(NSString *)type;
 
@@ -286,11 +287,15 @@
 
 - (IBAction)changeTypeOfBibAction:(id)sender{
     NSString *type = [[sender selectedItem] title];
-    [self setType:type];
+    BibItem *item = [self publication];
+    
+    if ([[item pubType] isEqualToString:type])
+        return;
+    
+    [item setPubType:type];
     [[NSUserDefaults standardUserDefaults] setObject:type 
forKey:BDSKPubTypeStringKey];
 
        [[self undoManager] setActionName:NSLocalizedString(@"Change Type", 
@"Undo action name")];
-    [itemTableView reloadData];
 }
 
 - (IBAction)importFromPasteboardAction:(id)sender{
@@ -556,9 +561,11 @@
        
        NSString *changeKey = [[notification userInfo] 
objectForKey:BDSKBibItemKeyKey];
     
-       if([changeKey isEqualToString:BDSKCiteKeyString]) {
-               [citeKeyField setStringValue:[item citeKey]];
+    if ([changeKey isEqualToString:BDSKCiteKeyString]) {
+        [citeKeyField setStringValue:[item citeKey]];
         [self setCiteKeyDuplicateWarning:[item isValidCiteKey:[item citeKey]] 
== NO];
+    } else if ([changeKey isEqualToString:BDSKPubTypeString]) {
+        [self updateType];
     } else {
         [itemTableView reloadData];
     }
@@ -706,13 +713,37 @@
     [itemTypeButton removeAllItems];
     [itemTypeButton addItemsWithTitles:[[BDSKTypeManager sharedManager] 
types]];
     
-    NSString *type = [[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKPubTypeStringKey];
+    [self updateType];
     
-    [self setType:type];
-    
     [itemTableView reloadData];
 }
 
+- (void)updateType{
+    NSString *type = [[self publication] pubType];
+    
+    [itemTypeButton selectItemWithTitle:type];
+    
+    BDSKTypeManager *typeMan = [BDSKTypeManager sharedManager];
+
+    [fields removeAllObjects];
+    
+    [fields addObjectsFromArray:[typeMan standardFieldsForType:type]];
+    [fields addNonDuplicateObjectsFromArray:@[BDSKAbstractString, 
BDSKAnnoteString]];
+    
+    NSMutableArray *extraFields = [NSMutableArray array];
+    NSSet *ignoredFields = [NSSet setWithObjects:BDSKDateAddedString, 
BDSKDateModifiedString, BDSKColorString, nil];
+    [[[self publication] pubFields] 
enumerateKeysAndObjectsUsingBlock:^(NSString *fieldName, NSString *value, BOOL 
*stop){
+        if ([NSString isEmptyAsComplexString:value] == NO && [fields 
containsObject:fieldName] == NO && [ignoredFields containsObject:fieldName] == 
NO)
+            [extraFields addObject:fieldName];
+    }];
+    if ([extraFields count]) {
+        [extraFields 
sortUsingSelector:@selector(caseInsensitiveNumericCompare:)];
+        [fields addObjectsFromArray:extraFields];
+    }
+    
+    [self updateColumnWidths];
+}
+
 - (void)setType:(NSString *)type{
     
     [itemTypeButton selectItemWithTitle:type];
@@ -1314,12 +1345,12 @@
 
 - (void)autoDiscoverFromFrameDictionary:(NSDictionary *)metaTagDict {
     BDSKTypeManager *typeMan = [BDSKTypeManager sharedManager];
-    
+    BibItem *item = [self publication];
+     
     for (NSString *metaName in metaTagDict) {
         NSString *fieldName = [typeMan fieldNameForDublinCoreTerm:metaName];
         fieldName = (fieldName ?: [metaName fieldName]);
         NSString *fieldValue = [metaTagDict objectForKey:metaName];
-        BibItem *item = [self publication];
         
         // Special-case DC.date to get month and year, but still insert 
"DC.date"
         //  to capture the day, which may be useful.
@@ -1353,10 +1384,11 @@
         
     }
     
-    NSString *bibtexType = [typeMan bibTeXTypeForDublinCoreType:[metaTagDict 
objectForKey:@"DC.type"]];
-    [self setType:(bibtexType ?: @"misc")];
-    
-    [itemTableView reloadData];
+    NSString *bibtexType = [typeMan bibTeXTypeForDublinCoreType:[metaTagDict 
objectForKey:@"DC.type"]] ?: @"misc";
+    if ([[item pubType] isEqualToString:bibtexType])
+        [self updateType];
+    else
+        [item setPubType:bibtexType];
 }
 
 - (void)autoDiscoverDataFromFrame:(WebFrame *)frame{
@@ -1468,8 +1500,10 @@
 - (void)autoDiscoverFromStringWithPublication:(BibItem *)pub {
     BibItem *item = [self publication];
     [item setPubFields:[pub pubFields]];
-    [self setType:[pub pubType]];
-    [itemTableView reloadData];
+    if ([[item pubType] isEqualToString:[pub pubType]])
+        [item setPubType:[pub pubType]];
+    else
+        [self updateType];
 }
 
 - (void)autoDiscoverDataFromString:(NSString *)string{

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

Reply via email to