如何删除ComboViewer的元素?

时间:2016-10-21 12:25:38

标签: java eclipse plugins swt jface

我创建了一个包含Project类型对象的ComboViewer。当我选择一个项目时,与ComboViewer相同的复合元素将使用所选的项目更新它们的值。当我尝试删除组合框的一个元素时出现问题。我使用setInput方法更新传递给ComboViewer的列表,然后刷新它。之后,我在用户单击组合元素时定义要管理的addSelectionListener事件会自动触发,并检测到该元素为空,从而产生NullPointerException。如何在删除元素时删除组合留下的空白区域并删除下一个元素?

代码

我创建了一个事件来控制何时选择组合框的元素并更新复合材料的其他元素:

        projectNameTextCombo.addSelectionChangedListener(new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {
                IStructuredSelection selection = (IStructuredSelection) event
                        .getSelection();
                selectedProjectCombo = (Project) selection.getFirstElement(); 
                updateEditionElements(selectedProjectCombo);
            }
        });

我已经选择删除ComboViewer元素:

    Button deleteProjectConfigButton = new Button(pluginPathCreatedComposite, SWT.NONE);
    deleteProjectConfigButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            projectList.getProjectList().remove(selectedProjectCombo);
            projectNameTextCombo.refresh();
        }             

Project类代码如下:

public class Project {
    private String name;
    private String path;
    private Integer quantity;
    private List<ProjectDirectory> projectDirectoryList;

    //Constructor
    public Project(String name, String path, Integer quantity, List<ProjectDirectory> projectDirectoriesList) {
        super();
        this.name = name;
        this.path = path;
        this.quantity = quantity;
        this.projectDirectoryList = projectDirectoriesList;
    }

    public Project() {
        super();
        // TODO Auto-generated constructor stub
    }
    //Getters & setters
}

我已经定义了ComboViewer标签提供程序,内容提供程序和setInput数据:

    setContentProvider(ArrayContentProvider.getInstance());

    setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof Project && element != null) {
                Project current = (Project) element;
                return current.getName();
            }
            return super.getText(element);
        }
    }); 

要填充组合框我使用此功能:

private void populateCombo(){
    if(projectList != null){
        //Returns a Project list
        projectNameTextCombo.setInput(projectList.getProjectList());

        //The first time update other composite elements manually
        projectNameTextCombo.getCombo().select(0);
        selectedProjectCombo = (Project) projectNameTextCombo.getElementAt(0);
        updateEditionElements(selectedProjectCombo);
    }
}

为了更新其他复合元素,我使用了这个函数:

private void updateEditionElements(Project project){
    editProjectPathText.setText(project.getPath());
    projectCompositeViewer.getTable().setEnabled(true);
    projectCompositeViewer.setInput(project);
}

谢谢。

2 个答案:

答案 0 :(得分:0)

选择侦听器中的这些行假设总是有一个选择:

selectedProjectCombo = (Project) selection.getFirstElement(); 
updateEditionElements(selectedProjectCombo);

事实并非如此。当您删除某个条目时,该选择将被清除,getFirstElement将返回null,因此您需要检查是否确实存在选择:

selectedProjectCombo = (Project) selection.getFirstElement(); 
if (selectedProjectCombo != null) {
  updateEditionElements(selectedProjectCombo);
}
else {
  // TODO deal with empty selection
}

答案 1 :(得分:0)

我已经使用Greg的建议来设法删除我的comboViewer的元素。

代码

    projectNameTextCombo.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event
                    .getSelection();
            selectedProjectCombo = (Project) selection.getFirstElement();
            if (selectedProjectCombo != null) {
                updateEditionElements(selectedProjectCombo);
            }
            else {
                // Select the 0 position element
                projectNameTextCombo.getCombo().select(0);
                //Get the 0 element project of the comboviewer
                selectedProjectCombo = (Project) projectNameTextCombo.getElementAt(0);
                //If it is not null, update composite labels, else show information label
                if(selectedProjectCombo != null){
                    updateEditionElements(selectedProjectCombo);
                }else{
                    projectCreatedComposite.setVisible(false);
                    projectNotCreatedComposite.setVisible(true);
                    st.topControl = projectNotCreatedComposite;
                }
            }
        }
    });