向表格单元格添加下拉列表

时间:2012-05-10 05:09:45

标签: eclipse-plugin eclipse-rcp

我在eclipse rcp中面临一些问题,用于设置特定单元格的下拉列表。 我的要求是在表格的第一行设置下拉列表。那个下拉列表也应该能够删除。 丢弃的另一件事应该是能够过滤表中的内容。所以我的问题是

1)是否可以仅将下拉列表添加到特定单元格或行? 2)该过滤器可以作为表的过滤器吗? 3)如果我将下拉列表添加到表格单元格中,如何删除?

1 个答案:

答案 0 :(得分:1)

是的,这完全有可能。我建议您先阅读Building and delivering a table editor with SWT/JFace,本教程包含您需要了解的所有内容。

作为粗略概述,您需要使内容模型中的第一项与数据项不同 - 它将存储过滤器值。然后在你的TableViewerColumn上设置编辑支持(这只是一个启动者 - 这段代码不能单独使用):

tableViewerColumn.setEditingSupport(new EditingSupport(tableViewer)
{
    @Override
    protected boolean canEdit(Object element) {
        if(object instanceof FilterDataObject) // your model object you are using to store the filter selections
        {
            return true;
        }
    }

    @Override
    protected CellEditor getCellEditor(Object element) 
    {               
        final ComboBoxCellEditor editor = new ComboBoxCellEditor(table, getPossibleFilterValues(), SWT.READ_ONLY);              
        ((CCombo)editor.getControl()).addModifyListener(new ModifyListener()
        {
            public void modifyText(ModifyEvent e) 
            {
                IStructuredSelection sel = (IStructuredSelection)m_tableViewer.getSelection();
                FilterDataObject filterValue = (FilterDataObject)sel.getFirstElement();
                // .. update the filter on your TableViewer
            }               
        });             
        return editor;
    }

    @Override
    protected Object getValue(Object element) 
    {
        if(object instanceof FilterDataObject) 
        {               
            // get the filter value
        }
        else
        {
            // get your data model's value for this column
        }
    }

    @Override
    protected void setValue(Object element, Object value) 
    {
        if(object instanceof FilterDataObject) 
        {
            // update your FilterDataObject
        }
    }       
});
相关问题