2 // PBDetailController.m
5 // Created by Pieter de Bie on 16-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
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"])
22 self.repository = theRepository;
23 [self showWindow:nil];
27 self.selectedViewIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedViewIndex"];
29 self.selectedViewIndex = -1;
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];
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
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"];
74 case 0: // swap in the "CustomImageViewController - NSImageView"
75 if (!historyViewController)
76 historyViewController = [[PBGitHistoryController alloc] initWithRepository:repository superController:self];
78 [historyViewController updateView];
79 viewController = historyViewController;
82 if (!commitViewController)
83 commitViewController = [[PBGitCommitController alloc] initWithRepository:repository superController:self];
85 [commitViewController updateView];
87 viewController = commitViewController;
91 // make sure we automatically resize the controller's view to the current window size
92 [[viewController view] setFrame: [contentView bounds]];
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]];
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
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];
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];
153 [item bind:@"selectedIndex" toObject:self withKeyPath:@"selectedViewIndex" options:0];
154 [item setEnabled: ![repository isBareRepository]];
156 [self.window setToolbar:toolbar];