2016-09-13 34 views
0

當前擺出來的代碼是 output imageSWT + Jface,tableviewer:如何在第三欄設置組合框?

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. 如何設置組合框在第3和表查看器的第4列。這裏FirstValueEditingSupport是combobox選項的值,它是顯示comobo下拉菜單
  2. String shortDescription,設置成第一列和第二列如何設置?

    輸出圖像「狀態」和「優先級」,它是在表中設置組合框的表的列名稱。

回答

0

請只問一個問題中的一個問題。

您可以在TableColumnViewer中爲要編輯的列設置編輯支持。所以,你要編輯的列做:

tableViewerColumn.setEditingSupport(new FirstValueEditingSupport()); 

要設置在您設置列的標籤提供每列中顯示的數據。

tableViewerColumn.setLabelProvider(column label provider); 

其中標籤提供由CellLabelProvider或它的許多子類之一的。

注:NEVER通過創建TableItem s設置表查看內容 - 你必須始終使用內容提供商和setInput呼叫表查看器。 TableViewer完全控制了潛在的Table,並且可以自由放棄您可能創建的任何TableItem。除了極少數例外,當使用TableViewer時,您絕不應該查看TableTableItem對象。

內容提供者應該爲表中的每一行提供一個對象,列標籤提供者應該使用該行對象來獲取單個列的標籤。

+0

非常感謝您的幫助。 –