Daniel Dura

All About the Adobe Flash Platform

Apollo Beta Sneak Peak: Drag and Drop

with 5 comments

Over the past few weeks I have been on the road in Tokyo, London, and now Paris. I am actually writing this post as I travel underneath the English Channel on the train! In Tokyo, Mike Chambers and I presented at the Apollo Camp there. My presentation was a sneak peak of some of the beta functionality that will be released in the Apollo Public Beta. The beta should be out in early summer (hmmm, isn’t it summer already?) Until that day comes, I am going to share some of what I have been presenting while on the road and give you a sneak peak of some of the new functionality that will be in this release.

One of the most exciting features in the Apollo Beta is Drag and Drop support. This is full OS level drag and drop support. You will be able to drag files, images, urls, text, serialized ActionScript objects, and other types to and from your Apollo application and other non Apollo applications and the desktop. You can control the image that is dragged and the modifiers that are allowed (copy, link, move, etc.) The first example I am going to share with you is how to start a drag operation from your Apollo application.

In this example, we are going to initiate a drag whenever a user clicks on a Button. We will use a snapshot of the button as the drag proxy (the image you see during the drag operation.) We will be transferring a URL with the drag which will allow us to drop this on browsers, text editors, and any other application that accepts URL types.

First you prepare the data that you are going to share with the drag and drop operation. To do this, you use an instance of the TransferableData class. This class allows you to add multiple data entries that are transfered with the drag operation by using the addData method. So for example, if I am dragging a URL, I might want to put both a URL format and a text format on the TransferableData object. The receiving application can then decide which data format is most appropriate.

[as]
var transfer:TransferableData = new TransferableData();
transfer.addData( “http://www.danieldura.com”, TransferableFormats.URL_FORMAT );
transfer.addData( “Daniel Dura’s Weblog”, TransferableFormats.TEXT_FORMAT );
[/as]

Next, we want to specify a custom image that will be dragged with the image and will represent our data. For this example, we will just take a snapshot of the button that the user is clicking on and use that as our drag proxy. The drag and drop API takes a BitmapData object for this purpose.

[as]
var dragImage:BitmapData = new BitmapData( dragButton.width, dragButton.height, true );
dragImage.draw( dragButton );
[/as]

Last, we initiate the drag by calling the DragManager.doDrag method. This method takes three arguments: the initiating display object, the TransferableData instance, and the BitmapData instance for the drag image.

[as]
DragManager.doDrag( dragButton, transfer, dragImage );
[/as]

Here is the full example:

[xml]



import flash.desktop.ClipboardManager;
import flash.desktop.TransferableFormats;
import flash.desktop.TransferableData;
import flash.desktop.DragManager;

private function handle_imageClick():void
{
var transferFormat:TransferableData = new TransferableData();
transferFormat.addData( "http://www.danieldura.com",
TransferableFormats.URL_FORMAT );

var dragImage:BitmapData = new BitmapData( dragButton.width,
dragButton.height, true );
dragImage.draw( dragButton );

DragManager.doDrag( dragButton, transferFormat, dragImage );
}

]]>

mouseDown="handle_imageClick()"
x="10" y="10"/>

[/xml]

So, this is just a very small taste of the funtionality available in the drag and drop API. In my next post, I will show how you can accept drops from other applications.

Written by Daniel Dura

June 8th, 2007 at 1:29 pm

Posted in AIR

5 Responses to 'Apollo Beta Sneak Peak: Drag and Drop'

Subscribe to comments with RSS or TrackBack to 'Apollo Beta Sneak Peak: Drag and Drop'.

  1. looks like a great API! Nice, to the point, easy to use. Can’t wait. Wonderful work guys.

    eric dolecki

    8 Jun 07 at 6:08 pm

  2. Apollo Beta Sneak Peak: Drag and Drop: by Daniel Dura…

    Daniel Dura wrote: “Over the past few weeks I have been on the road in Tokyo, London, and now Paris. I am actually writing this post as I travel underneath the English Channel on the train! In Tokyo, Mike Chambers……

  3. Do you know if this ability will carry into the htmlControl as well?

    greg

    9 Jun 07 at 4:11 am

  4. 1172: Definition flash.desktop:DragManager could not be found.
    1172: Definition flash.desktop:DragOptions could not be found.
    1172: Definition flash.desktop:TransferableData could not be found.
    1172: Definition flash.desktop:TransferableFormats could not be found.

    kanu kukreja

    1 May 08 at 8:35 am

  5. i have resolved that issue by changing packages in to
    import flash.desktop.ClipboardManager;
    import flash.desktop.ClipboardFormats;
    import flash.desktop.Clipboard;
    import flash.desktop.NativeDragManager;

    now i’m facing problem for:

    transferFormat.addData( “http://www.danieldura.com”, Clipboard.URL_FORMAT);

    kanu kukreja

    1 May 08 at 9:02 am

Leave a Reply