Permalink
Browse files

Implemented Undoing of some basic tasks. To be improved.

  • Loading branch information...
1 parent dce6cbe commit 6a8b66463bf5bef91c5cde73894c71e17d5fda4f svnuser committed Feb 26, 2010
@@ -26,6 +26,7 @@
@interface CSVDocument : NSObject {
id <CSVDocumentDelegate> delegate;
+ NSDocument *document; // we're keeping NSDocument separate from CSVDocument
NSString *separator;
NSMutableArray *rows;
@@ -48,6 +49,7 @@
}
@property (nonatomic, assign) id delegate;
+@property (nonatomic, assign) NSDocument *document;
@property (nonatomic, retain) NSString *separator;
@property (nonatomic, retain) NSMutableArray *rows;
@@ -28,6 +28,7 @@ - (void) notifyDelegateOfParsedRow:(CSVRow *)newRow;
@implementation CSVDocument
@synthesize delegate;
+@synthesize document;
@synthesize separator;
@synthesize rows;
@synthesize numRows;
@@ -72,6 +73,7 @@ + (id) csvDocument
- (void) dealloc
{
self.delegate = nil;
+ self.document = nil;
self.separator = nil;
self.rows = nil;
self.numRows = nil;
@@ -81,6 +83,7 @@ - (void) dealloc
self.rowController = nil;
#endif
+
[super dealloc];
}
#pragma mark -
@@ -271,7 +274,7 @@ - (BOOL) parseCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NS
}
}
else {
- newRow.headerRow = YES;
+ newRow.isHeaderRow = YES;
}
num_rows++;
View
@@ -21,13 +21,13 @@
@interface CSVRow : NSObject <NSCopying> {
CSVDocument *document;
NSMutableDictionary *rowValues;
- BOOL headerRow;
+ BOOL isHeaderRow;
NSUInteger headerRowPosition; // starts at 1 (uppermost row)
}
@property (nonatomic, assign) CSVDocument *document;
@property (nonatomic, copy) NSMutableDictionary *rowValues;
-@property (nonatomic, assign, getter=isHeaderRow) BOOL headerRow;
+@property (nonatomic, assign) BOOL isHeaderRow;
@property (nonatomic, assign) NSUInteger headerRowPosition;
+ (id) rowForDocument:(CSVDocument *)forDocument;
@@ -36,7 +36,6 @@
- (NSArray *) valuesForColumns:(NSArray *)columns;
- (NSArray *) valuesForColumnKeys:(NSArray *)columnKeys;
- (NSString *) valuesForColumns:(NSArray *)columns combinedByString:(NSString *)sepString;
-- (NSString *) valuesForColumns:(NSArray *)columns combinedByString:(NSString *)sepString quoted:(BOOL)quoteStrings;
- (NSString *) valueForColumn:(CSVColumn *)column;
- (NSString *) valueForColumnKey:(NSString *)columnKey;
- (BOOL) valueForColumnIsEmpty:(CSVColumn *)column;
View
@@ -18,7 +18,7 @@ @implementation CSVRow
@synthesize document;
@synthesize rowValues;
-@dynamic headerRow;
+@dynamic isHeaderRow;
@synthesize headerRowPosition;
@@ -75,24 +75,28 @@ - (void) dealloc
#pragma mark KVC
- (BOOL) isHeaderRow
{
- return headerRow;
+ return isHeaderRow;
}
-- (void) setHeaderRow:(BOOL)isHeader
+- (void) setIsHeaderRow:(BOOL)isHeader
{
- if (isHeader != headerRow) {
+ if (isHeader != isHeaderRow) {
+ NSUndoManager *undoManager = [[document document] undoManager];
+ [[undoManager prepareWithInvocationTarget:self] setIsHeaderRow:isHeaderRow];
+ //[undoManager setActionName:NSLocalizedString(@"Set Header Row", @"")];
+
[self changeHeaderRow:isHeader];
- [document row:self didBecomeHeaderRow:headerRow];
+ [document row:self didBecomeHeaderRow:isHeaderRow];
}
}
- (void) changeHeaderRow:(BOOL)isHeader
{
// this _silently_ changes the headerRow flag, only use from ourself or self.document!
[self willChangeValueForKey:@"headerRow"];
- headerRow = isHeader;
+ isHeaderRow = isHeader;
[self didChangeValueForKey:@"headerRow"];
- if (headerRow) {
+ if (isHeaderRow) {
highestHeaderRowPos += 1;
self.headerRowPosition = highestHeaderRowPos;
}
@@ -129,11 +133,6 @@ - (NSArray *) valuesForColumnKeys:(NSArray *)columnKeys
- (NSString *) valuesForColumns:(NSArray *)columns combinedByString:(NSString *)sepString
{
- return [self valuesForColumns:columns combinedByString:sepString quoted:NO];
-}
-
-- (NSString *) valuesForColumns:(NSArray *)columns combinedByString:(NSString *)sepString quoted:(BOOL)quoteStrings
-{
return [[self valuesForColumns:columns] componentsJoinedByString:sepString];
}
@@ -170,7 +169,7 @@ - (BOOL) isEmptyRow
-#pragma mark Setting Column Values
+#pragma mark Setting Values
- (void) setValue:(id)value forColumn:(CSVColumn *)column
{
[self setValue:value forColumnKey:column.key];
@@ -179,16 +178,32 @@ - (void) setValue:(id)value forColumn:(CSVColumn *)column
- (void) setValue:(id)value forColumnKey:(NSString *)key
{
if (nil != key) {
+ NSUndoManager *undoManager = [[document document] undoManager];
+ [[undoManager prepareWithInvocationTarget:self] setValue:[self valueForColumnKey:key] forColumnKey:key];
+ //[undoManager setActionName:NSLocalizedString(@"Set Value", nil)];
+
value = (nil != value) ? value : [NSNull null];
[rowValues setObject:value forKey:key];
}
}
-/*
+
- (void) setValue:(id)value forKeyPath:(NSString *)keyPath
{
- NSLog(@"%@ -> %@", keyPath, value);
+ // if rowValues get changed, we end up here
+ if (0 == [keyPath rangeOfString:@"rowValues"].location) {
+ NSUndoManager *undoManager = [[document document] undoManager];
+ [[undoManager prepareWithInvocationTarget:self] setValue:[self valueForKeyPath:keyPath] forKeyPath:keyPath];
+ //[undoManager setActionName:NSLocalizedString(@"Set Value", nil)];
+
+ // if we were editing the cell, an undo operation would not be visible, so circumvent this manually
+ if ([undoManager isUndoing]) {
+ // TODO: Update Cell
+ NSLog(@"Undoing row value change, update table cell content if currently editing...");
+ }
+ }
+
[super setValue:value forKeyPath:keyPath];
-} // */
+}
#pragma mark -
@@ -81,12 +81,30 @@ - (void) add:(id)sender
[super add:sender];
}
+- (void) addObject:(id)object
+{
+ NSUndoManager *undoManager = [[document document] undoManager];
+ [undoManager registerUndoWithTarget:self selector:@selector(removeObject:) object:object];
+ //[undoManager setActionName:NSLocalizedString(@"Add Row", nil)];
+
+ [super addObject:object];
+}
+
- (void) remove:(id)sender
{
[super remove:sender];
[document setNumRowsWithInt:[[self content] count]];
}
+- (void) removeObject:(id)object
+{
+ NSUndoManager *undoManager = [[document document] undoManager];
+ [undoManager registerUndoWithTarget:self selector:@selector(addObject:) object:object];
+ //[undoManager setActionName:NSLocalizedString(@"Remove Row", nil)];
+
+ [super removeObject:object];
+}
+
@end
#endif
Oops, something went wrong.

0 comments on commit 6a8b664

Please sign in to comment.