Created
May 7, 2011 14:39
-
-
Save xhan/960546 to your computer and use it in GitHub Desktop.
NSDropContainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDropContainer.h | |
#import <Cocoa/Cocoa.h> | |
@protocol NSDropContainerDelegate; | |
@interface NSDropContainer : NSBox { | |
id<NSDropContainerDelegate> _delegate; | |
NSString * _filePath; | |
NSSet * _legalExtensionss; | |
NSImageView * _imageView; | |
@private | |
} | |
@property (nonatomic,retain) NSImageView * imageView; | |
@property (nonatomic,retain) NSSet * legalExtensions; | |
@property (nonatomic,retain) NSString * filePath; | |
@property (nonatomic,assign) IBOutlet id<NSDropContainerDelegate> delegate; | |
@end | |
@protocol NSDropContainerDelegate <NSObject> | |
@required | |
- (void)containerCaptureFilePath:(NSString*)filePath dropContainer:(NSDropContainer*)dropContainer; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDropContainer.m | |
#import "NSDropContainer.h" | |
@implementation NSDropContainer | |
@synthesize delegate = _delegate; | |
@synthesize filePath = _filePath; | |
@synthesize legalExtensions = _legalExtensions; | |
@synthesize imageView = _imageView; | |
- (id)initWithFrame:(NSRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
// Initialization code here. | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
self.legalExtensions = nil; | |
self.delegate = nil; | |
self.filePath = nil; | |
[super dealloc]; | |
} | |
/* | |
- (void)drawRect:(NSRect)dirtyRect | |
{ | |
// Drawing code here. | |
} | |
*/ | |
- (void)viewWillMoveToSuperview:(NSView *)newSuperview{ | |
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; | |
} | |
- (BOOL)isPathLegal:(NSString *)filePath{ | |
if (_legalExtensionss) { | |
NSString *fileName = [filePath pathExtension]; | |
// return [[fileName lowercaseString] isEqualToString:_legalExtension]; | |
if ([_legalExtensionss containsObject:[fileName lowercaseString]]) { | |
return YES; | |
} | |
}else{ | |
return YES; | |
} | |
return NO; | |
} | |
- (void)handelTheFiles:(NSArray*)array{ | |
if (array && [array count] >0) { | |
for (NSString * filePath in array) { | |
if ([self isPathLegal:filePath]) { | |
self.filePath = filePath; | |
[self.delegate containerCaptureFilePath:filePath | |
dropContainer:self]; | |
return; | |
} | |
} | |
} | |
} | |
#pragma mark - Dragging Method | |
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender{ | |
NSPasteboard *pasteboard = [sender draggingPasteboard]; | |
if ( [[pasteboard types] containsObject:NSFilenamesPboardType] ) { | |
NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType]; | |
[self handelTheFiles:files]; | |
// Perform operation using the list of files | |
} | |
return YES; | |
} | |
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender{ | |
NSPasteboard *pboard; | |
NSDragOperation sourceDragMask; | |
sourceDragMask = [sender draggingSourceOperationMask]; | |
pboard = [sender draggingPasteboard]; | |
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) { | |
if (sourceDragMask & NSDragOperationGeneric) { | |
return NSDragOperationGeneric; | |
} | |
} | |
return NSDragOperationNone; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment