public void handleListChange(ListChangeEvent event) {
   if (viewer.getControl().isDisposed()) {
     return;
   }
   ListDiffEntry[] differences = event.diff.getDifferences();
   for (int i = 0; i < differences.length; i++) {
     ListDiffEntry entry = differences[i];
     if (entry.isAddition()) {
       knownElements.add(entry.getElement());
       if (viewer instanceof AbstractListViewer) {
         ((AbstractListViewer) viewer).add(entry.getElement());
       } else {
         ((TableViewer) viewer).insert(entry.getElement(), entry.getPosition());
       }
     } else {
       if (viewer instanceof AbstractListViewer) {
         ((AbstractListViewer) viewer).remove(entry.getElement());
       } else {
         ((TableViewer) viewer).remove(entry.getElement());
       }
       knownElements.remove(entry.getElement());
     }
   }
 }
  @Test
  public void test() {
    WritableSet set = new WritableSet(TestRealm.getDefault());
    SetToListAdapter adapter = new SetToListAdapter(set, null);

    ListChangeListener listener = new ListChangeListener();
    adapter.addListChangeListener(listener);

    set.add("Huhu");
    ListDiffEntry e1 = listener.event.diff.getDifferences()[0];
    assertTrue(e1.isAddition());
    assertEquals("Huhu", e1.getElement());
    assertEquals(1, adapter.size());

    set.add("Haha");
    e1 = listener.event.diff.getDifferences()[0];
    assertTrue(e1.isAddition());
    assertEquals("Haha", e1.getElement());
    assertEquals(1, e1.getPosition());
    assertEquals(2, adapter.size());

    set.add("Blabla");
    e1 = listener.event.diff.getDifferences()[0];
    assertTrue(e1.isAddition());
    assertEquals("Blabla", e1.getElement());
    assertEquals(2, e1.getPosition());
    assertEquals(3, adapter.size());

    List<String> added = Arrays.asList("Blublu", "Foo", "Bar");
    set.addAll(added);
    e1 = listener.event.diff.getDifferences()[0];
    ListDiffEntry e2 = listener.event.diff.getDifferences()[1];
    ListDiffEntry e3 = listener.event.diff.getDifferences()[2];
    assertEquals(3, listener.event.diff.getDifferences().length);
    assertEquals(6, adapter.size());
    assertTrue(e1.isAddition());
    assertTrue(e2.isAddition());
    assertTrue(e3.isAddition());
    assertTrue(added.contains(e1.getElement()));
    assertTrue(added.contains(e2.getElement()));
    assertTrue(added.contains(e3.getElement()));
    assertTrue(e1.getPosition() >= 3 && e1.getPosition() <= 5);
    assertTrue(e2.getPosition() >= 3 && e1.getPosition() <= 5);
    assertTrue(e3.getPosition() >= 3 && e1.getPosition() <= 5);

    set.remove("Haha");
    e1 = listener.event.diff.getDifferences()[0];
    assertTrue(!e1.isAddition());
    assertEquals("Haha", e1.getElement());
    assertEquals(1, e1.getPosition());
    assertEquals(5, adapter.size());

    set.removeAll(added);
    e1 = listener.event.diff.getDifferences()[0];
    e2 = listener.event.diff.getDifferences()[1];
    e3 = listener.event.diff.getDifferences()[2];
    assertEquals(3, listener.event.diff.getDifferences().length);
    assertEquals(2, adapter.size());
    assertTrue(!e1.isAddition());
    assertTrue(!e2.isAddition());
    assertTrue(!e3.isAddition());
    assertTrue(added.contains(e1.getElement()));
    assertTrue(added.contains(e2.getElement()));
    assertTrue(added.contains(e3.getElement()));
    assertTrue(e1.getPosition() >= 2 && e1.getPosition() <= 4);
    assertTrue(e2.getPosition() >= 2 && e1.getPosition() <= 4);
    assertTrue(e3.getPosition() >= 2 && e1.getPosition() <= 4);
  }