Search code examples
comboboxswtjfacetableviewer

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


Current out put of code is 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);
    }
}

}

Questions

  1. How to set combo box in the 3rd and 4th column of table viewer. here FirstValueEditingSupport is combobox options value which is display comobo dropdown
  2. String shortDescription, set into 1st and 2ed column how to set ?

    in Output image "Status" and "Priority", which is column name of the table where set combo box in the table.


Solution

  • Please only ask one question in a question.

    You set the editing support in the TableColumnViewer for the column you want to edit. So for the columns you want to edit do:

    tableViewerColumn.setEditingSupport(new FirstValueEditingSupport());
    

    To set the data shown in each column you set a label provider of the column.

    tableViewerColumn.setLabelProvider(column label provider);
    

    where the label provider is derived from CellLabelProvider or one of its many subclasses.

    Note: NEVER set the table viewer contents by creating TableItems - you must always use a content provider and a setInput call on the table viewer. TableViewer is in complete control of the underlying Table and is free to discard any TableItems you may create. With very few exceptions you should never be looking at the Table or TableItem objects when using TableViewer.

    The content provider should provide one object for each row in the table, the column label providers should use this row object to get labels for the individual columns.