Point pt = Display.getCurrent().map(null, viewer.getTable(), event.x, event.y);
TableItem item = viewer.getTable().getItem(pt);
// Get the index of the drop.
if (item != null) {
Playlist playlist = Controller.getInstance().getPlaylistController().getPlaylistByName(_id);
return playlist.getAudioItemIndex((AudioItem) item.getData());
} else {
return -1;
}
}
@SuppressWarnings("unchecked")
public void drop(DropTargetEvent event) {
if (Controller.getInstance().getDNDController().isSamePlaylistViewSource(_id)) {
// Drag from current view.
// Get the current item.
Point pt = Display.getCurrent().map(null, viewer.getTable(), event.x, event.y);
TableItem item = viewer.getTable().getItem(pt);
// Get the index of the drop.
Playlist playlist = Controller.getInstance().getPlaylistController().getPlaylistByName(_id);
int index = playlist.getAudioItemIndex((AudioItem) item.getData());
// Make a list of items to move.
AudioItem itemToMove;
AudioItem firstItem = null;
List<AudioItem> list = new ArrayList<AudioItem>();
ISelection selection = viewer.getSelection();
Iterator iter = ((IStructuredSelection) selection).iterator();
while (iter.hasNext()) {
itemToMove = (AudioItem) iter.next();
if (firstItem == null)
firstItem = itemToMove;
list.add(itemToMove);
}
// If moving up, move in reverse order to keep the initial order.
if (playlist.getAudioItemIndex(firstItem) > index) {
for (int i = list.size() - 1; i >= 0; i--) {
Controller.getInstance().getPlaylistController().moveTitleAtIndex(_id, index, list.get(i));
}
} else {
for (int i = 0; i < list.size(); i++) {