Using FieldEditors and getFieldEditorParent() on constructor [message #327592] |
Fri, 25 April 2008 13:05 |
Toby Weston![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=toby.weston%40gmail.com) Messages: 22 Registered: July 2009 |
Junior Member |
|
|
Hello,
I was reading that when using FieldEditors you should call the constructor using getFieldEditorParent() for example,
public class Foo extends FieldEditorPreferencePage {
...
user = new StringFieldEditor("something", "Username:", getFieldEditorParent());
pass = new StringFieldEditor("something", "Password:", getFieldEditorParent());
...
All seems reasonable and was working fine, however, when I switch to using a FieldEditorPreferencePage with a SWT.FLAT style, it goes a bit wonky.
Basically, because the getFieldEditorParent() looks like this;
protected Composite getFieldEditorParent() {
if (style == FLAT) {
// Create a new parent for each field editor
Composite parent = new Composite(fieldEditorParent, SWT.NULL);
parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
return parent;
}
// Just return the parent
return fieldEditorParent;
}
It'll create a new 'parent' each time its called. As the comment says "create a new parent for each field editor".
This is kind of ok, but methods on a FieldEditor like setEnabled(...) and getTextControl() all require you to remember what parent was used for the control (else the checkParent() method will throw an exception).
So, my question is around how to use FieldEditors when the style is SWT.FLAT. Do I really need to create a member variable to capture each field editors parent so that I can use this for the methods above throughout the life time of the page? Seems weird.
I thought I could just grab the control of say the label (as it should have been created at the same time), but to get that it requires a parent which again will do the checkParent check (and throw an exception).
Just out of interest, the exception and stack trace are shown below.
Cheers for any tips,
Toby
org.eclipse.core.runtime.AssertionFailedException: assertion failed: Different parents
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:109)
at org.eclipse.jface.preference.FieldEditor.checkParent(FieldEditor.java:170)
at org.eclipse.jface.preference.FieldEditor.getLabelControl(FieldEditor.java:392)
at org.eclipse.jface.preference.FieldEditor.setEnabled(FieldEditor.java:712)
at org.eclipse.jface.preference.StringFieldEditor.setEnabled(StringFieldEditor.java:518)
at ... my code ...
at org.eclipse.jface.preference.FieldEditor.fireValueChanged(FieldEditor.java:346)
at org.eclipse.jface.preference.FieldEditor.fireStateChanged(FieldEditor.java:329)
at org.eclipse.jface.preference.BooleanFieldEditor.valueChanged(BooleanFieldEditor.java:240)
at org.eclipse.jface.preference.BooleanFieldEditor$1.widgetSelected(BooleanFieldEditor.java:182)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:820)
at org.eclipse.jface.window.Window.open(Window.java:796)
at com.racediary.gui.actions.OpenSettingsAction.run(OpenSettingsAction.java:36)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:546)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:490)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:402)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.racediary.gui.Main.main(Main.java:16)
|
|
|
Powered by
FUDForum. Page generated in 0.04432 seconds