What IEclipseContext#declareModifiable is good for

There was a comment on IEclipseContext less code Rene asked how the publishing into parent contexts is done.

IEclipseContext provides 2 ways to publish information:

  • IEclipseContext#set
  • IEclipseContext#modify

I think 99% of the people are using IEclipseContext#set because they could make up what IEclipseContext#modify is good for.

The problem with IEclipseContext#set is that pushes the value in the context you have called it on, if you want to publish a value somewhere in the parent hiearchy you have to search the IEclipseContext hierarchy yourself == you need to know exactly how the thing working and in which parent-context the original creator of the expected the value to reside.

So you often see code like this:

IEclipseContext context;

public void publish() {
   TableViewer v = ...;
   v.addSelectionChangeListener( ... { context.getParent().set(Person.class, p) } );

for e4 parts the context you are publishing to is the PerspectiveContext or if there are no perspectives the WindowContext. Let’s suppose you have perspectives and now your requirements change and the value has to be published to WindowContext.

To break up this tight coupling between the publisher and publishing target you can make use of IEclipseContext#modify. Instead of publishing the value into the context instance the method is called on it search the parent hierarchy for the matching slot.

To know in which slot it publishes one has to call declare the variable on the context using IEclipseContext#declareModifiable.

If you are in a e4 application you don’t have to make this call. Haven’t you ever wondered about MContext#variables: List<String>. Congrats you’ve just learned how to define variables in contexts created by the e4 framework – no matter if you are using SWT or JavaFX as a rendering technology.

This makes the original code look like:

IEclipseContext context;

public void publish() {
   TableViewer v = ...;
   v.addSelectionChangeListener( ... { context.modify(Person.class, p) } );

or if you want to remove the IEclipseContext

ContextBoundValue<Person> value;

public void publish() {
   TableViewer v = ...;
   v.addSelectionChangeListener( ... { value.publish(p) } );
This entry was posted in e(fx)clipse, e4. Bookmark the permalink.

2 Responses to What IEclipseContext#declareModifiable is good for

  1. Lars Vogel says:

    What is this @ContextValue annotation. Is this a standard e4 annotation?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.