示例#1
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());
  }
  public LogEntryTable(Composite parent, int style) {
    super(parent, style);
    setLayout(new GridLayout(1, false));

    addPropertyChangeListener(
        new PropertyChangeListener() {

          @Override
          public void propertyChange(PropertyChangeEvent event) {
            switch (event.getPropertyName()) {
              case "logEntries":
                gridTableViewer.setSelection(null, true);
                gridTableViewer.setInput(logEntries.toArray(new LogEntry[logEntries.size()]));
                break;
              case "expanded":
                // TODO shroffk fix the refresh
                gridTableViewer.getGrid().setAutoHeight(expanded);
                gridTableViewer.setInput(logEntries.toArray(new LogEntry[logEntries.size()]));
                break;
              default:
                break;
            }
          }
        });

    errorBar = new ErrorBar(this, SWT.NONE);
    errorBar.setMarginBottom(5);

    gridTableViewer =
        new GridTableViewer(this, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.DOUBLE_BUFFERED);
    selectionProvider =
        new AbstractSelectionProviderWrapper(gridTableViewer, this) {

          @Override
          protected ISelection transform(IStructuredSelection selection) {
            return selection;
          }
        };

    grid = gridTableViewer.getGrid();
    grid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    grid.setAutoHeight(true);
    grid.setRowsResizeable(true);
    grid.setHeaderVisible(true);
    gridTableViewer.getGrid().setLinesVisible(true);
    gridTableViewer.setContentProvider(
        new IStructuredContentProvider() {

          @Override
          public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}

          @Override
          public void dispose() {}

          @Override
          public Object[] getElements(Object inputElement) {
            return (Object[]) inputElement;
          }
        });

    ColumnViewerToolTipSupport.enableFor(gridTableViewer, ToolTip.NO_RECREATE);

    // First Columns displays the Date
    GridViewerColumn column = new GridViewerColumn(gridTableViewer, SWT.NONE);
    GridColumn gridColumn = column.getColumn();
    gridColumn.setMoveable(true);
    column.setLabelProvider(
        new CellLabelProvider() {

          @Override
          public void update(ViewerCell cell) {
            LogEntry item = ((LogEntry) cell.getElement());
            String date =
                item == null || item.getCreateDate() == null
                    ? "No Data"
                    : DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
                        .format(item.getCreateDate());
            cell.setText(date);
          }
        });
    column.getColumn().setText("Date");
    column.getColumn().setWordWrap(true);
    new TableColumnViewerLayout(gridTableViewer, column, 15, 100);
    new ColumnViewerSorter(gridTableViewer, column) {
      @Override
      protected int doCompare(Viewer viewer, Object e1, Object e2) {
        return Long.compare(
            ((LogEntry) e1).getCreateDate().getTime(), ((LogEntry) e2).getCreateDate().getTime());
      }
    };

    // Second column is the first line of the logEntry

    GridViewerColumn gridViewerColumnDescription =
        new GridViewerColumn(gridTableViewer, SWT.DOUBLE_BUFFERED);
    gridViewerColumnDescription.setLabelProvider(
        new ColumnLabelProvider() {

          public String getText(Object element) {
            LogEntry item = ((LogEntry) element);
            return item == null ? "" : item.getText();
          }
        });
    GridColumn tblclmnDescription = gridViewerColumnDescription.getColumn();
    tblclmnDescription.setWordWrap(true);
    tblclmnDescription.setText("Description");
    new TableColumnViewerLayout(gridTableViewer, gridViewerColumnDescription, 50, 250);

    // Third column is the owner of the logEntry

    GridViewerColumn gridViewerColumnOwner =
        new GridViewerColumn(gridTableViewer, SWT.MULTI | SWT.WRAP | SWT.DOUBLE_BUFFERED);
    gridViewerColumnOwner.setLabelProvider(
        new ColumnLabelProvider() {
          public String getText(Object element) {
            LogEntry item = ((LogEntry) element);
            return item == null ? "" : item.getOwner();
          }
        });

    // gridViewerColumnOwner.getColumn().setSort(SWT.UP);
    GridColumn tblclmnOwner = gridViewerColumnOwner.getColumn();
    tblclmnOwner.setText("Owner");
    new TableColumnViewerLayout(gridTableViewer, gridViewerColumnOwner, 10, 75);
    new ColumnViewerSorter(gridTableViewer, gridViewerColumnOwner) {
      @Override
      protected int doCompare(Viewer viewer, Object e1, Object e2) {
        return ((LogEntry) e1).getOwner().compareTo(((LogEntry) e2).getOwner());
      }
    };

    // Forth column lists the logbooks
    GridViewerColumn gridViewerColumnLogbooks =
        new GridViewerColumn(gridTableViewer, SWT.MULTI | SWT.DOUBLE_BUFFERED);
    gridViewerColumnLogbooks.setLabelProvider(
        new ColumnLabelProvider() {

          @Override
          public String getText(Object element) {
            LogEntry item = ((LogEntry) element);
            StringBuilder logbooks = new StringBuilder();
            for (Logbook logbook : item.getLogbooks()) {
              logbooks.append(logbook.getName() + eol);
            }
            return item == null ? "" : logbooks.toString();
          }
        });
    GridColumn tblclmnLogbooks = gridViewerColumnLogbooks.getColumn();
    tblclmnLogbooks.setWordWrap(false);
    tblclmnLogbooks.setText("Logbooks");
    new TableColumnViewerLayout(gridTableViewer, gridViewerColumnLogbooks, 10, 75);

    // column lists the tags
    GridViewerColumn gridViewerColumnTags =
        new GridViewerColumn(gridTableViewer, SWT.DOUBLE_BUFFERED);
    gridViewerColumnTags.setLabelProvider(
        new ColumnLabelProvider() {

          public String getText(Object element) {
            LogEntry item = ((LogEntry) element);
            StringBuilder tags = new StringBuilder();
            for (Tag tag : item.getTags()) {
              tags.append(tag.getName() + eol);
            }
            return item == null ? "" : tags.toString();
          }
        });
    GridColumn tblclmnTags = gridViewerColumnTags.getColumn();
    tblclmnTags.setWordWrap(false);
    tblclmnTags.setText("Tags");
    new TableColumnViewerLayout(gridTableViewer, gridViewerColumnTags, 10, 75);

    // Attachments
    GridViewerColumn gridViewerColumnAttachments =
        new GridViewerColumn(gridTableViewer, SWT.DOUBLE_BUFFERED);
    gridViewerColumnAttachments.setLabelProvider(
        new ColumnLabelProvider() {

          @Override
          public String getText(Object element) {
            LogEntry item = ((LogEntry) element);
            return String.valueOf(item.getAttachment().size());
          }
        });
    GridColumn tblclmnAttachment = gridViewerColumnAttachments.getColumn();
    tblclmnAttachment.setText("Attachments");
    new TableColumnViewerLayout(gridTableViewer, gridViewerColumnAttachments, 5, 30);

    new ColumnViewerSorter(gridTableViewer, gridViewerColumnAttachments) {
      @Override
      protected int doCompare(Viewer viewer, Object e1, Object e2) {
        return Integer.compare(
            ((LogEntry) e1).getAttachment().size(), ((LogEntry) e2).getAttachment().size());
      }
    };
    gridTableViewer.refresh();
  }