Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Eclipse4/RCP/FAQ"

< Eclipse4‎ | RCP
(Why am I being injected with null?)
(Dependency Injection & Contexts)
Line 159: Line 159:
 
=== How do I provide singleton objects? ===
 
=== How do I provide singleton objects? ===
  
FIXME is there / do we support @Singleton?
+
Typical E4AP applications have a single injector, accessible through ''org.eclipse.e4.core.di.InjectorFactory#getDefault()''.  Within this injector, any class or interface annotated with the ''javax.inject.Singleton'' will be treated as a singleton instance.
  
Approach: use IContextFunctiin that checks and sets a value in top context
+
Another approach is to use a ''IContextFunction'' that checks and sets a value in the top context.
 +
 
 +
FIXME: Can the injector also be configured to bind @Singleton to a particular class?
  
 
=== Why am I getting a new instance of an object? ===
 
=== Why am I getting a new instance of an object? ===
Line 171: Line 173:
 
The fix is to annotate the use with "@Named(ACTIVE_SHELL)".
 
The fix is to annotate the use with "@Named(ACTIVE_SHELL)".
  
=== Why am I being injected with null? ===
+
=== Why am I being injected with ''null''? ===
  
 
Typically null values are only injected when an argument or field is marked as @Optional.  But a null value will be injected if the value has previously been explicitly set to null in the context.
 
Typically null values are only injected when an argument or field is marked as @Optional.  But a null value will be injected if the value has previously been explicitly set to null in the context.
Line 190: Line 192:
  
 
=== What is the difference between IEclipseContext#set and IEclipseContext#modify? ===
 
=== What is the difference between IEclipseContext#set and IEclipseContext#modify? ===
 +
 +
<source lang="java">
 +
public class MyPart {
 +
    @Inject IEclipseContext context;
 +
 +
    ...
 +
 +
    private void repositoryChanged(IRepository repository) {
 +
        //  set the variable "repository" in this part's context: the value is only visible to
 +
        // this part and any children
 +
        context.set("repository", repository);
 +
 +
        // search up the context stack to see if the variable exists in one of the the context's
 +
        // ancestors; otherwise it does a set in the specified context
 +
        context.modify("repository", repository);
 +
    }
 +
 +
    ...
 +
}
 +
</source>
 +
A placeholder can be made for ''#modify'' with ''IEclipseContext#declareModifiable()'' or a ''<variable>'' declaration in an ''Application.e4xmi''.
  
 
== UI ==
 
== UI ==

Revision as of 17:56, 17 October 2011

How do I adopt the Eclipse 4 Application Platform?

Accessing the status line

Eclipse 3.x Eclipse 4.0
getViewSite()
  .getActionsBars()
      .getStatusLineManager()
          .setMessage(msg);
@Inject
IStatusLineManager statusLine;
...
statusLine.setMessage(msg);

Associating help context with a control

getSite()
  .getWorkbenchWindow()
    .getWorkbench()
       .getHelpSystem().setHelp(
               viewer.getControl(), some_id)
@Inject
IWorkbenchHelpSystem helpSystem;
...
helpSystem.setHelp(
        viewer.getControl(), some_id);

Handling errors and exceptions

try {
    ...
} catch (Exception ex) {
    IStatus status = new Status(
       IStatus.ERROR, "plugin-id",
       "Error while ...", ex);
    StatusManager.getManager()
        .handle(status, StatusManager.SHOW);
}
@Inject
StatusReporter statusReporter;
...
try{
    ...
} catch (Exception ex) {
    statusReporter.show("Error while ...", ex);
}

Accessing preference values

IPreferenceStore store =
    IDEWorkbenchPlugin.getDefault()
        .getPreferenceStore();
boolean saveBeforeBuild = store
    .getBoolean(SAVE_BEFORE_BUILD);
@Inject @Preference(SAVE_BEFORE_BUILD)
boolean saveBeforeBuild;
IPreferenceStore store =
    IDEWorkbenchPlugin.getDefault()
        .getPreferenceStore();
store.putBoolean(SAVE_BEFORE_BUILD, false);
FIXME: there's way to do this...
@Inject @Preference(SAVE_BEFORE_BUILD)
Provider<Boolean> saveBeforeBuild;
...
saveBeforeBuild.set(false);

Why won't my application start?

E4AP products require having the following plugins:

  • org.eclipse.equinox.ds (must be started)
  • org.eclipse.equinox.event (must be started)

Note that org.eclipse.equinox.ds must be explicitly started. In your product file, you should have a section:

  <configurations>
     <plugin id="org.eclipse.core.runtime" autoStart="true" startLevel="2" />
     <plugin id="org.eclipse.equinox.ds" autoStart="true" startLevel="3" />
     <plugin id="org.eclipse.equinox.event" autoStart="true" startLevel="3" />
  </configurations>


Dependency Injection & Contexts

What services are available for injection?

See the list of provided services

How can I override a provided object?

FIXME For example, to provide an alternative StatusReporter or Logger

How do I provide singleton objects?

Typical E4AP applications have a single injector, accessible through org.eclipse.e4.core.di.InjectorFactory#getDefault(). Within this injector, any class or interface annotated with the javax.inject.Singleton will be treated as a singleton instance.

Another approach is to use a IContextFunction that checks and sets a value in the top context.

FIXME: Can the injector also be configured to bind @Singleton to a particular class?

Why am I getting a new instance of an object?

The injector attempts to resolve objects in the context. If they are not found in the context, but the class exists, then the injector will instantiate and return a new instance providing that its injectable dependencies can be resolved.

Why is my widget/part not displaying? Why am I getting a new Shell?

The fix is to annotate the use with "@Named(ACTIVE_SHELL)".

Why am I being injected with null?

Typically null values are only injected when an argument or field is marked as @Optional. But a null value will be injected if the value has previously been explicitly set to null in the context.

For example, the valueChanged() method will be injected with null:

@PostConstruct
public void init(IEclipseContext context) {
   context.set("value", null);
}
 
@Inject
public void valueChanged(@Named("value") String selection) {
   // ...
}

What is the difference between IEclipseContext#set and IEclipseContext#modify?

public class MyPart {
    @Inject IEclipseContext context;
 
    ...
 
    private void repositoryChanged(IRepository repository) {
        //  set the variable "repository" in this part's context: the value is only visible to
        // this part and any children
        context.set("repository", repository);
 
        // search up the context stack to see if the variable exists in one of the the context's 
        // ancestors; otherwise it does a set in the specified context
        context.modify("repository", repository);
    }
 
    ...
}

A placeholder can be made for #modify with IEclipseContext#declareModifiable() or a <variable> declaration in an Application.e4xmi.

UI

How do I enable Drag N Drop (DND) of parts?

The DND addon is found in the org.eclipse.e4.ui.workbench.addons.swt plugin. However it requires the compatibility layer and is not available for native E4AP applications.

Back to the top