在表格中移动项目SWT/RCP

问题描述:

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

我希望通过按钮或通过拖放移动表格项目来向上或向下移动所选行。

我使用文摘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:无法初始化降

当我将在新的项目(表索引)被告知是项目移至它将是足够的,那么我可以更改我的对象列表并重新绘制表格。

任何想法如何解决这个问题?

问候, Haythem

我想你需要添加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; 
       } 
      }); 

我已经意识到这样的事情,我不知道但是如果我把你的问题的权利。一般来说,您必须修改模型并在模型中存储元素索引的信息。然后通过应用比较器以正确的顺序呈现列表。然后通过相应的拖放实现来处理模型的修改。通过这种方式,您可以实现重新排列行和为用户提供正确的可视化。

这是你的意思吗?

这里我有一个简单的代码来交换/移动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(); 
       } 
    });