Dependency Injection problem [message #1095680] |
Tue, 27 August 2013 07:38  |
Eclipse User |
|
|
|
Hi all,
I am creating a custom editor. It is based on an EditorPart and is registered in the plugin.xml under the org.eclipse.ui.editors extension point. This editor opens up fine with the appropriate file type. Next, I wanted to load the data in the file and fill the contents of the editor to reflect this. I try to do this using the e4 DI injection mechanism to get the currently selected iFile, but I cannot get his to work (injected fields 'ESelectionService selectionService' and 'IEclipseContext context' have a null value when I put a debug break in the createPartControl method). Here is the class sofar (I have removed the control creation bits):
package nl.cncr.synplot.e4.ui.plugin.editors;
import javax.inject.Inject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
public class DataSourceEditor extends EditorPart {
public static final String ID = "nl.cncr.synplot.e4.ui.plugin.partdescriptor.datasourceeditor";
@Inject
ESelectionService selectionService;
@Inject
IEclipseContext context;
@Override
@PostConstruct
public void createPartControl(Composite parent) {
parent.setLayout(new FillLayout(SWT.HORIZONTAL));
// here controls are created
}
@Override
public void doSave(IProgressMonitor monitor) {
// TODO Auto-generated method stub
}
@Override
public void doSaveAs() {
// TODO Auto-generated method stub
}
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
setSite(site);
setInput(input);
}
@Override
public boolean isDirty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isSaveAsAllowed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
My extension point looks like:
<editor
class="nl.cncr.synplot.e4.ui.plugin.editors.DataSourceEditor"
default="false"
extensions="datasource_xml"
icon="icons/sample.gif"
id="nl.cncr.synplot.e4.ui.plugin.editors.datasourceeditor"
name="Datasource Editor">
</editor>
In order to try to get DI working, I added a PartDescriptor for the DataSourceEditor class to the application model:
<descriptors xmi:id="_1AZb0A8AEeOi8vdVGCtkrw"
elementId="nl.cncr.synplot.e4.ui.plugin.partdescriptor.datasourceeditor"
label="Datasource Editor"
iconURI="platform:/plugin/nl.cncr.synplot.e4.ui.plugin/resources/projecticon.svg"
allowMultiple="true" category="org.eclipse.e4.primaryDataStack"
closeable="true"
dirtyable="true"
contributionURI="bundleclass://nl.cncr.synplot.e4.ui.plugin/nl.cncr.synplot.e4.ui.plugin.editors.DataSourceEditor"
/>
Still this does not rescue the DI. Could anyone provide me with hints on how to solve this? Should it work in the current approach or am I missing an important component?
Thnx, Pim
|
|
|
Re: Dependency Injection problem [message #1097103 is a reply to message #1095680] |
Thu, 29 August 2013 04:31  |
Eclipse User |
|
|
|
Hi all, I found the solution. I had to use the e4 bridge mechanism to link the e3 plugin.xml definitions to the e4 DI mechanism. This is explained rather well in the Vogella tutorials (http://www.vogella.com/articles/Eclipse4MigrationGuide/article.html, section "Wrapping your Eclipse 4 components for Eclipse 3.x "). I just was not aware that this was relevant for my problem since I was not migrating anything.
For reference I report the classes setup that fixed DI problem. I installed the 3x bridge plugin from the e4 tools.
First I created an extension to the editors in the plugin.xml that points to a wrapper class.
<editor
class="nl.cncr.synplot.e4.ui.plugin.editors.DatasourceEditorWrapper"
default="false"
extensions="datasource_xml"
icon="icons/sample.gif"
id="nl.cncr.synplot.e4.ui.plugin.editors.datasourceeditorwrapper"
name="Datasource Editor">
</editor>
Then the DataSourceEditorWrapper class was created that extends the DIEditorPart class from the bridge plugin.
package nl.cncr.synplot.e4.ui.plugin.editors;
import org.eclipse.e4.tools.compat.parts.DIEditorPart;
public class DatasourceEditorWrapper extends DIEditorPart<DataSourceEditor> {
public DatasourceEditorWrapper() {
super(DataSourceEditor.class);
}
}
Finally, the actual DataSourceEditor e4 editor was created.
package nl.cncr.synplot.e4.ui.plugin.editors;
import javax.annotation.PostConstruct;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
public class CopyOfDataSourceEditor {
public static final String ID = "nl.cncr.synplot.e4.ui.plugin.partdescriptor.datasourceeditor";
@PostConstruct
public void createPartControl(Composite parent) {
parent.setLayout(new FillLayout(SWT.HORIZONTAL));
// here controls are created
}
@Focus
public void setFocus() {
}
@Persist
void doSave(@Optional IProgressMonitor monitor) {
}
}
Now, DI works for parts called from the plugin.xml.
Thnx, Pim
|
|
|
Powered by
FUDForum. Page generated in 0.06700 seconds