Automotive/EclipseRCP

Eclipse RCP (Rich Client Platform) - 10.The Eclipse context

TimeSave 2022. 1. 21. 21:24

 

10. The Eclipse context : eclipse context

 

10.1. What is the Eclipse context?

During startup of an Eclipse application the Eclipse runtime creates an object based on the IEclipseContext interface. This object is called the context or the Eclipse context.

 

The context is similar to a Map data structure, in which objects can be placed under a certain key. The key is a String and in several cases the fully qualified class name is used as key. The value (to which the key points) can be injected into other objects. But unlike a map, the Eclipse context is hierarchical and can also dynamically compute values for requested keys.

 

For certain model objects (see Which model elements have a local context?) a local context is created. Such a context is associated with an application model object.

 

The different context objects are connected to form a hierarchical tree structure based on the structure of your application model. The highest level in this hierarchy is the application context.

 

A sample context hierarchy is depicted in the following picture.

 

존재하지 않는 이미지입니다.

사진 삭제

사진 설명을 입력하세요.

Objects can be placed at different levels in the context hierarchy. This allows that the same key points to different objects in the hierarchy.

 

For example, a part can express a dependency to a Composite object via a field declaration similar to: @Inject Composite parent; Since parts have different local contexts they can receive different objects of the type Composite.

 

10.2. Which model elements have a local context?

Currently the following model elements implement the MContext interface and therefore have their own context:

 

MApplication

 

MWindow

 

MPerspective

 

MPart

 

MPopupMenu

 

10.3. Life cycle of the Eclipse context

The Eclipse framework creates the context hierarchy based on the application model during the start process. By default, it places certain objects under predefined keys into the context, e.g., services to control the Eclipse framework functionality.

 

The model objects and the created objects based on the class URI attributes are created by the Eclipse platform. For each model element with a custom context the Eclipse framework determines which objects should be available in the local context of the model object. If required, it also creates the required Java objects referred by the Class URI property of the model elements. This is for example the case if a part is visible to the user.

 

정보 : The renderer framework is responsible for creating the local context of the UI related model elements. This framework allows you to define classes which are responsible for setting up the UI implementation of the model objects. A class responsible for a model element is called the renderer for this model element.

For example, the ContributedPartRenderer class is the default renderer for part model objects. This renderer creates a Composite for every part and puts this Composite into the local context of the part.

 

After the initial creation of the Eclipse context hierarchy, the framework or the application code can change the key-value pairs stored in the context. In this case objects which were created with the related Eclipse functionality (for example by the Eclipse dependency injection framework) are updated with the new values.

 

Objects in the context are persisted in memory (transient), i.e., once the application is stopped the context gets destroyed.

 

10.4. How are objects selected for dependency injection

As described in On which objects does Eclipse perform dependency injection? an object which is created by Eclipse can use annotations to describe its class dependencies.

 

During dependency injection for an object created by Eclipse, the Eclipse framework searches for a fitting object based on the specified key. The search starts in the local context associated with the application model object. If this key is not available, Eclipse continues to search in the parent context. This process continues until the main context has been reached.

 

사진 삭제

사진 설명을 입력하세요.

As you learn in later chapters the Eclipse context is not the only possible source of objects which can get injected. Other examples which are covered later are OSGi services, preferences, events and custom objects. The search happens (mostly) transparently for the caller of the injection.

 

10.5. How to access the model objects?

For the class references in the application model, the Eclipse framework creates the corresponding objects when needed. Such an object has access to its corresponding model object via dependency injection.

 

For example, in the implementation of a part you can access the model information of a part via: @Inject MPart part;

 

10.6. Default entries in the Eclipse context

The Eclipse framework creates several objects in the context. These are:

 

model objects - contain the data of the application model

 

services - software components which are defined by the Eclipse platform or via the OSGi service registry

 

several other objects which have explicitly been added to the context

 

The context can be modified by the application code and the framework. As the Eclipse framework automatically tracks the dependencies of the objects it creates, it can update them as described in Dynamic dependency injection based on key / value changes.

 

10.7. Qualifiers for accessing the active part or shell

The Eclipse platform places the part which is currently selected and the active shell into the IEclipseContext of the application object. The related keys are defined in the IServiceConstants interface.

 

For example, the following method would allow you to track the current active part in another part.

 

// tracks the active part
@Inject
@Optional
public void receiveActivePart(
    @Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
    if (activePart != null) {
        System.out.println("Active part changed "
                + activePart.getLabel());
    }
}
 

 

To track the active shell use the IServiceConstants.ACTIVE_SHELL key.

// tracks the active shell
@Inject
@Optional
public void receiveActiveShell(
    @Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
    if (shell != null) {
        System.out.println("Active shell (Window) changed");
    }
}
 

정보 : Eclipse uses handlers to define actions which can be triggered via menu or toolbar entries. For a handler implementation class it is not necessary to use these qualifiers, as a handler is executed in the active context of the application.

 

10.8. Tracking a child context with @Active

The @Active annotation allows you to track values in a child context. The Eclipse framework keeps track of the current active branch in the hierarchy of the IEclipseContext. For example, if the user selects a part, the path in the IEclipseContext hierarchy from the root to the IEclipseContext of the part is the current active branch.

 

With the @Active annotation you can track values in the current active branch of a child element. Whenever the active branch changes and the value of the referred key changes this value is re-injected into the object which uses the @Active annotation.

 

The usage of this annotation is demonstrated by the followingcode snippet.

public class MyOwnClass {
    @Inject
    void setChildValue(
            @Optional @Named("key_of_child_value") @Active String value) {
        this.childValue = value;
    }
}
 

정보 : The @Active annotation is currently not used within the Eclipse framework itself and the author of this tutorial has not yet managed to find a good use case for this annotation.