如何在包含 table 查看器的另一个组合的顶部显示一个组合?

How to display a composite on the top of another composite which contains a table viewer?

Wizard页面的createControl(parent)方法中,代码如下-

    top = new Composite(parent, SWT.NONE);
    top.setLayout(new FillLayout());
    setControl(top);
    setPageComplete(false);
    createViewer(top);

我想在 top 表示的组合上方添加一个 row

它不允许我。如果我添加它,top 表示的 composite 就会丢失。

下面是为顶部合成渲染的 -

当我将下面的代码放在 top 组合之前时,top 组合丢失了 -

    upper = new Composite(parent, SWT.NONE);
    upper.setLayout(new FillLayout());
    setControl(upper);
    Label label = new Label(page, SWT.NONE);
    label.setText("Some text to disply");

请就如何实现相同目标提出建议。


以下是我的 createControl 片段。如果添加一行,它会破坏 table 查看器的滚动和大小。

@Override
public void createControl(Composite parent) {

    /*top = new Composite(parent, SWT.NONE);
    top.setLayout(new FillLayout());
    setControl(top);*/
    setPageComplete(false);

    top = new Composite(parent, SWT.NONE);
    top.setLayout(new GridLayout());
    setControl(top);

    Label label = new Label(top, SWT.NONE);
    label.setText("Some text to disply");
    label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));


    createViewer(top);
}

private void createViewer(Composite parent) {

    tableLayout = new TableColumnLayout();

    // A separate composite containing just the table viewer is required
    Composite tableComp = new Composite(parent, SWT.NONE);

    tableComp.setLayout(tableLayout);
    viewer = new TableViewer(tableComp, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    createColumns(parent, viewer);
    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    viewer.setContentProvider(new ArrayContentProvider());

    // Layout the viewer
    GridData gridData = new GridData();
    gridData.verticalAlignment = GridData.FILL;
    gridData.horizontalSpan = 2;
    gridData.grabExcessHorizontalSpace = true;
    gridData.grabExcessVerticalSpace = true;
    gridData.horizontalAlignment = GridData.FILL;
    viewer.getControl().setLayoutData(gridData);
}

public TableViewer getViewer() {
    return viewer;
}


// This will create the columns for the table
    private void createColumns(final Composite parent, final TableViewer viewer) {
        String[] titles = { "Node Status", "Node Type", "Node Name" };
        int[] bounds = { 100, 100, 100 };
        Image HEADER = CommonUtility.HEADER;

        TableViewerColumn col = null;

        // First column is for type
        col = createTableViewerColumn(HEADER, titles[0], bounds[0], 0);
        col.setLabelProvider(new CentredImageCellLabelProvider() {

            @Override
            public Image getImage(Object element) {
                GenerateSkeletonComponentsStatusModel model = (GenerateSkeletonComponentsStatusModel) element;
                if (model.isNew()) {
                    return CommonUtility.CHECKED;
                } else {
                    return CommonUtility.UNCHECKED;
                } 
            }
        });
        // Weight for column
        tableLayout.setColumnData(col.getColumn(), new ColumnWeightData(50));

        // now the newly created
        col = createTableViewerColumn(HEADER, titles[1], bounds[1], 1);
        col.setLabelProvider(new CentredImageCellLabelProvider() {

            @Override
            public Image getImage(Object element) {
                GenerateSkeletonComponentsStatusModel model = (GenerateSkeletonComponentsStatusModel) element;
                if ("ns_flow".equals(model.getIcon())) {
                    return CommonUtility.FLOW_SERVICE;
                } 
                if ("ns_open_interface".equals(model.getIcon())) {
                    return CommonUtility.FOLDER;
                } 
                if ("ns_record".equals(model.getIcon())) {
                    return CommonUtility.DOCUMENT_TYPE;
                } 
                if ("ns_package".equals(model.getIcon())) {
                    return CommonUtility.PACKAGE;
                } 
                if("RestResource".equals(model.getIcon())){
                    return CommonUtility.RESTFUL_FOLDER;
                }
                return null;
            }

        });
        // Weight for column
        tableLayout.setColumnData(col.getColumn(), new ColumnWeightData(50));


        // First column is for the component name
        col = createTableViewerColumn(HEADER, titles[2], bounds[2], 2);
        col.setLabelProvider(new ColumnLabelProvider() {
            @Override
            public String getText(Object element) {
                GenerateSkeletonComponentsStatusModel model = (GenerateSkeletonComponentsStatusModel) element;
                return model.getComponentName();
            }
        });
        // Weight for column
        tableLayout.setColumnData(col.getColumn(), new ColumnWeightData(100));

    }

    private TableViewerColumn createTableViewerColumn(Image image, String title, int bound, final int colNumber) {
        final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.LEAD);
        final TableColumn column = viewerColumn.getColumn();
        column.setImage(image);
        column.setText(title);
        column.setWidth(bound);
        column.setResizable(true);
        column.setMoveable(true);
        column.setAlignment(SWT.CENTER);
        return viewerColumn;

    }

一个向导页面只能有一个顶级组合。您可以向该组合添加多个控件(包括嵌套组合)。

所以对于上面的标签 table:

top = new Composite(parent, SWT.NONE);
top.setLayout(new GridLayout());
setControl(top);

Label label = new Label(top, SWT.NONE);
label.setText("Some text to disply");
label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

TableViewer viewer = new TableViewer(top, .... flags
viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));