Revision: 26197
          http://sourceforge.net/p/bibdesk/svn/26197
Author:   hofman
Date:     2021-06-10 16:15:34 +0000 (Thu, 10 Jun 2021)
Log Message:
-----------
Common superclass for tableview that can handle complex string values

Added Paths:
-----------
    trunk/bibdesk/BDSKComplexStringTableView.h
    trunk/bibdesk/BDSKComplexStringTableView.m

Added: trunk/bibdesk/BDSKComplexStringTableView.h
===================================================================
--- trunk/bibdesk/BDSKComplexStringTableView.h                          (rev 0)
+++ trunk/bibdesk/BDSKComplexStringTableView.h  2021-06-10 16:15:34 UTC (rev 
26197)
@@ -0,0 +1,46 @@
+//
+//  BDSKComplexStringTableView.h
+//  BibDesk
+//
+//  Created by Christiaan Hofman on 10/06/2021.
+/*
+ This software is Copyright (c) 2021
+ Christiaan Hofman. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.
+
+ - Neither the name of Christiaan Hofman nor the names of any
+    contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Cocoa/Cocoa.h>
+#import "BDSKTableView.h"
+
+
+@interface BDSKComplexStringTableView : BDSKTableView {
+    BOOL didSetValue;
+}
+@end

Added: trunk/bibdesk/BDSKComplexStringTableView.m
===================================================================
--- trunk/bibdesk/BDSKComplexStringTableView.m                          (rev 0)
+++ trunk/bibdesk/BDSKComplexStringTableView.m  2021-06-10 16:15:34 UTC (rev 
26197)
@@ -0,0 +1,88 @@
+//
+//  BDSKComplexStringTableView.m
+//  BibDesk
+//
+//  Created by Christiaan Hofman on 10/06/2021.
+/*
+ This software is Copyright (c) 2021
+ Christiaan Hofman. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+ - Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.
+
+ - Neither the name of Christiaan Hofman nor the names of any
+    contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "BDSKComplexStringTableView.h"
+
+@interface NSTableView (BDSKApplePrivate)
+- (void)_dataSourceSetValue:(id)value forColumn:(NSTableColumn *)tableColumn 
row:(NSInteger)row;
+@end
+
+@implementation BDSKComplexStringTableView
+
+// private method called from -[NSTableView textDidEndEditing:]
+- (void)_dataSourceSetValue:(id)value forColumn:(NSTableColumn *)tableColumn 
row:(NSInteger)row {
+    [super _dataSourceSetValue:value forColumn:tableColumn row:row];
+    didSetValue = YES;
+}
+
+- (void)textDidEndEditing:(NSNotification *)aNotification {
+    /*
+     NSTableView has an optimization of sorts where the value will not be set 
if the string in the cell
+     is equal to the old string.  When you want to change e.g. year={2009} to 
year=2009, this becomes
+     a problem.  I got fed up with deleting the old string, then setting the 
new raw string.
+     
+     Note that the current cell's objectValue is still the old value, so we 
have to work with the formatter
+     directly in order to get the (possibly complex) edited string.
+     */
+    NSInteger editedRow = [self editedRow];
+    NSInteger editedColumn = [self editedColumn];
+    BOOL shouldCheckValue = NO;
+    id newValue = nil;
+    if (editedColumn >= 0 && editedRow >= 0 && [self 
respondsToSelector:@selector(_dataSourceSetValue:forColumn:row:)]) {
+        NSCell *editedCell = [self preparedCellAtColumn:editedColumn 
row:editedRow];
+        NSFormatter *formatter = [editedCell formatter];
+        id oldValue = [editedCell objectValue];
+        newValue = [[aNotification object] string];
+        if (formatter == nil || [formatter getObjectValue:&newValue 
forString:newValue errorDescription:NULL]) {
+            shouldCheckValue = [oldValue 
respondsToSelector:@selector(isEqualAsComplexString:)] &&
+                               [newValue 
respondsToSelector:@selector(isEqualAsComplexString:)] &&
+                               [oldValue isEqualAsComplexString:newValue] == 
NO;
+        }
+        newValue = [newValue copy];
+    }
+    didSetValue = NO;
+    
+    [super textDidEndEditing:aNotification];
+    
+    // only try setting if NSTableView did not, and if these are not equal as 
complex strings
+    if (didSetValue == NO && shouldCheckValue)
+        [self _dataSourceSetValue:newValue forColumn:[[self tableColumns] 
objectAtIndex:editedColumn] row:editedRow];
+    [newValue release];
+}
+
+@end

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