Moving Item in a table SWT/RCP
I have a problem with moving cell in a Table. Have someone an idea how to move rows in an SWT Table? I want to change the order by user interaction and I din't need to sort the entries.
I would like to achieve this in moving a selected row up or down by buttonklick or开发者_开发百科 with moving a table items by drag and drop.
I am using eclips 3.6 and java 1.6
This is what I try with Drag and Drop but not working:
Transfer[] types = new Transfer[] { LocalSelectionTransfer.getTransfer()};
DragSource source = new DragSource(table, DND.DROP_MOVE );
source.setTransfer(types);
source.addDragListener(new DragSourceAdapter() {
public void dragSetData(DragSourceEvent event) {
// Get the selected items in the drag source
DragSource ds = (DragSource) event.widget;
Table table = (Table) ds.getControl();
TableItem[] selection = table.getSelection();
System.out.println(" drag "+ selection[0].getText());
}
});
DropTarget target = new DropTarget(table, DND.DROP_MOVE | DND.DROP_DEFAULT);
target.setTransfer(types);
TableViewer tb = new TableViewer(table);
tb.addDropSupport(DND.DROP_MOVE, types, new ViewerDropAdapter(viewer) {
@Override
public boolean validateDrop(Object target, int operation,
TransferData transferType) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean performDrop(Object data) {
// TODO Auto-generated method stub
return false;
}
});
The Item that I would like to move have more then a Column.
The error that I became is :
org.eclipse.swt.SWTError: Cannot initialize Drop
When I will be informed in which new Item (the index in Table) is the item moved it will be sufficient then I can change the List of my objects and redrew the table.
Any idea how to slove this problem?.
Regards, Haythem
I think you need to add a dragSupport to the table viewer before adding a dropSupport. You don't need to use a DragSource :
TableViewer viewer = new TableViewer(table);
Transfer[] types = new Transfer[] { PluginTransfer.getInstance() };
viewer.addDragSupport(DND.DROP_MOVE, types, new DragSourceAdapter() {
@Override
public void dragSetData(DragSourceEvent event) {
// Get the selected items in the drag source
DragSource ds = (DragSource) event.widget;
Table table = (Table) ds.getControl();
TableItem[] selection = table.getSelection();
System.out.println(" drag " + selection[0].getText());
}
});
viewer.addDropSupport(DND.DROP_MOVE, types, new ViewerDropAdapter(viewer) {
@Override
public boolean validateDrop(Object target, int operation, TransferData transferType) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean performDrop(Object data) {
// TODO Auto-generated method stub
return false;
}
});
I have realized something like that, I am not sure however if I got your question right. Generally you have to modify your model and store the information of the index of the element in your model too. The list is then presented in the right order by applying a Comparator. The modification of the model is then handled by the respective Drag/Drop implementation. In this way you can realize the re-arranging of rows and the correct visualisation to the user.
Is this what you meant?
here i have a simple code to swapping/moving row in RCP. i using a button UP and down to swapping the row of table Viewer.
I added a selection listener on my button.
take the selected item index in the table.
save the original input of table viewer in a list.
stored the selected item of table in temp variable.
then remove from the list.
add temp variable into the list with index(+1 for down and -1 for up)
example:-
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectionIndex = TableViewer.getTable().getSelectionIndex();
EObjectContainmentEList<Object> input = (EObjectContainmentEList<Object>) TableViewer.getInput();
Attribute basicGet = input.basicGet(selectionIndex);
input.remove(selectionIndex);
input.add(selectionIndex-1, basicGet);
TableViewer.setInput(input);
TableViewer.refresh();
}
});
精彩评论