Skip to main content
Jump to: navigation, search

Difference between revisions of "Eclipse4/RCP/Contexts"

< Eclipse4‎ | RCP
m
Line 7: Line 7:
 
A context (a <tt>IEclipseContext</tt>) is a transient key-value map with a parent, such that contexts are linked together to form a tree structure. When a key is not found in a context, the lookup is retried on the parent, repeating until either a value is found or the root of the tree has been reached.
 
A context (a <tt>IEclipseContext</tt>) is a transient key-value map with a parent, such that contexts are linked together to form a tree structure. When a key is not found in a context, the lookup is retried on the parent, repeating until either a value is found or the root of the tree has been reached.
  
Contexts are used as the sources for Dependency Injection.
+
Contexts are used as the sources for Dependency Injection.  They are somewhat analogous to <em>modules</em> in Guice.
  
 
== The Use of Contexts in Eclipse 4 ==
 
== The Use of Contexts in Eclipse 4 ==
Line 32: Line 32:
 
Proposed Topics:
 
Proposed Topics:
 
* the context chain and the active context
 
* the context chain and the active context
 +
* creating a context: IEclipseContext#newChild or EclipseContextFactory#create
 
* getting and setting variables vs modifying a variable
 
* getting and setting variables vs modifying a variable
* run and tracks
+
* run and tracks: special runnable that when executed by a context, tracks the values accessed; when one of these values is changed, the runnable is re-evaluated automatically
* creating a context
+
* context functions: A ContextFunction is a special object that the IEclipseContext will invoke to cause computation on a lookup.
* context functions
+
 
  
 
* creating a context
 
* creating a context

Revision as of 00:09, 26 July 2016

Stop.png
This page is a stub
This page is incomplete. Please feel free to append questions to the end.


The Eclipse 4 Application Platform uses a context to record the state used for injection. Normally code should not have to use or know about the context.

What is a Context?

A context (a IEclipseContext) is a transient key-value map with a parent, such that contexts are linked together to form a tree structure. When a key is not found in a context, the lookup is retried on the parent, repeating until either a value is found or the root of the tree has been reached.

Contexts are used as the sources for Dependency Injection. They are somewhat analogous to modules in Guice.

The Use of Contexts in Eclipse 4

The power of contexts comes as Eclipse 4 associates a context with most model elements, such as that the context tree matches the UI hierarchy. So an MPart, its MPerspective, its MWindow, and its MApplication all have a separate contexts. Looking up a key that is not found in the part will cause the lookup to continue at the perspective, window, and application. At the top of the tree is a special node that looks up keys among the available OSGi services. Many application-, window-, and view-level services are installed by the Eclipse 4 at various levels in the context hierarchy. Thus the placement of values within the context hierarchy, such as in the perspective or window's context, provides a natural form of variable scoping.


Context Variables

Being able to resolve a value from somewhere in the context hierarchyis very powerful. But to change the value, we need to know where in the context hierarchy the value should be set. Rather than hard code this location, we can instead declare a context variable: we declare the variable at the appropriate context, and instead modify, rather than set, the context value: the context then looks up the chain to find the variable declaration and sets the value there. This separates defining where a context value should be placed from the code that's actually setting it.



TODO

Proposed Topics:

  • the context chain and the active context
  • creating a context: IEclipseContext#newChild or EclipseContextFactory#create
  • getting and setting variables vs modifying a variable
  • run and tracks: special runnable that when executed by a context, tracks the values accessed; when one of these values is changed, the runnable is re-evaluated automatically
  • context functions: A ContextFunction is a special object that the IEclipseContext will invoke to cause computation on a lookup.


  • creating a context
  • when are contexts created?
  • how do I access the current context?

Integrate stuff from http://wiki.eclipse.org/E4/Contexts

Back to the top