fredsa / gwt-dnd

Library providing easy to use mouse or touch based drag-and-drop capabilities to GWT
42 stars 41 forks source link

Drag multi proxy items using AbstractInsertPanelDropController inserts items at wrong indexes #125

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Dragging multiple proxy items using a AbstractInsertPanelDropController inserts 
the items at indexes 2 apart and hence produces incorrect ordering after a drag 
and drop. I produced this using an app I'm building but can use the demo app as 
below.

What steps will reproduce the problem?
1. Go to 
http://allen-sauer.com/com.allen_sauer.gwt.dnd.demo.DragDropDemo/DragDropDemo.ht
ml#FlowPanelExample
2. Turn on multi select via ctrl/meta click
3. Turn on use drag proxies
4. Select item #1, #2, #3, #4, #5 and #6 using multi select (more items will 
show the error better)
5. Drag to after item #13 say
6. Drop items

What is the expected output? What do you see instead?
- Expecting items #12, #13, #1, #2, #3, #4, #5, #6, #14, #15 in that order
- Get items #12, #13, #1, #2, #14, #3, #15, #4, #16, #5, #17, #6, #18, #19 in 
that order

The items are clearly inserted in the incorrect order. To correct this I've 
copied AbstractInsertPanelDropController into another class and adjusted it 
slightly on line 54 (in the onDrop method):

dropTarget.insert(widget, dropIndex++);

It's the ++ which causes problems. Removing that the line becomes:

dropTarget.insert(widget, dropIndex);

This seems to work fine and is logically correct if you sit down and work out 
the indexes. Say we have 0, 1, 2, 3, 4, 5, 6, 7. We select 0, 1, 2 and drag 
after 6. 0 will need to be inserted before index 7. The insert call will then 
have moved 0 so then we have 1, 2, 3, 4, 5, 6, 0, 7. 1 will then need to be 
inserted before index 7. This applies all the way along the multi selected 
widgets.

Original issue reported on code.google.com by cknow...@gmail.com on 24 Sep 2010 at 8:14

GoogleCodeExporter commented 9 years ago
Sorry, that should have read "is logically *in this situation*"

A situation it doesn't work in is:
- Start with 012345678
- Select 12467 and drag to before 3
- Move 1 should give 021345678 => before 3
- Move 2 should give 012345678 => before 3
- Move 4 should give 012435678 => before 3
- Move 6 should give 012463578 => before 4
- Move 7 should give 012467358 => before 5

So the code needs to check the index of the widget it is moving first. If it is 
after the dropIndex then we need to increment the dropIndex after the insert.

So something a bit like:

int oldIndex = dropTarget.getWidgetIndex(widget);
dropTarget.insert(widget, dropIndex);
if (oldIndex > dropIndex) {
  dropIndex++;
}

I'll check that actually works soon. This may also solve issue 124 (not sure).

Original comment by cknow...@gmail.com on 24 Sep 2010 at 8:55