Layout problems in FieldEditorPreferencePage
Asked Answered
H

5

8

I have following problems with layout settings in the FieldEditorPreferencePage.
My code is something like this:

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.
 }

The problem is that it does not work with GridLayout.
The StringFieldEditors are not parallel. The number of columns is always 1. Also when I try to change the size of StringFieldEditors in the groups, it doesn’t work too.

Anybody have any ideas?
Thanks.

Hanyang answered 29/1, 2009 at 0:20 Comment(2)
Need more code to give a meaningful answerEasygoing
I have the same problem. And I've tried to replace FieldEditorPreferencePage by PreferencePage, it's still the same. If I use group layout on page, field editors are ok, but when I add groups layout goes insane...Grapeshot
L
7

The problem is that when you are using FieldEditorPreferencePage, you can use only FieldEditor subclasses as components. Here's a snippet from a documentation:

FieldEditorPreferencePage implements a page that uses these field editors to display and store the preference values on the page. Instead of creating SWT controls to fill its contents, a FieldEditorPreferencePage subclass creates field editors to display the contents. All of the fields on the page must be implemented as field editors.

That means you have two options how to achieve what you want:

  1. Implement your own subclass of FieldEditor, which would represent the Group widget.
  2. Do not extend FieldEditorPreferencePage, but only a PreferencePage instead. Then you have to implement createContents method instead of createFieldEditors. You will also have to manage loading and saving of the properties.

I think the second way might be easier if you want to provide some complex layout. You may find some information more here

Leopardi answered 31/3, 2009 at 14:24 Comment(0)
P
3

Another (easy) workaround: You can also create new Composites to create more columns. The problem is that these FieldEditors communicate with their parent and mess up your layout. So, by creating an "empty" composite, they can communicate as much as they want :)

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);
Phonate answered 20/11, 2013 at 11:4 Comment(0)
M
1

Two things to understand about FieldEditorPreferencePage (with GRID style):

  1. The layout of field editor parents is always set to GridLayout even for "custom" components like Groups;
  2. The number of columns in layout is adjusted according to the maximum number of components in any of the field editors (which is 2 in case of StringFieldEditor).

In the above example, the layout data of Groups should take this into account:

GridDataFactory.defaultsFor(pv).grab(true, false).span(2, 1).applyTo(pv);
GridDataFactory.defaultsFor(of).grab(true, false).span(2, 1).applyTo(of);
Moslemism answered 14/11, 2011 at 7:25 Comment(0)
S
1

I implementer the Group-FieldEditor which can contain other FieldEditors and layout them as a Group.


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); } } } }
Serrate answered 17/10, 2012 at 7:36 Comment(0)
S
0

Yet another workaround:

Use Labels to separate groups of fields. The following creates a vertical line separator and puts text directly beneath it:

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");
Stalk answered 17/5, 2018 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.