Пример #1
0
  public void testSetComparator() {
    final ITableRidget ridget = getRidget();
    final Grid control = getWidget();

    // sorts from a to z
    final Comparator<Object> comparator = new TypedComparator<String>();

    try {
      ridget.setComparator(-1, comparator);
      fail();
    } catch (final RuntimeException rex) {
      ok();
    }

    try {
      ridget.setComparator(2, comparator);
      fail();
    } catch (final RuntimeException rex) {
      ok();
    }

    ridget.setSortedAscending(true);

    final int lastItemIndex = control.getItemCount() - 1;

    assertEquals("John", control.getItem(0).getText(0)); // $NON-NLS-1$
    assertEquals("Frank", control.getItem(lastItemIndex).getText(0)); // $NON-NLS-1$

    ridget.setComparator(0, comparator);

    assertEquals("John", control.getItem(0).getText(0)); // $NON-NLS-1$
    assertEquals("Frank", control.getItem(lastItemIndex).getText(0)); // $NON-NLS-1$

    ridget.setSortedColumn(0);

    assertEquals("Frank", control.getItem(0).getText(0)); // $NON-NLS-1$
    assertEquals("John", control.getItem(lastItemIndex).getText(0)); // $NON-NLS-1$

    ridget.setComparator(0, null);

    assertEquals("John", control.getItem(0).getText(0)); // $NON-NLS-1$
    assertEquals("Frank", control.getItem(lastItemIndex).getText(0)); // $NON-NLS-1$

    ridget.setComparator(1, comparator);
    ridget.setSortedColumn(1);

    assertEquals("Doe", control.getItem(0).getText(1)); // $NON-NLS-1$
    assertEquals("Zappa", control.getItem(lastItemIndex).getText(1)); // $NON-NLS-1$

    ridget.setSortedAscending(false);

    assertEquals("Zappa", control.getItem(0).getText(1)); // $NON-NLS-1$
    assertEquals("Doe", control.getItem(lastItemIndex).getText(1)); // $NON-NLS-1$
  }
Пример #2
0
  public void testSetSortedAscending() {
    final Grid control = getWidget();
    final ITableRidget ridget = getRidget();

    ridget.bindToModel(
        manager,
        "persons",
        Person.class,
        new String[] {"lastname", "firstname"},
        null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    ridget.updateFromModel();
    final int lastItemIndex = control.getItemCount() - 1;

    assertEquals(-1, ridget.getSortedColumn());
    assertFalse(ridget.isSortedAscending());

    ridget.setComparator(0, new TypedComparator<String>());
    ridget.setSortedColumn(0);

    assertTrue(ridget.isSortedAscending());
    assertEquals("Doe", control.getItem(0).getText(0)); // $NON-NLS-1$
    assertEquals("Zappa", control.getItem(lastItemIndex).getText(0)); // $NON-NLS-1$

    ridget.setSortedAscending(false);

    assertFalse(ridget.isSortedAscending());
    assertEquals("Zappa", control.getItem(0).getText(0)); // $NON-NLS-1$
    assertEquals("Doe", control.getItem(lastItemIndex).getText(0)); // $NON-NLS-1$

    ridget.setSortedAscending(true);

    assertTrue(ridget.isSortedAscending());
    assertEquals("Doe", control.getItem(0).getText(0)); // $NON-NLS-1$
    assertEquals("Zappa", control.getItem(lastItemIndex).getText(0)); // $NON-NLS-1$

    ridget.setComparator(0, null);

    assertEquals(-1, ridget.getSortedColumn());
    assertFalse(ridget.isSortedAscending());
  }
Пример #3
0
  public void testIsColumnSortable() {
    final ITableRidget ridget = getRidget();

    try {
      assertFalse(ridget.isColumnSortable(-1));
      fail();
    } catch (final RuntimeException rex) {
      ok();
    }

    try {
      assertFalse(ridget.isColumnSortable(2));
      fail();
    } catch (final RuntimeException rex) {
      ok();
    }

    for (int i = 0; i < 2; i++) {
      assertFalse(ridget.isColumnSortable(i));

      // columns are sortable by default, when they have a comparator
      ridget.setComparator(i, new TypedComparator<String>());

      assertTrue(ridget.isColumnSortable(i));

      ridget.setColumnSortable(i, false);

      assertFalse(ridget.isColumnSortable(i));

      ridget.setColumnSortable(i, true);

      assertTrue(ridget.isColumnSortable(i));

      // columns are not sortable without a comparator
      ridget.setComparator(i, null);

      assertFalse(ridget.isColumnSortable(i));
    }
  }
Пример #4
0
  public void testGetSortedColumn() {
    final ITableRidget ridget = getRidget();

    try {
      ridget.setSortedColumn(2);
      fail();
    } catch (final RuntimeException rex) {
      ok();
    }

    assertEquals(-1, ridget.getSortedColumn());

    ridget.setComparator(0, new TypedComparator<String>());

    assertEquals(-1, ridget.getSortedColumn());

    ridget.setSortedColumn(0);

    assertEquals(0, ridget.getSortedColumn());

    ridget.setComparator(0, null);

    assertEquals(-1, ridget.getSortedColumn());

    ridget.setComparator(1, new TypedComparator<String>());
    ridget.setSortedColumn(1);

    assertEquals(1, ridget.getSortedColumn());

    ridget.setSortedColumn(-1);

    assertEquals(-1, ridget.getSortedColumn());

    // no comparator in column 0
    ridget.setSortedColumn(0);

    assertEquals(-1, ridget.getSortedColumn());
  }
Пример #5
0
  public void testColumnHeaderChangesSortability() {
    final ITableRidget ridget = getRidget();
    final Grid table = getWidget();

    ridget.setColumnSortable(0, true);
    ridget.setComparator(0, new TypedComparator<String>());
    ridget.setColumnSortable(1, true);
    ridget.setComparator(1, new TypedComparator<String>());

    ridget.setSortedColumn(-1);

    assertEquals(-1, ridget.getSortedColumn());
    assertFalse(ridget.isSortedAscending());

    final Event e = new Event();
    e.type = SWT.Selection;
    e.widget = table.getColumn(0);
    e.widget.notifyListeners(SWT.Selection, e);

    assertEquals(0, ridget.getSortedColumn());
    assertTrue(ridget.isSortedAscending());

    e.widget.notifyListeners(SWT.Selection, e);

    assertEquals(0, ridget.getSortedColumn());
    assertFalse(ridget.isSortedAscending());

    e.widget.notifyListeners(SWT.Selection, e);

    assertEquals(-1, ridget.getSortedColumn());
    assertFalse(ridget.isSortedAscending());

    e.widget.notifyListeners(SWT.Selection, e);

    assertEquals(0, ridget.getSortedColumn());
    assertTrue(ridget.isSortedAscending());
  }
Пример #6
0
  public void testGetOptionWithSorting() {
    final ITableRidget ridget = getRidget();

    assertEquals(0, ridget.indexOfOption(person1));
    assertEquals(person1, ridget.getOption(0));

    ridget.setComparator(1, new StringComparator());
    ridget.setSortedColumn(1); // sort by last name
    ridget.setSortedAscending(false);

    final int last = ridget.getOptionCount() - 1;
    assertEquals(last, ridget.indexOfOption(person1));
    assertEquals(person1, ridget.getOption(last));

    ridget.setSortedAscending(true);

    assertEquals(0, ridget.indexOfOption(person1));
    assertEquals(person1, ridget.getOption(0));
  }
Пример #7
0
  public void testSetSelectionWithSorting() {
    final ITableRidget ridget = getRidget();

    assertEquals(-1, ridget.getSelectionIndex());
    assertTrue(ridget.getSelection().isEmpty());

    ridget.setSelection(0);

    assertEquals(0, ridget.getSelectionIndex());
    assertEquals(person1, ridget.getSelection().get(0));

    ridget.setComparator(1, new StringComparator());
    ridget.setSortedColumn(1); // sort by last name
    ridget.setSortedAscending(false);

    final int last = ridget.getOptionCount() - 1;
    assertEquals(last, ridget.getSelectionIndex());
    assertEquals(person1, ridget.getSelection().get(0));

    ridget.setSortedAscending(true);

    assertEquals(0, ridget.getSelectionIndex());
    assertEquals(person1, ridget.getSelection().get(0));
  }