简体   繁体   中英

Problems trying to create an org.eclipse.swt.widgets.Composite that contains two TableViewers

I've been tasked with creating a Composite that contains two TableViewers. The top table contains a single row of editable cells. These table cells will be used to filter data in the bottom table. There are a number of things that are not working properly, but I'll limit my inquiry, here, to one in particular. The TableViewers' tables extend beyond the width and height of the Composite that contains them, and I am not able to view table columns beyond the right edge (or rows of the bottom table that extend below the bottom) of the Composite. The Composite's horizontal scroll bar scrolls properly, but only as far as the width of the Composite (not surprisingly). I'm sure it is as simple as tweaking GridData or SWT constants during construcion. Sadly, I've tried several different combinations without success. Here's a snippet of my relevant code:

public class ControlledColumnFilterTableComposite<T> extends Composite {
private TableViewer filterViewer;
private TableViewer mainViewer;
public ControlledColumnFilterTableComposite(Composite parent,
   IControlledColumnTableContentProvider<T> content) {
  super(parent, SWT.H_SCROLL);
  createComposite(parent, content);
}
private void createComposite(Composite parent,
   IControlledColumnTableContentProvider<T> content) {
  this.setLayout(new GridLayout(1, false));
  GridData data = getLayoutData(parent);
  this.setLayoutData(data);
  addHorizontalBarListener();
  this.filterViewer = createFilterViewer(parent, content);
  this.mainViewer = createMainViewer(parent, content);
}
private TableViewer createFilterViewer(Composite parent,
   IControlledColumnTableContentProvider<T> content) {
  TableViewer viewer = new TableViewer(this, SWT.MULTI
     | SWT.FULL_SELECTION | SWT.BORDER);
  viewer.getTable().setHeaderVisible(true);
  viewer.getTable().setLinesVisible(true);
  viewer.setContentProvider(content);
  ControlledColumnTableMetadata<T> metadata = content.getMetadata();
  for (int i = 0; i < metadata.getBean().getDeclaredFields().length; i++) {
   getViewerColumn(viewer, metadata.getBean().getDeclaredFields()[i],
    true);
  }
  return viewer;
}
private TableViewer createMainViewer(Composite parent,
   IControlledColumnTableContentProvider<T> content) {
  TableViewer viewer = new TableViewer(this, SWT.MULTI | SWT.V_SCROLL
   | SWT.FULL_SELECTION | SWT.BORDER);
  viewer.getTable().setHeaderVisible(false);
  viewer.getTable().setLinesVisible(true);
  viewer.setContentProvider(content);
  viewer.setInput(((IPagingContentProvider<T>) content)
   .getModelProvider().getTableRows());
  ControlledColumnTableMetadata<T> metadata = content.getMetadata();
  for (int i = 0; i < metadata.getBean().getDeclaredFields().length; i++) {
   getViewerColumn(viewer, metadata.getBean().getDeclaredFields()[i],
    false);
  }
  viewer.getTable().setLayoutData(getLayoutData(parent));
  /*
   * I've tried getLayoutData(this) and I've also tried manipulating the
   * grabExcessHorizontalSpace and grabExcessVerticalSpace on the returned
   * GridData without getting the desired results
   */
  return viewer;
}
private TableViewerColumn getViewerColumn(TableViewer viewer, Field field,
   boolean listen) {
  final TableViewerColumn viewerColumn = new TableViewerColumn(viewer,
   SWT.NONE);
  final TableColumn column = viewerColumn.getColumn();
  setColumnProperties(viewer, column, field);
  viewerColumn.setLabelProvider(getColumnLabelProvider(field));
  // Only the filter TableViewer should have Listeners and EditingSupport
  if (listen) {
   // add sorting support
   viewerColumn.getColumn().addListener(SWT.Selection,
    new SortListener(field));
    // add support for saving column width preferences
    viewerColumn.getColumn()
     .addControlListener(createControlListener());
    // add editing support
    viewerColumn.setEditingSupport(getEditingSupport(viewer, field));
  }
  return viewerColumn;
}
private void setColumnProperties(TableViewer viewer, TableColumn column,
   Field field) {
  ControlledColumnTableMetadata<T> metadata =  ((IControlledColumnTableContentProvider<T>) viewer
   .getContentProvider()).getMetadata();
  column.setText(metadata.getColumnHeader(field));
  column.setToolTipText(metadata.getColumnHeader(field));
  if (metadata.isVisible(field)) {
   column.setWidth(metadata.getColumnWidth(field));
   column.setResizable(true);
   column.setMoveable(true);
  } else {
   column.setWidth(0);
   column.setResizable(false);
   column.setMoveable(false);
  }
}
private GridData getLayoutData(Composite parent) {
  GridLayout layout = (GridLayout) parent.getLayout();
  // Layout the viewer
  GridData gridData = new GridData();
  gridData.verticalAlignment = GridData.FILL;
  gridData.horizontalAlignment = GridData.FILL;
  gridData.horizontalSpan = layout.numColumns;
  gridData.grabExcessHorizontalSpace = true;
  gridData.grabExcessVerticalSpace = true;
  return gridData;
}
 ...
}

Any suggestions are GREATLY appreciated. Thanks, in advance.

  1. dont do this

     GridData data = getLayoutData(parent); // inside getLayoutData: GridLayout layout = (GridLayout) parent.getLayout(); this.setLayoutData(data); 

    setLayout should be used by composite which arranges child-controls (this.setlayout). same as this.someControl.setLayoutData(). In your case: parent handles where ControlledColumnFilterTableComposite is placed and ControlledColumnFilterTableComposite handles how TableViewer are arranged. This means that you should never call: parent.getLayout() and this.setLayoutData() because you might not now which Layout parent uses and using invalid LayoutData leads to some exceptions.

  2. try this

     this.setLayout(new GridLayout(1,false)); this.filterViewer = createFilterViewer(); this.filterViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true)); this.mainViewer = createMainViewer(); this.mainViewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true)); 

    GridLayout works as table in HTML - places controls in cells. SWT.FILL means that control will fill sorounding cell horizontally and vertically, and true (3rd and 4th argument) means that cell will expand to maximum available size horizontally and vertically

  3. remove the H_SCROLL, If you want Scrolling in your Composite use ScrolledComposite

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM