Re: Accepting the right file drops
Re: Accepting the right file drops
- Subject: Re: Accepting the right file drops
- From: Steve Klingsporn <email@hidden>
- Date: Sun, 20 Oct 2002 23:41:40 -0500
Here is a chunk of Java code that handles accepting or rejecting
the right file drops. Does one get yelled at on here for uttering the
J-word?
I save the filenames from the pasteboard, so I don't have to examine
them from both draggingEntered and from performDragOperation.
Change Pascal-like syntax to square-bracketed and starry-eyed
madness and things should work pretty much the same. ;o)
Steve
/**
Called when a file is dragged into the view
@param dragInfo information about the drag
**/
public int draggingEntered(NSDraggingInfo dragInfo)
{
int result = NSDraggingInfo.DragOperationNone;
NSPasteboard payload = dragInfo.draggingPasteboard();
Object files = payload.propertyListForType(
NSPasteboard.FilenamesPboardType);
String filenames[] = filterAppearanceFiles((NSArray)files);
if (filenames != null && filenames.length > 0)
{
mDrawDragBorder = true;
mDraggedFilenames = filenames;
result = NSDraggingInfo.DragOperationCopy;
display();
}
return result;
}
/**
Called when a drag falls out of the view
@param dragInfo information about the drag
**/
public void draggingExited(NSDraggingInfo dragInfo)
{
mDrawDragBorder = false;
mDraggedFilenames = null;
display();
}
/**
Called to perform the appearance drag install
@param dragInfo information about the drag
@returns true if the drag succeeds, else false
**/
public boolean performDragOperation(NSDraggingInfo dragInfo)
{
boolean result = false;
String lastLoaded = null;
if (mDraggedFilenames != null)
{
for (int i = 0; i < mDraggedFilenames.length; i++)
{
String filename = mDraggedFilenames[i];
NSImage image = new NSImage(filename, false);
if (image != null && image.isValid())
{
NSSize size = image.size();
if (size.width() == APPEARANCE_IMAGE_WIDTH &&
size.height() == APPEARANCE_IMAGE_HEIGHT)
{
result = copyFileToResources(filename);
lastLoaded = filePathToAppearanceName(filename);
}
}
}
mDraggedFilenames = null;
}
mDrawDragBorder = false;
if (result == true)
{
setAppearance(lastLoaded);
mController.setPreference(AtaxxController.APPEARANCE_PREFERENCE,
lastLoaded);
getAppearanceNames(true);
}
else
display();
return result;
}
/**
Copies a file into our "Resources" directory, and
returns true if the operation succeeds.
@param filename the filename to copy
@returns true if the copy was successful
**/
private boolean copyFileToResources(String filename)
{
String resourcePath = NSBundle.mainBundle().resourcePath();
String sourcePath = filename.substring(0,
filename.lastIndexOf(File.separator));
filename =
filename.substring(filename.lastIndexOf(File.separator) + 1);
int result = NSWorkspace.sharedWorkspace().performFileOperation(
NSWorkspace.CopyOperation, sourcePath, resourcePath,
new NSArray(filename));
return (result >= 0);
}
/**
Returns an array of filtered filenames that match
as probable appearance files
@param files the unfiltered list of files
@returns an array of filenames that are cool, or null
**/
public String[] filterAppearanceFiles(NSArray files)
{
String results[] = null;
Vector vector = new Vector();
for (int i = 0; i < files.count(); i++)
{
String filename = (String)files.objectAtIndex(i);
if (filename.endsWith(APPEARANCE_SUFFIX))
vector.addElement(files.objectAtIndex(i));
}
if (vector.size() > 0)
{
results = new String[vector.size()];
vector.copyInto(results);
}
return results;
}
/**
Returns the appearance name for its filename
@param filename the filename
@returns the appearance name
**/
public String filePathToAppearanceName(String filename)
{
filename =
filename.substring(filename.lastIndexOf(File.separator) + 1);
filename = filename.substring(0,
filename.indexOf(APPEARANCE_SUFFIX));
return filename;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.