Jaime Jaime - 1 month ago 4
Java Question

How to delete elements of a ComboViewer?

I have created a ComboViewer that contains Project type objects. When I select a Project, the elements of the same composite as the ComboViewer one update their values with the selected Project ones. The problem comes when I try to delete one element of the combobox. I update the list I pass to the ComboViewer with the setInput method and I refresh it. After that, the addSelectionListener event that I have defined to manage when the user clicks an element of the combo automatically triggers and it detects that the element is null thowing a NullPointerException. How can I make to delete the empty space that the combo leaves when I delete an element and to take the next one?

Code

I have created an event to control when an element of the combobox is selected and to update the other elements of the composite:

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


I have given the option to delete ComboViewer element:

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


The Project class code is the following:

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
}


I have defined the ComboViewer label provider, the content provider and the setInput data:

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);
}
});


To populate the combobox I use this function:

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);
}
}


And to update the other composite elements I use this function:

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


Thank you.

Answer

I have used GregĀ“s advice to manage to delete the elements of my comboViewer.

Code

    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;
                }
            }
        }
    });
Comments