I recently committed support for a drag and drop API in LWUIT to simplify the task of implementing this functionality. LWUIT always allowed dragging and dropping which was a part of our demo for quite a while now, however we didn't have a convenient generic drag and drop API.
Unlike other platforms who tried to create overly generic catch all API's I tried to make things as simple as possible. We always drag a component and always drop it onto another component, if something else is dragged to some other place it must be wrapped in a component, the logic of actually performing the operation indicated by the drop is the responsibility of the person implementing the drop.
There is a minor sample of this in the LWUITDemo whose drag and drop behavior is now implemented using this API. However, the LWUITDemo relies on builtin drop behavior of container specifically designed for this purpose.
To enable dragging a component it must be flagged as draggable using setDraggable(true), to allow dropping the component onto another component you must first enable the drop target with setDropTarget(true) and override some methods (more on that later).
Notice that is a drop target is a container that has children, dropping a component on the child will automatically find the right drop target. You don't have to make "everything" into a drop target.
You can override these methods in the draggable components:
getDragImage - this generates an image preview of the component that will be dragged. This automatically generates a sensible default so you don't need to override it.
drawDraggedImage - this method will be invoked to draw the dragged image at a given location, it might be useful to override it if you want to display some drag related information such an additional icon based on location etc. (e.g. a move/copy icon).
In the drop target you can override the following methods:
draggingOver - returns true is a drop operation at this point is permitted. Otherwise releasing the component will have no effect.
dragEnter/Exit - useful to track and cleanup state related to draging over a specific component.
drop - the logic for dropping/moving the component must be implemented here!
Notice that Container.java has a simple sample drop implementation you can use to get started.
Monday, August 1, 2011
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment