예제 #1
0
  /**
   * ************************************************************************* Create the table
   * columns ************************************************************************
   */
  protected void createColumns() {
    for (CodeViewerColumn colModel : CodeViewerColumn.values()) {
      // Create the TableColumn with right alignment
      int style = colModel.getAlignment() | SWT.H_SCROLL;
      GridViewerColumn viewerColumn = new GridViewerColumn(this, style);

      GridColumn column = viewerColumn.getColumn();
      column.setText(colModel.getName());
      column.setAlignment(colModel.getAlignment());
      column.setWidth(colModel.getInitialWidth());
      column.setResizeable(colModel.isResizable());
    }
  }
  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();
  }