// // P4MenuStatusViewController.m // P4Menu // // Created by Michael Bishop on 7/1/11. // Copyright 2011 Numerical Garden LLC. All rights reserved. // #import "NGAUtilities.h" #import "P4MenuStatusViewController.h" #import "P4MenuShadedView.h" #import "P4MenuLocalFile.h" #import "P4Connection.h" #import "P4Client.h" #import "P4LocalFileManager.h" #import "P4ErrorCodes.h" #import "P4ServerEntry.h" #import "P4MenuController.h" static NSString * kCurrentLocalFileKeyPath = @"currentLocalFile"; static NSString * kCurrentFileManager = @"currentFileManager"; static NSString * kCurrentLocalFileFStatInfo = @"currentLocalFileFStatInfo"; static NSString * kCurrentConnectionError = @"connectionError"; @interface P4MenuStatusViewController () -(void)_configureViews; -(NSString*)_nameForView:(NSView*)view; @end @implementation P4MenuStatusViewController @synthesize fileDescriptionTextView; @synthesize statusSubview = _statusSubview ,menuController = _menuController ; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ( !(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) ) return nil; [self registerAllAutoObservationMethods]; return self; } -(id)init { return [self initWithNibName:NSStringFromClass([self class]) bundle:[NSBundle bundleForClass:[self class]]]; } -(void)dealloc { [self unregisterAllAutoObservationMethods]; [_statusSubview removeFromSuperview]; [_statusSubview release]; self.menuController = nil; [super dealloc]; } -(void)awakeFromNib { shadedView.baseColor = [NSColor colorWithDeviceRed:0.92 green:0.67 blue:0.0 alpha:1.0]; self.view = fileNotFoundStatusView; } -(void)valueDidChangeForMenuController_currentLocalFileChange_comment:(NSDictionary*)changes { id newValue = NEW_VALUE(changes); if ( !newValue || [[NSNull null] isEqual:newValue] ) newValue = @""; [self.fileDescriptionTextView setString:newValue]; [self.fileDescriptionTextView setFont:[NSFont userFixedPitchFontOfSize:9.0]]; } -(void)valueDidChangeForMenuController_currentLocalFile { [self _configureViews]; } -(void)valueDidChangeForMenuController_currentFileManager { [self _configureViews]; } -(void)valueDidChangeForMenuController_problemConnection { [self _configureViews]; } -(void)valueDidChangeForMenuController_currentLocalFileFStatInfo { NSString * haveRev = [_menuController.currentLocalFileFStatInfo objectForKey:@"haveRev"]; if ( haveRev ) [fileStatusStatusText.cell setPlaceholderString:@"Not open for edit"]; else [fileStatusStatusText.cell setPlaceholderString:@"Not added to Perforce"]; [self _configureViews]; } -(void)valueDidChangeForMenuController_servers { [self _configureViews]; } -(void)valueDidChangeForMenuController_currentWindowTitle { if ( _menuController.currentWindowTitle.length == 0 ) fileNotFoundLabel.stringValue = @"The current window does not seem to contain a file"; else fileNotFoundLabel.stringValue = [NSString stringWithFormat:@"The current window, titled \"%@\", does not seem to contain a filename", _menuController.currentWindowTitle]; } -(NSString*)statusMessage { return [statusTextField stringValue]; } -(void)setStatusMessage:(NSString*)statusMessage { if ( !statusMessage ) statusMessage = @""; [statusTextField setStringValue:statusMessage]; } -(void)setStatusSubview:(NSView*)statusSubview { if ( _statusSubview == statusSubview ) return; [_statusSubview setHidden:YES]; [_statusSubview removeFromSuperview]; [_statusSubview release]; _statusSubview = [statusSubview retain]; if ( !statusSubview ) return; [fileStatusContainerView addSubview:statusSubview]; [statusSubview setHidden:NO]; NSRect shadeRect = [shadedView frame]; NSRect subviewRect = [statusSubview frame]; NSRect viewRect = [self.view frame]; // move the rect to just below the shaded view subviewRect.origin.x = 0; subviewRect.origin.y = 0; viewRect.size.width = subviewRect.size.width; viewRect.size.height = shadeRect.size.height + subviewRect.size.height; subviewRect.size.width = subviewRect.size.width; [self.view setFrame:viewRect]; [statusSubview setFrame:subviewRect]; } -(NSString*)_nameForView:(NSView*)view { if (view == shadedView) { return @"Shaded Header"; } if (view == fileNotFoundStatusView) { return @"File Not Found"; } if (view == fileNotTrackedStatusView) { return @"File Not Tracked"; } if (view == passwordErrorStatusSubview) { return @"Password Error"; } if (view == fileStatusSubview) { return @"File Status"; } if (view == errorDescriptionStatusSubview) { return @"Generic Perforce Error"; } return @""; } -(void)setView:(NSView*)view { [super setView:view]; } #pragma mark PRIVATE -(void)_configureViews { if ( _menuController.currentServerEntry.connection.needsNewPassword ) { self.view = fileStatusContainerView; self.statusSubview = passwordErrorStatusSubview; return; } if ( !_menuController.currentLocalFile ) { self.view = fileNotFoundStatusView; return; } P4LocalFileManager * fileManager = _menuController.currentFileManager; if ( !fileManager ) { self.view = fileNotTrackedStatusView; return; } self.view = fileStatusContainerView; if ( fileManager.lastError == nil ) { self.statusSubview = fileStatusSubview; return; } self.statusSubview = fileStatusSubview; return; } @end