FieldEditorPreferencePage中的布局问题

时间:2009-01-29 00:20:08

标签: eclipse eclipse-plugin swt eclipse-rcp jface

我在FieldEditorPreferencePage中遇到布局设置问题 我的代码是这样的:

public void createFieldEditors () {
  Group pv = new group(getfieldEditorParent(), SWT.SHADOW_OUT);
  Group of = new group(getfieldEditorParent(), SWT.SHADOW_OUT);
  pv.setText(“pv”);
  of.setText(“of”);
  GridLayout layout = new GridLayout(2,false);
  pv.setLayout(layout);
  of.setLayout(layout);
  addField(new StringFieldEditor(“PreferenceStore name”,“Text:”, pv);
  addField(new StringFieldEditor(“PreferenceStore name”,“Text:”, pv);
  addField(new StringFieldEditor(“PreferenceStore name”,“Text:”, of);
  addField(new StringFieldEditor(“PreferenceStore name”,“Text:”, of);
  and so on.
 }

问题是它不适用于GridLayout StringFieldEditors不是并行的。列数始终为1.此外,当我尝试更改组中StringFieldEditors的大小时,它也不起作用。

有人有什么想法吗? 感谢。

5 个答案:

答案 0 :(得分:7)

问题在于,当您使用FieldEditorPreferencePage时,您只能使用FieldEditor个子类作为组件。这是文档的片段:

  

FieldEditorPreferencePage实现了一个   使用这些字段编辑器的页面   显示和存储首选项   页面上的值。代替   创建SWT控件来填充它   内容,一个FieldEditorPreferencePage   子类创建字段编辑器   显示内容。 所有的   必须实现页面上的字段   作为现场编辑

这意味着您有两种选择如何实现您想要的目标:

  1. 实现您自己的FieldEditor子类,它代表Group小部件。
  2. 请勿展开FieldEditorPreferencePage,而只展开PreferencePage。然后,您必须实施createContents方法而不是createFieldEditors。您还必须管理加载和保存属性。
  3. 如果您想提供一些复杂的布局,我认为第二种方式可能会更容易。您可能会发现更多信息here

答案 1 :(得分:2)

另一个(简单)解决方法: 您还可以创建新的复合材料以创建更多列。问题是这些FieldEditors与他们的父母沟通并弄乱你的布局。因此,通过创建“空”复合,他们可以根据需要进行通信:)

someGroup = new Group(..., SWT.NONE);
someGroup .setLayout(new GridLayout(16, false));

Composite myC1= new Composite(someGroup,SWT.NONE);
addField(new BooleanFieldEditor(...,C1);

Composite myC2= new Composite(someGroup,SWT.NONE);
addField(new BooleanFieldEditor(...,C2);

答案 2 :(得分:1)

有关FieldEditorPreferencePageGRID样式)的两件事要理解:

  1. 即使对于GridLayout等“自定义”组件,字段编辑器父级的布局也始终设置为Groups;
  2. 布局中的列数根据任何字段编辑器中的最大组件数进行调整(StringFieldEditor)时为2。
  3. 在上面的示例中,Groups的布局数据应考虑到这一点:

    GridDataFactory.defaultsFor(pv).grab(true, false).span(2, 1).applyTo(pv);
    GridDataFactory.defaultsFor(of).grab(true, false).span(2, 1).applyTo(of);
    

答案 3 :(得分:1)

我实现了Group-FieldEditor,它可以包含其他FieldEditor并将它们布局为一个组。


import java.util.Collection;

import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

/**
 * Class is intended to create a Group Widgets, inside of the {@link FieldEditorPreferencePage}
 * objects.
 * This class should be used as following:
 * 
    *
  • use the {@link #getFieldEditorParent()} to as a parent, while creating new Field Editors. *
  • use {@link #setFieldEditors(Collection)} to add the collection of FieldEditors to the * {@link GroupFieldEditor}. *
* * @author alf * */ public class GroupFieldEditor extends FieldEditor { private String name; private Collection members; private int numcolumns; private Group group; private Composite parent; /** * The gap outside, between the group-frame and the widgets around the group */ private static final int GROUP_PADDING = 5; // px /** * The gap inside, between the group-frame and the content */ private static final int GROUP_VERTICAL_MARGIN = 5; // px /** * The inside-distance creates a new boolean field editor */ protected GroupFieldEditor() { } /** * Creates a Group of {@link FieldEditor} objects * * @param name * - name * @param fieldEditorParent * - parent */ public GroupFieldEditor(String name, Composite fieldEditorParent) { this.name = name; // the parent is a Composite, which is contained inside of the preference page. Initially it // does not have any layout. this.parent = fieldEditorParent; FillLayout fillLayout = new FillLayout(); fillLayout.marginHeight = GROUP_VERTICAL_MARGIN; this.parent.setLayout(fillLayout); this.group = new Group(parent, SWT.DEFAULT); this.group.setText(this.name); } /** * The parent for all the FieldEditors inside of this Group. * * @return - the parent */ public Composite getFieldEditorParent() { return group; } /** * Sets the FieldeditorChildren for this {@link GroupFieldEditor} * * @param membersParam */ public void setFieldEditors(Collection membersParam) { this.members = membersParam; doFillIntoGrid(getFieldEditorParent(), numcolumns); } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override protected void adjustForNumColumns(int numColumns) { this.numcolumns = numColumns; } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override protected void doFillIntoGrid(Composite parentParam, int numColumns) { GridLayout gridLayout = new GridLayout(); gridLayout.marginLeft = GROUP_PADDING; gridLayout.marginRight = GROUP_PADDING; gridLayout.marginTop = GROUP_PADDING; gridLayout.marginBottom = GROUP_PADDING; this.group.setLayout(gridLayout); this.parent.layout(); this.parent.redraw(); if (members != null) { for (FieldEditor editor : members) { editor.fillIntoGrid(getFieldEditorParent(), 1); } } } /* * (non-Javadoc) Method declared on FieldEditor. Loads the value from the * preference store and sets it to the check box. */ @Override protected void doLoad() { if (members != null) { for (FieldEditor editor : members) { editor.load(); } } } /* * (non-Javadoc) Method declared on FieldEditor. Loads the default value * from the preference store and sets it to the check box. */ @Override protected void doLoadDefault() { if (members != null) { for (FieldEditor editor : members) { editor.loadDefault(); } } } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override protected void doStore() { if (members != null) { for (FieldEditor editor : members) { editor.store(); } } } @Override public void store() { super.store(); doStore(); } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override public int getNumberOfControls() { return 1; } /* * (non-Javadoc) Method declared on FieldEditor. */ @Override public void setFocus() { if (members != null && !members.isEmpty()) { members.iterator().next().setFocus(); } } /* * @see FieldEditor.setEnabled */ @Override public void setEnabled(boolean enabled, Composite parentParam) { if (members != null) { for (FieldEditor editor : members) { editor.setEnabled(enabled, parentParam); } } } @Override public void setPreferenceStore(IPreferenceStore store) { super.setPreferenceStore(store); if (members != null) { for (FieldEditor editor : members) { editor.setPreferenceStore(store); } } } }

答案 4 :(得分:0)

另一种解决方法:

使用标签分隔字段组。 下面创建一个垂直线分隔符并将文本直接放在它下面:

new Label(getFieldEditorParent(), SWT.SEPARATOR | SWT.HORIZONTAL)
        .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
new Label(getFieldEditorParent(), SWT.NONE).setText("My Group Title");