Skip to content

Instantly share code, notes, and snippets.

@xhan
Created May 7, 2011 14:39
Show Gist options
  • Save xhan/960546 to your computer and use it in GitHub Desktop.
Save xhan/960546 to your computer and use it in GitHub Desktop.
NSDropContainer
//
// 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
//
// 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