/* 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 "PendingViewSelection.h" #import "AppUtils.h" #import "DepotViewProtocol.h" #import "PerforceChangeFile.h" #import "PerforceChangeList.h" #import "PerforceActionEdit.h" static NSString* kPendingChangesDragType = @"PendingChangesDragType"; @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(handleChangeChanged:) name:kActionSyncCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleChangeChanged:) name:kActionEditCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleChangeChanged:) name:kActionDeleteCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleChangeChanged:) name:kActionRevertCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleChangeChanged:) name:kActionAddCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleChangeChanged:) name:kActionSubmitCommandComplete object:nil]; [nCenter addObserver:self selector:@selector(handleChildChanged:) name:kPendingChangesChildChanged object:nil]; } [fPendingChangesView setTarget:self]; [fPendingChangesView setDoubleAction:@selector(doubleAction:)]; [fPendingChangesView setVerticalMotionCanBeginDrag:NO]; [fPendingChangesView registerForDraggedTypes:[NSArray arrayWithObject:kPendingChangesDragType]]; [fPendingChangesView registerForDraggedTypes:[NSArray arrayWithObject:NSStringPboardType]]; } - (void) doubleAction: (id) sender { NSWindow* window = [fPendingChangesView window]; NSPoint clickPoint = [NSEvent mouseLocation]; clickPoint = [window convertScreenToBase:clickPoint]; clickPoint = [fPendingChangesView convertPoint:clickPoint fromView:nil]; int rowIndex = [fPendingChangesView rowAtPoint:clickPoint]; if ( rowIndex >= 0 ) { id item = [fPendingChangesView itemAtRow:rowIndex]; if ( [item canViewInEditor] ) { [item doViewInEditor]; } } } - (NSArray*) getSelectedItems { // 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]]; } return objects; } - (void) setSelectedItems:(NSArray*)objects { // now select the items that were selected before the expansion [fPendingChangesView deselectAll:self]; int numItems = [objects count]; int n; for ( n = 0; n < numItems; n++ ) { int rowIndex = [fPendingChangesView rowForItem:[objects objectAtIndex:n]]; if ( rowIndex >= 0 ) { [fPendingChangesView selectRow:rowIndex byExtendingSelection:YES]; } } } - (void) handleChangeChanged: (NSNotification*) notification { // This sucks, but at the moment all the children are // destroyed and recreated leaving the existing selection // referencing rows to invalid objects [fPendingChangesView deselectAll:self]; [self refreshClientData]; } - (void) handleChildChanged: (NSNotification*) notification { // This ultimately doesn't work because of the stale // data. It works in the depot view though, so we // should be able to get it to work here. //NSArray* selectedItems = [self getSelectedItems]; [fPendingChangesView reloadData]; //[self setSelectedItems:selectedItems]; } - (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]]; } } - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item { // WEIRD: Must implement this delegate function and return NO in order for // double click to work return NO; } // // 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) canResolve { id selection = [self getSelection]; return [selection canResolve]; } - (BOOL) canRevert { id selection = [self getSelection]; return [selection canRevert]; } - (BOOL) canDiff { id selection = [self getSelection]; return [selection canDiff]; } - (BOOL) canRevealInFinder { id selection = [self getSelection]; return [selection canRevealInFinder]; } - (BOOL) canViewInEditor { id selection = [self getSelection]; return [selection canViewInEditor]; } // 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) doResolve { id selection = [self getSelection]; [selection doResolve]; } - (void) doRevert { id selection = [self getSelection]; [selection doRevert]; } - (void) doRevertUnchanged { id selection = [self getSelection]; [selection doRevertUnchanged]; } - (void) doDiff { id selection = [self getSelection]; [selection doDiff]; } - (void) doRevealInFinder { id selection = [self getSelection]; [selection doRevealInFinder]; } - (void) doViewInEditor { id selection = [self getSelection]; [selection doViewInEditor]; } - (void) appendRevertPath: (NSMutableArray*) revertPathList { // the controller does nothing. TODO: This means the protocol definition is too broad // and needs to be broken into more granular protocols. } // // // - (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 { // only drag items that are client change files BOOL canDrag = YES; int numItems = [items count]; int n; for ( n = 0; (n < numItems) && (canDrag == YES); n++ ) { id anItem = [items objectAtIndex:n]; if ( [anItem isKindOfClass:[PerforceChangeFile class]] ) { PerforceChangeList* parentList = [anItem parentChangelist]; if ( ![parentList isClientChangelist] ) { canDrag = NO; } } else // we can't drag anything other than files { canDrag = NO; } } if ( canDrag ) { // Put our own custom type into the pasteboard, which is the pointer to the array of items unsigned int arrayPtrVal = (unsigned int)items; NSData* arrayData = [NSData dataWithBytes:&arrayPtrVal length:sizeof(unsigned int)]; [pb declareTypes:[NSArray arrayWithObject:kPendingChangesDragType] owner:nil]; [pb setData:arrayData forType:kPendingChangesDragType]; } return canDrag; } // Destination, i.e. target of drag - (NSDragOperation)outlineView:(NSOutlineView*)olv validateDrop:(id )info proposedItem:(id)item proposedChildIndex:(int)index { // drop on change lists only, not children and only client change lists if ( [item isKindOfClass:[PerforceChangeFile class]] ) { PerforceChangeList* parentList = [item parentChangelist]; if ( [parentList isClientChangelist] ) { [olv setDropItem:parentList dropChildIndex:NSOutlineViewDropOnItemIndex]; return NSDragOperationCopy; } } else if ( [item isKindOfClass:[PerforceChangeList class]] ) { if ( [item isClientChangelist] ) { [olv setDropItem:item dropChildIndex:NSOutlineViewDropOnItemIndex]; return NSDragOperationCopy; } } [olv setDropItem:nil dropChildIndex:NSOutlineViewDropOnItemIndex]; return NSDragOperationNone; } // Destination, i.e. target of drag - (BOOL)outlineView:(NSOutlineView*)olv acceptDrop:(id )info item:(id)item childIndex:(int)index { NSPasteboard* pb = [info draggingPasteboard]; NSData* arrayData = nil; NSString* type; type = [pb availableTypeFromArray: [NSArray arrayWithObject:kPendingChangesDragType]]; if ( type ) { arrayData = [pb dataForType:kPendingChangesDragType]; if ( arrayData ) { NSArray* draggingArray = nil; [arrayData getBytes:&draggingArray]; //NSLog([draggingArray description]); // Get the selection NSArray* selectedItems = [self getSelectedItems]; [item reopen:draggingArray]; [fPendingChangesView reloadData]; [self setSelectedItems:selectedItems]; return YES; } } NSString* value = ReadStringFromPasteboard (pb); if ( value ) { // read up to the first break NSScanner* scanner = [NSScanner scannerWithString:value]; NSString* resultString = nil; NSMutableArray* filesToEdit = [[[NSMutableArray alloc] init] autorelease]; while ( [scanner scanUpToString:@"\n" intoString:&resultString] == YES ) { [filesToEdit addObject:resultString]; //[scanner scanUpToString:@"\n" intoString:nil]; } if ( [filesToEdit count] > 0) { [PerforceActionEdit defaultRun:filesToEdit changeNumber:[item changeNumber]]; [fPendingChangesView reloadData]; } return YES; } return NO; } // // // @end