NatTable and context/dependency injection [message #1767926] |
Wed, 12 July 2017 08:28 |
Ouliana Badaoui 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 #1768310 is a reply to message #1768309] |
Mon, 17 July 2017 09:14 |
Dirk Fauth Messages: 2903 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
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05865 seconds