home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / FlatFileLoginPanel.m < prev    next >
Encoding:
Text File  |  1996-09-06  |  6.9 KB  |  257 lines

  1. /*
  2.         Copyright (c) 1996, NeXT Software, Inc.
  3.         All rights reserved.
  4.  
  5.         You may freely copy, distribute and reuse the code in this example.
  6.         NeXT disclaims any warranty of any kind, expressed or implied,
  7.         as to its fitness for any particular use.
  8. */
  9. #import "FlatFileAdaptor.h"
  10. #import "FlatFileLoginPanel.h"
  11.  
  12. #import <AppKit/AppKit.h>
  13.  
  14. @interface FlatFileLoginPanel (NSTableDataSource)
  15. - (int)numberOfRowsInTableView:(NSTableView *)tableView;
  16. - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)column row:(int)rowIndex;
  17. @end
  18.  
  19. #define Newline @"\n"
  20. #define Tab     @"\t"
  21.  
  22. @implementation FlatFileLoginPanel
  23.  
  24. extern char *FlatFile_VERS_NUM[];
  25.  
  26. - init
  27. {
  28.     [super init];
  29.     [NSBundle loadNibNamed:@"FlatFileLoginPanel" owner:self];
  30.  
  31.     [versionField setStringValue:[NSString stringWithFormat:[versionField stringValue], FlatFile_VERS_NUM]];
  32.     return self;
  33. }
  34.  
  35. - (void)dealloc
  36. {
  37.     [files release];
  38.     [super dealloc];
  39. }
  40.  
  41.  
  42. - (NSDictionary *)runPanelForAdaptor:(EOAdaptor *)adaptor validate:(BOOL)validate
  43. {
  44.     NSDictionary *result;
  45.     BOOL success;
  46.  
  47.     [self setConnectionDictionary:[adaptor connectionDictionary]];
  48.     do {
  49.         if ([NSApp runModalForWindow:[pathField window]] != NSRunContinuesResponse) {
  50.             result = nil;
  51.             [[pathField window] orderOut:nil];
  52.             break;
  53.         }
  54.         result = [self connectionDictionary];
  55.         [[pathField window] orderOut:nil];
  56.  
  57.         if (!validate) break;
  58.  
  59.         success = YES;
  60.         [adaptor setConnectionDictionary:result];
  61.         NS_DURING
  62.             [adaptor assertConnectionDictionaryIsValid];
  63.         NS_HANDLER
  64.             NSRunAlertPanel (NULL, @"%@", NULL, NULL, NULL, [localException reason]);
  65.             [self setConnectionDictionary:[adaptor connectionDictionary]];
  66.             success = NO;
  67.         NS_ENDHANDLER        
  68.     } while (!success);
  69.  
  70.     return result;
  71. }
  72.  
  73. - (void)cancel:sender
  74. {
  75.     [NSApp stopModalWithCode:NSRunAbortedResponse];
  76. }
  77.  
  78. - (void)login:sender
  79. {
  80.     [NSApp stopModalWithCode:NSRunContinuesResponse];
  81. }
  82.  
  83. - (NSString *)rootDirectoryFromPaths:(NSArray *)paths files:(NSArray **)filesP
  84. {
  85.     // given a list of paths return a directory and a list of files
  86.     int count = [paths count];
  87.     NSString *path;
  88.     BOOL isDirectory;
  89.     NSMutableArray *array;
  90.     
  91.     *filesP = nil;
  92.     if (!count) return nil;
  93.  
  94.     // if they selected a directory, return all files in the directory
  95.     path = [paths objectAtIndex:0];
  96.     if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory)
  97.     {
  98.         *filesP = [[NSFileManager defaultManager] directoryContentsAtPath:path];
  99.         return path;
  100.     }
  101.  
  102.     // if they selected files, return the common directory
  103.     *filesP = array = [NSMutableArray array];
  104.     while (count--) {
  105.         [array addObject:[[paths objectAtIndex:count] lastPathComponent]];
  106.     }
  107.     return [path stringByDeletingLastPathComponent];
  108. }
  109.  
  110. - (void)setFiles:sender
  111. {
  112.     NSOpenPanel *openPanel = [NSOpenPanel openPanel];
  113.     NSString *directory = [pathField stringValue];
  114.  
  115.     [openPanel setCanChooseDirectories:YES];
  116.     [openPanel setCanChooseFiles:YES];
  117.     [openPanel setAllowsMultipleSelection:YES];
  118.     if (directory) [openPanel setDirectory:directory];
  119.     if ([openPanel runModalForTypes:nil] != NSOKButton)
  120.         return;
  121.  
  122.     [files autorelease];
  123.     directory = [self rootDirectoryFromPaths:[openPanel filenames] files:&files];
  124.     [files retain];
  125.  
  126.     [pathField setStringValue:directory];
  127.     [fileTable reloadData];
  128. }
  129.  
  130. - (void)removeFile:sender
  131. {
  132.     NSEnumerator *selectedRows = [fileTable selectedRowEnumerator];
  133.     NSMutableArray *deadFiles = [NSMutableArray arrayWithCapacity:[files count]];
  134.     NSNumber *rowIndex;
  135.  
  136.     while (rowIndex = [selectedRows nextObject])
  137.         [deadFiles addObject:[files objectAtIndex:[rowIndex intValue]]];
  138.  
  139.     [files removeObjectsInArray:deadFiles];
  140.  
  141.     if (![files count]) {
  142.         [pathField setStringValue:@""];
  143.         [removeFileButton setEnabled:NO];
  144.     }
  145.  
  146.     [fileTable reloadData];
  147. }
  148.  
  149. - (void)setConnectionDictionary:(NSDictionary *)connectionInfo
  150. {
  151.     NSArray *fileList;
  152.     NSString *path, *separator, *useHeadings;
  153.  
  154.     path = [connectionInfo objectForKey:FlatFilePathKey];
  155.     if (!path)
  156.         path = @"";
  157.  
  158.     [pathField setStringValue:path];
  159.  
  160.     [files autorelease];
  161.     fileList = [connectionInfo objectForKey:FlatFileFilesKey];
  162.     if (fileList && [fileList count])
  163.         files = [[NSMutableArray alloc] initWithArray:fileList];
  164.     else
  165.         files = nil;
  166.  
  167.     [fileTable reloadData];
  168.  
  169.     separator = [connectionInfo objectForKey:FlatFileRowSeparatorKey];
  170.     if (!separator)
  171.         separator = @"";
  172.  
  173.     if ([separator isEqualToString:Newline])
  174.         [rowSeparatorMatrix selectCellWithTag:0];
  175.     else {
  176.         [rowSeparatorField setStringValue:separator];
  177.         [rowSeparatorMatrix selectCellWithTag:1];
  178.     }
  179.  
  180.     separator = [connectionInfo objectForKey:FlatFileColumnSeparatorKey];
  181.     if (!separator)
  182.         separator = @"";
  183.  
  184.     if ([separator isEqualToString:Tab])
  185.         [columnSeparatorMatrix selectCellWithTag:0];
  186.     else {
  187.         [columnSeparatorField setStringValue:separator];
  188.         [columnSeparatorMatrix selectCellWithTag:1];
  189.     }
  190.  
  191.     useHeadings = [connectionInfo objectForKey:FlatFileUseHeadersKey];
  192.     [columnHeadingButton setState:[useHeadings isEqualToString:@"Y"]];    
  193. }
  194.  
  195. - (NSDictionary *)connectionDictionary
  196. {
  197.     NSMutableDictionary *info;
  198.     NSString *rowSeparator, *columnSeparator, *useHeadings, *path;
  199.     
  200.     info = [NSMutableDictionary dictionaryWithCapacity:5];
  201.  
  202.     path = [pathField stringValue];
  203.     if (!path)
  204.         path = @"";
  205.  
  206.     [info setObject:path forKey:FlatFilePathKey];
  207.  
  208.     if (files && [files count])
  209.         [info setObject:files forKey:FlatFileFilesKey];
  210.  
  211.     if ([[rowSeparatorMatrix selectedCell] tag] == 0)
  212.         rowSeparator = Newline;
  213.     else {
  214.         rowSeparator = [rowSeparatorField stringValue];
  215.         if (!rowSeparator)
  216.             rowSeparator = @"";
  217.     }
  218.  
  219.     [info setObject:rowSeparator forKey:FlatFileRowSeparatorKey];
  220.  
  221.     if ([[columnSeparatorMatrix selectedCell] tag] == 0)
  222.         columnSeparator = Tab;
  223.     else {
  224.         columnSeparator = [columnSeparatorField stringValue];
  225.         if (!columnSeparator)
  226.             columnSeparator = @"";
  227.     }
  228.  
  229.     [info setObject:columnSeparator forKey:FlatFileColumnSeparatorKey];
  230.  
  231.     useHeadings = [columnHeadingButton state] ? @"Y" : @"N";
  232.     [info setObject:useHeadings forKey:FlatFileUseHeadersKey];
  233.     
  234.     return info;
  235. }
  236.  
  237. - (void)didChangeSelection:(NSTableView *)sender
  238. {
  239.     [removeFileButton setEnabled:([sender numberOfSelectedRows] != 0)];
  240. }
  241.  
  242. - (int)numberOfRowsInTableView:(NSTableView *)tableView
  243. {
  244.     return [files count];
  245. }
  246.  
  247. - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)column row:(int)rowIndex
  248. {
  249.     if ((rowIndex >= 0) && (rowIndex < [files count]))
  250.         return [files objectAtIndex:rowIndex];
  251.  
  252.     return nil;
  253. }
  254.  
  255. @end
  256.  
  257.