1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
|
/*
Project: UL
Copyright (C) 2005 Michael Johnston & Jordi Villa-Freixa
Author: Michael Johnston
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#include <AppKit/AppKit.h>
#include "ULSystemViewController.h"
@interface ULSystemViewController (PDBDownloadAdditions)
- (void) downloadPDB: (NSString*) pdbID;
@end
@implementation ULSystemViewController
/************************
Initialisation Methods
************************/
- (id) init
{
if((self = [super init]))
{
if([NSBundle loadNibNamed: @"System" owner: self] == NO)
{
NSWarnLog(@"Problem loading system controller interface");
return nil;
}
systemController = [ULSystemController new];
metadataController = [ULPropertiesPanel propertiesPanel];
allowedFileTypes = [[NSArray arrayWithObjects: @"PDB", @"pdb", nil] retain];
isBuilding = NO;
//register for some notifications from about the build process state
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleNotification:)
name: @"ULSystemBuildSectionCompleted"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleNotification:)
name: @"ULSystemBuildWillStart"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleNotification:)
name: @"ULSystemBuildDidAbortNotification"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleNotification:)
name: @"ULSystemBuilderWillBeginInitialisationNotification"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleNotification:)
name: @"ULSystemBuilderWillBeginInitialisationStepNotification"
object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleNotification:)
name: @"ULSystemBuilderCompletedInitialisationStepNotification"
object: nil];
[tabView setDelegate: self];
preprocessTabViewItem = [[tabView tabViewItemAtIndex: 0] retain];
[systemWindow center];
[systemWindow setDelegate: self];
}
return self;
}
- (void) dealloc
{
[outlineDelegate release];
[currentOptions release];
[systemController release];
[allowedFileTypes release];
[super dealloc];
}
- (void) _insertString: (NSString*) string fontType: (NSString*) fontType
{
NSRange fontRange, range;
NSFont* font;
NSFontManager *fontManager = [NSFontManager sharedFontManager];
font = [NSFont userFontOfSize: 12];
if([fontType isEqual: @"Bold"])
font = [fontManager convertFont: font toHaveTrait: NSBoldFontMask];
range = NSMakeRange([[logView textStorage] length], 0);
[[logView textStorage] replaceCharactersInRange: range withString: string];
fontRange = NSMakeRange(range.location, [string length]);
[[logView textStorage] beginEditing];
[[logView textStorage] addAttribute: NSFontAttributeName value: font range: fontRange];
[[logView textStorage] endEditing];
[logView scrollRangeToVisible: NSMakeRange([[logView textStorage] length], 0)];
}
- (void) handleNotification: (NSNotification*) aNotification
{
NSMutableString* buildInfo;
[progressView display];
[progressIndicator display];
[progressBar display];
if([[aNotification name] isEqual: @"ULSystemBuildWillStart"])
{
[progressBar setDoubleValue: 0.0];
[progressIndicator setStringValue: @"Beginning Build"];
NSDebugLLog(@"ULSystemViewController", @"Received %@", [aNotification name]);
}
else if([[aNotification name] isEqual: @"ULSystemBuildSectionCompleted"])
{
[progressBar incrementBy: 25];
[progressIndicator setStringValue: [aNotification object]];
NSDebugLLog(@"ULSystemViewController", @"Received %@", [aNotification name]);
[self _insertString: [aNotification object] fontType: @"Bold"];
buildInfo = [NSMutableString stringWithCapacity: 1];
[buildInfo appendString: @"\n"];
[buildInfo appendString: [[aNotification userInfo]
objectForKey: @"ULBuildSectionUserInfoKey"]];
[buildInfo appendString: @"\n"];
[self _insertString: buildInfo fontType: @"Normal"];
}
else if([[aNotification name] isEqual: @"ULSystemBuildDidAbortNotification"])
{
[progressIndicator setStringValue:
[NSString stringWithFormat: @"Build Aborted! - At position %@", [aNotification object]]];
[self _insertString: [aNotification object] fontType: @"Bold"];
buildInfo = [NSMutableString stringWithCapacity: 1];
[buildInfo appendString: @"\n"];
[buildInfo appendString: [[aNotification userInfo]
objectForKey: @"ULBuildSectionUserInfoKey"]];
[buildInfo appendString: @"\n"];
[self _insertString: buildInfo fontType: @"Normal"];
}
else if([[aNotification name] isEqual: @"ULSystemBuilderWillBeginInitialisationNotification"])
{
[progressBar setDoubleValue: 0.0];
[progressIndicator setStringValue: [aNotification object]];
}
else if([[aNotification name] isEqual: @"ULSystemBuilderCompletedInitialisationStepNotification"])
{
[progressBar incrementBy: 50.0];
[progressIndicator setStringValue: [aNotification object]];
}
else if([[aNotification name] isEqual: @"ULSystemBuilderWillBeginInitialisationStepNotification"])
{
[progressIndicator setStringValue: [aNotification object]];
}
[progressIndicator display];
[progressBar display];
[progressView display];
}
- (void) _setDelegateForOptions: (id) options
{
outlineDelegate = [(ULOutlineViewDelegate*)[ULOutlineViewDelegate alloc]
initWithOptions: options];
[optionsView setDataSource: outlineDelegate];
[optionsView setDelegate: outlineDelegate];
[optionsView reloadData];
}
/*
* Updating the force field
*/
- (void) forceFieldDidChange: (id) sender
{
[systemController setForceField:
[forcefieldList titleOfSelectedItem]];
}
/*
* Updating the plugin options
*/
- (void) pluginDidChange: (id) sender
{
//Load the new plugin
[[systemController systemBuilder]
loadPreprocessPlugin: [pluginList titleOfSelectedItem]];
//Update options
[currentOptions release];
currentOptions = [systemController preprocessOptions];
[currentOptions retain];
[self _setDelegateForOptions: currentOptions];
}
/*
* Opening and closing the create system window
*/
//Called on loading a new molecule or opening a create system window.
//Updates the plugin list and loads the first plugin (if available).
- (void) _updatePluginList
{
NSArray* plugins;
plugins = [[systemController systemBuilder] availablePreprocessPlugins];
[pluginList removeAllItems];
if([plugins count] == 0)
{
[pluginList addItemWithTitle: @"None"];
[pluginList selectItemWithTitle: @"None"];
}
else
{
[pluginList addItemsWithTitles: plugins];
[pluginList selectItemAtIndex: 0];
//load the selected plugin
[[systemController systemBuilder]
loadPreprocessPlugin: [pluginList titleOfSelectedItem]];
}
}
- (void) open: (id) sender
{
NSString* defaultForceField;
//if the window is already open do nothing
//except bring it to the front if neccessary
if([systemWindow isVisible])
{
if(![systemWindow isKeyWindow])
[systemWindow makeKeyAndOrderFront: self];
return;
}
[[logView textStorage] deleteCharactersInRange:
NSMakeRange(0, [[logView textStorage] length])];
//Clear everything from previous build
currentOptions = nil;
[self _setDelegateForOptions: currentOptions];
[systemController removeMolecule];
[self _updatePluginList];
[forcefieldLabel setStringValue: @"Force Field"];
[forcefieldList removeAllItems];
[forcefieldList addItemsWithTitles:
[[systemController systemBuilder]
availableForceFields]];
defaultForceField = [[NSUserDefaults standardUserDefaults]
objectForKey: @"DefaultForceField"];
[forcefieldList selectItemWithTitle: defaultForceField];
[systemController setForceField: defaultForceField];
[moleculePathField setStringValue: @""];
[systemWindow setTitle: @"Create System"];
[progressIndicator setStringValue: @""];
[progressBar setDoubleValue: 0.0];
[buttonOne setTitle: @"Create"];
[systemWindow makeKeyAndOrderFront: self];
[systemWindow setAutodisplay: YES];
[buttonTwo setEnabled: NO];
[buttonThree setEnabled: NO];
if([tabView numberOfTabViewItems] == 1)
[tabView insertTabViewItem: preprocessTabViewItem
atIndex: 0];
//removes some display problems with the inserted tab
[tabView selectFirstTabViewItem: self];
[tabView selectLastTabViewItem: self];
[[tabView selectedTabViewItem] setLabel: @"Build"];
}
- (void) close: (id) sender
{
[optionsView setDelegate: nil];
[optionsView setDataSource: nil];
[optionsView reloadData];
[outlineDelegate release];
outlineDelegate = nil;
[currentOptions release];
currentOptions = nil;
[moleculePathField setStringValue: @""];
[systemController removeMolecule];
[[logView textStorage] deleteCharactersInRange:
NSMakeRange(0, [[logView textStorage] length])];
}
- (void) doButtonAction: (id) sender
{
[self createSystem: sender];
}
- (void) doButtonThreeAction: (id) sender
{
[self continueBuild];
}
- (void) doButtonTwoAction: (id) sender
{
[self cancelBuild];
}
/************************
Creating a System
************************/
/*
Called when a user loads a pdb.
Writes out data on the pdb to the text-view of the create sytem window
*/
- (void) _outputInfoForPDBFile: (NSString*) filename
{
MTStructure* structure;
ULAnalysisManager* analysisManager;
NSString* infoString;
//Ignore REMARK since some programs add data here that breaks the pdb format
//Ignore COMPND and SOURCE - For some reason the MOL_ID entry in these sections
//causes MolTalk to create a phantom extra chain (only for the first model however).
structure = [MTStructureFactory newStructureFromPDBFile: filename
options: 256 + 16 + 32];
analysisManager = [ULAnalysisManager managerWithDefaultLocations];
[analysisManager addInputObject: structure];
if([[analysisManager pluginsForCurrentInputs] containsObject: @"PDBInfo"])
{
[analysisManager applyPlugin: @"PDBInfo"
withOptions: nil
error: NULL];
infoString = [analysisManager outputString];
if(infoString != nil)
{
[self _insertString: @"PDB Summary\n\n" fontType: @"Bold"];
[self _insertString: infoString fontType: @"Normal"];
}
}
}
- (void) getStructureFile: (id) sender
{
NSString* fieldValue;
fieldValue = [moleculePathField stringValue];
if([fieldValue length] == 4)
{
[self downloadPDB: fieldValue];
}
else
{
[moleculePathField setStringValue: @""];
[self showFileBrowser: self];
}
}
- (void) loadStructureFile: (NSString*) fileName
{
NSRange endRange;
NSMutableString* failString;
[[logView textStorage] deleteCharactersInRange:
NSMakeRange(0, [[logView textStorage] length])];
endRange = NSMakeRange([[logView string] length], 0);
failString = [NSMutableString string];
NS_DURING
{
[systemController setBuildMolecule: fileName];
[self _updatePluginList];
//Output some information on the pdb
[self _outputInfoForPDBFile: fileName];
//Show the correct options depending on the selected tab.
[currentOptions release];
if([[[tabView selectedTabViewItem] label] isEqual: @"Build"])
currentOptions = [systemController buildOptions];
else
currentOptions = [systemController preprocessOptions];
[currentOptions retain];
[self _setDelegateForOptions: currentOptions];
}
NS_HANDLER
{
failString = [NSMutableString stringWithCapacity: 1];
[failString appendString: @"Failed to load molecule ...\n"];
[failString appendString: [localException reason]];
[failString appendString: @"\n"];
if([[localException name] isEqual: @"ULBuildException"])
{
[failString appendString: [[localException userInfo]
objectForKey:
@"ULBuildRecoverySuggestionKey"]];
[failString appendString: @"\n"];
}
[logView replaceCharactersInRange: endRange withString: failString];
NSRunAlertPanel(@"Load Molecule Failed",
[localException reason],
@"Dismiss",
nil,
nil);
}
NS_ENDHANDLER
}
- (void) showFileBrowser: (id) sender
{
id fileBrowser;
int result;
//if there is a current build prevent loading
//another until it ends
if(isBuilding)
{
NSRunAlertPanel(@"Alert",
@"You must continue or cancel the current build!",
@"Dismiss",
nil,
nil);
return;
}
[progressBar setDoubleValue: 0.0];
fileBrowser = [NSOpenPanel openPanel];
[fileBrowser setTitle: @"Load PDB"];
[fileBrowser setDirectory: [[NSUserDefaults standardUserDefaults] stringForKey:@"PDBDirectory"]];
[fileBrowser setAllowsMultipleSelection: NO];
result = [fileBrowser runModalForTypes: allowedFileTypes];
if(result == NSOKButton)
{
if([allowedFileTypes containsObject: [[fileBrowser filename] pathExtension]])
[moleculePathField setStringValue: [fileBrowser filename]];
else
NSRunAlertPanel(@"Error",
[NSString stringWithFormat: @"Unknown file type - %@\nSupported types %@",
[[fileBrowser filename] pathExtension], allowedFileTypes],
@"Dismiss",
nil,
nil);
[self loadStructureFile: [fileBrowser filename]];
}
}
- (void) _logBuildError: (NSError*) buildError
{
NSMutableString* failString;
NSString* recoveryString;
[self _insertString: @"***** Detected Build Errors *****\n\n" fontType: @"Bold"];
failString = [NSMutableString string];
[failString appendString: @"\n"];
[failString appendString: [[buildError userInfo]
objectForKey: @"ULBuildErrorDetailedDesciptionKey"]];
[failString appendString: @"\n"];
[self _insertString: failString fontType: @"Normal"];
recoveryString = [[buildError userInfo]
objectForKey: @"ULBuildErrorRecoverySuggestionKey"];
if(recoveryString != nil)
{
[self _insertString: @"***** Recovery Suggestion *****\n\n"
fontType: @"Bold"];
[failString deleteCharactersInRange: NSMakeRange(0, [failString length])];
[failString appendFormat: @"%@\n", recoveryString];
[self _insertString: failString fontType: @"Normal"];
}
}
- (void) _logULBuildException: (NSException*) localException
{
NSMutableString* failString;
[self _insertString: @"***** Build Failed *****\n\n" fontType: @"Bold"];
failString = [NSMutableString stringWithCapacity: 1];
[failString appendString: [localException reason]];
[failString appendString: @"\n"];
if([[localException userInfo] objectForKey: @"ULBuildExceptionDetailedDescriptionKey"] != nil)
{
[failString appendString: [[localException userInfo]
objectForKey: @"ULBuildExceptionDetailedDescriptionKey"]];
[failString appendString: @"\n"];
}
if([[localException userInfo] objectForKey: @"ULBuildRecoverySuggestionKey"] != nil)
{
[failString appendString: [[localException userInfo]
objectForKey: @"ULBuildRecoverySuggestionKey"]];
[failString appendString: @"\n"];
}
[self _insertString: failString fontType: @"Normal"];
NSRunAlertPanel(@"Build Failed", [localException reason], @"Dismiss", nil, nil);
}
- (void) _logUnexpectedBuildException: (NSException*) localException
{
NSMutableString* failString;
failString = [NSMutableString stringWithCapacity: 1];
[self _insertString: @"***** Unexpected Build Failure *****\n\n" fontType: @"Bold"];
[failString appendString: [localException reason]];
[failString appendString: @"\n"];
if([localException userInfo] != nil)
[failString appendString: [[localException userInfo] description]];
[failString appendString: @"\n"];
[failString appendString:
@"Please file a support request with the pdb used attached and this build log "];
[failString appendString: @"at the Adun Project Page:"];
[failString appendString: @"\nhttp://gna.org/projects/adun\n"];
[self _insertString: failString fontType: @"Normal"];
NSRunAlertPanel(@"Unexpected build failure",
@"See log for details",
@"Dismiss",
nil,
nil);
}
- (void) createSystem: (id)sender
{
BOOL result;
NSMutableString* failString;
NSError* buildError;
/*
* isBuilding is set to yes until the build
* A) Completes
* B) raises an exception
* C) is canceled by the user
*/
isBuilding = YES;
NS_DURING
{
[[logView textStorage] deleteCharactersInRange:
NSMakeRange(0, [[logView textStorage] length])];
[self _insertString: @"***** Beginning Build *****\n\n" fontType: @"Bold"];
if(currentOptions == nil)
[NSException raise: @"ULBuildException"
format: @"No molecule has been loaded"];
buildError = nil;
if(![systemController buildSystemWithOptions: currentOptions error: &buildError])
{
failString = [NSMutableString string];
[failString appendString:[[buildError userInfo]
objectForKey: NSLocalizedDescriptionKey]];
[failString appendString: @"\n\n ** Check the log window for more details. **\n\n"];
[failString appendString: @"Choose 'Continue' to ignore the errors "];
[failString appendString: @" or 'Cancel' to abort."];
[self _logBuildError: buildError];
NSRunAlertPanel(@"Build Error",
failString,
@"Dismiss",
nil,
nil);
[buttonOne setEnabled: NO];
[buttonTwo setEnabled: YES];
[buttonThree setEnabled: YES];
}
else
{
[progressIndicator setStringValue: @"Complete"];
[metadataController displayMetadataForModelObject: [systemController valueForKey:@"system"]
allowEditing: YES
runModal: YES];
result = [metadataController result];
[self _insertString: @"***** Build Complete *****\n" fontType: @"Bold"];
if(result)
[systemController saveSystem];
[systemWindow orderFront: self];
isBuilding = NO;
}
}
NS_HANDLER
{
isBuilding = NO;
if([[localException name] isEqual: @"ULBuildException"])
[self _logULBuildException: localException];
else
[self _logUnexpectedBuildException: localException];
}
NS_ENDHANDLER
}
- (void) continueBuild
{
BOOL result;
NSError* buildError;
NSMutableString* failString;
NS_DURING
{
[self _insertString: @"Resuming build - ignoring previous errors.\n\n"
fontType: @"Bold"];
buildError = nil;
if(![systemController resumeBuild: currentOptions error: &buildError])
{
[self _logBuildError: buildError];
failString = [NSMutableString stringWithCapacity: 1];
[failString appendString:[[buildError userInfo]
objectForKey: NSLocalizedDescriptionKey]];
[failString appendString: @"\nCheck the log window for more details.\n\n"];
[failString appendString: @"Choose 'Continue' to ignore the errors "];
[failString appendString: @" or 'Cancel' to abort."];
NSRunAlertPanel(@"Build Error",
failString,
@"Dismiss",
nil,
nil);
}
else
{
[progressIndicator setStringValue: @"Complete"];
[metadataController displayMetadataForModelObject: [systemController valueForKey:@"system"]
allowEditing: YES
runModal: YES];
result = [metadataController result];
[self _insertString: @"***** Build Complete *****\n" fontType: @"Bold"];
if(result)
[systemController saveSystem];
[systemWindow orderFront: self];
[buttonOne setEnabled: YES];
[buttonTwo setEnabled: NO];
[buttonThree setEnabled: NO];
isBuilding = NO;
}
}
NS_HANDLER
{
if([[localException name] isEqual: @"ULBuildException"])
[self _logULBuildException: localException];
else
[self _logUnexpectedBuildException: localException];
[buttonOne setEnabled: YES];
[buttonTwo setEnabled: NO];
[buttonThree setEnabled: NO];
isBuilding = NO;
}
NS_ENDHANDLER
}
- (void) cancelBuild
{
NSMutableString* failString;
[self _insertString: @"***** Build Canceled *****\n\n" fontType: @"Bold"];
failString = [NSMutableString stringWithCapacity: 1];
[failString appendString: @"User canceled build due to above errors."];
[failString appendString: @"\n"];
[self _insertString: failString fontType: @"Normal"];
[progressIndicator setStringValue:
[NSString stringWithFormat: @"Build Canceled"]];
[buttonOne setEnabled: YES];
[buttonTwo setEnabled: NO];
[buttonThree setEnabled: NO];
[systemController cancelBuild];
isBuilding = NO;
}
/*
* Applying the plugin
*/
- (void) applyPlugin: (id) sender
{
[[logView textStorage] deleteCharactersInRange:
NSMakeRange(0, [[logView textStorage] length])];
[self _insertString: [NSString stringWithFormat:
@"***** Applying Preprocess Plugin - %@ *****\n\n",
[pluginList titleOfSelectedItem]]
fontType: @"Bold"];
[[systemController systemBuilder]
applyPreprocessPlugin: currentOptions];
[self _insertString: [[systemController systemBuilder]
preprocessOutputString]
fontType: nil];
[self _insertString: @"\n***** Complete *****\n"
fontType: @"Bold"];
}
/*
* Save the current structure
*/
- (void) saveStructure: (id) sender
{
int result;
NSString* currentPath, *filename;
NSSavePanel* savePanel;
currentPath = [[systemController systemBuilder]
buildMolecule];
if(currentPath != nil)
{
savePanel = [NSSavePanel savePanel];
[savePanel setTitle: @"Save Structure"];
[savePanel setDirectory:
[[NSUserDefaults standardUserDefaults]
objectForKey: @"PDBDirectory"]];
result = [savePanel runModal];
filename = [savePanel filename];
if(result == NSOKButton)
[[systemController systemBuilder]
writeStructureToFile: filename];
}
else
NSRunAlertPanel(@"Alert",
@"No molecule has been loaded.",
@"Dismiss",
nil,
nil);
}
/******************
Tabview Delegate methods
********************/
- (void)tabView: (NSTabView *)tabView didSelectTabViewItem: (NSTabViewItem *)tabViewItem
{
id holder;
holder = currentOptions;
if([[tabViewItem label] isEqual: @"Preprocess"])
currentOptions = [systemController preprocessOptions];
else
currentOptions = [systemController buildOptions];
[currentOptions retain];
[holder release];
[self _setDelegateForOptions: currentOptions];
}
- (BOOL) tabView: (NSTabView *) tabView shouldSelectTabViewItem: (NSTabViewItem*) tabViewItem
{
if(isBuilding)
{
NSRunAlertPanel(@"Alert",
@"You must continue or cancel the current build!",
@"Dismiss",
nil,
nil);
return NO;
}
return YES;
}
/******************
Window Delegate Methods
*******************/
- (void) windowWillClose: (NSNotification*) aNotification
{
[self close: self];
[systemWindow orderOut:self];
}
- (BOOL) windowShouldClose: (id) sender
{
if(isBuilding)
{
NSRunAlertPanel(@"Alert",
@"You must continue or cancel the current build!",
@"Dismiss",
nil,
nil);
return NO;
}
return YES;
}
@end
@implementation ULSystemViewController (PDBDownloadAdditions)
- (void) downloadPDB: (NSString*) pdbID
{
NSError* error;
//Create a download connection
urlDownload = [ULURLDownload downloadForPDB: pdbID];
[urlDownload retain];
//Register for download end notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(downloadFinished)
name: ULURLDownloadDidEndNotification
object: urlDownload];
//Create and run a progress panel
progressPanel = [[ULProgressPanel alloc] initWithTitle: @"Downloading"
message: [NSString stringWithFormat: @"Downloading %@.pdb", pdbID]
progressInfo: @"Waiting to connect ..."];
[progressPanel updateStatusOnNotification: ULURLDownloadStatusNotification
fromObject: urlDownload];
[progressPanel runProgressPanel: NO];
//Start the download
if(![urlDownload beginDownloadToFile: nil overwrite: YES])
{
//Handle error
[progressPanel endPanel];
[progressPanel release];
error = [urlDownload downloadError];
ULRunErrorPanel(error);
[urlDownload release];
urlDownload = nil;
[moleculePathField setStringValue: @""];
}
else
{
//Lock the text field and load button
[loadButton setEnabled: NO];
[moleculePathField setSelectable: NO];
}
}
- (void) downloadFinished
{
BOOL deleteFile;
NSString* filename;
NSError* error;
//Let the panel linger briefly
[progressPanel orderFront];
sleep(1.0);
[progressPanel endPanel];
[progressPanel release];
//Check the result
if((error = [urlDownload downloadError]) == nil)
{
filename = [urlDownload filename];
//Check it ends in pdb - otherwise no pdb was downloaded
if(![[filename pathExtension] isEqual: @"pdb"])
{
NSLog(@"Error downloading pdb");
NSLog(@"URL %@", [urlDownload URL]);
NSLog(@"See the file %@ for what was retrieved", filename);
error = AdCreateError(ULFrameworkErrorDomain,
10,
@"Download Error",
@"Retrieved data does not correspond to a pdb structure",
@"Check UL.log for more information");
ULRunErrorPanel(error);
[moleculePathField setStringValue: @""];
}
else
{
//Load the file then delete it (if asked)
[self loadStructureFile: filename];
deleteFile = [[NSUserDefaults standardUserDefaults]
boolForKey: @"DeleteDownloadedPDBs"];
if(deleteFile)
[[NSFileManager defaultManager]
removeFileAtPath: filename handler: NULL];
//Trick so hitting load will open the file browser
//instead of restarting the same download.
[moleculePathField setStringValue: [filename lastPathComponent]];
}
}
else
{
ULRunErrorPanel(error);
[moleculePathField setStringValue: @""];
}
//Clean up
[[NSNotificationCenter defaultCenter]
removeObserver: self
name: ULURLDownloadDidEndNotification
object: urlDownload];
[urlDownload release];
urlDownload = nil;
[loadButton setEnabled: YES];
[moleculePathField setSelectable: YES];
[moleculePathField setEditable: YES];
}
@end
|