Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 552 lines (440 sloc) 15.743 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
//
// CSVWindowController.m
// CSVManipulator
//
// Created by Pascal Pfiffner on 06.01.09.
// This sourcecode is released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0.html
//

#import "CSVWindowController.h"
#import "MyDocument.h"
#import "PPStringFormat.h"
#import "PPStringFormatManager.h"
#import "CSVDocument.h"
#import "CSVColumn.h"
#import "CSVInspector.h"
#import "DataTableColumn.h"
#import "DataTableHeaderCell.h"
#import "DataTableCell.h"
#import "PPToolbarView.h"

#define COLUMN_MIN_WIDTH 40
#define kHeaderColumnIdentifier @"_theHeaderRowColumn"


@interface CSVWindowController ()

- (void) addColumnWithKey:(NSString *)columnKey atPosition:(NSUInteger)position;
- (void) removeColumn:(NSTableColumn *)tableColumn;
- (void) moveColumn:(NSInteger)oldPosition ofTable:(NSTableView *)aTableView to:(NSInteger)newPosition;
- (void) didMoveColumn:(NSInteger)oldPosition ofTable:(NSTableView *)aTableView to:(NSInteger)newPosition;

@end


@implementation CSVWindowController

@synthesize document;
@synthesize mainTable;
@synthesize mainToolbar;
@synthesize progressSheet;

@synthesize addRowItem;
@synthesize removeRowItem;
@synthesize addColumnItem;
@synthesize removeColumnItem;
@synthesize restoreOrderItem;
@synthesize showFormatsItem;


- (void) dealloc
{
self.document = nil;

self.mainTable = nil;
self.mainToolbar = nil;

self.progressSheet = nil;

self.addRowItem = nil;

self.addRowItem = nil;
self.removeRowItem = nil;
self.addColumnItem = nil;
self.removeColumnItem = nil;
self.restoreOrderItem = nil;
self.showFormatsItem = nil;

[super dealloc];
}

- (void) awakeFromNib
{
mainToolbar.borderWidth = PPBorderWidthMake(1.f, 0.f, 0.f, 0.f);

// the following enables prevention of moving a column before the first column
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tableViewColumnDidMove:)
name:NSTableViewColumnDidMoveNotification
object:mainTable];

// document is still loading
if (!document.documentLoaded) {
[self performSelector:@selector(showProgressSheet) withObject:nil afterDelay:0.01f];
}

// document did already load all data
else {
[self redefineTable];
}
}
/*
- (void) windowWillClose:(NSNotification *)notification
{
} //*/



#pragma mark - Data Control
- (IBAction)addNewColumn:(id)sender
{
[self addColumnWithKey:[document.csvDocument nextAvailableColumnKey] atPosition:UINT_MAX];
// TODO: Tell the CSVInspector when we do crazy stuff like this
}

- (void)addColumnWithKey:(NSString *)columnKey atPosition:(NSUInteger)position
{
CSVColumn *newColumn = [CSVColumn columnWithKey:columnKey];
if ([document.csvDocument addColumn:newColumn]) {
[self addColumn:newColumn toTable:mainTable atPosition:position withWidth:0.f resizeTable:YES];

// allow undo
NSUndoManager *undoManager = [document undoManager];
[undoManager registerUndoWithTarget:self selector:@selector(removeColumnWithIdentifier:) object:columnKey];
[undoManager setActionName:NSLocalizedString([undoManager isUndoing] ? @"Remove Column" : @"Add Column", nil)];
}
}


- (IBAction)removeSelectedColumns:(id)sender
{
NSIndexSet *indexes = [mainTable selectedColumnIndexes];
if ([indexes count] > 0) {
NSArray *exColumns = [[mainTable tableColumns] objectsAtIndexes:indexes];

for (NSTableColumn *tableColumn in exColumns) {
[self removeColumn:tableColumn];
}
}
}

- (void)removeColumnWithIdentifier:(NSString *)columnIdentifier
{
for (NSTableColumn *tableColumn in [mainTable tableColumns]) {
if ([[tableColumn identifier] isEqualToString:columnIdentifier]) {
[self removeColumn:tableColumn];
return;
}
}
NSLog(@"Can't remove column with identifier '%@'", columnIdentifier);
}

- (void)removeColumn:(NSTableColumn *)tableColumn
{
NSUndoManager *undoManager = [document undoManager];

id columnIdentifier = [tableColumn identifier];
NSUInteger columnIndex = [mainTable columnWithIdentifier:columnIdentifier];

[[undoManager prepareWithInvocationTarget:self] addColumnWithKey:columnIdentifier atPosition:columnIndex];
[undoManager setActionName:NSLocalizedString([undoManager isUndoing] ? @"Add Column" : @"Remove Column", nil)];

// remove
CSVColumn *csvColumn = [document.csvDocument columnWithKey:columnIdentifier];
if ([document.csvDocument removeColumn:csvColumn]) {
[mainTable removeTableColumn:tableColumn];
[mainTable sizeLastColumnToFit];
}
else {
NSLog(@"Can't remove column %@ as it is not a valid column", columnIdentifier);
}
}

- (void)moveColumn:(NSInteger)oldPosition ofTable:(NSTableView *)aTableView to:(NSInteger)newPosition
{
if (oldPosition != newPosition) {
[mainTable moveColumn:oldPosition toColumn:newPosition];
[self didMoveColumn:oldPosition ofTable:aTableView to:newPosition];
}
}

- (void)didMoveColumn:(NSInteger)oldPosition ofTable:(NSTableView *)aTableView to:(NSInteger)newPosition
{
if (oldPosition != newPosition) {
NSUndoManager *undoManager = [document undoManager];
[[undoManager prepareWithInvocationTarget:self] moveColumn:newPosition ofTable:aTableView to:oldPosition];
[undoManager setActionName:NSLocalizedString(@"Column Order", nil)];

// collect column keys
NSArray *allColumns = [aTableView tableColumns];
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:[allColumns count]];
for (DataTableColumn *col in allColumns) {
if (![kHeaderColumnIdentifier isEqualToString:[col identifier]]) {
[arr addObject:[col identifier]];
}
}

// propagate changes to CSVDocument
[document.csvDocument setColumnOrderByKeys:arr];
}
}


- (IBAction)addCSVRow:(id)sender
{
[document.csvDocument.rowController add:sender];
}

- (IBAction)removeCSVRow:(id)sender
{
[document.csvDocument.rowController remove:sender];
}

- (void)removeSelectedRows:(id)sender
{
NSIndexSet *indexes = [mainTable selectedRowIndexes];
if ([indexes count] > 0) {
NSArray *exRows = [document.csvDocument.rows objectsAtIndexes:indexes];

for (CSVRow *row in exRows) {
[document.csvDocument removeRow:row];
}
}
}

- (IBAction)restoreOriginalOrder:(id)sender;
{
[document restoreOriginalOrder];
}

- (void)didRestoreOriginalOrder
{
[mainTable setSortDescriptors:nil];
[mainTable setSortDescriptorsWithColumn:nil]; // necessary since we have overridden setSortDescriptors:
[[mainTable headerView] setNeedsDisplay:YES];
}



#pragma mark - TableView Delegate
- (void)redefineTable
{
// remove OLD columns
NSArray *columnsToRemove = [[mainTable tableColumns] copy];
for (NSTableColumn *oldColumn in columnsToRemove) {
[oldColumn unbind:@"headerTitle"];
[oldColumn unbind:@"value"];
[mainTable removeTableColumn:oldColumn];
}
[columnsToRemove release];

// first column is a checkbox column to specify header cells
CGFloat firstColumnWidth = 30.0;
NSButtonCell *firstDataCell = [[[NSButtonCell alloc] init] autorelease];
[firstDataCell setButtonType:NSSwitchButton];
[firstDataCell setControlSize:NSSmallControlSize];
[firstDataCell setTitle:@""];

DataTableColumn *firstTableColumn = [DataTableColumn column];
[firstTableColumn setIdentifier:kHeaderColumnIdentifier];
[firstTableColumn setDataCell:firstDataCell];
[firstTableColumn setWidth:firstColumnWidth];
firstTableColumn.resizingMask = NSTableColumnNoResizing;
[[firstTableColumn headerCell] setTitle:@"⚑"];
[[firstTableColumn headerCell] setAlignment:NSCenterTextAlignment];
[[firstTableColumn headerCell] setShowsCheckbox:NO];

[mainTable addTableColumn:firstTableColumn];

// bind checkbox values
[firstTableColumn bind:@"value"
toObject:document.csvDocument.rowController
withKeyPath:@"arrangedObjects.isHeaderRow"
options:nil];

// new headers, new bindings
NSInteger numColumns = [document numColumns];
if (numColumns > 0) {
NSRect mainTableBounds = [mainTable frame];
int columnWidth = ceilf((mainTableBounds.size.width - firstColumnWidth) / numColumns);
columnWidth = (columnWidth < COLUMN_MIN_WIDTH) ? COLUMN_MIN_WIDTH : columnWidth;

// loop columns to add the table columns
for (CSVColumn *column in [document columns]) {
[self addColumn:column toTable:mainTable atPosition:UINT_MAX withWidth:columnWidth resizeTable:NO];
}

[mainTable sizeLastColumnToFit];
}
}

- (void)addColumn:(CSVColumn *)newColumn toTable:(NSTableView *)aTableView atPosition:(NSUInteger)position withWidth:(CGFloat)width resizeTable:(BOOL)resize
{
if (aTableView == mainTable) {

// create the table column
DataTableColumn *tableColumn = [DataTableColumn column];
[tableColumn setIdentifier:newColumn.key];
[tableColumn setDataCell:[DataTableCell cell]];
[tableColumn setMinWidth:COLUMN_MIN_WIDTH];
if (width > COLUMN_MIN_WIDTH) {
[tableColumn setWidth:width];
}
[[tableColumn headerCell] setEditable:YES];
[[tableColumn headerCell] setChecked:newColumn.active];

// bind the column row values
[tableColumn bind:@"value"
toObject:document.csvDocument.rowController
withKeyPath:[NSString stringWithFormat:@"arrangedObjects.rowValues.%@", newColumn.key]
options:nil];

// [tableColumn bind:@"active"
// toObject:document.csvDocument.columnDict
// withKeyPath:[NSString stringWithFormat:@"%@.active", key]
// options:nil]; // does somehow not work. Using the delegate method for now

// also bind column header
[tableColumn bind:@"headerTitle"
toObject:document.csvDocument.columnDict
withKeyPath:[NSString stringWithFormat:@"%@.type", newColumn.key]
options:nil];

// add the column
[mainTable addTableColumn:tableColumn];
NSUInteger lastColumnIndex = [[mainTable tableColumns] count] - 1;
if (position < lastColumnIndex) {
[mainTable moveColumn:lastColumnIndex toColumn:position];
}

// resize the table width
if (resize) {
[mainTable sizeToFit];
}
}
}


- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
NSInteger selected_row = [mainTable selectedRow];
if (selected_row >= 0) {

// if an empty row was selected, jump into edit mode of the first field
if (![document hasAnyDataAtRow:selected_row]) {
[mainTable editColumn:1 row:selected_row withEvent:nil select:YES];
}
}
}


- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn
{
// set sort descriptors
if (mainTable == tableView) {
[(DataTableView *)tableView setSortDescriptorsWithColumn:(DataTableColumn *)tableColumn];
[document setDataIsAtOriginalOrder:NO];
}
}


- (void)tableView:(DataTableView *)tableView didChangeTableColumnState:(DataTableColumn *)tableColumn
{
[document.csvDocument setColumnActive:tableColumn.active forColumnKey:[tableColumn identifier]];
}


- (void)tableView:(NSTableView*)aTableView mouseDownInHeaderOfTableColumn:(NSTableColumn*)tableColumn
{
if (0 == [[aTableView tableColumns] indexOfObject:tableColumn]) {
[aTableView setAllowsColumnReordering:NO];
}
else {
[aTableView setAllowsColumnReordering:YES];
}
}

- (void)tableView:(NSTableView *)tableView didDragTableColumn:(NSTableColumn *)tableColumn
{
if (mainTable == tableView) {
if (![kHeaderColumnIdentifier isEqualToString:[tableColumn identifier]]) {
NSInteger newPos = [[tableView tableColumns] indexOfObject:tableColumn];

// a move only happened when newPos is >= 0
if (newPos >= 0) {

// determine old position for the undo operation
NSInteger oldPos = 0;
for (CSVColumn *column in document.csvDocument.columns) {
if ([column.key isEqualToString:[tableColumn identifier]]) {
break;
}
oldPos++;
}
oldPos++; // compensating for the first column, which is not a column of csvDocument

[self didMoveColumn:oldPos ofTable:mainTable to:newPos];
}
}
}
}

- (void)tableViewColumnDidMove:(NSNotification*)aNotification
{
NSDictionary* userInfo = [aNotification userInfo];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

// temporarily stop listening to column moves to prevent recursion
[center removeObserver:self name:NSTableViewColumnDidMoveNotification object:nil];

// if the user tries to move the first column out, move it back (should not be possible since we disable moving the first column)
if (0 == [[userInfo objectForKey:@"NSOldColumn"] intValue]) {
[mainTable moveColumn:[[userInfo objectForKey:@"NSNewColumn"] intValue] toColumn:0];
}

// if the user tries to move a column in front of the first column, move it back
else if (0 == [[userInfo objectForKey:@"NSNewColumn"] intValue]) {
[mainTable moveColumn:0 toColumn:[[userInfo objectForKey:@"NSOldColumn"] intValue]];
}

// listen again for column moves
[center addObserver:self selector:@selector(tableViewColumnDidMove:) name:NSTableViewColumnDidMoveNotification object:mainTable];
}
#pragma mark -



#pragma mark TableView DataSource
- (NSInteger) numberOfRowsInTableView:(NSTableView*)aTableView
{
return 0; // will not be used since we use bindings, but must be implemented to avoid warnings
}

- (id) tableView:(NSTableView*)aTableView objectValueForTableColumn:(NSTableColumn*)aTableColumn row:(int)rowIndex
{
return nil; // will not be used since we use bindings, but must be implemented to avoid warnings
}

- (void) tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
NSArray *currentDescriptors = [aTableView sortDescriptors];
document.dataIsAtOriginalOrder = (nil == currentDescriptors || 0 == [currentDescriptors count]);
}

- (void)refreshData
{
[mainTable reloadData];
}



#pragma mark - Toolbar
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
{
if (addRowItem == theItem) {
return [document.csvDocument.rowController canInsert];
}
if (removeRowItem == theItem) {
return [document.csvDocument.rowController canRemove];
}
if (addColumnItem == theItem) {
return YES;
}
if (removeColumnItem == theItem) {
return ([mainTable numberOfSelectedColumns] > 0);
}
if (showFormatsItem == theItem) {
return YES;
}
if (restoreOrderItem == theItem) {
return !document.dataIsAtOriginalOrder;
}
return YES;
}



#pragma mark - Inspector
- (IBAction) showInspector:(id)sender
{
[CSVInspector show:sender];
}



#pragma mark Progress Sheet
- (void) showProgressSheet
{
if (!document.documentLoaded) {
[NSApp beginSheet:progressSheet modalForWindow:[self window] modalDelegate:nil didEndSelector:NULL contextInfo:nil];
}
}

- (void) updateProgressSheetProgress:(CGFloat)percentage
{
if ([progressSheet isVisible]) {
[progressIndicator setIndeterminate:NO];
[progressIndicator setDoubleValue:(double)percentage];
[progressPercentage setFloatValue:(100 * percentage)];
}
}

- (void) setProgressSheetIndeterminate:(NSNumber *)flag
{
if ([progressSheet isVisible]) {
[progressIndicator setIndeterminate:[flag boolValue]];
[progressIndicator startAnimation:nil];
}
}

- (void) hideProgressSheet
{
if ([progressSheet isVisible]) {
[self updateProgressSheetProgress:1.0];
[progressIndicator stopAnimation:nil];

[progressSheet orderOut:nil];
[NSApp endSheet:progressSheet];
}
}

- (void) abortImport:(id)sender
{
[document abortImport];
}

- (void) didAbortImport:(BOOL)flag
{
[importAbortedField setHidden:!flag];
}
#pragma mark -



#pragma mark Export
- (IBAction) showExportFormats:(id)sender
{
[PPStringFormatManager show:sender];
}
#pragma mark -



#pragma mark Notifications
- (void) windowDidBecomeMain:(NSNotification *)notification
{
[document windowDidBecomeMain:notification];
}

- (void) windowDidResignMain:(NSNotification *)notification
{
[document windowDidResignMain:notification];
}

- (void) windowWillClose:(NSNotification *)notification
{
[document windowDidResignMain:notification];
}


@end
Something went wrong with that request. Please try again.