jkteater
jkteater

Reputation: 1391

Listening for changes in WritableList

Data Model Class

Creating a IObservableList

  1. IObservableList observableList = new WritableList();
  2. Add Method to add data to observableList
  3. Method to return observableList

Table Viewer Class

  1. Set content Provider to ObservableListContentProvider

GUI Class

  1. Viewer setInput to get the ObservableList from the Data Model Class

Everything seems to be working as desired, the table viewer updates with the changes to the ObservableList.

But the issue I am having is how to update my buttons in the GUI. I have a few buttons that enable and and disable according to the count of objects in the ObservableList in the Data Model. In order to run that logic, I have to know when there is a change in the list.

I have tried to implement IListChangeListener in the GUI class. Then add the method

public void handleListChange(ListChangeEvent arg0) {
  updateButtons();  <-- My method to run the logic

}

This is not working, should I be implementing something else instead of IListChangeListener?

I am not sure what I should implement in the GUI class to listen for the changes?

EDIT

Do I have to add a listener to the viewer?

viewer = new AplotDataTableViewer(parent, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
viewer.setInput(AplotDataModel.getInstance().getObservableList());
viewer.addListener(etc... )

This is the only option I have

 addSelectionChangedListener(ISelectionChangeListener listener)

I don't care about selection changed - only if the list has changed.

Upvotes: 1

Views: 1156

Answers (1)

Jan Krakora
Jan Krakora

Reputation: 2610

I have tried this example and that works for me:

IObservableList observableList = new WritableList();
observableList.addListChangeListener(new IListChangeListener() {

    @Override
    public void handleListChange(ListChangeEvent event) {
        System.out.println(event.diff);
    }
});
observableList.add("element");
observableList.add("element");
observableList.add("element");

TableViewer viewer = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL);
viewer.getTable().setHeaderVisible(true);
viewer.getTable().setHeaderVisible(true);
viewer.setContentProvider(new ObservableListContentProvider());

TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
column.getColumn().setText("Column");
column.getColumn().setWidth(200);

column.setLabelProvider(new ColumnLabelProvider());

// Provide the input to the ContentProvider
viewer.setInput(observableList);

Now the IListChangeListener is called three times when application starts and then every time I add an element to the observableList.

Upvotes: 3

Related Questions