如何删除复合材料顶部不需要的空间?

时间:2016-06-22 14:33:19

标签: eclipse-plugin swt eclipse-rcp

我正在尝试创建自定义表单编辑器页面。在表单页面中,我需要有一个表,我可以列出键值对。我想要创建的属性列表表作为可重用的组件。

我创建了表单但是在属性表组合之前有不需要的空间(见下图)。我怎样才能摆脱这个空旷的空间?

enter image description here

以下是表单页面的代码

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.editor.FormPage;

public class EnvironmentPage extends FormPage {

    public EnvironmentPage(FormEditor editor) {
        super(editor, Activator.PLUGIN_ID + ".amf.environment", "Environment");
    }

    @Override
    protected void createFormContent(IManagedForm managedForm) {
        FormToolkit toolkit = managedForm.getToolkit();
        ScrolledForm form = managedForm.getForm();
        form.setText("Environment");

        Composite body = form.getBody();
        GridLayout gridLayout = new GridLayout(2, true);
        body.setLayout(gridLayout);
        toolkit.paintBordersFor(body);

        Composite leftComposite = toolkit.createComposite(body, SWT.NONE);
        leftComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        GridLayout leftCompositeLayout = new GridLayout();
        leftCompositeLayout.marginWidth = 0;
        leftCompositeLayout.marginHeight = 0;
        leftComposite.setLayout(leftCompositeLayout);

        createPropertiesSection(toolkit, leftComposite);

        Composite rightComposite = toolkit.createComposite(body, SWT.NONE);
        rightComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        GridLayout rightCompositeLayout = new GridLayout();
        rightCompositeLayout.marginWidth = 0;
        rightCompositeLayout.marginHeight = 0;
        rightComposite.setLayout(rightCompositeLayout);

        // TODO Controls on left composite

        super.createFormContent(managedForm);
    }

    private void createPropertiesSection(FormToolkit toolkit, Composite leftComposite) {
        Section section = toolkit.createSection(leftComposite,
                ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED | ExpandableComposite.TWISTIE);
        section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        section.setText("Log4J Properties");

        Composite log4jComposite = toolkit.createComposite(section, SWT.BORDER);
        toolkit.adapt(log4jComposite);
        GridLayout gridLayout = new GridLayout();
        gridLayout.marginBottom = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        log4jComposite.setLayout(gridLayout);
        log4jComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
        section.setClient(log4jComposite);

        PropertiesEditor propsEditor = new PropertiesEditor(log4jComposite, SWT.NONE) {

            @Override
            public void loadData() {
                // TODO Auto-generated method stub
            }
        };
        propsEditor.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));

        toolkit.paintBordersFor(section);
    }

}

属性编辑器类

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.FormToolkit;

public abstract class PropertiesEditor extends Composite {

    protected FormToolkit toolkit;

    protected Button btnRemove;

    protected Button btnAdd;

    protected Table propertiesTable;

    public PropertiesEditor(Composite parent, int style) {
        super(parent, style);
        toolkit = new FormToolkit(parent.getDisplay());
        createComposite(parent);
    }

    private void createComposite(Composite parent) {
        Composite propertiesGroup = toolkit.createComposite(parent, SWT.BORDER);
        GridLayout gridLayout = new GridLayout(2, false);
        gridLayout.marginWidth = 1;
        gridLayout.marginHeight = 1;
        gridLayout.verticalSpacing = 1;
        propertiesGroup.setLayout(gridLayout);
        propertiesGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        propertiesTable = toolkit.createTable(propertiesGroup, SWT.BORDER | SWT.FULL_SELECTION);
        propertiesTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        propertiesTable.setHeaderVisible(true);
        propertiesTable.setLinesVisible(true);
        propertiesTable.setData("name", "properties-table");
        toolkit.adapt(propertiesTable, true, true);

        TableColumn tableColumnKey = new TableColumn(propertiesTable, SWT.NONE);
        tableColumnKey.setText("Key");

        TableColumn tableColumValue = new TableColumn(propertiesTable, SWT.NONE);
        tableColumValue.setText("Value");

        propertiesTable.addControlListener(new ControlAdapter() {

            public void controlResized(ControlEvent e) {
                tableColumnKey.setWidth((int) (propertiesTable.getClientArea().width * 0.3));
                tableColumValue.setWidth((int) (propertiesTable.getClientArea().width * 0.7));
            }
        });

        Composite buttonsGroup = toolkit.createComposite(propertiesGroup, SWT.NONE);
        buttonsGroup.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, false, true, 1, 1));
        GridLayout buttonsLayout = new GridLayout();
        buttonsLayout.marginWidth = 10;
        buttonsGroup.setLayout(buttonsLayout);

        new Label(buttonsGroup, SWT.NONE);
        new Label(buttonsGroup, SWT.NONE);

        btnAdd = toolkit.createButton(buttonsGroup, "Add", SWT.NONE);
        GridData gd_btnAdd = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
        gd_btnAdd.widthHint = 60;
        btnAdd.setLayoutData(gd_btnAdd);
        toolkit.adapt(btnAdd, true, true);

        btnRemove = toolkit.createButton(buttonsGroup, "Remove", SWT.NONE);
        GridData gd_btnRemove = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
        gd_btnRemove.widthHint = 60;
        btnRemove.setLayoutData(gd_btnRemove);
        toolkit.adapt(btnRemove, true, true);
    }

    public abstract void loadData();

}

1 个答案:

答案 0 :(得分:2)

您的PropertiesEditor扩展Composite但是您没有对该复合做任何事情,而是在Composite方法中创建了另一个createComposite,其父级是父级传递给PropertiesEditor构造函数。我认为顶部的差距是PropertiesEditor复合材料。