Permalink
Browse files

Many fixes and improvements. Still lot to do. Working on the table he…

…ader click.
  • Loading branch information...
svnuser
svnuser committed Oct 14, 2009
1 parent b4b2e20 commit 2c88a6090354945b7ae5d25df60ffa3b3b5ca73d
View
@@ -17,11 +17,13 @@
@interface CSVColumn : NSObject {
NSString *name; // the name to display
NSString *key; // a unique key
+ NSString *type; // type of the values (NSString, NSNumber, NSDate) TODO: implement!
BOOL active;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *key;
+@property (nonatomic, copy) NSString *type;
@property (nonatomic, assign, getter=isActive) BOOL active;
View
@@ -11,10 +11,21 @@
@implementation CSVColumn
-@synthesize key, active;
+@synthesize key;
@dynamic name;
+@synthesize type;
+@synthesize active;
+- (id) init
+{
+ self = [super init];
+ if (self) {
+ self.type = @"NSString";
+ }
+ return self;
+}
+
+ (id) columnWithKey:(NSString *)newKey
{
CSVColumn *column = [[[self alloc] init] autorelease];
@@ -27,6 +38,7 @@ - (void) dealloc
{
self.key = nil;
self.name = nil;
+ self.type = nil;
[super dealloc];
}
View
@@ -25,10 +25,9 @@
id <CSVDocumentDelegate> delegate;
NSString *separator;
- NSMutableArray *headerRows; // array full of CSVRow header rows
NSMutableArray *rows;
NSNumber *numRows;
-
+
#ifndef IPHONE
CSVRowController *rowController;
#endif
@@ -48,15 +47,14 @@
@property (nonatomic, assign) id delegate;
@property (nonatomic, retain) NSString *separator;
-@property (nonatomic, retain) NSMutableArray *headerRows;
@property (nonatomic, retain) NSMutableArray *rows;
@property (nonatomic, copy) NSNumber *numRows;
#ifndef IPHONE
@property (nonatomic, retain) CSVRowController *rowController;
#endif
@property (nonatomic, retain) NSArray *columns;
-@property (nonatomic, readonly) NSDictionary *columnDict;
+@property (nonatomic, readonly, retain) NSDictionary *columnDict;
@property (nonatomic, assign) BOOL parseSuccessful;
@property (nonatomic, assign) BOOL autoDetectSeparator;
@@ -80,7 +78,6 @@
// column handling
- (void) addColumn:(CSVColumn *) newColumn;
- (BOOL) isFirstColumnKey:(NSString *)columnKey;
-- (void) setHeaderRow:(CSVRow *)newHeaderRow;
- (BOOL) hasColumnKey:(NSString *)columnKey;
- (void) setColumnOrderByKeys:(NSArray *)newOrderKeys;
@@ -92,6 +89,9 @@
// row handling
- (CSVRow *) rowAtIndex:(NSUInteger)rowIndex;
+- (void) changeNumHeaderRows:(NSUInteger)newNum;
+- (void) rearrangeRows;
+- (void) row:(CSVRow *)thisRow didBecomeHeaderRow:(BOOL)itDid;
@end
View
@@ -16,6 +16,8 @@
@interface CSVDocument ()
+@property (nonatomic, readwrite, retain) NSDictionary *columnDict;
+
- (void) setColumnDict:(NSDictionary *)newColumnDict;
- (void) notifyDelegateOfParsedRow:(CSVRow *)newRow;
@@ -35,12 +37,11 @@ @implementation CSVDocument
#endif
@synthesize parseSuccessful;
@synthesize autoDetectSeparator;
-@synthesize headerRows;
@synthesize mustAbortImport;
@synthesize didAbortImport;
@synthesize reportEveryRowParsed;
@synthesize columns;
-@dynamic numHeaderRows;
+@synthesize numHeaderRows;
- (id) init
@@ -75,74 +76,13 @@ - (void) dealloc
#ifndef IPHONE
self.rowController = nil;
#endif
- self.headerRows = nil;
[super dealloc];
}
#pragma mark -
-#pragma mark KVC
-- (void) setColumnDict:(NSDictionary *)newColumnDict
-{
- if (newColumnDict != columnDict) {
- [columnDict release];
- columnDict = [newColumnDict retain];
- }
-}
-
-- (NSUInteger) numHeaderRows
-{
- return numHeaderRows;
-}
-- (void) setNumHeaderRows:(NSUInteger)newNum
-{
- // MOVE all current header rows to the body
- if ([headerRows count] > 0) {
- for (CSVRow *hr in headerRows) {
-#ifdef IPHONE
- [rows insertObject:hr atIndex:0];
-#else
- [rowController insertObject:hr atArrangedObjectIndex:0];
-#endif
- hr.isHeaderRow = NO;
- [headerRows removeObject:hr];
- }
- }
-
- // COPY data from the first x rows to the header and remove the rows from the body
- if (newNum > 0) {
- NSUInteger i;
- for (i = 0; i < newNum; i++) {
- if ([rows count] > i) {
-#ifdef IPHONE
- [self setHeaderRow:[rows objectAtIndex:i]];
- [rows removeObjectAtIndex:i];
-#else
- [self setHeaderRow:[[rowController arrangedObjects] objectAtIndex:i]];
- [rowController removeObjectAtArrangedObjectIndex:i];
-#endif
- }
- }
- }
-
- // update num rows
- [self setNumRowsWithInt:[rows count]];
-
- numHeaderRows = newNum;
-}
-
-- (void) setNumRowsWithInt:(NSInteger)num_rows
-{
- [self willChangeValueForKey:@"numRows"];
- self.numRows = [NSNumber numberWithInt:num_rows];
- [self didChangeValueForKey:@"numRows"];
-}
-#pragma mark -
-
-
-
#pragma mark Parsing
- (NSUInteger) numRowsToExpect:(NSString *)string
{
@@ -345,17 +285,16 @@ - (BOOL) parseCSVString:(NSString *)string maxRows:(NSUInteger)maxRows error:(NS
}
- // one row scanned - add to our header and body arrays and save the columns
+ // one row scanned - add to our array, save the columns and report to delegate if desired
+ [rows addObject:newRow];
+
if (num_rows >= numHeaderRows) {
- [rows addObject:newRow];
-
- // report to delegate if desired
if (sendRowUpdateToDelegate) {
[self performSelectorOnMainThread:@selector(notifyDelegateOfParsedRow:) withObject:newRow waitUntilDone:NO];
}
}
else {
- [self setHeaderRow:newRow];
+ newRow.headerRow = YES;
}
num_rows++;
@@ -415,19 +354,12 @@ - (NSString *) stringInFormat:(PPStringFormat *)format withColumns:(NSArray *)co
format = [PPStringFormat csvFormat];
}
-
// extract keys from column objects
NSMutableArray *columnKeys = [NSMutableArray arrayWithCapacity:[columnArray count]];
for (CSVColumn *column in columnArray) {
[columnKeys addObject:column.key];
}
- // get header row
-// NSArray *headerRows = nil;
-// if (headerFlag && (nil != headerRow)) {
-// headerRows = [NSArray arrayWithObject:headerRow];
-// }
-
// get desired row indexes if not given
if (nil == rowIndexes) {
if (NSNotFound == [rowController selectionIndex]) {
@@ -440,9 +372,8 @@ - (NSString *) stringInFormat:(PPStringFormat *)format withColumns:(NSArray *)co
}
NSArray *exportRows = [[rowController arrangedObjects] objectsAtIndexes:rowIndexes];
-
// get the string from the formatter
- return [format stringForRows:exportRows headerRows:headerRows withKeys:columnKeys];
+ return [format stringForRows:exportRows includeHeaderRows:headerFlag withColumnKeys:columnKeys];
}
#endif
#pragma mark -
@@ -482,20 +413,6 @@ - (BOOL) isFirstColumnKey:(NSString *)columnKey
return NO;
}
-- (void) setHeaderRow:(CSVRow *)newHeaderRow
-{
- if (newHeaderRow ) {
- newHeaderRow.isHeaderRow = YES;
- [headerRows addObject:newHeaderRow];
-
- // update column names (if they don't yet have a name)
- for (CSVColumn *column in columns) {
- if (!column.name) {
- column.name = [newHeaderRow valueForColumn:column];
- }
- }
- }
-}
- (BOOL) hasColumnKey:(NSString *)columnKey
{
@@ -551,7 +468,59 @@ - (void) setHeaderActive:(BOOL)active forColumnKey:(NSString *)columnKey
#pragma mark Row Handling
- (CSVRow *) rowAtIndex:(NSUInteger)rowIndex
{
+#ifdef IPHONE
return [rows objectAtIndex:rowIndex];
+#else
+ return [[rowController arrangedObjects] objectAtIndex:rowIndex];
+#endif
+}
+
+- (void) changeNumHeaderRows:(NSUInteger)newNum
+{
+ NSUInteger i;
+ NSUInteger max = (newNum > numHeaderRows) ? newNum : numHeaderRows;
+ NSUInteger num_rows = [rows count];
+
+ for (i = 0; i < max; i++) {
+ if (num_rows > i) {
+ CSVRow *row = [self rowAtIndex:i];
+ [row changeHeaderRow:(i < newNum)];
+ }
+ }
+
+ [self rearrangeRows];
+ self.numHeaderRows = newNum;
+}
+
+- (void) rearrangeRows
+{
+#ifdef IPHONE
+ // TODO: rearrange on iPhone!
+#else
+ [rowController rearrangeObjects];
+#endif
+}
+
+- (void) setNumRowsWithInt:(NSInteger)num_rows
+{
+ [self willChangeValueForKey:@"numRows"];
+ self.numRows = [NSNumber numberWithInt:num_rows];
+ [self didChangeValueForKey:@"numRows"];
+}
+
+- (void) row:(CSVRow *)thisRow didBecomeHeaderRow:(BOOL)itDid
+{
+ if (itDid) {
+ numHeaderRows += 1;
+ }
+ else {
+ numHeaderRows -= 1;
+ }
+
+ [self rearrangeRows];
+ if ([delegate respondsToSelector:@selector(csvDocument:didChangeRowOrderToOriginalOrder:)]) {
+ [delegate csvDocument:self didChangeRowOrderToOriginalOrder:NO];
+ }
}
#pragma mark -
@@ -24,4 +24,6 @@
- (void) csvDocument:(CSVDocument *)document didParseNumRows:(NSUInteger)num_parsed;
- (void) csvDocument:(CSVDocument *)document didParseRow:(CSVRow *)row;
+- (void) csvDocument:(CSVDocument *)document didChangeRowOrderToOriginalOrder:(BOOL)isOriginalOrder;
+
@end
View
@@ -21,12 +21,14 @@
@interface CSVRow : NSObject <NSCopying> {
CSVDocument *document;
NSMutableDictionary *rowValues;
- BOOL isHeaderRow;
+ BOOL headerRow;
+ NSUInteger headerRowPosition; // starts at 1 (uppermost row)
}
@property (nonatomic, assign) CSVDocument *document;
@property (nonatomic, copy) NSMutableDictionary *rowValues;
-@property (nonatomic, assign) BOOL isHeaderRow;
+@property (nonatomic, assign, getter=isHeaderRow) BOOL headerRow;
+@property (nonatomic, assign) NSUInteger headerRowPosition;
+ (id) rowForDocument:(CSVDocument *)forDocument;
+ (id) rowFromDict:(NSMutableDictionary *)dict forDocument:(CSVDocument *)forDocument;
@@ -44,5 +46,7 @@
- (void) setValue:(id)value forColumn:(CSVColumn *)column;
- (void) setValue:(id)value forColumnKey:(NSString *)key;
+- (void) changeHeaderRow:(BOOL)isHeader; // changes the header row status without telling the document - only the document should use this!
+
@end
Oops, something went wrong.

0 comments on commit 2c88a60

Please sign in to comment.