AIR-to-Desktop Drag-and-Drop: Two Simple Utility Classes

AIR-to-Desktop Drag-and-Drop: Two Simple Utility Classes

One of the exciting features of AIR is that it enables drag-and-drop between your Flex applications and the desktop: For example, you could drag a chart from your Flex application and drop it on your desktop as a JPG, or directly inside a Word document. Similarly, you could grab some rows from a Datagrid and drop them on your desktop as an XLS file, or directly inside an Excel spreadsheet.

I spend a lot of time talking with Financial Services organizations, and this is a feature that they are really interested in.

This is “native” drag-and-drop handled by the operating system. The way it works is that, on “drag start” in your application, you are responsible for packaging the relevant data in one or different formats (text, bitmap, file). When the user mouses over another desktop application, that application will look at the formats available, and decide if it wants to accept a drop. If it does, it will adjust the drag icon accordingly and consume the data as appropriate if the user releases the mouse button.

I have been using this feature in the SalesBuilder application I posted yesterday. In the process of building SalesBuilder I created two utility classes to abstract the details of generating bitmaps, XLS files, etc…

  • DragBitmap allows you to drag any Flex visual component and drop it on the desktop as JPG or in any other application that accepts bitmaps.

    To use it simply add the following line to your code:

    DragBitmap packages your data in two formats: a raw bitmap format and a JPG file.

  • DragExcel allows you to drag the selected rows of a Datagrid and drop them on the desktop as an XLS file or directly in an open Excel spreadsheet.

    To use it simply add the following line to your code:

    DragExcel packages your data in two formats: a raw text format and an HTML file with an XLS extension (Excel can read HTML table). A CSV format may be a better choice for greater portability.

Click here to install a very simple sample application.

This application is just a few lines of code…

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"
	creationComplete="srv.send()">

	<mx:ArrayCollection id="results"/>
	<mx:HTTPService id="srv" url="results.xml"/>
	<mx:Binding source="srv.lastResult.data.item" destination="results"/>

	<DragBitmap displayObject="{lc}"/>
	<DragExcel dataGrid="{dg}"/>

	<mx:DataGrid id="dg" dataProvider="{results}" allowMultipleSelection="true"/>

	<mx:ColumnChart id="lc" dataProvider="{results}">
		<mx:horizontalAxis>
			<mx:CategoryAxis categoryField="month"/>
		</mx:horizontalAxis>
		<mx:series>
			<mx:ColumnSeries yField="revenue"/>
			<mx:ColumnSeries yField="costs"/>
		</mx:series>
	</mx:ColumnChart>

</mx:WindowedApplication>

… and allows you to:

  • Drag selected rows in the DataGrid and drop them to the desktop as an XLS file or directly in an open Excel spreadsheet.
  • Drag the chart and drop it on the desktop as a JPG file or directly in an Open Word document (or any other application that consumes bitmaps).

These are very basic classes. They can certainly be improved in many ways, but hopefully they can give you a jumpstart when experimenting with AIR-to-desktop drag-and-drop.

Click here to download the source code.

This entry was posted in Flex. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

52 Comments

  1. Matt Poole
    Posted June 15, 2007 at 2:08 pm | Permalink

    Great stuff Christophe, I’ll be playing with this over the weekend!

  2. Kalvin
    Posted June 18, 2007 at 4:36 pm | Permalink

    Christophe, thanks for the all the useful info– I have just one question: does AIR support Desktop-to-AIR drag-and-drop? For instance, am I able to implement a feature where the user can drag a JPG from a browser window to the AIR program and have the AIR program display it?

  3. Kalvin
    Posted June 19, 2007 at 3:00 pm | Permalink

    I believe this part of the docs answers my question (“Yes”), but if I’m misinterpreting it please let me know:

    “Gestures between application

    When a drag gesture enters an AIR application window from a non-AIR application, there will be no initiator object to dispatch the nativeDragStart or nativeDragComplete events. The events dispatched during the gesture will otherwise follow the same pattern as that of a gesture starting and ending within the same AIR application

    When a drag gesture leaves an AIR application window, there will be no target object to dispatch nativeDragEnter, nativeDragOver, or nativeDragDrop events. The initiator object still dispatches a nativeDragComplete event, which reports the drag action set by the native operating system (or “none,” if the drop was not accepted).”

  4. Posted June 21, 2007 at 10:29 pm | Permalink

    Hello! Good Site! Thanks you! lhtibbwscqsaah

  5. Posted July 12, 2007 at 6:15 pm | Permalink

    Christophe..

    great info. Can you follow up with an example of Desktop-to-AIR drag-and-drop?

    For instance, drag a text or document file from the desktop or a folder to the air ap, and have air ap open the file IE: read the contents and do something with it (say encrypt the file – and later decrypt the file) and then display it as list of files in a grid – then use the AIR to Desktop to allow a drag back to desktop and let the user see the changes?
    Dan

  6. after8
    Posted July 24, 2007 at 2:23 am | Permalink
  7. Posted September 6, 2007 at 3:38 pm | Permalink

    Can you drag a node from a file tree that is an image to your desktop or photoshop? We are planning a big project and figuring out a way for an employee to be able to quickly find photos by searching, and also the ability to drag to another directory or photoshop

  8. Evan Gifford
    Posted October 9, 2007 at 4:50 pm | Permalink

    Awesome! I can always count on you Christophe to come up with the perfect examples.

    One update from release notes of Beta 2: http://labs.adobe.com/wiki/index.php/AIR:Release_Notes

    You’ll need to change TransferableData to Clipboard, TransferableFormats to ClipboardFormats and resolve() to resolvePath() to get this to work with Beta 2

  9. Mike Kidder
    Posted October 11, 2007 at 12:03 pm | Permalink

    Thanks Evan.

    Worked for me updating this sample to Beta 2. Flex Builder also showed me the issues, but it is helpful to know exactly what to change things too.

  10. Posted October 16, 2007 at 6:39 am | Permalink

    Thank you very much for publishing this example. This is just the kind of thing I was looking for in order to implement my senior project for college.

    Your post has been very helpful.

  11. Carlos Escobar
    Posted October 24, 2007 at 6:58 am | Permalink

    Hi Christophe.

    Could you help with issue when I m trying to run your example about DragExcel that you have placed above.

    I got the following message: Could not resolve to a component implementation.

    Do you have any idea which could be the problem or how can I resolve it.

    Thanks in advanced for you support.

    Futher your initative about this is implementation is great!!.. congratulations.

    Best,
    Carlos

  12. Posted November 26, 2007 at 5:59 am | Permalink

    cqkgjytmwg cqkgjytmwg cqkgjytmwgcqkgjytmwg
    cqkgjytmwgcqkgjytmwgcqkgjytmwg cqkgjytmwg

  13. Posted January 6, 2008 at 9:21 am | Permalink

    I couldn’t port this to Flex3 Beta3. I heard that in Beta 3, AIR applications use the native drag and drop. Does that affect these components?

  14. Aubweb
    Posted January 18, 2008 at 7:12 am | Permalink

    Yes it does… just replace by the correct classes.

  15. RAVI
    Posted January 21, 2008 at 10:06 pm | Permalink

    in flex 3 beta 3 version i am not getting clipboard.addData() function,and which class i have to use in beta 3 ….

  16. Jordan
    Posted February 19, 2008 at 11:22 am | Permalink

    Having the same problem as RAVI…clipboard.addData() is not appearing as a function…where would there be any documentation on this?

  17. Jordan
    Posted February 19, 2008 at 11:36 am | Permalink

    found it! instead of Clipboard.addData() it’s Clipboard.setData() with the parameters switched…

  18. ED
    Posted March 31, 2008 at 10:55 pm | Permalink

    Hi I have a question related to drag and drop – maybe you would have some insights into this situation.

    I would actually like to DISABLE the drag/drop functionality that is built into the TextField object (i.e. prevent users from dragging selected text from one TextField object to another).

    You can do this in a class based on ListBase, where there are properties “dragEnabled” and “allowDragSelection” that can be set to false.

    However, no equivalent properties seem to exist for as TextField. Would you have any ideas on how to get around this? Thanks for any pointers, now matter how remote!

  19. Mark
    Posted April 7, 2008 at 4:45 am | Permalink

    I also want to disable this feature for TextFields!

    My TextField uses an external stylesheet and so every time a user attempts to drag text to the TextField, the following error is thrown:

    Error: Error #2009: This method cannot be used on a text field with a style sheet.
    at flash.text::TextField/replaceSelectedText()
    at flash.text::TextField/_dragDrop()

    Any ideas anyone?

  20. Mark
    Posted April 7, 2008 at 6:40 am | Permalink

    I’ve now managed to avoid this error by explicitly setting the textarea.editable to false.

  21. piroko
    Posted April 12, 2008 at 2:21 am | Permalink

    To make folk’s life easier… the classes that I had to update for Flex3 were

    TransferableData -> Clipboard
    DragOptions -> NativeDragOptions
    TransferableFormats -> ClipboardFormats
    DragManager ->NativeDragManager

  22. Patrick
    Posted June 3, 2008 at 3:56 am | Permalink

    Your source is not 1.0, still. Thanks

  23. Posted August 22, 2008 at 6:34 am | Permalink

    Your source is not 1.0 still Thanks

  24. Posted August 22, 2008 at 6:35 am | Permalink

    Thank you very much for publishing this example.

  25. Posted August 22, 2008 at 6:36 am | Permalink

    Thank you very much

  26. Posted September 17, 2008 at 2:12 am | Permalink

    I’ve now managed to avoid this error by explicitly setting the textarea.editable to false.

  27. Posted September 17, 2008 at 2:13 am | Permalink

    Yes it does… just replace by the correct classes.

  28. Posted December 1, 2008 at 1:04 pm | Permalink

    Thank you for your share.

    I’m sending migration souce code to AIR 1.5 at my link.

    http://www.wfcampus.co.kr/temp/dragdrop.zip

    Go!

  29. Posted January 7, 2009 at 1:10 pm | Permalink

    AIR support Desktop-to-AIR drag-and-drop? For instance, am I able to implement a feature where the user can drag a JPG from a browser window to the AIR program and have the AIR program display it?

  30. Posted January 7, 2009 at 1:10 pm | Permalink

    [...] Comment! Christophe Coenraets :: AIR-to-Desktop Drag-and-Drop: Two Simple Utility Classes [...]

  31. Posted March 17, 2009 at 8:50 am | Permalink

    For instance, am I able to implement a feature where the user can drag a JPG from a browser window to the AIR program and have the AIR program display it?

  32. Posted May 10, 2009 at 11:35 pm | Permalink

    thanks you admin good post

  33. Posted May 16, 2009 at 2:07 pm | Permalink

    I’ve now managed to avoid this error by explicitly setting the textarea.editable to false.

  34. sohbet
    Posted May 17, 2009 at 8:52 am | Permalink

    hallo i wish you verry succes operator

  35. Posted May 18, 2009 at 12:16 am | Permalink

    This is awesome stuff Christophe.
    Will try it out asap

  36. Posted May 19, 2009 at 8:29 am | Permalink

    hallo i wish you verry succes operator

  37. Posted June 5, 2009 at 5:43 pm | Permalink

    hallo dear friends thanks a lot for your workshop

  38. Posted July 10, 2009 at 7:02 pm | Permalink

    Having the same problem as RAVI…clipboard.addData() is not appearing as a function

  39. Posted July 23, 2009 at 9:26 am | Permalink

    Thank you very much for publishing this example. This is just the kind of thing I was looking for in order to implement my senior project for college.

    Your post has been very helpful.

  40. Posted July 23, 2009 at 11:44 am | Permalink

    Could you help with issue when I m trying to run your example about DragExcel that you have placed above.

    I got the following message: Could not resolve to a component implementation.

    Do you have any idea which could be the problem or how can I resolve it.

    Thanks in advanced for you support.

    Futher your initative about this is implementation is great!!.. congratulations.

  41. Philippe
    Posted July 25, 2009 at 4:16 pm | Permalink

    Christophe,
    I have enjoyed all your post and I am always impressed with what you come up with and also the capabilities of Air and Flex.
    Recently, each time I am trying to install your air examples, I get the same error by the installer:
    This application requires an update to Adobe @AIR that is not available for your system.
    I run Vista 32 on a regular PC. This is really frustrating.
    Any idea?
    Thanks

  42. Posted July 29, 2009 at 8:58 am | Permalink

    Кстати, я сейчас посмотрел, ваш блог в Гугле хорошие места занимает ,если имя блога туда вбивать.

  43. Posted August 23, 2009 at 6:13 pm | Permalink

    thanks admin good post

  44. Posted October 28, 2009 at 6:10 pm | Permalink

    hey friend excellent information about AIR-to-Desktop Drag-and-Drop: Two Simple Utility Classes

  45. Posted November 24, 2009 at 6:24 am | Permalink

    how are you baby melisa

  46. Posted December 3, 2009 at 3:22 pm | Permalink

    very very good

  47. Philippe
    Posted December 3, 2009 at 6:28 pm | Permalink

    Christophe,
    Great post as usual. The only caveat is that the example uses the MOUSE_MOVE event as the trigger for the drag operation. Unfortunately, this catches resizes of the datagrid columns…
    Is there a reasin why you did not use the NATIVE_DRAG_ENTER instead?
    Thx

  48. Philippe
    Posted December 3, 2009 at 10:42 pm | Permalink

    OK, I got it all wrong, the NATIVE_DRAG_ENTER event is used for a drag-in gesture, not a drag-out.
    But I found a cheap workaround for the column resize problem.
    Just replace

    if (!event.buttonDown)

    by

    if (!event.buttonDown|| _dataGrid.mouseY<_dataGrid.headerHeight)

    et voila!

  49. Posted December 25, 2009 at 10:55 am | Permalink

    Having the same problem as RAVI…clipboard.addData() is not appearing as a function

  50. Posted January 22, 2010 at 9:16 am | Permalink

    AIR support Desktop-to-AIR drag-and-drop? For instance, am I able to implement a feature where the user can drag a JPG from a browser window to the AIR program and have the AIR program display it?

10 Trackbacks

  1. [...] AIR-to-Desktop Drag-and-Drop: Two Simple Utility Classes Christophe Coenraet wrote: One of the exciting features of AIR is that it enables drag-and-drop between your Flex applications and the desktop: For example, you could drag a chart from your Flex application and drop it on your desktop as a JPG, or directly inside a Word document. Similarly, you could grab some rows from a Datagrid and drop them on your desktop as an XLS file, or directly inside an Excel spreadsheet. [...]

  2. By 十分期待 Flex + AIR « Ocean Apple on June 18, 2007 at 2:16 am

    [...] 係 Christophe Coenraets 個 Blog 度有個 Simple Application Example,一定要 download 來試試。 [...]

  3. By viagra on November 2, 2007 at 11:05 am

    online viagra…

    denuclearising ribalds…

  4. By butalbital on December 11, 2007 at 4:08 pm

    butalbital…

    barring crosier…

  5. By online baccarat spiele on May 1, 2008 at 4:41 am

    card credit processing terminal virtual…

    Egalement 7 card stud high download info personal remember ringtones verizon…

  6. By strip poker en ligne gratuites on May 1, 2008 at 5:12 pm

    gratis poker im internet…

    As you see free ringtones tracfone professional video poker…

  7. [...] some useful utility classes for working with XLS and Bitmap formats which you can read about here.  I’ve updated his source to work with AIR 1.0 and you can download it [...]

  8. [...] 我看到flexSpaces实现了在多个flex/AIR之间的拖放。我想应该是可以实现的。 AIR-TO-DESKTOP drag-and-drop是可以实现的。 Tags: drag, flex Posted under flex [...]

  9. By About Desktop Applications « It’s all about RIA on October 14, 2008 at 12:15 am

    [...] Christophe Coenraets :: AIR-to-Desktop Drag-and-Drop: Two Simple Utility Classes [...]

  10. By Adobe AIR « Flex Generation Weblog on October 14, 2008 at 2:52 am

    [...] Comment! Christophe Coenraets :: AIR-to-Desktop Drag-and-Drop: Two Simple Utility Classes [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>