SWT+ Jface,tableviewer : 如何在第三列设置组合框?

SWT+ Jface,tableviewer : How to set combo box in the 3rd column?

代码的当前输出是 output image

import java.io.Serializable;
import java.util.HashMap;
import java.util.Objects;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.part.ViewPart;


public class Theartview extends ViewPart implements Serializable {
Table table;
private TableViewer tableViewer;

public void createPartControl(Composite parent) {
    System.out.println("createPartControl call");



    Composite tableComposite = new Composite(parent, SWT.NONE);
    TableColumnLayout tableColumnLayout = new TableColumnLayout();
    tableComposite.setLayout(tableColumnLayout);
    tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
            true));

    tableViewer = new TableViewer(tableComposite, SWT.MULTI | SWT.H_SCROLL
            | SWT.V_SCROLL);
    tableViewer.setContentProvider(ArrayContentProvider.getInstance());
    // TODO viewer.setLabelProvider(new ViewLabelProvider());
    table = tableViewer.getTable();
    // Table table = tableViewer.getTable();
    System.out.println("#############table$$$$$$$$" + table);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    String[] titles = { "Threat Name", "Category Name", "Status",
            "Priority", "Description", "Justification" };

    for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
        TableViewerColumn tableViewerColumn = new TableViewerColumn(
                tableViewer, SWT.NONE);
        TableColumn tblclmn = tableViewerColumn.getColumn();
        tableColumnLayout.setColumnData(tblclmn, new ColumnPixelData(200,
                true, true));
        tblclmn.setText(titles[loopIndex]);
    }
 }



private static class Dummy {
    public String value;

    public Dummy(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}



    @SuppressWarnings("static-access")
    public void fillTableRoWData() {
    System.out.println("After connection fillTableRoWData call");
    System.out.println("No of Connected object = no of fillTableRoWData call ");
    if (Connection.Number_Of_Connection != 0) {
        // item = new TableItem(table, SWT.NONE);
        if (Service.class.isInstance(sourceNode)) {
            String id = "S1";
            shortDescription = threattypexmltoobject.shortdescription(id,
                    sourceNode.getName(), targetNode.getName(), null);

            category = "Spoofing";
            description = threattypexmltoobject.longdescription(id,
                    sourceNode.getName(), targetNode.getName(), null);

            fillRows(shortDescription, category, description);
        }

        if (Service.class.isInstance(sourceNode)
                && (connectionType == Connection.CONNECTION_DESIGN)) {
            String id = "T1";

            System.out.println(conn.getConnectionDesign());

            shortDescription = threattypexmltoobject.shortdescription(id,
                    sourceNode.getName(), targetNode.getName(),
                    conn.getConnectionDesign());
            category = "Tampering";
            description = threattypexmltoobject.longdescription(id,
                    sourceNode.getName(), targetNode.getName(),
                    conn.getConnectionDesign());
            fillRows(shortDescription, category, description);
        }
    }



private void fillRows(String shortdesc, String categ, String descp) {
    System.out.println("fillRows call from above method.");
    TableItem item = new TableItem(table, SWT.NONE);
    // for Threat_Name
    item.setText(0, "x");
     // For Category_Name
    item.setText(1, "y");
    // For Status_Name
    item.setText(2, "z");
    // For Priority_Name
    item.setText(3, "a");
    // For Descrption_Name
    item.setText(4, "b");
    // For justification
    item.setText(5, "c");
     }



public static class FirstValueEditingSupport extends EditingSupport {

    private final TableViewer viewer;
    private final CellEditor editor;

    private final String[] possibleValues = { "Mitigated",
            "Not Applicable", "Not Started", "Needs Investigation" };

    public FirstValueEditingSupport(TableViewer viewer) {
        super(viewer);
        this.viewer = viewer;
        this.editor = new ComboBoxCellEditor(viewer.getTable(),
                possibleValues);
    }

    @Override
    protected CellEditor getCellEditor(Object element) {
        return editor;
    }

    @Override
    protected boolean canEdit(Object element) {
        return true;
    }

    @Override
    protected Object getValue(Object element) {
        Dummy dummy = (Dummy) element;

        int index = 0;

        for (int i = 0; i < possibleValues.length; i++) {
            if (Objects.equals(possibleValues[i], dummy.getValue())) {
                index = i;
                break;
            }
        }

        return index;
    }

    @Override
    protected void setValue(Object element, Object value) {
        Dummy dummy = (Dummy) element;

        int index = (Integer) value;

        dummy.setValue(possibleValues[index]);

        viewer.update(element, null);
    }
}

}

问题

  1. 如何在 table 查看器的第 3 列和第 4 列设置组合框。这里 FirstValueEditingSupport 是组合框选项值,它是显示组合下拉列表
  2. String shortDescription, set into 1st and 2ed column 如何设置?

    in 输出图像 "Status" 和 "Priority",这是 table 的列名称,其中在 table 中设置了组合框。

一个问题请只问一个问题

您在 TableColumnViewer 中为要编辑的列设置了编辑支持。因此,对于要编辑的列,请执行以下操作:

tableViewerColumn.setEditingSupport(new FirstValueEditingSupport());

要设置每列中显示的数据,您需要设置该列的标签提供程序。

tableViewerColumn.setLabelProvider(column label provider);

其中标签提供者派生自 CellLabelProvider 或其众多子类之一。

注意:从不 通过创建 TableItem 来设置 table 查看器内容 - 您必须始终使用内容提供程序和 setInput调用 table 查看器。 TableViewer 完全控制底层 Table 并且可以自由丢弃您可能创建的任何 TableItem。除了极少数例外,在使用 TableViewer.

时,您永远不应该查看 TableTableItem 对象

内容提供者应为 table 中的每一行提供一个对象,列标签提供者应使用此行对象来获取各个列的标签。