Мне нужен номер строки для перетаскивания строк таблицы. Я решил это в другом проекте с Control и toControl с позицией. у event.item Tabelitem не было getSelectionIndex ()
, но теперь я получил ошибку - метод getParent () не определен для типа new DropTargetAdapter () {} - и я не нашелпричина
package table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class DragDropTable {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Table table = new Table(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
DragSource source = new DragSource(table, DND.DROP_MOVE | DND.DROP_COPY);
String[] buff = new String[2];
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("Zeile Tabelle " + table.getSelectionIndex());
for (int a=0; a<table.getColumnCount(); a++) {
buff[a]=selection[0].getText(a);
}
table.remove(table.getSelectionIndex());
event.data = buff.toString();
}
});
// Create the drop target
DropTarget target = new DropTarget(table, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
target.setTransfer(types);
target.addDropListener(new DropTargetAdapter() {
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY : DND.DROP_NONE;
}
// Allow dropping text only
for (int i = 0, n = event.dataTypes.length; i < n; i++) {
if (TextTransfer.getInstance().isSupportedType(event.dataTypes[i])) {
event.currentDataType = event.dataTypes[i];
}
}
}
public void dragOver(DropTargetEvent event) {
event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
}
public void drop(DropTargetEvent event) {
if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
// Get the dropped data
DropTarget target = (DropTarget) event.widget;
Table table = (Table) target.getControl();
String data = (String) event.data;
TableItem item = (TableItem) event.item;
System.out.println("row Ziel " + table.getSelectionIndex());
Control absolut = getParent().getDisplay().getFocusControl();
int row=1;
if (item != null){
row = getColumn(table,event.x + absolut.toControl(item.getBounds(0).x,item.getBounds(0).y).x);
}
TableItem insert = new TableItem(table, SWT.NONE, row);
insert.setText(0, buff[0]);
insert.setText(1, buff[1]);
table.redraw();
}
}
});
TableColumn column = new TableColumn(table, SWT.NONE);
TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
column.setWidth(100);
tblclmnNewColumn.setWidth(100);
// Seed the table
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0,"A");
item.setText(1,"AA");
item = new TableItem(table, SWT.NONE);
item.setText(0,"B");
item.setText(1,"BB");
item = new TableItem(table, SWT.BORDER);
item.setText(0,"C");
item.setText(1,"CC");
item = new TableItem(table, SWT.BORDER);
item.setText(0,"D");
item.setText(1,"DD");
item = new TableItem(table, SWT.BORDER);
item.setText(0,"E");
item.setText(1,"EE");
//column.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static int getColumn(Table table, int x)
{
int overallWidth = 0;
for(int i = 0; i < table.getColumnCount(); i++)
{
overallWidth += table.getColumn(i).getWidth();
System.out.println("overallWidth "+ overallWidth);
if(x < overallWidth)
{
return i;
}
}
return -1;
}
}