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.
Difference between revisions of "E4/Contexts"
(→IEquinoxContext) |
(→IEquinoxContext) |
||
Line 7: | Line 7: | ||
[https://bugs.eclipse.org/bugs/show_bug.cgi?id=259423 Bug 259423 Add notion of "context" that can be injected into objects] | [https://bugs.eclipse.org/bugs/show_bug.cgi?id=259423 Bug 259423 Add notion of "context" that can be injected into objects] | ||
− | <code>org.eclipse.core.runtime.RegistryContextFactory</code> - Creates objects based on the registry's configuration element | + | <code>org.eclipse.core.runtime.RegistryContextFactory</code> - Creates objects based on the registry's configuration element. Creates a user-specified class and injects into it contents of the configuration element and, optionally, the supplied context |
<code>org.eclipse.equinox.context.IEquinoxContext</code> - This interface represents a hierarchical context with dependency injection capabilities. The context might have number of objects associated with it. For instance, a context might include log, status, and preferences. We'll call such associated objects "services" (through they don't have to be OSGi services). | <code>org.eclipse.equinox.context.IEquinoxContext</code> - This interface represents a hierarchical context with dependency injection capabilities. The context might have number of objects associated with it. For instance, a context might include log, status, and preferences. We'll call such associated objects "services" (through they don't have to be OSGi services). |
Revision as of 10:30, 9 January 2009
Contexts
In the general, execution or evaluation contexts, that can provide information to the currently executing code.
IEquinoxContext
Bug 259423 Add notion of "context" that can be injected into objects
org.eclipse.core.runtime.RegistryContextFactory
- Creates objects based on the registry's configuration element. Creates a user-specified class and injects into it contents of the configuration element and, optionally, the supplied context
org.eclipse.equinox.context.IEquinoxContext
- This interface represents a hierarchical context with dependency injection capabilities. The context might have number of objects associated with it. For instance, a context might include log, status, and preferences. We'll call such associated objects "services" (through they don't have to be OSGi services).
An example use (from tests):
Integer testInt = new Integer(123); String testString = new String("abc"); // create context IEquinoxContext context = ContextFactory.createContext(); // elements to be populated via fields context.addObject("Integer", testInt); context.addObject("string", testString); // this checks capitalization as well ObjectBasic userObject = new ObjectBasic(); context.injectInto(userObject);
IServiceLocator
In Eclipse 3.x we have the notion of part hierarchy: Workbench, WorkbenchWindow, WorkbenchPart (Editor/View), and nested part (MultiPageEditorPart or PageBookView).
Bug 92646 comment #11 [Workbench] [RCP] Allow developers to register Workbench services
There is a description of our services/service locator hierarchy in comment #11, basically we use this model to support 3 things:
- A service behaviour accessed through a local context (not PlatformUI :-)
- Scoping of the service to the local context
- Local service lifecycle, allowing the de-activation of contributions and listener clean up
IContextService contextService = (IContextService) getSite().getService(IContextService.class); contextService.activateContext("org.eclipse.ui.textScope");
Each service is hierarchical and mirrors the IServiceLocator hierarchy. Services are created lazily on the call to getService(Class)
. The lookup algorithm is:
- Check our cache to see if we already have an instance of the service
- Check our local
org.eclipse.ui.services.AbstractServiceFactory
to see if we can create a local override version - Go to the org.eclipse.ui.services registry,
org.eclipse.ui.internal.services.WorkbenchServiceRegistry
to see if we can create the service - Use the parent locator to try and look up the service
When creating a service, you have access to:
Object create(Class serviceInterface, IServiceLocator parentLocator, IServiceLocator locator);
- serviceInterface: the service we need to create
- parentLocator: A locator that can return a parent service instance if desired
- locator: the service locator which can be used to retrieve dependent services
IEvaluationContext
Provided by org.eclipse.core.expressions this is the application context used in 3.x by the IEvaluationService (and hence all declarative expressions like enabledWhen/activeWhen/visibleWhen)