我希望有一个表作为放置目标,因为它实际上与放置项目的位置相关。因此,用户可能希望在已经开始拖动后重新调整可见项目。
我缺少的默认行为是:将鼠标悬停在最后一条可见线上并向下滚动表格。将鼠标悬停在第一条可见线上并向上滚动。
所以我现在正在做的是尝试将用户将鼠标悬停在桌面中央的图标滚动:
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
// drag source: the label
final Label label = new Label(shell, SWT.NONE);
label.setText("Drag from me!");
label.setDragDetect(true);
label.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());
final DragSource source = new DragSource(label, DND.DROP_MOVE | DND.DROP_COPY);
source.setTransfer(new Transfer[]{TextTransfer.getInstance()});
source.addDragListener(new DragSourceAdapter() {
@Override
public void dragSetData(DragSourceEvent event) {
event.data = label.getText();
}
});
// drop target: the viewer
final TableViewer viewer = new TableViewer(shell,
SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setLabelProvider(new ColumnLabelProvider());
viewer.setInput(IntStream.range(1, 100).mapToObj(String::valueOf).collect(Collectors.toList()));
viewer.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(100, 100).create());
final DropTarget target = new DropTarget(viewer.getTable(), DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
target.setTransfer(new Transfer[]{TextTransfer.getInstance()});
target.addDropListener(new DropTargetAdapter() {
private static final int TIME_BETWEEN_MOVEMENTS = 700;
private long lastMovementTime;
@Override
public void dragOver(DropTargetEvent event) {
final Table table = viewer.getTable();
final TableItem item = table.getItem(table.toControl(new Point(event.x, event.y)));
if (item != null) {
final long currentTime = System.currentTimeMillis();
if (currentTime > this.lastMovementTime + TIME_BETWEEN_MOVEMENTS) {
table.setTopIndex(Math.max(0, table.indexOf(item) - getVisibleItems(table) / 2));
this.lastMovementTime = currentTime;
}
table.setSelection(item);
}
}
private int getVisibleItems(Table table) {
return (table.getClientArea().height - table.getHeaderHeight()) / table.getItemHeight();
}
});
// open everything
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
这可以快速滚动高桌而不是小桌子,所以我想知道是否有更好的方法,因为这是我使用的大部分软件中非常常见的用例。 / p>
如何在拖动时滚动表格?
答案 0 :(得分:2)
好的,我进行了一些实验并提出了一个至少应该给你一个良好起点的解决方案。它的作用是,它监听各种鼠标事件并记住已选择的项目。到达边缘(顶部或底部)时,它会将表格上下移动一个项目。
private static boolean dragging = false;
private static int start = -1;
private static int end = -1;
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
List<String> input = IntStream.range(1, 100)
.mapToObj(String::valueOf)
.collect(Collectors.toList());
final TableViewer viewer = new TableViewer(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setLabelProvider(new ColumnLabelProvider());
viewer.setInput(input);
Table table = viewer.getTable();
// Stop dragging when the mouse is released
table.addListener(SWT.MouseUp, e -> dragging = false);
// Start dragging when the mouse is clicked
table.addListener(SWT.MouseDown, e -> {
dragging = true;
start = -1;
end = -1;
});
// Listen to mouse movement
table.addListener(SWT.MouseMove, e -> {
// If we're currently dragging
if (dragging)
{
// Get the item under the mouse pointer
TableItem item = table.getItem(new Point(e.x, e.y));
if(item != null)
{
int current = table.indexOf(item);
// If this is the first selected item, remember the position
if (start == -1 && end == -1)
{
start = current;
end = current;
}
// Else, just remember the "end" of the drag
else
end = current;
// Tell the viewer to select the items
viewer.setSelection(new StructuredSelection(input.subList(Math.min(start, end), Math.max(start, end))));
// Now, we actually move the table when the drag movement reaches the top or bottom
int tableHeight = table.getClientArea().height;
int itemHeight = table.getItemHeight();
if (e.y < itemHeight && current > 0)
table.showItem(table.getItem(current - 1));
else if (e.y > tableHeight - itemHeight && current < table.getItemCount() - 1)
table.showItem(table.getItem(current + 1));
}
}
});
shell.pack();
shell.open();
shell.setSize(400, 300);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}