Revision: 11884
          http://bibdesk.svn.sourceforge.net/bibdesk/?rev=11884&view=rev
Author:   hofman
Date:     2007-12-12 12:33:59 -0800 (Wed, 12 Dec 2007)

Log Message:
-----------
Treat empty string field values the same as nil when setting a field. We don't 
display custom fields that are equal to the empty string, but standard fields 
that are displayed may be nil. Remove addField: and removeField: methods, 
change fields only through setField:toValue:. Correct adding/removing/changing 
fields in the editor to take into account that empty fields may either be nil 
or an empty string. this also requires changes to the way we handle changes to 
the item.

Modified Paths:
--------------
    trunk/bibdesk/BDSKEditor.m
    trunk/bibdesk/BDSKTextImportController.m
    trunk/bibdesk/BibItem.h
    trunk/bibdesk/BibItem.m

Modified: trunk/bibdesk/BDSKEditor.m
===================================================================
--- trunk/bibdesk/BDSKEditor.m  2007-12-12 19:33:05 UTC (rev 11883)
+++ trunk/bibdesk/BDSKEditor.m  2007-12-12 20:33:59 UTC (rev 11884)
@@ -1295,25 +1295,19 @@
     [[self undoManager] setActionName:NSLocalizedString(@"Edit Publication", 
@"Undo action name")];
 }
 
-// 
----------------------------------------------------------------------------------------
-#pragma mark add-Field-Sheet Support
-// Add field sheet support
-// 
----------------------------------------------------------------------------------------
+#pragma mark Add field
 
-#warning Empty fields: this assumes the displayed fields are all non-nil 
fields, which may not be true
-
 - (void)addFieldSheetDidEnd:(BDSKAddFieldSheetController *)addFieldController 
returnCode:(int)returnCode contextInfo:(void *)contextInfo{
+    NSArray *currentFields = [(NSArray *)contextInfo autorelease];
        NSString *newField = [addFieldController field];
     if(returnCode == NSCancelButton || newField == nil)
         return;
     
-    NSArray *currentFields = [publication allFieldNames];
     newField = [newField fieldName];
     if([currentFields containsObject:newField] == NO){
                [tabView selectFirstTabViewItem:nil];
-        [publication addField:newField];
+        [publication setField:newField toValue:[NSString stringWithFormat:@"%@ 
%@",NSLocalizedString(@"Add data for field:", @"Default value for new field"), 
newField]];
                [[self undoManager] setActionName:NSLocalizedString(@"Add 
Field", @"Undo action name")];
-               [self setupFields];
                [self setKeyField:newField];
     }
 }
@@ -1321,45 +1315,46 @@
 // raises the add field sheet
 - (IBAction)raiseAddField:(id)sender{
     BDSKTypeManager *typeMan = [BDSKTypeManager sharedManager];
-    NSArray *currentFields = [publication allFieldNames];
-    NSArray *fieldNames = [typeMan allFieldNamesIncluding:[NSArray 
arrayWithObject:BDSKCrossrefString] excluding:currentFields];
+    NSArray *fieldNames;
+    NSMutableArray *currentFields = [fields mutableCopy];
     
+    [currentFields addObjectsFromArray:[[typeMan ratingFieldsSet] allObjects]];
+    [currentFields addObjectsFromArray:[[typeMan booleanFieldsSet] 
allObjects]];
+    [currentFields addObjectsFromArray:[[typeMan triStateFieldsSet] 
allObjects]];
+    [currentFields addObjectsFromArray:[[typeMan noteFieldsSet] allObjects]];
+    
+    fieldNames = [typeMan allFieldNamesIncluding:[NSArray 
arrayWithObject:BDSKCrossrefString] excluding:currentFields];
+    
     BDSKAddFieldSheetController *addFieldController = 
[[BDSKAddFieldSheetController alloc] initWithPrompt:NSLocalizedString(@"Name of 
field to add:", @"Label for adding field")
                                                                                
               fieldsArray:fieldNames];
        [addFieldController beginSheetModalForWindow:[self window]
                                    modalDelegate:self
                                   
didEndSelector:@selector(addFieldSheetDidEnd:returnCode:contextInfo:)
-                                     contextInfo:NULL];
+                                     contextInfo:currentFields];
     [addFieldController release];
 }
 
-// 
----------------------------------------------------------------------------------------
-#pragma mark ||  delete-Field-Sheet Support
-// 
----------------------------------------------------------------------------------------
+#pragma mark Delete field
 
-#warning Empty fields: this assumes the displayed fields are all non-nil 
fields, which may not be true
-
 - (void)removeFieldSheetDidEnd:(BDSKRemoveFieldSheetController 
*)removeFieldController returnCode:(int)returnCode contextInfo:(void 
*)contextInfo{
        NSString *oldField = [removeFieldController field];
     NSString *oldValue = [[[publication valueOfField:oldField] retain] 
autorelease];
     NSArray *removableFields = [removeFieldController fieldsArray];
-    if(returnCode == NSCancelButton || oldField == nil || [removableFields 
count] == 0)
-        return;
-       
-    [tabView selectFirstTabViewItem:nil];
-    [publication removeField:oldField];
-    [self userChangedField:oldField from:oldValue to:@""];
-    [[self undoManager] setActionName:NSLocalizedString(@"Remove Field", 
@"Undo action name")];
-    [self setupFields];
+    
+    if (returnCode == NSOKButton && oldField != nil && [removableFields 
count]) {
+        [tabView selectFirstTabViewItem:nil];
+        [publication setField:oldField toValue:nil];
+        [self userChangedField:oldField from:oldValue to:@""];
+        [[self undoManager] setActionName:NSLocalizedString(@"Remove Field", 
@"Undo action name")];
+        [self setupFields];
+    }
 }
 
-// raises the del field sheet
 - (IBAction)raiseDelField:(id)sender{
     // populate the popupbutton
     NSString *currentType = [publication pubType];
        BDSKTypeManager *typeMan = [BDSKTypeManager sharedManager];
-       NSMutableArray *removableFields = [[publication allFieldNames] 
mutableCopy];
-       [removableFields removeObjectsInArray:[NSArray 
arrayWithObjects:BDSKAnnoteString, BDSKAbstractString, 
BDSKRssDescriptionString, nil]];
+       NSMutableArray *removableFields = [fields mutableCopy];
        [removableFields removeObjectsInArray:[typeMan 
requiredFieldsForType:currentType]];
        [removableFields removeObjectsInArray:[typeMan 
optionalFieldsForType:currentType]];
        [removableFields removeObjectsInArray:[typeMan 
userDefaultFieldsForType:currentType]];
@@ -1392,81 +1387,62 @@
 
 #pragma mark Change field name
 
-#warning Empty fields: this assumes the displayed fields are all non-nil 
fields, which may not be true
-
 - (void)changeFieldSheetDidEnd:(BDSKChangeFieldSheetController 
*)changeFieldController returnCode:(int)returnCode contextInfo:(void 
*)contextInfo{
        NSString *oldField = [changeFieldController field];
     NSString *newField = [changeFieldController newField];
     NSString *oldValue = [[[publication valueOfField:oldField] retain] 
autorelease];
     int autoGenerateStatus = 0;
     
-    if(returnCode == NSCancelButton || [NSString isEmptyString:newField] || 
-       [newField isEqualToString:oldField] || [[publication allFieldNames] 
containsObject:newField])
-        return;
-    
-    NSString *currentType = [publication pubType];
-    BDSKTypeManager *typeMan = [BDSKTypeManager sharedManager];
-    NSMutableSet *nonNilFields = [NSMutableSet 
setWithObjects:BDSKAnnoteString, BDSKAbstractString, BDSKRssDescriptionString, 
nil];
-       [nonNilFields addObjectsFromArray:[typeMan 
requiredFieldsForType:currentType]];
-       [nonNilFields addObjectsFromArray:[typeMan 
optionalFieldsForType:currentType]];
-       [nonNilFields addObjectsFromArray:[typeMan 
userDefaultFieldsForType:currentType]];
-    
-    [tabView selectFirstTabViewItem:nil];
-    [publication addField:newField];
-    [publication setField:newField toValue:[publication 
valueOfField:oldField]];
-    if([nonNilFields containsObject:oldField])
-        [publication setField:oldField toValue:@""];
-    else
-        [publication removeField:oldField];
-    autoGenerateStatus = [self userChangedField:oldField from:oldValue to:@""];
-    [self userChangedField:newField from:@"" to:oldValue 
didAutoGenerate:autoGenerateStatus];
-    [[self undoManager] setActionName:NSLocalizedString(@"Change Field Name", 
@"Undo action name")];
-    [self setupFields];
-    [self setKeyField:newField];
+    if (returnCode == NSOKButton && [NSString isEmptyString:newField] == NO  
&& 
+        [newField isEqualToString:oldField] == NO && [fields 
containsObject:newField] == NO) {
+        
+        [tabView selectFirstTabViewItem:nil];
+        [publication setField:newField toValue:[publication 
valueOfField:oldField inherit:NO]];
+        autoGenerateStatus = [self userChangedField:oldField from:oldValue 
to:@""];
+        [self userChangedField:newField from:@"" to:oldValue 
didAutoGenerate:autoGenerateStatus];
+        [[self undoManager] setActionName:NSLocalizedString(@"Change Field 
Name", @"Undo action name")];
+        [self setKeyField:newField];
+    }
 }
 
 - (void)raiseChangeFieldSheetForField:(NSString *)field{
     BDSKTypeManager *typeMan = [BDSKTypeManager sharedManager];
-    NSArray *currentFields = [publication allFieldNames];
-    NSArray *fieldNames = [typeMan allFieldNamesIncluding:[NSArray 
arrayWithObject:BDSKCrossrefString] excluding:currentFields];
-       NSMutableArray *removableFields = [[publication allFieldNames] 
mutableCopy];
-    [removableFields removeObjectsInArray:[[typeMan noteFieldsSet] 
allObjects]];
+    NSArray *fieldNames;
+    NSMutableArray *currentFields = [fields mutableCopy];
     
-    if([removableFields count] == 0){
+    [currentFields addObjectsFromArray:[[typeMan ratingFieldsSet] allObjects]];
+    [currentFields addObjectsFromArray:[[typeMan booleanFieldsSet] 
allObjects]];
+    [currentFields addObjectsFromArray:[[typeMan triStateFieldsSet] 
allObjects]];
+    [currentFields addObjectsFromArray:[[typeMan noteFieldsSet] allObjects]];
+    
+    fieldNames = [typeMan allFieldNamesIncluding:[NSArray 
arrayWithObject:BDSKCrossrefString] excluding:currentFields];
+    
+    if([fields count] == 0){
+        [currentFields release];
         NSBeep();
-        [removableFields release];
         return;
     }
     
     BDSKChangeFieldSheetController *changeFieldController = 
[[BDSKChangeFieldSheetController alloc] initWithPrompt:NSLocalizedString(@"Name 
of field to change:", @"Label for changing field name")
-                                                                               
                        fieldsArray:removableFields
+                                                                               
                        fieldsArray:fields
                                                                                
                          newPrompt:NSLocalizedString(@"New field name:", 
@"Label for changing field name")
                                                                                
                     newFieldsArray:fieldNames];
-    if (field == nil) {
-        int selectedRow = [tableView selectedRow];
-        field = selectedRow == -1 ? nil : [fields objectAtIndex:selectedRow];
-        if([removableFields containsObject:field] == NO)
-            field = nil;
-    }
+    if (field == nil)
+        field = [tableView selectedRow] == -1 ? nil : [fields 
objectAtIndex:[tableView selectedRow]];
     
-    if([removableFields containsObject:field]){
-        [changeFieldController setField:field];
-        // if we don't deselect this cell, we can't remove it from the form
-        [self finalizeChangesPreservingSelection:NO];
-    }else if(field){
-        // double clicked title of a field we cannot change
-        [changeFieldController release];
-        [removableFields release];
-        return;
-    }
+    OBASSERT(field == nil || [fields containsObject:field]);
     
-       [removableFields release];
+    // if we don't deselect this cell, we can't remove it from the form
+    [self finalizeChangesPreservingSelection:NO];
     
+    [changeFieldController setField:field];
+    
        [changeFieldController beginSheetModalForWindow:[self window]
                                       modalDelegate:self
                                      
didEndSelector:@selector(changeFieldSheetDidEnd:returnCode:contextInfo:)
                                         contextInfo:NULL];
        [changeFieldController release];
+    [currentFields release];
 }
 
 - (IBAction)raiseChangeFieldName:(id)sender{
@@ -1902,11 +1878,11 @@
 }
 
 - (void)bibDidChange:(NSNotification *)notification{
-// unused      BibItem *notifBib = [notification object];
        NSDictionary *userInfo = [notification userInfo];
        NSString *changeType = [userInfo objectForKey:@"type"];
        NSString *changeKey = [userInfo objectForKey:@"key"];
        NSString *newValue = [userInfo objectForKey:@"value"];
+       NSString *oldValue = [userInfo objectForKey:@"oldValue"];
        BibItem *sender = (BibItem *)[notification object];
        NSString *crossref = [publication valueOfField:BDSKCrossrefString 
inherit:NO];
        OFPreferenceWrapper *pw = [OFPreferenceWrapper sharedPreferenceWrapper];
@@ -1917,12 +1893,19 @@
     // If it is not our item or his crossref parent, we don't care, but our 
parent may have changed his cite key
        if (sender != publication && !parentDidChange)
                return;
+       
+    // these should always be updated
+    if([changeKey isEqualToString:BDSKTitleString] || [changeKey 
isEqualToString:BDSKChapterString] || [changeKey 
isEqualToString:BDSKPagesString]){
+               [[self window] setTitle:[publication displayTitle]];
+       }
+       else if([changeKey isPersonField]){
+               [authorTableView reloadData];
+       }
 
-       if([changeType isEqualToString:@"Add/Del Field"]){
-               if(![[pw stringArrayForKey:BDSKRatingFieldsKey] 
containsObject:changeKey] &&
-                  ![[pw stringArrayForKey:BDSKBooleanFieldsKey] 
containsObject:changeKey] &&
-                  ![[pw stringArrayForKey:BDSKTriStateFieldsKey] 
containsObject:changeKey]){
-                       // no need to rebuild the form when we have a field in 
the matrix
+       if ([changeType isEqualToString:@"Change Field"]) {
+        if ((([NSString isEmptyAsComplexString:newValue] && [fields 
containsObject:changeKey]) || 
+             ([NSString isEmptyAsComplexString:oldValue] && [fields 
containsObject:changeKey] == NO)) &&
+            [changeKey isRatingField] == NO && [changeKey isBooleanField] == 
NO && [changeKey isTriStateField] == NO && [changeKey isNoteField] == NO) {
                        [self setupFields];
                        return;
                }
@@ -1943,45 +1926,16 @@
         [self setupFields];
                [[self window] setTitle:[publication displayTitle]];
                [authorTableView reloadData];
-               return;
        }
-
-       if([changeKey isEqualToString:BDSKPubTypeString]){
+       else if([changeKey isEqualToString:BDSKPubTypeString]){
                [self setupFields];
                [self updateTypePopup];
-               return;
        }
-       
-       if([[pw stringArrayForKey:BDSKRatingFieldsKey] 
containsObject:changeKey] || 
-          [[pw stringArrayForKey:BDSKBooleanFieldsKey] 
containsObject:changeKey] || 
-          [[pw stringArrayForKey:BDSKTriStateFieldsKey] 
containsObject:changeKey]){
-               
-               NSEnumerator *cellE = [[extraBibFields cells] objectEnumerator];
-               NSButtonCell *entry = nil;
-               while(entry = [cellE nextObject]){
-                       if([[entry representedObject] 
isEqualToString:changeKey]){
-                               [entry setIntValue:[publication 
intValueOfField:changeKey]];
-                               [extraBibFields setNeedsDisplay:YES];
-                               break;
-                       }
-               }
-               return;
-       }
-       
-       if([changeKey isEqualToString:BDSKCiteKeyString]){
+       else if([changeKey isEqualToString:BDSKCiteKeyString]){
                [citeKeyField setStringValue:newValue];
                [self updateCiteKeyAutoGenerateStatus];
         [self updateCiteKeyDuplicateWarning];
-       }else{
-               [tableView reloadData];
        }
-       
-    if([changeKey isEqualToString:BDSKTitleString] || [changeKey 
isEqualToString:BDSKChapterString] || [changeKey 
isEqualToString:BDSKPagesString]){
-               [[self window] setTitle:[publication displayTitle]];
-       }
-       else if([changeKey isPersonField]){
-               [authorTableView reloadData];
-       }
     else if([changeKey isEqualToString:BDSKAnnoteString]){
         if(ignoreFieldChange) return;
         // make a copy of the current value, so we don't overwrite it when we 
set the field value to the text storage
@@ -2010,7 +1964,22 @@
             [[self window] makeFirstResponder:[self window]];
         [rssDescriptionViewUndoManager removeAllActions];
     }
-            
+       else if([changeKey isRatingField] || [changeKey isBooleanField] || 
[changeKey isTriStateField]){
+               
+               NSEnumerator *cellE = [[extraBibFields cells] objectEnumerator];
+               NSButtonCell *entry = nil;
+               while(entry = [cellE nextObject]){
+                       if([[entry representedObject] 
isEqualToString:changeKey]){
+                               [entry setIntValue:[publication 
intValueOfField:changeKey]];
+                               [extraBibFields setNeedsDisplay:YES];
+                               break;
+                       }
+               }
+       }
+    else{
+               [tableView reloadData];
+       }
+    
 }
        
 - (void)bibWasAddedOrRemoved:(NSNotification *)notification{
@@ -2879,7 +2848,7 @@
     e = [newFields objectEnumerator]; \
     while(tmp = [e nextObject]){ \
         if ([ignoredKeys containsObject:tmp]) continue; \
-        if (checkEmpty && [[publication valueOfField:tmp inherit:NO] 
isEqualToString:@""]) continue; \
+        if (checkEmpty && [[publication valueOfField:tmp inherit:NO] 
isEqualAsComplexString:@""]) continue; \
         [ignoredKeys addObject:tmp]; \
         [fields addObject:tmp]; \
     }
@@ -3150,15 +3119,9 @@
     
     // This is a fix for bug #1483613 (and others).  We set some of the 
BibItem's fields to -[[NSTextView textStorage] mutableString] for efficiency in 
tracking changes for live editing updates in the main window preview.  However, 
this causes a retain cycle, as the text storage retains its text view; any font 
changes to the editor text view will cause the retained textview to message its 
delegate (BDSKEditor) which is garbage in -[NSTextView _addToTypingAttributes].
     NSEnumerator *fieldE = [[[BDSKTypeManager sharedManager] noteFieldsSet] 
objectEnumerator];
-    NSString *currentValue = nil;
-    NSString *fieldName = nil;
-    while(fieldName = [fieldE nextObject]){
-        currentValue = [[publication valueOfField:fieldName inherit:NO] copy];
-        // set without undo, or we dirty the document every time the editor is 
closed
-        if(nil != currentValue)
-            [publication setField:fieldName toValueWithoutUndo:currentValue];
-        [currentValue release];
-    }
+    NSString *field = nil;
+    while(field = [fieldE nextObject])
+        [publication replaceValueOfFieldByCopy:field];
 }
 
 @end

Modified: trunk/bibdesk/BDSKTextImportController.m
===================================================================
--- trunk/bibdesk/BDSKTextImportController.m    2007-12-12 19:33:05 UTC (rev 
11883)
+++ trunk/bibdesk/BDSKTextImportController.m    2007-12-12 20:33:59 UTC (rev 
11884)
@@ -384,7 +384,7 @@
     int row = [fields count];
     
     [fields addObject:newField];
-    [item addField:newField];
+    [item setField:newField toValue:[NSString stringWithFormat:@"%@ 
%@",NSLocalizedString(@"Add data for field:", @"Default value for new field"), 
newField]];
     [[item undoManager] setActionName:NSLocalizedString(@"Add Field", @"Undo 
action name")];
     [itemTableView reloadData];
     [itemTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] 
byExtendingSelection:NO];

Modified: trunk/bibdesk/BibItem.h
===================================================================
--- trunk/bibdesk/BibItem.h     2007-12-12 19:33:05 UTC (rev 11883)
+++ trunk/bibdesk/BibItem.h     2007-12-12 20:33:59 UTC (rev 11884)
@@ -630,14 +630,7 @@
 - (void)setField: (NSString *)key toValue: (NSString *)value;
 - (void)setField: (NSString *)key toValue: (NSString *)value 
withModDate:(NSCalendarDate *)date;
 
-/*!
-    @method     setField:toValueWithoutUndo:
-    @abstract   Bypasses the undo and change notification mechanisms.  Only 
for use by BDSKEditor.
-    @discussion (comprehensive description)
-    @param      key (description)
-    @param      value (description)
-*/
-- (void)setField:(NSString *)key toValueWithoutUndo:(NSString *)value;
+- (void)replaceValueOfFieldByCopy:(NSString *)key;
 
 /*!
     @method valueOfField:
@@ -658,12 +651,6 @@
 */
 - (NSString *)valueOfField: (NSString *)key inherit: (BOOL)inherit;
 
-- (void)removeField: (NSString *)key;
-- (void)removeField: (NSString *)key withModDate:(NSCalendarDate *)date;
-
-- (void)addField:(NSString *)key;
-- (void)addField:(NSString *)key withModDate:(NSCalendarDate *)date;
-
 - (NSDictionary *)pubFields;
 - (NSArray *)allFieldNames;
 

Modified: trunk/bibdesk/BibItem.m
===================================================================
--- trunk/bibdesk/BibItem.m     2007-12-12 19:33:05 UTC (rev 11883)
+++ trunk/bibdesk/BibItem.m     2007-12-12 20:33:59 UTC (rev 11884)
@@ -1135,7 +1135,13 @@
     OBPRECONDITION(key != nil);
     // use a copy of the old value, since this may be a mutable value
     NSString *oldValue = [[pubFields objectForKey:key] copy];
-       if ([self undoManager]) {
+    if ([oldValue isEqualAsComplexString:@""]) {
+        [oldValue release];
+        oldValue = nil;
+    }
+    if ([value isEqualAsComplexString:@""] && [key isNoteField] == NO)
+        value = nil;
+    if ([self undoManager]) {
                NSCalendarDate *oldModDate = [self dateModified];
                
                [[[self undoManager] prepareWithInvocationTarget:self] 
setField:key 
@@ -1143,25 +1149,16 @@
                                                                                
                         withModDate:oldModDate];
        }
        
-    if(value != nil){
-               [pubFields setObject:value forKey:key];
-               // to allow autocomplete:
+    [pubFields setValue:value forKey:key];
+    // to allow autocomplete:
+    if (value)
                [[NSApp delegate] addString:value forCompletionEntry:key];
-       }else{
-               [pubFields removeObjectForKey:key];
-       }
-       if (date != nil) {
-               [pubFields setObject:[date description] 
forKey:BDSKDateModifiedString];
-       } else {
-               [pubFields removeObjectForKey:BDSKDateModifiedString];
-       }
+    [pubFields setValue:[date description] forKey:BDSKDateModifiedString];
        [self updateMetadataForKey:key];
        
-       NSDictionary *notifInfo;
-       if(oldValue != nil && value != nil)
-               notifInfo = [NSDictionary dictionaryWithObjectsAndKeys:value, 
@"value", key, @"key", @"Change", @"type", oldValue, @"oldValue", owner, 
@"owner", nil];
-       else
-               notifInfo = [NSDictionary dictionaryWithObjectsAndKeys:key, 
@"key", @"Add/Del Field", @"type", owner, @"owner", nil];
+       NSMutableDictionary *notifInfo = [NSMutableDictionary 
dictionaryWithObjectsAndKeys:key, @"key", @"Change Field", @"type", owner, 
@"owner", nil];
+    [notifInfo setValue:value forKey:@"value"];
+    [notifInfo setValue:oldValue forKey:@"oldValue"];
     [oldValue release];
     
        [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKBibItemChangedNotification
@@ -1169,12 +1166,13 @@
                                                                                
                          userInfo:notifInfo];
 }
 
-- (void)setField:(NSString *)key toValueWithoutUndo:(NSString *)value{
+- (void)replaceValueOfFieldByCopy:(NSString *)key{
     NSParameterAssert(nil != key);
-    NSParameterAssert(nil != value);
     // this method is intended as a workaround for a BDSKEditor issue with 
using -[NSTextStorage mutableString] to track changes
-    OBPRECONDITION([value isEqualToString:[pubFields objectForKey:key]]);
-    [pubFields setObject:value forKey:key];
+    NSString *value = [[pubFields objectForKey:key] copy];
+    if (value)
+        [pubFields setObject:value forKey:key];
+    [value release];
 }
 
 - (NSString *)valueOfField: (NSString *)key{
@@ -1182,85 +1180,22 @@
 }
 
 - (NSString *)valueOfField: (NSString *)key inherit: (BOOL)inherit{
-    NSString* value = [pubFields objectForKey:key];
+    NSString *value = [pubFields objectForKey:key];
        
        if (inherit && [NSString isEmptyAsComplexString:value] && 
[fieldsToWriteIfEmpty containsObject:key] == NO) {
                BibItem *parent = [self crossrefParent];
-               if (parent) {
+               value = nil;
+        if (parent) {
                        NSString *parentValue = [parent valueOfField:key 
inherit:NO];
                        if ([NSString isEmptyAsComplexString:parentValue] == NO)
-                               return [NSString 
stringWithInheritedValue:parentValue];
+                               value = [NSString 
stringWithInheritedValue:parentValue];
                }
        }
        
-       return value;
+    // @@ empty fields: or should we return nil for empty fields?
+       return [NSString isEmptyAsComplexString:value] ? @"" : value;
 }
 
-- (void)addField:(NSString *)key{
-       [self addField:key withModDate:[NSCalendarDate date]];
-}
-
-- (void)addField:(NSString *)key withModDate:(NSCalendarDate *)date{
-       if ([self undoManager]) {
-               [[[self undoManager] prepareWithInvocationTarget:self] 
removeField:key
-                                                                               
                                withModDate:[self dateModified]];
-       }
-       
-       NSString *defaultValue = nil;
-       if ([key isBooleanField] || [key isTriStateField] || [key 
isRatingField]) {
-        defaultValue = @"";
-    } else {
-        defaultValue = [NSString stringWithFormat:@"%@ 
%@",NSLocalizedString(@"Add data for field:", @"Default value for new field"), 
key];
-    }
-    [self setField:key toValue:defaultValue];
-       
-       if (date != nil) {
-               [pubFields setObject:[date description] 
forKey:BDSKDateModifiedString];
-       } else {
-               [pubFields removeObjectForKey:BDSKDateModifiedString];
-       }
-       [self updateMetadataForKey:key];
-       
-       NSDictionary *notifInfo = [NSDictionary 
dictionaryWithObjectsAndKeys:key, @"key", @"Add/Del Field", @"type", owner, 
@"owner", nil];
-       [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKBibItemChangedNotification
-                                                                               
                                object:self
-                                                                               
                          userInfo:notifInfo];
-
-}
-
-- (void)removeField: (NSString *)key{
-       [self removeField:key withModDate:[NSCalendarDate date]];
-}
-
-- (void)removeField: (NSString *)key withModDate:(NSCalendarDate *)date{
-       
-    OBPRECONDITION(key != nil);
-    
-       if ([self undoManager]) {
-        if(![NSString isEmptyString:[pubFields objectForKey:key]])
-            // this will ensure that the current value can be restored when 
the user deletes a non-empty field
-            [self setField:key toValue:@""];
-        
-               [[[self undoManager] prepareWithInvocationTarget:self] 
addField:key
-                                                            withModDate:[self 
dateModified]];
-       }
-       
-    [pubFields removeObjectForKey:key];
-       
-       if (date != nil) {
-               [pubFields setObject:[date description] 
forKey:BDSKDateModifiedString];
-       } else {
-               [pubFields removeObjectForKey:BDSKDateModifiedString];
-       }
-       [self updateMetadataForKey:key];
-
-       NSDictionary *notifInfo = [NSDictionary 
dictionaryWithObjectsAndKeys:@"Add/Del Field", @"type", owner, @"owner", nil];
-       [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKBibItemChangedNotification
-                                                                               
                                object:self
-                                                                               
                          userInfo:notifInfo];
-       
-}
-
 #pragma mark Derived field values
 
 - (id)valueForUndefinedKey:(NSString *)key{
@@ -1349,8 +1284,6 @@
 }
 
 - (void)setField:(NSString *)field 
toTriStateValue:(NSCellStateValue)triStateValue{
-       if(![[self allFieldNames] containsObject:field])
-               [self addField:field];
        [self setField:field toValue:[NSString 
stringWithTriStateValue:triStateValue]];
 }
 
@@ -3493,12 +3426,8 @@
             }
             else NSLog(@"*** Unable to create file for %@", value);
         }
-        if (removeField && converted) {
-            if ([[[BDSKTypeManager sharedManager] 
userDefaultFieldsForType:[self pubType]] containsObject:(id)key])
-                [self setField:(id)key toValue:@""];
-            else
-                [self removeField:(id)key];
-        }
+        if (removeField && converted)
+            [self setField:(id)key toValue:nil];
     }
 }
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to