Murky / Source / RepoController.m

  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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
//
//  RepoController.m
//  Murky
//
//  Copyright 2008-2009 Jens Alfke. All rights reserved.
//

#import "RepoController.h"
#import "HgRepository.h"
#import "HgDir.h"
#import "HgUncommittedRevision.h"
#import "HgTempDir.h"
#import "RevisionGraphColumn.h"
#import "SourceHighlighting.h"
#import "MYTask.h"


/** Repository-relative path to where Murky stores its per-repository settings */
#define kSettingsFilePath @".hg/com.mooseyard.Murky.plist"


@interface RepoController ()
- (void) _updateWindowTitle;
- (void) _expandPaths;
- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn;
@end


@implementation RepoController


@synthesize currentOperation=_currentOperation;


+ (NSArray*) allRepoControllers
{
    NSMutableArray *openRepos = [NSMutableArray array];
    for( NSWindow *window in [NSApp windows]) {
        id delegate = [window delegate];
        if( [window isVisible] && [delegate isKindOfClass: self] ) {
            [openRepos addObject: delegate];
        }
    }
    return openRepos;
}


+ (RepoController*) existingRepoControllerWithRepository: (HgRepository*)repo
{
    for( RepoController *repoController in [self allRepoControllers]) {
        if( [repoController repository] == repo )
            return repoController;
    }
    return nil;
}


+ (RepoController*) existingRepoControllerWithDirectory: (NSString*)directory
{
    directory = [HgRepository findRootOf: directory localPath: nil];
    if (!directory)
        return nil;
    for( RepoController *repoController in [self allRepoControllers]) {
        if( [[repoController directory] isEqualToString: directory] )
            return repoController;
    }
    return nil;
}


+ (RepoController*) repoControllerWithDirectory: (NSString*)directory error: (NSError**)error
{
    RepoController *controller = [self existingRepoControllerWithDirectory: directory];
    if (!controller)
        controller = [[self alloc] initWithDirectory: directory error: error];
    return controller;
}


- (id) initWithDirectory: (NSString*)path error: (NSError**)error
{
    BOOL isDir;
    if( ! [[NSFileManager defaultManager] fileExistsAtPath: path isDirectory: &isDir] ) {
        MYMiscError(error, 
                    NSLocalizedString(@"No such directory %@", 
                                      @"Error message from Open command"),
                    path);
        return nil;
    } else if( ! isDir ) {
        MYMiscError(error,
                    NSLocalizedString(@"%@ is not a directory", 
                                      @"Error message from Open command"),
                    path);
        return nil;
    }
    HgRepository *repo = [[HgRepository alloc] initWithPath: path localPath: NULL];
    if( ! repo ) {
        NSInteger result = NSRunAlertPanel(NSLocalizedString(@"Convert to Mercurial repository?", 
                                                       @"Title of conversion alert from Open command"), 
                                     NSLocalizedString(@"The directory <%@> is not already a Mercurial repository. Would you like to make it one?", 
                                                       @"Body of conversion alert from Open command"),
                                     NSLocalizedString(@"Convert", 
                                                       @"Button of conversion alert from Open command"), 
                                     NSLocalizedString(@"Cancel", @"Button"), nil,
                                     path);
        if( result != NSOKButton ) {
            if (error) *error = nil;
            return nil;
        }
        repo = [[HgRepository alloc] initNewWithPath: path error: error];
        if( ! repo )
            return nil;
    }
    return [self initWithRepository: repo];
}


- (id) initWithRepository: (HgRepository*)repo;
{
    Assert(repo);
    self = [super initWithWindowNibName: @"Repo"];
    if (self != nil) {
        _repo = repo;
        _settings = [[NSMutableDictionary alloc] initWithContentsOfFile: 
                     [repo.absolutePath stringByAppendingPathComponent: kSettingsFilePath]];
        if( ! _settings ) {
            _settings = [NSMutableDictionary dictionary];
            self.shouldCascadeWindows = NO;
        }
    }
    return self;
}


- (void)close
{
    [self saveSettings];
    [super close];
}


- (void) awakeFromNib
{
    self.window.representedFilename = _repo.absolutePath;
    
    [_outline setDoubleAction: @selector(openSelectedFile:)];
    [_revisionTable setDoubleAction: @selector(showRevision:)];
    
    _revisionTable.sortDescriptors = [NSArray arrayWithObject:
                                      [[NSSortDescriptor alloc] initWithKey: @"localNumber" ascending: NO]];
    NSSize spacing = _revisionTable.intercellSpacing;
    spacing.height = 0;
    _revisionTable.intercellSpacing = spacing;
    
    [_tree addObserver: self forKeyPath: @"selectedObjects"
               options: 0
               context: @selector(_fileSelectionChanged:change:)];
    
    [_revisions addObserver: self forKeyPath: @"selectedObjects"
                    options: NSKeyValueObservingOptionInitial
                    context: @selector(_revisionSelectionChanged:change:)];
    
    [self performSelector: @selector(_initialLoadFiles) withObject: nil afterDelay: 0];
}

- (void)windowDidLoad
{
    NSString *str = [_settings objectForKey: @"WindowFrame"];
    if( str ) {
        [self.window setFrameFromString: str];
        self.shouldCascadeWindows = NO;
    }
    
    self.showsOnlyUncleanFiles = [[_settings objectForKey: @"HideUnmodified"] boolValue];
}


- (void) _initialLoadFiles
{
    NSString *uncommittedComments = [_settings objectForKey: @"UncommittedComments"];
    if( uncommittedComments ) {
        HgUncommittedRevision *last = _repo.modifiedRevision;
        if( last ) {
            last.formattedDescription = uncommittedComments;
            [self _updateInfoView];
        }
    }
    
    [self tableView: _revisionTable didClickTableColumn: nil];  // set up graph column
    
    [_repo addObserver: self forKeyPath: @"revisions"
               options: NSKeyValueObservingOptionInitial
               context: @selector(_revisionListChanged)];
    [_repo addObserver: self forKeyPath: @"modifiedRevision.root.statusImage"
               options: 0 
               context: @selector(_updateWindowTitle)];
    [_repo addObserver: self forKeyPath: @"modifiedRevision.formattedDescription"
               options: 0 
               context: @selector(_uncommittedRevisionDescChanged)];
    
    _expandedPaths = [_settings objectForKey: @"ExpandedPaths"];
    if( ! _expandedPaths )
        _expandedPaths = [NSArray arrayWithObject: @""];     // by default just expand root
    [self _expandPaths];
}


- (void) _updateWindowTitle
{
    NSString *title = _repo.webName;
    NSImage *statusIcon = nil;
    HgRevision *rev = self.selectedRevision;
    if( rev && !rev.isUncommitted )
        title = [NSString stringWithFormat: NSLocalizedString(@"%@ [rev %i]", 
                                                              @"Repository window title format"),
                                            title, rev.localNumber];
    else {
        HgDir *root = _repo.uncommittedRevision.root;
        if (root.status != kClean)
            statusIcon = root.statusImage;
    }
    
    self.window.title = title;

    if (statusIcon)
        [[self.window standardWindowButton: NSWindowDocumentIconButton] setImage: statusIcon];
    else
        self.window.representedFilename = _repo.absolutePath;   // restore default folder icon
}


#pragma mark -
#pragma mark ACCESSORS:


- (HgRepository*) repository
{
    return _repo;
}

- (NSString*) directory
{
    return _repo.absolutePath;
}

- (HgTempDir*) tempDir
{
    if( ! _tempDir )
        _tempDir = [[HgTempDir alloc] init];
    return _tempDir;
}


- (void) saveSettings
{
    if( _settingsChanged ) {
        LogTo(Settings,@"Saving settings");
        [_repo ignoreNextFileChanges];
        [_settings writeToFile: [_repo.absolutePath stringByAppendingPathComponent: kSettingsFilePath]
                    atomically: YES];
        _settingsChanged = NO;
    }
}

- (void) setValue: (id)value forSetting: (NSString*)setting
{
    id oldValue = [_settings objectForKey: setting];
    if( value!=oldValue && ![value isEqual: oldValue] ) {
        LogTo(Settings,@"%@ = %@",setting,value);
        if( value )
            [_settings setObject: value forKey: setting];
        else
            [_settings removeObjectForKey: setting];
        if( ! _settingsChanged ) {
            _settingsChanged = YES;
            [self performSelector: @selector(saveSettings) withObject: nil afterDelay: 2.0];
        }
    }
}


#pragma mark -
#pragma mark DISPLAY OPTIONS:


- (void) _setVisibility: (BOOL)visible ofColumnID: (NSString*)identifier
{
    NSTableColumn *col = [_outline tableColumnWithIdentifier: identifier];
    [col setHidden: !visible];
}


- (void) _rememberExpandedPaths
{
    if( ! self.showsOnlyUncleanFiles ) {
        _expandedPaths = [NSMutableArray array];
        NSUInteger nRows = [_outline numberOfRows];
        for( NSUInteger row=0; row<nRows; row++ ) {
            NSTreeNode *item = [_outline itemAtRow: row];
            if( [_outline isItemExpanded: item] )
                [_expandedPaths addObject: [[item representedObject] path]];
        }
    }
}

- (void) _expandPaths
{
    if( ! self.showsOnlyUncleanFiles ) {
        NSArray *rootNodes = [[_tree arrangedObjects] childNodes];
        if( rootNodes.count > 0 ) {
            NSTreeNode *rootNode = [rootNodes objectAtIndex: 0];
            for( NSString *path in _expandedPaths ) {
                HgFile *file = [self.selectedRevision fileAtPath: path];
                if( file ) {
                    NSTreeNode *node;
                    if( file.isRoot )
                        node = rootNode;
                    else
                        node = [rootNode descendantNodeAtIndexPath: file.indexPath];
                    if( node )
                        [_outline expandItem: node];
                    else
                        Log(@"? Couldn't find tree node for %@",file);
                } else
                    Log(@"? Couldn't find HgFile for path %@",path);
            }
        }
    }
}


- (void)outlineViewItemDidExpand:(NSNotification *)notification
{
    [self _rememberExpandedPaths];
    [self setValue: _expandedPaths forSetting: @"ExpandedPaths"];
}

- (void)outlineViewItemDidCollapse:(NSNotification *)notification
{
    [self _rememberExpandedPaths];
    [self setValue: _expandedPaths forSetting: @"ExpandedPaths"];
}

static NSString* modifiedString(NSUInteger n) {
    if (n == 0)
        return nil;
    NSString *format;
    if (n == 1)
        format = NSLocalizedString(@"%i modified file", @"View Mode Button Title Path singular");
    else 
        format = NSLocalizedString(@"%i modified files", @"View Mode Button Title Path");
    return [NSString stringWithFormat: format, n];
}

static NSString* conflictString(NSUInteger n) {
    if (n == 0)
        return nil;
    NSString *format;
    if (n == 1)
        format = NSLocalizedString(@"%i conflict", @"View Mode Button Title Path singular");
    else 
        format = NSLocalizedString(@"%i conflicts", @"View Mode Button Title Path");
    return [NSString stringWithFormat: format, n];
}

- (void) _updateViewModeButtonTitle
{
    NSArray *modified = self.selectedRevision.uncleanFiles;
    NSUInteger nConflicts = 0;
    for (HgFile *file in modified)
        if (file.mergeStatus == kConflict)
            nConflicts++;
    NSString *viewModeTitle = modifiedString(modified.count);
    if (nConflicts > 0)
        viewModeTitle = [viewModeTitle stringByAppendingFormat: @", %@", conflictString(nConflicts)];

    if (!self.showsOnlyUncleanFiles) {
        viewModeTitle = [NSString stringWithFormat: 
                            NSLocalizedString(@"All Files (%@)", @"View Mode Button Title Files"),
                            viewModeTitle];
    }
    [_viewModeButton setTitle: viewModeTitle];
}

- (BOOL) showsOnlyUncleanFiles
{
    return [[[_tree infoForBinding: @"content"] objectForKey: NSObservedKeyPathKey] isEqual: @"selection.uncleanFiles"];
}

- (void) setShowsOnlyUncleanFiles: (BOOL)showUnclean
{
    NSString *contentPath;
    if( showUnclean ) {
        //_tree.leafKeyPath = @"isNonRepository";
        contentPath =  @"selection.uncleanFiles";
    } else {
        //_tree.leafKeyPath = @"isFile";
        contentPath = @"selection.rootAsArray";
    }
    
    if(_hideShowUnmodifiedToolbarItem.selectedSegment != showUnclean){
        _hideShowUnmodifiedToolbarItem.selectedSegment = showUnclean;
    }
    
    [_tree bind: @"content" toObject: _revisions withKeyPath: contentPath options: nil];
    [_tree rearrangeObjects];
    [self _setVisibility:  showUnclean ofColumnID: @"path"];
    [self _setVisibility: !showUnclean ofColumnID: @"name"];
    [_outline sizeLastColumnToFit];
    
    [self setValue: [NSNumber numberWithBool: showUnclean] forSetting: @"HideUnmodified"];
    
    if( ! showUnclean )
        [self _expandPaths];
    
    [self _updateViewModeButtonTitle];
}


#pragma mark -
#pragma mark NOTIFICATION HANDLERS:


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
    [self performSelector: (SEL)context withObject: object withObject: change];
}


- (void) _uncommittedRevisionDescChanged
{
    [self setValue: _repo.modifiedRevision.formattedDescription forSetting: @"UncommittedComments"];
}


- (void) _revisionListChanged
{
#if 0
    NSMutableString *dump = [NSMutableString string];
    [_repo dumpTo: dump];
    Log(@"*** Revision list changed: ***\n%@", dump);
#endif
    
    // Update the graph column's list of revisions:
    RevisionGraphCell *graphCell = [_revisionTable tableColumnWithIdentifier: @"graph"].dataCell;
    if( graphCell.revisions )
        graphCell.revisions = _repo.revisions;
    [_revisionTable setNeedsDisplayInRect: [_revisionTable rectOfColumn: [_revisionTable columnWithIdentifier: @"graph"]]];
}


#pragma mark -
#pragma mark UI DELEGATES:


- (void)windowDidResize:(NSNotification *)notification
{
    if( self.window.isVisible )
        [self setValue: self.window.stringWithSavedFrame forSetting: @"WindowFrame"];
}

- (void)windowDidMove:(NSNotification *)notification
{
    if( self.window.isVisible )
        [self setValue: self.window.stringWithSavedFrame forSetting: @"WindowFrame"];
}


- (void)outlineView:(NSOutlineView *)outlineView
    willDisplayCell:(id)cell
     forTableColumn:(NSTableColumn *)tableColumn 
               item:(id)item
{
    if( [tableColumn.identifier isEqualToString: @"name"] ) {
        // Set the file icon to show along with the name:
        NSTreeNode *node = item;
        HgFile *file = node.representedObject;
        [cell setIcon: file.icon];
    }
}


- (void)tableView:(NSTableView *)tableView 
  willDisplayCell:(NSTextFieldCell*)cell
   forTableColumn:(NSTableColumn *)tableColumn 
              row:(NSInteger)row
{
    if( [tableColumn.identifier isEqualToString: @"graph"] )
        return;
    
    static NSColor *normalColor, *unrelatedColor, *uncommittedColor, *containsFileColor;
    static NSFont *normalFont, *currentFont;
    if( ! normalColor ) {
        normalColor = [NSColor controlTextColor];
        unrelatedColor = [NSColor grayColor];
        uncommittedColor = [NSColor darkGrayColor];
        containsFileColor = [NSColor colorWithCalibratedRed: 0.75f green: 0 blue: 0 alpha: 1];
        normalFont = cell.font;
        currentFont = [[NSFontManager sharedFontManager] convertFont: normalFont 
                                                         toHaveTrait: NSBoldFontMask];
    }
    
    HgRevision *rev = [[_revisions arrangedObjects] objectAtIndex: row];
    HgRevision *cur = _repo.currentRevision;
    HgRevision *working = _repo.modifiedRevision ?:cur;
    
    NSFont *font = normalFont;
    NSColor *color = normalColor;
    if( rev.isUncommitted ) {
        font = currentFont;
        color = uncommittedColor;
    } else if( rev==cur ) {
        font = currentFont;
    } else if( ! [working descendsFrom: rev] ) {
        color = unrelatedColor;
    }
    
    /*HgFile *sel = self.actuallySelectedFile;
    if( sel && [rev containsFile: sel] )
        color = containsFileColor;*/
    
    if( [tableView isRowSelected: row] )
    color = normalColor;

    cell.textColor = color;
    if( ! [tableColumn.identifier isEqualToString: @"ID"] )
        cell.font = font;
}


- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn
{
    // Sort key/order may have changed.
    RevisionGraphCell *graphCell = [_revisionTable tableColumnWithIdentifier: @"graph"].dataCell;
    NSSortDescriptor *sortDesc = [_revisionTable.sortDescriptors objectAtIndex: 0];
    if( [[sortDesc key] isEqualToString: @"localNumber"] ) {
        if( graphCell.revisions == nil ) {
            graphCell.revisions = _repo.revisions;
        }
        graphCell.flipped = sortDesc.ascending;
    } else {
        if( graphCell.revisions ) {
            graphCell.revisions = nil;
        }
    }
}


#pragma mark -
#pragma mark INFO PANE:


enum {
    kInfoLogSegment = 0,
    kInfoContentsSegment,
    kInfoDiffSegment
};


// Table-view action for the revision list; called when a row is clicked.
- (IBAction) revisionTableAction: (id)sender {
    if (_infoChooser.selectedSegment != kInfoLogSegment) {
        _infoChooser.selectedSegment = kInfoLogSegment;
        [self _updateInfoView];
    }
}

// Table-view action for the file list; called when a row is clicked.
- (IBAction) fileTableAction: (id)sender {
    if (_infoChooser.selectedSegment == kInfoLogSegment) {
        _infoChooser.selectedSegment = kInfoDiffSegment;
        [self _updateInfoView];
    }
}


- (void) _updateInfoChooser {
    BOOL hasRev = self.selectedRevision != nil;
    BOOL hasFile = self.selectedFile.isFile;
    [_infoChooser setEnabled: hasRev  forSegment: kInfoLogSegment];
    [_infoChooser setEnabled: hasFile forSegment: kInfoContentsSegment];
    [_infoChooser setEnabled: hasFile forSegment: kInfoDiffSegment];
}


- (IBAction) infoChooserChanged: (id)sender {
    [self _updateInfoView];
}


- (void) _updateInfoView
{
    NSAttributedString *text = nil;
    BOOL editable = NO, wraps = NO;
    
    HgRevision *revision = self.selectedRevision;
    HgFile *file = self.selectedFile;
    if (!file.isFile)
        file = nil;
    
    switch (_infoChooser.selectedSegment) {
        case kInfoLogSegment:
            text = revision.richFormattedDescription;
            editable = revision.isUncommitted;
            wraps = YES;
            break;
        case kInfoContentsSegment:
            if (file) {
                NSString *str = nil;
                NSError *error;
                NSData *contents = [revision getFileContents: file error: &error];
                if (contents) {
                    str = [[NSString alloc] initWithData: contents encoding: NSUTF8StringEncoding];
                } else {
                    str = error.localizedRecoverySuggestion;
                }
                text = AttributedStringForSourceCode(str);
            }
            break;
        case kInfoDiffSegment:
            if (file) {
                NSError *error;
                NSString *str = [revision diffFile: file withRevision: revision.parent error: &error];
                if (!str)
                    str = error.localizedRecoverySuggestion;
                NSMutableAttributedString *mtext = AttributedStringForSourceCode(str);
                HighlightDiffs(mtext);
                text = mtext;
            }
            break;
    }
    
    [_infoView setEditable: editable];
    [_infoView setHorizontallyResizable: !wraps];
    NSSize containerSize = _infoView.textContainer.containerSize;
    containerSize.width = wraps ?_infoView.enclosingScrollView.documentVisibleRect.size.width :FLT_MAX;
    [[_infoView textContainer] setContainerSize: containerSize];
    [[_infoView textContainer] setWidthTracksTextView: wraps];
    [_infoView.enclosingScrollView setHidden: NO];
    
    if (text) {
        [_infoView.textStorage setAttributedString: text];
    } else {
        [_infoView setString: @""];
		if (file && _infoChooser.selectedSegment == kInfoContentsSegment) {
			[_infoView.enclosingScrollView setHidden: YES];
            NSImage *image = [[NSImage alloc] initWithContentsOfFile:file.absolutePath];
            if( image ) {
                [_infoImageView setImage: image];
            } else {
                [_infoImageView setImage: [NSImage imageNamed:@"no_file.png"]];
            }
            [image release];
        }
    }
    [_infoView setNeedsDisplay: YES];
}

- (void)textDidChange:(NSNotification *)notification
{
    if( notification.object == _infoView ) {
        HgRevision *revision = self.selectedRevision;
        if( revision.isUncommitted )
            ((HgUncommittedRevision*)revision).formattedDescription = _infoView.string;
    }
}

- (void) infoViewFocusChanged: (NSNotification*)n {
    HgRevision *revision = self.selectedRevision;
    if (revision.isUncommitted && !revision.richFormattedDescription) {
        NSString *text = @"";
        if (_infoView != _infoView.window.firstResponder)
            text = @"(Describe your changes here)";
        [_infoView setString: text];
    }
}



#pragma mark -
#pragma mark SELECTION:


- (HgFile*) selectedFile
{
    NSArray *sel = self.selectedFiles;
    if( sel.count == 1 )
        return [sel objectAtIndex: 0];
    else
        return nil;
}

- (NSArray*) selectedFiles
{
    // During a right-click, use the clicked row if it's not selected:
    NSInteger row = _outline.clickedRow;
    if( row >= 0 && ! [_outline isRowSelected: row] )
        return [NSArray arrayWithObject: [[_outline itemAtRow: row] representedObject]];
    
    return _tree.selectedObjects;
}


/* Returns the file that's actually selected in the file tree.
   This will be the same as -selectedFile, except while handling a right-click contextual menu
   (where -selectedFile returns the file right-clicked, but this returns the selected file.) */
- (HgFile*) actuallySelectedFile
{
    NSArray *sel = _tree.selectedObjects;
    if( sel.count == 1 )
        return [sel objectAtIndex: 0];
    else
        return nil;
}


- (HgRevision*) selectedRevision
{
    NSArray *sel = [_revisions selectedObjects];
    if( sel.count > 0 )
        return [sel objectAtIndex: 0];
    else
        return nil;
}

- (void) setSelectedRevision: (HgRevision*)rev
{
    [_revisions setSelectedObjects: (rev ?[NSArray arrayWithObject: rev] :nil)];
}


- (void) _fileSelectionChanged: (NSTreeController*)controller change: (NSDictionary*)change
{
    //Log(@"File Selection = %@",self.selectedFile);
    [self _updateInfoChooser];
    if (_infoChooser.selectedSegment != kInfoLogSegment)
        [self _updateInfoView];
}

- (void) _revisionSelectionChanged: (NSTreeController*)controller change: (NSDictionary*)change
{
    Log(@"Revision Selection = %@",self.selectedRevision);
    [self _updateInfoChooser];
    [self _updateInfoView];
    [self _updateWindowTitle];
    [self _updateViewModeButtonTitle];
}

@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.