Skip to content

Commit

Permalink
Allow to customize output file names [#154]
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyvit committed Apr 1, 2012
1 parent 76b6a20 commit 3b113ca
Show file tree
Hide file tree
Showing 10 changed files with 653 additions and 364 deletions.
Expand Up @@ -15,4 +15,6 @@
@property (assign) IBOutlet NSTableView *pathTableView;
@property (assign) IBOutlet NSButton *chooseFolderButton;

- (IBAction)chooseOutputFileName:(id)sender;

@end
163 changes: 158 additions & 5 deletions LiveReload/Classes/Controllers/CompilationSettingsWindowController.m
Expand Up @@ -45,7 +45,7 @@ void compilable_file_free(compilable_file_t *file) {



@interface CompilationSettingsWindowController () <NSTabViewDelegate, NSTableViewDataSource, NSTableViewDelegate, NSWindowDelegate> {
@interface CompilationSettingsWindowController () <NSTabViewDelegate, NSTableViewDataSource, NSTableViewDelegate, NSWindowDelegate, NSTextFieldDelegate> {
NSArray *_compilerOptions;
BOOL _populatingRubyVersions;
NSArray *_rubyVersions;
Expand All @@ -54,13 +54,26 @@ @interface CompilationSettingsWindowController () <NSTabViewDelegate, NSTableVie
CGFloat _outputPathsWindowHeight;

kvec_t(compilable_file_t *) _fileList;

IBOutlet NSButton *_changeOutputFileButton;
IBOutlet NSTextField *_outputFileNameMask;
IBOutlet NSButton *_applyOutputFileNameMaskButton;
IBOutlet NSButton *_applyButton;

NSSet *_bulkMaskEditingFiles;
BOOL _bulkMaskEditingInProgress;
}

- (void)populateToolVersions;
- (void)updateOutputPathsTabData;
- (void)resizeWindowForTab:(NSTabViewItem *)item animated:(BOOL)animated;
- (void)didDetectChange;

- (void)updateOutputPathsButtonStates;
- (void)updateApplyMaskButton;
- (NSString *)draftFileNameMask;
- (void)endBulkMaskEditing;

@end


Expand Down Expand Up @@ -315,8 +328,10 @@ - (void)updateOutputPathsTabData {
file->file_options = [fileOptions retain];
kv_push(compilable_file_t *, _fileList, file);
}

}

[self updateOutputPathsButtonStates];
[self updateApplyMaskButton];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
Expand All @@ -336,7 +351,12 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum
} else if (column == output_paths_table_column_output) {
if (imported)
return @"(imported)";
return file->file_options.destinationDirectoryForDisplay;

NSString *draftMask = [self draftFileNameMask];
if (draftMask.length > 0 && [_bulkMaskEditingFiles containsObject:file->file_options])
return [NSString stringWithFormat:@"%@ *", [file->file_options destinationDisplayPathForMask:draftMask]];
else
return file->file_options.destinationPathForDisplay;
} else {
return nil;
}
Expand All @@ -349,7 +369,7 @@ - (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableCol
file->file_options.enabled = [object boolValue];
} else if (column == output_paths_table_column_source) {
} else if (column == output_paths_table_column_output) {
file->file_options.destinationDirectory = [object stringByExpandingTildeInPath];
file->file_options.destinationPathForDisplay = [object stringByExpandingTildeInPath];
} else {
}
}
Expand Down Expand Up @@ -378,8 +398,29 @@ - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColu
}
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification {
[self endBulkMaskEditing];
[self updateOutputPathsButtonStates];
}

#pragma mark -

- (NSArray *)selectedFileOptions {
NSIndexSet *indexSet = [_pathTableView selectedRowIndexes];
NSMutableArray *selection = [NSMutableArray array];
for (NSUInteger currentIndex = [indexSet firstIndex]; currentIndex != NSNotFound; currentIndex = [indexSet indexGreaterThanIndex:currentIndex]) {
compilable_file_t *file = kv_A(_fileList, currentIndex);
[selection addObject:file->file_options];
}

// no selection => act on all files
if ([selection count] == 0) {
kv_each(compilable_file_t *, _fileList, file, [selection addObject:file->file_options]);
}

return selection;
}

- (IBAction)chooseOutputDirectory:(id)sender {
if (kv_size(_fileList) == 0) {
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
Expand Down Expand Up @@ -443,7 +484,7 @@ - (IBAction)chooseOutputDirectory:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories:YES];
[openPanel setCanCreateDirectories:YES];
[openPanel setPrompt:@"Choose folder"];
[openPanel setPrompt:@"Set Output Folder"];
[openPanel setCanChooseFiles:NO];
[openPanel setDirectoryURL:[NSURL fileURLWithPath:initialPath isDirectory:YES]];
NSInteger result = [openPanel runModal];
Expand All @@ -459,6 +500,118 @@ - (IBAction)chooseOutputDirectory:(id)sender {
}
}

- (IBAction)chooseOutputFileName:(id)sender {
NSArray *selection = [self selectedFileOptions];
if ([selection count] != 1)
return;

FileCompilationOptions *fileOptions = [selection objectAtIndex:0];

NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setCanCreateDirectories:YES];
[savePanel setNameFieldStringValue:fileOptions.destinationName];
[savePanel setMessage:[NSString stringWithFormat:@"Choose an output file for %@", fileOptions.sourcePath]];
[savePanel setDirectoryURL:[NSURL fileURLWithPath:(fileOptions.destinationDirectory.length > 0 ? [_project pathForRelativePath:fileOptions.destinationDirectory] : [[_project pathForRelativePath:fileOptions.sourcePath] stringByDeletingLastPathComponent]) isDirectory:YES]];
NSInteger result = [savePanel runModal];

if (result == NSFileHandlingPanelOKButton) {
NSURL *url = [savePanel URL];
NSString *absolutePath = [url path];
NSString *relativePath = [_project relativePathForPath:absolutePath];

fileOptions.destinationPath = relativePath;
[_pathTableView reloadData];
}
}

- (void)updateOutputPathsButtonStates {
NSArray *selection = [self selectedFileOptions];

_changeOutputFileButton.enabled = (selection.count == 1);

[[_outputFileNameMask cell] setPlaceholderString:@"e.g. *.shtml"];
if (selection.count == 0) {
[_outputFileNameMask setStringValue:@""];
} else {
NSString *commonMask = [FileCompilationOptions commonDestinationNameMaskFor:selection inProject:_project];
if (commonMask.length == 0) {
[[_outputFileNameMask cell] setPlaceholderString:@"(multiple)"];
[_outputFileNameMask setStringValue:@""];
} else {
[_outputFileNameMask setStringValue:commonMask];
}
}
}

- (void)startBulkMaskEditing {
if (_bulkMaskEditingInProgress)
return;
_bulkMaskEditingInProgress = YES;
_bulkMaskEditingFiles = [[NSSet alloc] initWithArray:[self selectedFileOptions]];

_applyButton.keyEquivalent = @"";
_applyOutputFileNameMaskButton.keyEquivalent = @"\r";

[self updateApplyMaskButton];
}

- (void)endBulkMaskEditing {
if (!_bulkMaskEditingInProgress)
return;
_bulkMaskEditingInProgress = NO;
[_bulkMaskEditingFiles release], _bulkMaskEditingFiles = nil;
[self.window setDefaultButtonCell:[_applyButton cell]];

_applyOutputFileNameMaskButton.keyEquivalent = @"";
_applyButton.keyEquivalent = @"\r";

[_pathTableView reloadData];
[self updateOutputPathsButtonStates];
[self updateApplyMaskButton];
}

- (NSString *)draftFileNameMask {
if (_bulkMaskEditingInProgress)
return _outputFileNameMask.stringValue;
else
return nil;
}

- (void)updateApplyMaskButton {
NSString *commonMask = [FileCompilationOptions commonDestinationNameMaskFor:[_bulkMaskEditingFiles allObjects] inProject:_project];
_applyOutputFileNameMaskButton.enabled = [self draftFileNameMask].length > 0 && (commonMask.length == 0 || ![commonMask isEqualToString:[self draftFileNameMask]]);
}

- (void)controlTextDidBeginEditing:(NSNotification *)obj {
NSLog(@"controlTextDidBeginEditing");
}

- (void)controlTextDidEndEditing:(NSNotification *)obj {
NSLog(@"controlTextDidEndEditing");
}

- (void)controlTextDidChange:(NSNotification *)obj {
NSLog(@"controlTextDidChange");
if (obj.object != _outputFileNameMask)
return;

[self startBulkMaskEditing];
[self updateApplyMaskButton];
[_pathTableView reloadData];
}

- (IBAction)applyFileNameMask:(id)sender {
NSLog(@"applyFileNameMask");
NSString *mask = _outputFileNameMask.stringValue;

NSArray *selection = [self selectedFileOptions];
for (FileCompilationOptions *fileOptions in selection) {
fileOptions.destinationNameMask = mask;
}

[self endBulkMaskEditing];
}


#pragma mark - Settings Restore

Expand Down

0 comments on commit 3b113ca

Please sign in to comment.