/* Copyright (C) 2002-2003, Jeffrey D. Argast. The authors make NO WARRANTY or representation, either express or implied, with respect to this software, its quality, accuracy, merchantability, or fitness for a particular purpose. This software is provided "AS IS", and you, its user, assume the entire risk as to its quality and accuracy. Permission is hereby granted to use, copy, modify, and distribute this software or portions thereof for any purpose, without fee, subject to these conditions: (1) If any part of the source code is distributed, then this statement must be included, with this copyright and no-warranty notice unaltered. (2) Permission for use of this software is granted only if the user accepts full responsibility for any undesirable consequences; the authors accept NO LIABILITY for damages of any kind. */ #import "PendingChangesController.h" #import "OutlineItem.h" #import "AppDefaults.h" #import "MessageDefs.h" #import "PerforceChanges.h" #import "DepotViewProtocol.h" #import "PendingViewSelection.h" @implementation PendingChangesController - (void) comeForward { fForward = YES; [fPendingChangesView reloadData]; } - (void) stepBack { fForward = NO; } - (void) dealloc { NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter]; [nCenter removeObserver:self]; [fChildren release]; [fCachedSelection release]; [super dealloc]; } - (void) buildChildren { NSMutableArray* childArray = [[NSMutableArray alloc] initWithCapacity:2]; NSMutableString* nsString = [NSMutableString stringWithString:@"Pending Changelists (client '"]; AppDefaults* appDefaults = [AppDefaults defaultAppDefaults]; [nsString appendString:[appDefaults getPerforceClient]]; [nsString appendString:@"')"]; [childArray addObject:[[[PerforceChanges alloc] initWithName:nsString clientChanges:YES] autorelease]]; [childArray addObject:[[[PerforceChanges alloc] initWithName:@"Pending Changelists (other clients)" clientChanges:NO] autorelease]]; fChildren = childArray; } - (void) awakeFromNib { NSTableColumn *tableColumn = nil; NSBrowserCell *browserCell = nil; tableColumn = [fPendingChangesView tableColumnWithIdentifier:@"1"]; browserCell = [[[NSBrowserCell alloc] init] autorelease]; [browserCell setEditable:NO]; [browserCell setLeaf:YES]; [tableColumn setDataCell:browserCell]; { NSNotificationCenter* nCenter = [NSNotificationCenter defaultCenter]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kDefaultsUserChanged object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kDefaultsClientChanged object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kDefaultsPortChanged object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kGlobalSyncComplete object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kDefaultsConfigurationsChanged object:nil]; [nCenter addObserver:self selector:@selector(handleDefaultsChanged:) name:kRefreshAllViews object:nil]; [nCenter addObserver:self selector:@selector(handleChangeChanged:) name:kChangeChanged object:nil]; [nCenter addObserver:self selector:@selector(handleChildChanged:) name:kPendingChangesChildChanged object:nil]; } [fPendingChangesView setVerticalMotionCanBeginDrag:NO]; } - (void) handleChangeChanged: (NSNotification*) notification { [self refreshClientData]; } - (void) handleChildChanged: (NSNotification*) notification { [fPendingChangesView reloadData]; } - (void) handleDefaultsChanged: (NSNotification*) notification { [self refreshData]; } /* ** The outline view datasource protocol */ // outlineView:child:ofItem: - (id) outlineView: (NSOutlineView *) outlineView child: (int) index ofItem: (id) item { if ( item ) { return [item getChildAtIndex: index]; } if ( !fForward ) { /* skip entirely. It works ok this way and has the advantage of not hitting the server when the panel is not forward if ( fChildren ) { return [fChildren objectAtIndex: index]; } */ return nil; } if ( !fChildren ) { [self buildChildren]; } return [fChildren objectAtIndex: index]; } // outlineView:isItemExpandable: - (BOOL) outlineView: (NSOutlineView *) outlineView isItemExpandable: (id) item { if ( item ) { return [item hasChildren]; } return YES; } // outlineView:numberOfChildrenOfItem: - (int) outlineView: (NSOutlineView*) outlineView numberOfChildrenOfItem: (id) item { if ( item ) { return [item getNumberOfChildren]; } if ( !fForward ) { /* skip entirely. It works ok this way and has the advantage of not hitting the server when the panel is not forward if ( fChildren ) { return [fChildren count]; } */ return 0; } if ( !fChildren ) { [self buildChildren]; } return [fChildren count]; } // outlineView:objectValueForTableColumn:byItem: - (id) outlineView: (NSOutlineView *) outlineView objectValueForTableColumn: (NSTableColumn *) tableColumn byItem: (id) item { return [item getCellText]; } /* ** outline view delegate methods */ - (void) outlineView: (NSOutlineView *) olv willDisplayCell: (NSCell *) cell forTableColumn: (NSTableColumn *) tableColumn item: (id) item { if ( [[tableColumn identifier] isEqualToString:@"1"] ) { [(NSBrowserCell*)cell setImage:[item getCellImage]]; } } // // getSelection // - (id) getSelection { if ( !fCachedSelection ) { // plus one because the num items selected may be 0 NSMutableArray* objects = [NSMutableArray arrayWithCapacity:([fPendingChangesView numberOfSelectedRows] + 1)]; NSEnumerator* items = [fPendingChangesView selectedRowEnumerator]; NSNumber* rowNum; while ( rowNum = [items nextObject] ) { int rowIdx = [rowNum intValue]; [objects addObject:[fPendingChangesView itemAtRow:rowIdx]]; } fCachedSelection = [[PendingViewSelection alloc] initWithItems:objects]; } return fCachedSelection; } - (void)outlineViewSelectionDidChange:(NSNotification *)notification { [fCachedSelection release]; fCachedSelection = nil; } // // // // The enablement - (BOOL) canEdit { id selection = [self getSelection]; return [selection canEdit]; } - (BOOL) canDescribe { id selection = [self getSelection]; return [selection canDescribe]; } - (BOOL) canDelete { id selection = [self getSelection]; return [selection canDelete]; } - (BOOL) canSubmit { id selection = [self getSelection]; return [selection canSubmit]; } - (BOOL) canRevert { id selection = [self getSelection]; return [selection canRevert]; } - (BOOL) canDiff { id selection = [self getSelection]; return [selection canDiff]; } // The actions - (void) doEdit { id selection = [self getSelection]; [selection doEdit]; } - (void) doDescribe { id selection = [self getSelection]; [selection doDescribe]; } - (void) doDelete { id selection = [self getSelection]; [selection doDelete]; } - (void) doSubmit { id selection = [self getSelection]; [selection doSubmit]; } - (void) doRevert { id selection = [self getSelection]; [selection doRevert]; } - (void) doDiff { id selection = [self getSelection]; [selection doDiff]; } // // // - (void) refreshData { if ( fChildren ) { [fChildren release]; fChildren = nil; [fPendingChangesView reloadData]; } } - (void) refreshClientData { if ( fChildren && ( [fChildren count] > 0 ) ) { PerforceChanges* clientChanges = [fChildren objectAtIndex:0]; [clientChanges refreshClientData]; [fPendingChangesView reloadData]; } } // // Drag and drop // // Source, i.e. starting a drag - (BOOL)outlineView:(NSOutlineView *)olv writeItems:(NSArray*)items toPasteboard:(NSPasteboard*)pb { if ( [items count] > 1 ) return NO; id item = [items objectAtIndex:0]; WriteStringToPasteboard ([item getPasteboardData], pb); return YES; } // Destination, i.e. target of drag - (NSDragOperation)outlineView:(NSOutlineView*)olv validateDrop:(id )info proposedItem:(id)item proposedChildIndex:(int)index { [olv setDropItem:nil dropChildIndex:NSOutlineViewDropOnItemIndex]; return NSDragOperationCopy; } // Destination, i.e. target of drag - (BOOL)outlineView:(NSOutlineView*)olv acceptDrop:(id )info item:(id)item childIndex:(int)index { NSPasteboard* pb = [info draggingPasteboard]; NSArray* value = nil; NSString* type; type = [pb availableTypeFromArray: [NSArray arrayWithObject:NSFilenamesPboardType]]; if ( type ) { value = [pb propertyListForType:NSFilenamesPboardType]; if ( value ) { } } return NO; } // // // @end