GitIndex: Add a few notifications
[GitX.git] / PBGitWindowController.m
blobaba1987b072962a6e1dd187f5f5468d15de6de83
1 //
2 //  PBDetailController.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 16-06-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBGitWindowController.h"
10 #import "PBGitHistoryController.h"
11 #import "PBGitCommitController.h"
13 @implementation PBGitWindowController
16 @synthesize repository, viewController, selectedViewIndex;
18 - (id)initWithRepository:(PBGitRepository*)theRepository displayDefault:(BOOL)displayDefault
20         if(self = [self initWithWindowNibName:@"RepositoryWindow"])
21         {
22                 self.repository = theRepository;
23                 [self showWindow:nil];
24         }
25         
26         if (displayDefault) {
27                 self.selectedViewIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedViewIndex"];
28         } else {
29                 self.selectedViewIndex = -1;
30         }
32         return self;
35 - (void)windowWillClose:(NSNotification *)notification
37         NSLog(@"Window will close!");
38         if (historyViewController)
39                 [historyViewController removeView];
40         if (commitViewController)
41                 [commitViewController removeView];
44 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
46         if ([menuItem action] == @selector(showCommitView:) || [menuItem action] == @selector(showHistoryView:)) {
47                 return ![repository isBareRepository];
48         }
49         return YES;
52 - (void) setSelectedViewIndex: (int) i
54         [self changeViewController: i];
57 - (void)changeViewController:(NSInteger)whichViewTag
59         [self willChangeValueForKey:@"viewController"];
61         if (viewController != nil)
62                 [[viewController view] removeFromSuperview];
64         if ([repository isBareRepository]) {    // in bare repository we don't want to view commit
65                 whichViewTag = 0;               // even if it was selected by default
66         }
68         // Set our default here because we might have changed it (based on bare repo) before
69         selectedViewIndex = whichViewTag;
70         [[NSUserDefaults standardUserDefaults] setInteger:whichViewTag forKey:@"selectedViewIndex"];
72         switch (whichViewTag)
73         {
74                 case 0: // swap in the "CustomImageViewController - NSImageView"
75                         if (!historyViewController)
76                                 historyViewController = [[PBGitHistoryController alloc] initWithRepository:repository superController:self];
77                         else
78                                 [historyViewController updateView];
79                         viewController = historyViewController;
80                         break;
81                 case 1:
82                         if (!commitViewController)
83                                 commitViewController = [[PBGitCommitController alloc] initWithRepository:repository superController:self];
84                         else
85                                 [commitViewController updateView];
87                         viewController = commitViewController;
88                         break;
89         }
91         // make sure we automatically resize the controller's view to the current window size
92         [[viewController view] setFrame: [contentView bounds]];
93         
94         //// embed the current view to our host view
95         [contentView addSubview: [viewController view]];
97         [self useToolbar: [viewController viewToolbar]];
99         // Allow the viewcontroller to catch actions
100         [self setNextResponder: viewController];
101         [self didChangeValueForKey:@"viewController"];  // this will trigger the NSTextField's value binding to change
103         [[self window] makeFirstResponder:[viewController firstResponder]];
106 - (void)awakeFromNib
108         [[self window] setDelegate:self];
109         [[self window] setAutorecalculatesContentBorderThickness:NO forEdge:NSMinYEdge];
110         [[self window] setContentBorderThickness:35.0f forEdge:NSMinYEdge];
111         [self showHistoryView:nil];
114 - (void) showCommitView:(id)sender
116         if (self.selectedViewIndex != 1)
117                 self.selectedViewIndex = 1;
120 - (void) showHistoryView:(id)sender
122         if (self.selectedViewIndex != 0)
123                 self.selectedViewIndex = 0;
126 - (void)showMessageSheet:(NSString *)messageText infoText:(NSString *)infoText
128         [[NSAlert alertWithMessageText:messageText
129                          defaultButton:nil
130                        alternateButton:nil
131                            otherButton:nil
132              informativeTextWithFormat:infoText] beginSheetModalForWindow: [self window] modalDelegate:self didEndSelector:nil contextInfo:nil];
135 - (void)showErrorSheet:(NSError *)error
137         [[NSAlert alertWithError:error] beginSheetModalForWindow: [self window] modalDelegate:self didEndSelector:nil contextInfo:nil];
141 #pragma mark -
142 #pragma mark Toolbar Delegates
144 - (void) useToolbar:(NSToolbar *)toolbar
146         NSSegmentedControl *item = nil;
147         for (NSToolbarItem *toolbarItem in [toolbar items]) {
148                 if ([[toolbarItem view] isKindOfClass:[NSSegmentedControl class]]) {
149                         item = (NSSegmentedControl *)[toolbarItem view];
150                         break;
151                 }
152         }
153         [item bind:@"selectedIndex" toObject:self withKeyPath:@"selectedViewIndex" options:0];
154         [item setEnabled: ![repository isBareRepository]];
156         [self.window setToolbar:toolbar];
159 @end