Menu

[r11]: / trunk / SWFInvestigator / src / osUtils / DragAndDropManager.as  Maximize  Restore  History

Download this file

102 lines (90 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
/****************************************************************************
* ADOBE SYSTEMS INCORPORATED
* Copyright 2012 Adobe Systems Incorporated and it’s licensors
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
* ****************************************************************************/
package osUtils
{
import flash.desktop.ClipboardFormats;
import flash.desktop.NativeDragManager;
import flash.display.InteractiveObject;
import flash.events.NativeDragEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.utils.ByteArray;
public class DragAndDropManager
{
private var callbackFunction:Function;
private var obj:InteractiveObject;
/**
* Constructor
*
* @param target The InteractiveObject that the DragManager will listen on
* @param callback The callback function that the DragManager will call when it has a file. This function must take three parameters: data:ByteArray, filename:string, url:String
*/
public function DragAndDropManager(target:InteractiveObject, callback:Function)
{
this.obj = target;
obj.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);
obj.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop);
this.callbackFunction = callback;
}
/**
* Allows the caller to verify NativeDragManager is supported
*
* @return A boolean indicating support
*/
public static function isSupported():Boolean {
return NativeDragManager.isSupported;
}
/**
* Called when the user drags an item into the component area
*
* @param e The NativeDragEvent for the DragIn event
*/
private function onDragIn(e:NativeDragEvent):void
{
//check and see if files are being drug in
if(e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
{
//get the array of files
var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
//make sure only one file is dragged in (i.e. this app doesn't
//support dragging in multiple files)
if(files.length == 1)
{
//accept the drag action
NativeDragManager.acceptDragDrop(this.obj);
}
}
}
/**
* Called when the user drops an item over the component
* It is assumed the receiver will do the data validation
*
* @param e The NativeDragEvent for the DragDrop event
*/
private function onDragDrop(e:NativeDragEvent):void
{
//get the array of files being drug into the app
var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
//grab the files file
var f:File = File(arr[0]);
//create a FileStream to work with the file
var fs:FileStream = new FileStream();
//open the file for reading
fs.open(f, FileMode.READ);
//read the file as a ByteArray
var data:ByteArray = new ByteArray();
fs.readBytes(data,0,fs.bytesAvailable);
//close the file
fs.close();
//Return the data to the callback function
this.callbackFunction(data, f.name, f.url);
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.