Ejemplo n.º 1
0
  /**
   * Creates table viewer and builds table contents
   *
   * @param parent the parent composite
   */
  private void createTable(Composite parent) {
    tableViewer =
        new TableViewer(
            parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    // setup table widget
    tableViewer.setUseHashlookup(true);
    Table table = tableViewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    GridData data = new GridData(GridData.FILL_BOTH);
    data.grabExcessHorizontalSpace = true;
    data.grabExcessVerticalSpace = true;
    data.horizontalSpan = 1;
    data.widthHint = convertWidthInCharsToPixels(30);
    table.setLayoutData(data);
    table.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event e) {
            handleSelection();
          }
        });

    // Add the columns to the table
    final String[] columnNames = new String[collectionColumns.size()];
    int i = 0;
    Iterator iter = collectionColumns.iterator();
    while (iter.hasNext()) {
      SortFilterCollectionColumn columnInfo = (SortFilterCollectionColumn) iter.next();
      TableColumn column = new TableColumn(table, columnInfo.getAlignment());
      column.setText(columnInfo.getCaption());
      column.setResizable(columnInfo.isResizable());
      column.setWidth(columnInfo.getWidth());
      column.setAlignment(columnInfo.getAlignment());
      if (columnInfo.getColumnSorter() != null) {
        column.addSelectionListener(
            new HeaderSelectionListener(
                (SortFilterViewerSorter) columnInfo.getColumnSorter(), tableViewer));
        // Intialize the table sorter
        if (_sortColumn != null && column.getText().equals(_sortColumn)) {
          SortFilterViewerSorter sorter = (SortFilterViewerSorter) columnInfo.getColumnSorter();
          if (sorter != null) {
            if (SortingDirection.ASCENDING_LITERAL.equals(_sortingDirection)) {
              // Use the ascending image
              Image image = DiagramUIPluginImages.DESC_SORT_ARROW_UP.createImage();
              column.setImage(image);
              column.pack();
            } else if (SortingDirection.DESCENDING_LITERAL.equals(_sortingDirection)) {
              sorter.toggleSortingDirection();
              // Use the descending image
              Image image = DiagramUIPluginImages.DESC_SORT_ARROW_DN.createImage();
              column.setImage(image);
              column.pack();
            }
          }
          tableViewer.setSorter(sorter);
        }
      }
      columnNames[i++] = columnInfo.getCaption();
    }
    // setup table viewer
    tableViewer.setContentProvider(new SortFilterContentProvider());
    if (labelProvider != null) tableViewer.setLabelProvider(labelProvider);
    tableViewer.setColumnProperties(columnNames);
    tableViewer.setCellModifier(new SortFilterCellModifier());

    // Can only changes the first column - the visible column
    CellEditor[] editors = new CellEditor[collectionColumns.size()];
    editors[0] = new CheckboxCellEditor(table);
    for (i = 1; i < collectionColumns.size(); i++) {
      editors[i] = null;
    }
    tableViewer.setCellEditors(editors);

    if (elementCollection != null && !elementCollection.isEmpty())
      tableViewer.setInput(elementCollection);

    // Update the model
    TableItem[] tableItems = tableViewer.getTable().getItems();
    List newModel = new ArrayList();
    for (i = 0; i < tableItems.length; i++) {
      SortFilterElement ey = (SortFilterElement) tableItems[i].getData();
      newModel.add(i, ey);
    }
    tableViewer.setInput(newModel);
    elementCollection = newModel;
    createBackUp();
    // pack column widths
    TableColumn[] tableColumns = table.getColumns();
    for (i = 0; i < tableColumns.length; i++) {
      tableColumns[i].pack();
    }
  }
Ejemplo n.º 2
0
    private void handleEvent(SelectionEvent e) {
      // Clear any existing header images
      TableColumn[] columns = _tableViewer.getTable().getColumns();
      for (int i = 0; i < columns.length; i++) {
        columns[i].setImage(null);
      }
      // Add the image based on the sort
      int columnIndex = 0;
      if (e.widget instanceof TableColumn) {
        TableColumn column = (TableColumn) e.widget;
        columnIndex = findColumnIndexFromProperty(column.getText());
        if (columnIndex != -1) {
          Image image = null;
          SortFilterViewerSorter newSorter = (SortFilterViewerSorter) _tableViewer.getSorter();
          if (newSorter != null) {
            newSorter.toggleSortingDirection();
            if (newSorter == null
                || SortingDirection.ASCENDING_LITERAL.equals(newSorter.getSortingDirection())) {
              // Use the ascending image
              image = DiagramUIPluginImages.DESC_SORT_ARROW_UP.createImage();
            } else {
              // Use the descending image
              image = DiagramUIPluginImages.DESC_SORT_ARROW_DN.createImage();
            }
            columns[columnIndex].setImage(image);
          }
        }
      }

      if (sorter != null) {
        // Does the actual sorting
        _tableViewer.setSorter(null);
        // Necessary to use ascending/descending sorting
        _tableViewer.setSorter(getSorter());

        // Update the model
        TableItem[] tableItems = _tableViewer.getTable().getItems();
        List newModel = new ArrayList();
        for (int i = 0; i < tableItems.length; i++) {
          SortFilterElement ey = (SortFilterElement) tableItems[i].getData();
          newModel.add(i, ey);
        }
        elementCollection.clear();
        elementCollection.addAll(newModel);
        _tableViewer.refresh();

        _sorting = Sorting.AUTOMATIC_LITERAL;
        Object[] columnNames = tableViewer.getColumnProperties();
        if (_sortingKeys != Collections.EMPTY_MAP) {
          _sortingKeys.clear();

        } else {
          _sortingKeys = new HashMap();
        }
        _sortingKeys.put(columnNames[columnIndex], getSorter().getSortingDirection());

        handleSelection();

        updateApplyButton();
      }
    }