Macromedia flex 2-migrating applications to flex 2 Manuale Utente

Pagina di 184
Using the drag-and-drop feature
179
Flex 2:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
import mx.managers.DragManager;
// Import events package for MouseEvent:
import mx.events.*;
// Specify types for all arguments:
function dragIt(event:MouseEvent, text:String, format:String) {
var ds:mx.core.DragSource = new mx.core.DragSource();
ds.addData(text, format);
// New doDrag signature:
DragManager.doDrag(event.target, ds, event);
}
function doDragEnter(event:DragEvent) {
if (event.dragSource.hasFormat('color')) {
/? For a drop target to accept an item for dropping, it calls the 
// acceptDragDrop() method and does not use the 
// event.handled property:
DragManager.acceptDragDrop(event.target);
}
}
function doDragDrop(event:DragEvent) {
var data:Object = event.dragSource.dataForFormat('color');
myCanvas.setStyle("backgroundColor", data);
}
]]></mx:Script>
<mx:HBox>
<mx:Canvas backgroundColor="#FF0000" borderStyle="solid" width="30"
height="30" mouseMove="dragIt(event, 'red', 'color')"/>
<mx:Canvas backgroundColor="#00FF00" borderStyle="solid" width="30"
height="30" mouseMove="dragIt(event, 'green', 'color')"/>
</mx:HBox>
<mx:Label text="Drag the item into this canvas"/>
<mx:Canvas id="myCanvas" backgroundColor="#FFFFFF" borderStyle="solid" 
width="100" height="100" dragEnter="doDragEnter(event)"
dragDrop="doDragDrop(event)"/>
 </mx:Application>