Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » NatTable and context/dependency injection(How to use NatTable native methods in a different handler class thanks to context injection of a NatTable object)
NatTable and context/dependency injection [message #1767926] Wed, 12 July 2017 08:28 Go to next message
Ouliana Badaoui is currently offline Ouliana BadaouiFriend
Messages: 6
Registered: July 2017
Junior Member
Hi everyone,

I'm currently working as an intern in a lab and I have to use contexts, dependency injections, and NatTable elements in my project. Problem is, I've never used any of those before and I have a bit of trouble getting the gist of it. In my project, I need to export the NatTable object created in a specific class through another class. The export should be associated to a handler linked to a button. The button was created in the application model (I think? it's called fragment.e4xmi).

For the export, I'm using native NatTable commands that work well when I use them within the same class as the one where the NatTable object is. However, I can't do that if I want to associate the export to the button I should link it to; I need to put the NatTable in the context, then inject it to the class I need to use it in. But then again, I'm not sure this is how it should be done...

Here is what I do (I only show you the snippets of code I added to the project):



    In the class "UiHazopTable.java":


@Inject
	public NatTable init(IEclipseContext context) {
       //it initiliazes the nattable object before I put it in the context (not shown)
		context.set("UiHazopTable.nat", natTable);
        }




    In the class "ExportHazopTable.java" (handler for the export button):


@Inject
	private NatTable natTable;

public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell s , @Named("fr.laas.hazopuml.ui.commandparameter.0") Object o) {
		// TO RETRIEVE OBJECT
		ExportCommand cmd = new ExportCommand(natTable.getConfigRegistry(), natTable.getShell()); 
		natTable.doCommand(cmd);


I tried to follow Vogella's tutorials but I still can't proper grasp how things should be done. I hope I'm clear enough.

Thank you for reading through!
Re: NatTable and context/dependency injection [message #1768288 is a reply to message #1767926] Mon, 17 July 2017 05:29 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Never use field injection in handler classes. Always use method injection there.

public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell s , @Named("fr.laas.hazopuml.ui.commandparameter.0") Object o, @Named("UiHazopTable.nat") NatTable natTable)  {
		ExportCommand cmd = new ExportCommand(natTable.getConfigRegistry(), natTable.getShell()); 
		natTable.doCommand(cmd);
}
Re: NatTable and context/dependency injection [message #1768299 is a reply to message #1768288] Mon, 17 July 2017 08:06 Go to previous messageGo to next message
Ouliana Badaoui is currently offline Ouliana BadaouiFriend
Messages: 6
Registered: July 2017
Junior Member
Thank you for your reply! It fixed a few issues I had and clarified quite a few doubts. However, the command still won't be launched when I click on the button. Does it matter where I inject the NatTable object into the context? And once it's there, is it updated in the context as it is updated in the program, or do I need to force it to be updated?

Thank you for reading through.

Re: NatTable and context/dependency injection [message #1768301 is a reply to message #1768299] Mon, 17 July 2017 08:18 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
If you add it to the context, you need to ensure that it is also updated when the instance changes that should be tracked. There is no magic listener that knows what you are trying to achieve. ;) You simply add an object to the context. The context does not know anything about the purpose of that object or where it comes from.
Re: NatTable and context/dependency injection [message #1768309 is a reply to message #1768301] Mon, 17 July 2017 09:06 Go to previous messageGo to next message
Ouliana Badaoui is currently offline Ouliana BadaouiFriend
Messages: 6
Registered: July 2017
Junior Member
It does make sense! So if I understood well, when an object is added to the context, it's a sort of copy that's made? I thought the object in the context was like a reference to the actual object (hence my confusion).

Another, perhaps silly question: is it possible to export an empty nattable object? Because if so, then my issue most likely comes from elsewhere.

Thank you again!
Re: NatTable and context/dependency injection [message #1768310 is a reply to message #1768309] Mon, 17 July 2017 09:14 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Java is call-by-value, but you are passing the reference, so of course the reference to the object is stored in the context and not the object itself. As you don't pass the object that contains the NatTable (a part for example) but only the reference to the NatTable object, the container can not know that you changed the NatTable in the containing object.

Think of it like this:
There is a room, and in that room sits a dog. Someone passes you the dog leash. So you know that you have the dog leash for the dog in the room. Now someone takes the dog out of the room and puts another dog in the room. But he forgets to pass you the dog leash of the new dog. You will still hold the dog that previously sat in the room and know nothing about the new dog that was put into the room.
Does that makes sense?

I think you can export an empty NatTable. I would not know a reason why not.

[Updated on: Mon, 17 July 2017 10:47]

Report message to a moderator

Re: NatTable and context/dependency injection [message #1768321 is a reply to message #1768310] Mon, 17 July 2017 13:19 Go to previous messageGo to next message
Ouliana Badaoui is currently offline Ouliana BadaouiFriend
Messages: 6
Registered: July 2017
Junior Member
It makes sense. I think my issue is unrelated to NatTable though, because I tried with a simple int to see if I understood how contexts worked (and to test it) and I still can't get my program to show what I want.

I've got this:
		int a = 2; 
		context.set("prem", a);

	
public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell s , 
			@Named("fr.laas.hazopuml.ui.command.clicksum") Object o, 
			@Named("prem") int a)  {
		
		int b = 10; 
		int i = a + b ; 
		
		MessageDialog.openInformation(s, "Sum", "Result expected: 12 \n" + "Result obtained: " + i);
}

The handler described above is linked to a button I made in the fragment (I checked if the IDs of the handled tool item, handler, and command matched).

There must be something I'm doing wrong fundamentally because the button doesn't do anything...
Re: NatTable and context/dependency injection [message #1768322 is a reply to message #1768321] Mon, 17 July 2017 13:31 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
sounds like the injection is not executed at all. Is there an object available for the key "fr.laas.hazopuml.ui.command.clicksum"?
Re: NatTable and context/dependency injection [message #1768325 is a reply to message #1768322] Mon, 17 July 2017 13:38 Go to previous message
Ouliana Badaoui is currently offline Ouliana BadaouiFriend
Messages: 6
Registered: July 2017
Junior Member
I was convinced the "fr.laas.hazopuml.ui.command.clicksum" was supposed to be the ID of the command called by the class, but I just deleted it altogether and now it works! Both my test and the exportation of the table. I guess once I point the handler in the fragment to the corresponding class, the handler's ID doesn't need to be added to the class's execute.

Thank you so much for your help! :)
Previous Topic:Views open in wrong pane when using v3.x compatibilty layer
Next Topic:RCP Project Project Explorer - Unable to create class CompatibilityView
Goto Forum:
  


Current Time: Thu Apr 25 20:56:51 GMT 2024

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

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

Back to the top