4

I'm using the FieldEditorPreferencePage class in order to create a preferences page for my Eclipse plug-in. I've been able to add all the controls I need to the page, however there are several controls that I'd like to group together using a panel.

Here's an example from Eclipse's documentation. However, there's no information about how the "Open mode" panel was added to the page, and how controls were added to it.

How do I add a panel to an Eclipse PreferencePage?

enter image description here

mittelmania
  • 3,393
  • 4
  • 23
  • 48
  • You can do a group of radio buttons on a field editor preference page but I think it would be a struggle to do the enabled/disabled check boxes as well. The particular preference page you show just extends `PreferencePage` and creates normal SWT controls rather than using field editors. – greg-449 Mar 28 '16 at 14:03

3 Answers3

1

I ended up writing my own GroupFieldEditor class, based on two implementations I found that didn't exactly meet my requirements. FieldEditors can be added to that control, and the group can then be added to a FieldEditorPreferencePage object using the addField() method.

The implementation is available here

For example, this is how the group looks in my plug-in project:

enter image description here

mittelmania
  • 3,393
  • 4
  • 23
  • 48
  • The link for the GroupFieldEditor implementation on your comment "The implementation is available here" is broken. Could you update the link or share your implementation so we can have a look the way you solved the issue? Thank you. – MELS Jul 22 '22 at 10:34
0

A brief steps for adding a preferencePage:

  1. open plugin.xml and go to Extension tab.
  2. Add extension point org.eclipse.ui.preferencePages
  3. Specify id/name and other attributes.
  4. Implement class extend from PreferencePage.

If you want to group several controls in a panel and use it in FieldEditorPreferencePage, you can create new class extended from org.eclipse.jface.preference.FieldEditor. For example:

public class MyFieldEditor extends FieldEditor {
    public MyFieldEditor(String name, String labelText, Composite parent) {
        super(name, labelText, parent);
    }

    protected void doFillIntoGrid(Composite parent, int numColumns) {
        //...add your controls to panel
        Group grpOpenMode = new Group(parent, SWT.NONE);
        grpOpenMode.setText("Open mode");
        grpOpenMode.setBounds(10, 10, 230, 117);

        btnDoubleClick = new Button(grpOpenMode, SWT.RADIO);
        btnDoubleClick.setBounds(10, 21, 90, 16);
        btnDoubleClick.setText("Double click");

        btnSingleClick = new Button(grpOpenMode, SWT.RADIO);
        btnSingleClick.setBounds(10, 43, 90, 16);
        btnSingleClick.setText("Single Click");

        Button btnSelectOnHover = new Button(grpOpenMode, SWT.CHECK);
        btnSelectOnHover.setBounds(20, 63, 123, 16);
        btnSelectOnHover.setText("Select on hover");

        Button btnOpenUsingArrowKey = new Button(grpOpenMode, SWT.CHECK);
        btnOpenUsingArrowKey.setBounds(20, 85, 188, 16);
        btnOpenUsingArrowKey.setText("Open when using arrow keys");
    }

    protected void doLoad() {
        // get preference value, and set data to UI, Invoked after doFillIntoGrid(...)
        String prefix = getPreferenceName();
        boolean isDoubleClick = getPreferenceStore().getBoolean(prefix+"_isDoubleClick");
        btnDoubleClick.setSelection(isDoubleClick);
        btnSingleClick.setSelection(!isDoubleClick);
        btnSelectOnHoverget.setSelection(getPreferenceStore().getBoolean(prefix+"_selectOnHover");
    }

    protected void doLoadDefault() {
        //Invoked in preferencePages.performDefaults() - user click "Restore Default" button
    }

    protected void doStore() {
        //save data of UI to preference store. Invoked in preferencePages.performOk() - user click "OK"/"Apply" button
        getPreferenceStore().setValue(getPreferenceName()+"_isDoubleClick",btnDoubleClick.getSelection());
    }
}
Beck Yang
  • 3,004
  • 2
  • 21
  • 26
  • Thanks for the answer, however it's not comprehensive enough to answer my specific question – mittelmania Mar 29 '16 at 08:08
  • I edit the answer.Please check it. Actually, if the *Panel* only used once. I prefer to implement class extend from PreferencePage. Just like the "Open mode" panel. I think it's created by **protected void createOpenModeGroup(Composite composite)**. The source code is in org.eclipse.ui.ide.source_???.jar\org\eclipse\ui\internal\dialogs\WorkbenchPreferencePage.java – Beck Yang Mar 29 '16 at 15:59
0

You can use RadioGroupFieldEditor to add a set of radio buttons surrounded by a Group control.

Something like:

RadioGroupFieldEditor groupPref = new RadioGroupFieldEditor("preference key",
      "Group title message", 3, 
      new String[][] {
        {"radio 1 text", "preference value 1"}, 
        {"radio 2 text", "preference value 2"}, 
        {"radio 3 text", "preference value 3"}},
     getFieldEditorParent(), true /* use a group */);
addField(groupPref);
greg-449
  • 109,219
  • 232
  • 102
  • 145