在表 SWT/RCP 中移动项目

发布于 2024-11-02 04:23:13 字数 1556 浏览 2 评论 0原文

我在移动表格中的单元格时遇到问题。 有人知道如何在 SWT 表中移动行吗?我想按用户更改订单 交互,我不需要对条目进行排序。

我想通过单击按钮向上或向下移动选定的行或通过拖放移动表格项目来实现此目的。

我正在使用 eclips 3.6 和 java 1.6

这是我尝试拖放但不起作用的方法:

 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;
        }
    });

我想要移动的项目不止一个列。

我遇到的错误是:

org.eclipse.swt.SWTError:无法初始化 Drop

当我被告知哪个新项目(表中的索引)是移动的项目时,这就足够了,然后我可以更改我的对象列表并重新绘制了表格。

知道如何解决这个问题吗?

问候, 海瑟姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-11-09 04:23:14

这里我有一个简单的代码来交换/移动 RCP 中的行。我使用向上和向下按钮来交换表查看器的行。

  • 我在按钮上添加了一个选择侦听器。

    获取表中所选项目的索引。

    将表格查看器的原始输入保存在列表中。

    将表的选定项目存储在临时变量中。

    然后从列表中删除。

    将临时变量添加到带有索引的列表中(+1表示向下,-1表示向上)

示例:-

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();
                }
    });

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();
                }
    });
要走就滚别墨迹 2024-11-09 04:23:13

我认为您需要在添加 dropSupport 之前向表查看器添加 DragSupport。您不需要使用 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 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 2024-11-09 04:23:13

我已经意识到类似的事情,但我不确定我是否正确回答了你的问题。通常,您必须修改模型并将元素的索引信息也存储在模型中。然后通过应用比较器以正确的顺序显示该列表。然后模型的修改由相应的拖/放实现来处理。通过这种方式,您可以实现行的重新排列以及向用户的正确可视化。

这是你的意思吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文