+- (IBAction)mappingPressed:(id)sender {
+ [popover showRelativeToRect:popoverActivate.bounds
+ ofView:popoverActivate
+ preferredEdge:NSMinXEdge];
+}
+
+- (void)popoverWillShow:(NSNotification *)notification {
+ popoverActivate.state = NSOnState;
+}
+
+- (void)popoverWillClose:(NSNotification *)notification {
+ popoverActivate.state = NSOffState;
+}
+
+- (IBAction)moveUpPressed:(id)sender {
+ NSUInteger idx = [_mappings indexOfObject:_currentMapping];
+ if (idx > 1 && idx != NSNotFound) {
+ [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx - 1];
+ [self mappingsChanged];
+ }
+}
+
+- (IBAction)moveDownPressed:(id)sender {
+ NSUInteger idx = [_mappings indexOfObject:_currentMapping];
+ if (idx < _mappings.count - 1) {
+ [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx + 1];
+ [self mappingsChanged];
+ }
+}
+
+- (BOOL)tableView:(NSTableView *)tableView_
+ acceptDrop:(id <NSDraggingInfo>)info
+ row:(NSInteger)row
+ dropOperation:(NSTableViewDropOperation)dropOperation {
+ NSPasteboard *pboard = [info draggingPasteboard];
+ if ([pboard.types containsObject:PB_ROW]) {
+ NSString *value = [pboard stringForType:PB_ROW];
+ NSUInteger srcRow = [value intValue];
+ [_mappings moveObjectAtIndex:srcRow toIndex:row];
+ [self mappingsChanged];
+ return YES;
+ } else if ([pboard.types containsObject:NSURLPboardType]) {
+ NSURL *url = [NSURL URLFromPasteboard:pboard];
+ NSError *error;
+ NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url
+ mappings:_mappings
+ error:&error];
+ if (error) {
+ [tableView_ presentError:error];
+ return NO;
+ } else {
+ [_mappings insertObject:mapping atIndex:row];
+ [self mappingsChanged];
+ return YES;
+ }
+ } else {
+ return NO;
+ }
+}
+
+- (NSDragOperation)tableView:(NSTableView *)tableView_
+ validateDrop:(id <NSDraggingInfo>)info
+ proposedRow:(NSInteger)row
+ proposedDropOperation:(NSTableViewDropOperation)dropOperation {
+ NSPasteboard *pboard = [info draggingPasteboard];
+ if ([pboard.types containsObject:PB_ROW]) {
+ [tableView_ setDropRow:MAX(1, row) dropOperation:NSTableViewDropAbove];
+ return NSDragOperationMove;
+ } else if ([pboard.types containsObject:NSURLPboardType]) {
+ NSURL *url = [NSURL URLFromPasteboard:pboard];
+ if ([url.pathExtension isEqualToString:@"enjoyable"]) {
+ [tableView_ setDropRow:MAX(1, row) dropOperation:NSTableViewDropAbove];
+ return NSDragOperationCopy;
+ } else {
+ return NSDragOperationNone;
+ }
+ } else {
+ return NSDragOperationNone;
+ }
+}
+
+- (NSArray *)tableView:(NSTableView *)tableView_
+namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
+forDraggedRowsWithIndexes:(NSIndexSet *)indexSet {
+ NJMapping *toSave = self[indexSet.firstIndex];
+ NSString *filename = [[toSave.name stringByFixingPathComponent]
+ stringByAppendingPathExtension:@"enjoyable"];
+ NSURL *dst = [dropDestination URLByAppendingPathComponent:filename];
+ dst = [NSFileManager.defaultManager generateUniqueURLWithBase:dst];
+ NSError *error;
+ if (![toSave writeToURL:dst error:&error]) {
+ [tableView_ presentError:error];
+ return @[];
+ } else {
+ return @[dst.lastPathComponent];
+ }
+}
+
+- (BOOL)tableView:(NSTableView *)tableView_
+writeRowsWithIndexes:(NSIndexSet *)rowIndexes
+ toPasteboard:(NSPasteboard *)pboard {
+ if (rowIndexes.count == 1 && rowIndexes.firstIndex != 0) {
+ [pboard declareTypes:@[PB_ROW, NSFilesPromisePboardType] owner:nil];
+ [pboard setString:@(rowIndexes.firstIndex).stringValue forType:PB_ROW];
+ [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType];
+ return YES;
+ } else {
+ return NO;
+ }
+}
+