- Drag and Drop in an Application (DragManager Class)
- Drag and Drop Between the OS and the Application (NativeDragManager)
- Drag and Drop Items into an AIR Application from the OS
- Drag Items into the dpTimeTracker Application
- Drag Items from an AIR Application to the OS
- Drag Items into the OS
- Next Steps
Drag Items into the dpTimeTracker Application
The dpTimeTracker application uses the techniques you’ve learned this chapter to exchange data with the OS. The application allows you to drag a CSV file to the Clients page as an alternative to manually typing in new client entries.
Launch the dpTimeTracker application and log in using your username and password. Click the Admin link and navigate to the Clients page. The Client Admin page will allow you to drag any file with a .csv extension onto the datagrid. The import code will assume that the first column is the client name and the second column is the description. A sample CSV file is included in the assets directory of the dpTimeTracker project.
Much like the clipboard code in the previous chapter, most of the code that handles this drag-and-drop technique exists in the ListToExcelClipboardDragDropHandler class in the net/digitalprimates/dragDrop directory.
When the file is dragged into the DataGrid, a nativeDragEnter event is broadcast. The following code shows the dragEnter handler that responds to this event.
Listing 7.
private function dragEnter( event:NativeDragEvent ):void { if( event.clipboard.hasFormat( ClipboardFormats.FILE_LIST_FORMAT ) ){ var fileList:Array = event.clipboard.getData( ClipboardFormats.FILE_LIST_FORMAT ) as Array; var csvFile:File = fileList[0] as File; if ( csvFile.extension == 'csv' ) { NativeDragManager.acceptDragDrop( event.currentTarget as InteractiveObject ); } } }
Before the drop is accepted, the handler ensures that the clipboard has a format of FILE_LIST_FORMAT. It then further inspects the data in this format to ensure that the file has a .csv extension. If these conditions are met, the acceptDragDrop() method is called, passing the datagrid as the target.
If the user releases the mouse button over this datagrid, a nativeDragDrop event is broadcast and handled by the doDragDrop() method.
This method uses the getData() method from the event.clipboard object to retrieve the file list. It then uses the File API to read data from that file into the AIR application. Once this data has been read, it parses the data, converting it into an object with rows and columns. A confirmation screen is then displayed, allowing you to import this data into your AIR application, if it is correct.