简体   繁体   English

在表SWT / RCP中移动项目

[英]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? 有谁知道如何在SWT表中移动行? 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. 我想通过buttonklick上下移动选定的行或通过拖放移动表格项来实现此目的。

I am using eclips 3.6 and java 1.6 我正在使用Eclipse 3.6和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 org.eclipse.swt.SWTError:无法初始化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 问候,Haythem

here i have a simple code to swapping/moving row in RCP. 在这里,我有一个简单的代码可以在RCP中交换/移动行。 i using a button UP and down to swapping the row of table Viewer. 我使用向上和向下按钮交换表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. 将表的所选项目存储在temp变量中。

    then remove from the list. 然后从列表中删除。

    add temp variable into the list with index(+1 for down and -1 for up) 将临时变量添加到带有索引的列表中(向下为+1,向上为-1)

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

I think you need to add a dragSupport to the table viewer before adding a dropSupport. 我认为您需要在添加dropSupport之前向表查看器添加dragSupport。 You don't need to use a DragSource : 您不需要使用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? 这是你的意思吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM