Ejemplo n.º 1
0
  public void testUpdateFromModel() {
    final ITableRidget ridget = getRidget();
    final Table control = getWidget();
    final List<ChangeEvent> changeEvents = new ArrayList<ChangeEvent>();
    final IChangeListener listener =
        new IChangeListener() {
          public void handleChange(final ChangeEvent event) {
            changeEvents.add(event);
          }
        };
    ridget.getObservableList().addChangeListener(listener);

    final int oldCount = manager.getPersons().size();

    assertEquals(oldCount, ridget.getObservableList().size());
    assertEquals(oldCount, control.getItemCount());

    manager.getPersons().remove(person1);

    final int newCount = oldCount - 1;

    assertEquals(newCount, manager.getPersons().size());
    assertEquals(oldCount, ridget.getObservableList().size());
    assertEquals(oldCount, control.getItemCount());
    assertEquals(0, changeEvents.size());

    ridget.updateFromModel();

    assertEquals(newCount, manager.getPersons().size());
    assertEquals(newCount, ridget.getObservableList().size());
    assertEquals(newCount, control.getItemCount());
    // TODO [ev] discuss with team
    //		assertEquals(1, changeEvents.size());
    assertEquals(0, changeEvents.size());
  }
Ejemplo n.º 2
0
  /** Bug 301682 */
  public void testSetColumnWidthsRelative() {
    final ITableRidget ridget = createRidget();
    final Table table = createWidget(getShell());
    ridget.setUIControl(table);

    final String[] columns3 = {
      Person.PROPERTY_FIRSTNAME, Person.PROPERTY_LASTNAME, Person.PROPERTY_BIRTHDAY
    };
    ridget.bindToModel(manager, "persons", Person.class, columns3, null); // $NON-NLS-1$
    ridget.updateFromModel();

    ridget.setColumnWidths(
        new Object[] {
          new ColumnWeightData(1, 1, true),
          new ColumnWeightData(2, 1, true),
          new ColumnWeightData(3, 1, true)
        });

    assertTrue(
        "column 0 should be smaller than column 1",
        table.getColumn(0).getWidth() < table.getColumn(1).getWidth()); // $NON-NLS-1$
    assertTrue(
        "column 1 should be smaller than column 2",
        table.getColumn(1).getWidth() < table.getColumn(2).getWidth()); // $NON-NLS-1$
  }
Ejemplo n.º 3
0
  /** As per bug 301182 */
  public void testRefresh() {
    final ITableRidget ridget = createRidget();
    final Table control = createWidget(getShell());
    ridget.setUIControl(control);

    final StringPojo word1 = new StringPojo("eclipse"); // $NON-NLS-1$
    final StringPojo word2 = new StringPojo("riena"); // $NON-NLS-1$
    final WritableList values = new WritableList(Arrays.asList(word1, word2), StringPojo.class);
    final String[] columns = {"value"}; // $NON-NLS-1$
    ridget.bindToModel(values, StringPojo.class, columns, null);
    ridget.updateFromModel();

    assertEquals("eclipse", control.getItem(0).getText()); // $NON-NLS-1$
    assertEquals("riena", control.getItem(1).getText()); // $NON-NLS-1$

    word1.setValue("alpha"); // $NON-NLS-1$
    word2.setValue("beta"); // $NON-NLS-1$

    assertEquals("eclipse", control.getItem(0).getText()); // $NON-NLS-1$
    assertEquals("riena", control.getItem(1).getText()); // $NON-NLS-1$

    ridget.refresh(word1);

    assertEquals("alpha", control.getItem(0).getText()); // $NON-NLS-1$
    assertEquals("riena", control.getItem(1).getText()); // $NON-NLS-1$
  }
Ejemplo n.º 4
0
  public void testSetColumnFormatter() {
    final ITableRidget ridget = getRidget();
    final Grid table = getWidget();
    final IColumnFormatter formatter =
        new ColumnFormatter() {
          @Override
          public String getText(final Object element) {
            final Person person = (Person) element;
            return person.getLastname().toUpperCase();
          }
        };
    final String lastName = person1.getLastname();
    final String lastNameUpperCase = lastName.toUpperCase();

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

    try {
      ridget.setColumnFormatter(99, formatter);
      fail();
    } catch (final RuntimeException rex) {
      ok();
    }

    ridget.setColumnFormatter(1, formatter);

    assertEquals(lastName, table.getItem(0).getText(1));

    ridget.updateFromModel();

    assertEquals(lastNameUpperCase, table.getItem(0).getText(1));

    ridget.setColumnFormatter(1, null);

    assertEquals(lastNameUpperCase, table.getItem(0).getText(1));

    ridget.updateFromModel();

    assertEquals(lastName, table.getItem(0).getText(1));
  }
Ejemplo n.º 5
0
  public void testUpdateFromModelRemovesSelection() {
    final ITableRidget ridget = getRidget();

    ridget.setSelection(person2);

    assertSame(person2, ridget.getSelection().get(0));

    manager.getPersons().remove(person2);
    ridget.updateFromModel();

    assertTrue(ridget.getSelection().isEmpty());
  }
Ejemplo n.º 6
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());
  }
Ejemplo n.º 7
0
  public void testContainsOption() {
    final ITableRidget ridget = getRidget();

    assertTrue(ridget.containsOption(person1));
    assertTrue(ridget.containsOption(person2));
    assertTrue(ridget.containsOption(person3));

    assertFalse(ridget.containsOption(null));
    assertFalse(ridget.containsOption(new Person("", ""))); // $NON-NLS-1$ //$NON-NLS-2$

    final java.util.List<Person> persons = Arrays.asList(new Person[] {person3});
    final PersonManager manager = new PersonManager(persons);
    ridget.bindToModel(
        manager,
        "persons",
        Person.class,
        new String[] {"firstname", "lastname"},
        null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    ridget.updateFromModel();

    assertFalse(ridget.containsOption(person1));
    assertTrue(ridget.containsOption(person3));
  }
Ejemplo n.º 8
0
  /** As per Bug 295305 */
  public void testPreserveColumnWidths() {
    final int[] widths = {50, 100, 150};
    final ITableRidget ridget = createRidget();
    final Table control = new Table(getShell(), SWT.FULL_SELECTION | SWT.SINGLE);
    for (final int width : widths) {
      final TableColumn column = new TableColumn(control, SWT.NONE);
      column.setWidth(width);
    }
    ridget.setUIControl(control);

    final String[] columns3 = {
      Person.PROPERTY_FIRSTNAME, Person.PROPERTY_LASTNAME, Person.PROPERTY_BIRTHDAY
    };
    ridget.bindToModel(manager, "persons", Person.class, columns3, null); // $NON-NLS-1$
    ridget.updateFromModel();

    for (int i = 0; i < 3; i++) {
      final int actual = control.getColumn(i).getWidth();
      final String msg =
          String.format("col #%d, exp:%d, act:%d", i, widths[i], actual); // $NON-NLS-1$
      assertEquals(msg, widths[i], actual);
    }
  }
Ejemplo n.º 9
0
  @Override
  public void testAddClickListener() {
    final ITableRidget ridget = getRidget();
    ridget.updateFromModel();
    final Grid control = getWidget();
    final RowData rowData = new RowData();
    rowData.width = 300;
    rowData.height = 300;
    control.setLayoutData(rowData);
    control.getColumn(0).setWidth(100);
    control.getColumn(1).setWidth(100);
    control.getShell().pack();
    control.getParent().update();

    try {
      ridget.addClickListener(null);
      fail();
    } catch (final RuntimeException npe) {
      ok();
    }

    final FTClickListener listener1 = new FTClickListener();
    ridget.addClickListener(listener1);

    final FTClickListener listener2 = new FTClickListener();
    ridget.addClickListener(listener2);
    ridget.addClickListener(listener2);

    final Event mdEvent = new Event();
    mdEvent.widget = control;
    mdEvent.type = SWT.MouseDown;
    mdEvent.button = 2;
    final GridItem row2 = control.getItem(2);
    mdEvent.item = row2;
    mdEvent.x = row2.getBounds(0).x + 1;
    mdEvent.y = row2.getBounds(0).y + 1;
    control.notifyListeners(SWT.MouseDown, mdEvent);

    assertEquals(0, listener1.getCount());
    assertEquals(0, listener2.getCount());

    mdEvent.type = SWT.MouseUp;
    control.notifyListeners(SWT.MouseUp, mdEvent);

    assertEquals(1, listener1.getCount());
    assertEquals(1, listener2.getCount());

    final ClickEvent event = listener2.getEvent();
    assertEquals(ridget, event.getSource());
    assertEquals(2, event.getButton());
    assertEquals(0, event.getColumnIndex());
    assertEquals(row2.getData(), event.getRow());

    ridget.removeClickListener(listener1);

    mdEvent.type = SWT.MouseDown;
    control.notifyListeners(SWT.MouseDown, mdEvent);
    mdEvent.type = SWT.MouseUp;
    control.notifyListeners(SWT.MouseUp, mdEvent);

    assertEquals(1, listener1.getCount());
  }