home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / Rhapsody / Graphics / ImageBrowser-1.0 / DAImageBrowser.m < prev    next >
Encoding:
Text File  |  1998-01-11  |  7.4 KB  |  222 lines

  1. #import "DAImageBrowser.h"
  2.  
  3. @implementation DAImageBrowser
  4.  
  5. + (DAImageBrowser *)openNewDirectory
  6. {
  7.     DAImageBrowser    *browser = nil;
  8.  
  9.     DURING
  10.         int        result;
  11.         NSString    *path = NSHomeDirectory();
  12.         NSOpenPanel    *oPanel = [NSOpenPanel openPanel];
  13.         NSArray        *fileTypes = [NSArray arrayWithObjects:@"tiff", @"gif", @"jpg", nil];
  14.  
  15.         [oPanel setCanChooseDirectories:YES];
  16.  
  17.         if ( [NSApp keyWindow] != nil )
  18.             path = [[[NSApp keyWindow] representedFilename] stringByDeletingLastPathComponent];
  19.  
  20.         result = [oPanel runModalForDirectory:path file:nil types:fileTypes];
  21.  
  22.         if (result == NSOKButton)
  23.         {
  24.             NSString    *file = [oPanel filename];
  25.             NSFileManager *fm = [NSFileManager defaultManager];
  26.             BOOL    isDir = NO;
  27.  
  28.             browser = [[DAImageBrowser allocDocumentZone:NSStringFromClass([self class])] init];
  29.  
  30.             if ( [fm fileExistsAtPath:file isDirectory:&isDir] == YES )
  31.             {
  32.                 if ( isDir == NO )
  33.                 {
  34.                     file = [file stringByDeletingLastPathComponent];
  35.                 }
  36.             }
  37.             [browser setRoot:file];
  38.             [browser display];
  39.         }
  40.     HANDLER
  41.     ENDHANDLER
  42.     return browser;
  43. }
  44.  
  45. - (void)awakeFromNib;
  46. {
  47.     if ( root == nil )
  48.         [self setRoot:NSHomeDirectory()];
  49. }
  50.  
  51.  
  52. - (void)setRoot:(NSString *)value
  53. {
  54.     [root autorelease];
  55.     root = [value retain];
  56.     if ( root )
  57.     {
  58.         [window setTitleWithRepresentedFilename:root];
  59.         topFileInfo = [[FilteredFileInfo alloc] initWithPath:root];
  60.     }
  61. }
  62. - (NSString *)root
  63. {
  64.     return root;
  65. }
  66.  
  67. -(void)imageNext:sender
  68. {
  69.     int    selected = [outlineView selectedRow] + 1;
  70.  
  71.     if ( selected >= [outlineView numberOfRows] )
  72.         selected = 0;
  73.  
  74.     [outlineView selectRow:selected byExtendingSelection:NO];
  75.     [outlineView scrollRowToVisible:selected];
  76.     [self displayImage:self];
  77. }
  78.  
  79. -(void)imagePrev:sender
  80. {
  81.     int    selected = [outlineView selectedRow] - 1;
  82.  
  83.     if ( selected < 0 )
  84.         selected = 0;
  85.  
  86.     [outlineView selectRow:selected byExtendingSelection:NO];
  87.     [outlineView scrollRowToVisible:selected];
  88.     [self displayImage:self];
  89. }
  90.  
  91. - (void)delete:sender;
  92. {
  93.     DURING
  94.     int            row = [outlineView selectedRow];
  95.     FilteredFileInfo    *item = [outlineView itemAtRow:row];
  96.     FilteredFileInfo    *parent = [item parent];
  97.     NSFileManager     *fm = [NSFileManager defaultManager];
  98.  
  99.     // this is ugly, but I am trying to hand all formatting issues to Project builder
  100.  
  101.     if (([item filetype] != NSFileTypeDirectory) || (NSRunAlertPanel(@"Delete Directory !!!", @"Are you sure you want to delete all the contents of %@", @"No", @"Yes", nil, [item filename]) == NSAlertAlternateReturn))
  102.     {
  103.         // maybe because the selected cell is being deleted we get an exception?
  104.         [outlineView deselectAll:self];
  105.  
  106.         if ( [fm removeFileAtPath:[item fullPathName] handler:self] == YES )
  107.             NSLog( @"Removed %@\n", [item fullPathName] );
  108.         else
  109.             NSLog( @"Error, could not remove %@\n", [item fullPathName] );
  110.  
  111.         // make the parent reload it's contained files array, should reload next 
  112.         // time we ask for the containedFiles
  113.         [parent setContainedFiles:nil];
  114.  
  115.         // have the outline view reload the parent.  The outline view should
  116.         // shrink to remove the deleted image.  This causes an exception.
  117.  
  118.         // if ( parent == topFileInfo )
  119.             [outlineView reloadData];
  120.         // else
  121.         //    [outlineView reloadItem:parent];
  122.         // else
  123.  
  124.         // try to get the outline view to shrink!!!
  125.         [outlineView noteNumberOfRowsChanged];
  126.  
  127.         // select the next row
  128.         row++;
  129.         while ((row >= [outlineView numberOfRows]) && ( row > 0 ))
  130.             row--;
  131.  
  132.         [outlineView selectRow:row byExtendingSelection:NO];
  133.         [outlineView scrollRowToVisible:row];
  134.  
  135.         // display the next image
  136.         [self displayImage:self];
  137.     }
  138.  
  139.     HANDLER
  140.     ENDHANDLER
  141. }
  142.  
  143. - (BOOL)fileManager:(NSFileManager *)fm shouldProceedAfterError:(NSDictionary *)errorInfo
  144. {
  145.     return NO;
  146. }
  147.  
  148. - (void)displayImage:sender
  149. {
  150.     DURING
  151.     id        item = [outlineView itemAtRow:[outlineView selectedRow]];
  152.     NSImage    *image = [[[NSImage allocWithZone:[self zone]] initWithContentsOfFile:[item fullPathName]] autorelease];
  153.  
  154.     if ( image )
  155.     {
  156.         NSSize size = [image size];
  157.         [imageView setImage:image];
  158.         [window setTitleWithRepresentedFilename:[item fullPathName]];
  159.         [descText setStringValue:[NSString stringWithFormat:@"%@ (%.1f x %.1f)", [item filename], size.width, size.height]];
  160.     }
  161.     else
  162.     {
  163.         [imageView setImage:nil];
  164.         if ([item filetype] == NSFileTypeDirectory)
  165.             [outlineView expandItem:item];
  166.     }
  167.  
  168.     HANDLER
  169.     ENDHANDLER
  170. }
  171.  
  172. /* This routine asks for the object corresponding to the indexth child of item.  If the object is at the top level (i.e. the files contained in your home directory), the outlineView passes an item==nil, so that has to be special cased.  In this case, there is a single FilteredFileInfo corresponding to the home directory that contains all of the top level FilteredFileInfos.
  173. */
  174. - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
  175. {
  176.     NSArray    *content = nil;
  177.     if (item == nil)  //it's the topmost level
  178.         content = [topFileInfo containedFiles];
  179.     else
  180.         content = [item containedFiles];
  181.  
  182.     if ( content && ( index < [content count] ))
  183.         return [content objectAtIndex: index];
  184.  
  185.     return nil;
  186. }
  187.  
  188. /* This routine checks to see whether a given item is expandable (i.e. needs a little spinning triangle thingy.  In this case, it does if and only if it is a directory.  Of course that doesn't mean that there are necessarily any subitems, since it should be possible to open an empty directory even though there's nothing inside.
  189. */
  190. - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable: (id)item
  191. {
  192.     return ([item filetype] == NSFileTypeDirectory);
  193. }
  194.  
  195. /* outlineView:numberOfChildrenOfItem: performs a query to see how many children a particular item has.  Presumably the item has already been tested by outlineView:isItemExpandable to make sure it can be opened in the first place.  Again, an item==nil means that it's the top-level (see outlineView:child:ofItem:)
  196. */
  197. - (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
  198. {
  199.     if (item == nil)
  200.         return [[topFileInfo containedFiles] count];
  201.     else
  202.         return [[item containedFiles] count];
  203. }
  204.  
  205.  
  206. /*  now that it's determined what the right object for a given row is, the NSOutlineView will ask what the appropriate data it should stick into each of the columns for that item.  In this case, the NSTableColumns have been setup in Interface Builder to have identifiers that correspond exactly to the correct accessor methods of the FileInfo objects.  Using the power of the Objective C runtime, these NSStrings are converted into selectors (similar to pointers to virtual functions in C++), which are then called on the items to return the correct information.
  207. */
  208. - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
  209. {
  210.     return [item performSelector: NSSelectorFromString([tableColumn identifier])];
  211. }
  212.  
  213. - (void)dealloc
  214. {
  215.     /* don't need to release finderListWindow since we never retained it. in general, we probably should have, but since it was only used in awakeFromNib, it wasn't necessary.
  216.     */
  217.     [topFileInfo release];
  218. }
  219.  
  220.  
  221. @end
  222.