Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » e(fx)clipse » @ContextValue problem (How can I get notified of changes in the context value?)
@ContextValue problem [message #1771526] Sat, 26 August 2017 18:01 Go to next message
Marcelo Ruiz is currently offline Marcelo RuizFriend
Messages: 80
Registered: April 2017
Member
Hi Community,

I am having some trouble getting notified of context value updates with @ContextValue. I followed the example posted here: https://wiki.eclipse.org/Efxclipse/Runtime/Recipes#Publishing_to_the_IEclipseContext and I am able to receive only 2 events: the first one is the 'null' value when the property is created and the second one is the first modification to that property. Subsequent modifications are not received. As far as I understand, the use of @ContextValue creates the slot in the IEclipseContext and sets the value as modifiable, preventing the user to declare the dependency to the IEclipseContext. Shouldn't I be notified of changes all the time?

I created the variable in the Perspective where the 2 Parts that need to communicate reside (editing the fragment.e4xmi file and adding the variable name under 'Variables').

The code that publishes changes in the context looks like the following:

@Inject
@ContextValue(value = ContextKeys.SELECTED_SUPPLIERS)
private ContextBoundValue<List<? extends Supplier>> selectedSuppliersCtxValue;

private Subscription subscription;

@PostConstruct
publishToContext() {
  subscription = FXObservableUtil.onChange(
      model.selectedSuppliersProperty(),
      new ListChangeListener<Supplier>() {
        @Override
	public void onChanged(Change<? extends Supplier> c) {
          System.err.println("publishing to context: " + c.getList().toString());
          selectedSuppliersCtxValue.publish(c.getList());;
	}
     });
}
	
@PreDestroy
public void cleanUpSubscription() {
  subscription.dispose();
}


In the receiving end I have the following:

@Inject
@Optional
public void handleSelectedSuppliersContextUpdate(
    @ContextValue(value = ContextKeys.SELECTED_SUPPLIERS)
    Property<List<? extends Supplier>> selectedSuppliersProperty) {
  System.err.println("receiving from context: " + selectedSuppliersProperty.toString());
}


And when I run the program and trigger changes in the model.selectedSuppliersProperty() by manipulating the UI (selecting a different values in a ListView), I get the following output:

receiving from context: ObjectProperty [value: null]
publishing to context: ListProperty [value: [com.example.Supplier@66d116d3]]
receiving from context: ObjectProperty [value: ListProperty [value: [com.example.Supplier@66d116d3]]]
publishing to context: ListProperty [value: []]
publishing to context: ListProperty [value: [com.example.Supplier@927af95]]
publishing to context: ListProperty [value: []]
publishing to context: ListProperty [value: [com.example.Supplier@28d1f4ba]]


So, the first two notifications are fine, but I do not receive anything else afterwards. What is wrong with my code?

As a side note, I found that it is really hard to debug the application when the program being debugged is a graphic application (with a displayed window). I can only press the Function keys to control the debugger, but I am unable to interact with the mouse at all in Eclipse. Is this normal? Is there anything I could do to prevent this?

Thanks!
Re: @ContextValue problem [message #1771528 is a reply to message #1771526] Sat, 26 August 2017 20:33 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
The receiver should be:

@Inject
@Optional
public void handleSelectedSuppliersContextUpdate(
    @Named(ContextKeys.SELECTED_SUPPLIERS) List<? extends Supplier> selectedSuppliersProperty) {
  System.err.println("receiving from context: " + selectedSuppliersProperty.toString());
}
Re: @ContextValue problem [message #1771530 is a reply to message #1771528] Sat, 26 August 2017 22:55 Go to previous messageGo to next message
Marcelo Ruiz is currently offline Marcelo RuizFriend
Messages: 80
Registered: April 2017
Member
Thanks for your answer Tom!

For some reason I get a NullPointerException because I receive null in the first call to handleSelectedSuppliersContextUpdate(). Shouldn't that be avoided by the use use of @Optional?

Also, to my surprise, the behavior did not change. Using your code (taking out the toString() method call)the result I get is exactly the same:

receiving from context: null
publishing to context: ListProperty [value: [com.example.Supplier@2a61cc4b]]
receiving from context: ListProperty [value: [com.example.Supplier@2a61cc4b]]
publishing to context: ListProperty [value: []]
publishing to context: ListProperty [value: [com.example.Supplier@6410c383]]
publishing to context: ListProperty [value: []]
publishing to context: ListProperty [value: [com.example.Supplier@2a61cc4b]]


Should I be looking at a different cause for this problem? I am positive the value for the key is typed correctly in my fragment.e4xmi file...

[Updated on: Sat, 26 August 2017 23:10]

Report message to a moderator

Re: @ContextValue problem [message #1771538 is a reply to message #1771530] Sun, 27 August 2017 07:47 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Unfortunately this is a bug/miss behavior in the DI container so you need to add a null check in your method and remove the @Optional. I think the problem you see is that for the DI system the value has not really changed because you always publish the same instance try changing the publish to new ArrayList(c.getList()) will most likely fix this
Re: @ContextValue problem [message #1771559 is a reply to message #1771538] Mon, 28 August 2017 01:45 Go to previous messageGo to next message
Marcelo Ruiz is currently offline Marcelo RuizFriend
Messages: 80
Registered: April 2017
Member
Thanks Tom

That was exactly the case. Not the behavior I was expecting, though. I guess I thought that if I was explicitly calling publish the framework should honor it...

For anyone else having trouble when debugging, you could try the adding the following java argument:
-Dsun.awt.disablegrab=true

That might disrupt drag & drop operations, though...
Re: @ContextValue problem [message #1771609 is a reply to message #1771559] Mon, 28 August 2017 15:23 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Publish is nothing more than IEclipseContext#modify() and because the value you published is a mutable value the for the IEclipseContext no value has changed
Previous Topic:jetty upgrade problem
Next Topic:Has anyone tried the maven repositories for e(fx)clipse?
Goto Forum:
  


Current Time: Tue Mar 19 03:12:28 GMT 2024

Powered by FUDForum. Page generated in 0.02099 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top