Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » [solved] singleton part (make Editor as singleton )
icon14.gif  [solved] singleton part [message #1060685] Mon, 27 May 2013 19:22 Go to next message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 39
Registered: April 2013
Member
hi all.

this is what I want to do

I created a single Tableviewer in postContruction, and the user load the data from an external XML file via a menu command.

but when I try to load data via a method in the class, the recovered viewer is null (the method is called from a new instance of the class).
and I have a NullPointerException

I tried to make a class as singleton with anotation @Singleton, but i can't get existing instance (

it's the basic approach look like, but it's been two days that I'm trying to fix without success.

can someone help me by telling me how to proceed.

thanks in advance




My viepart class
@Singleton
public class MyViewPart {

private TableViewer viewer;

@PostConstruct
public void createControls(Composite parent)
  viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
		        | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
 { // my code here }
public void loadData(filename)()
 {
  // my code here
 }
 	
}


My Menu commande handler

public class LoadDataHandler {

@Execute
public void execute() {
	    
		
	    Shell shell = new Shell(Display.getDefault());
	    DirectoryDialog dialog = new DirectoryDialog(shell);
	    String fileSelected = dialog.open();
	   
	    MyViewPart data = new MyViewPart();
	    data.loadData(fileSelected); 
  }

}

[Updated on: Tue, 28 May 2013 18:23]

Report message to a moderator

Re: singleton part [message #1060702 is a reply to message #1060685] Tue, 28 May 2013 06:48 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
If you create your instance with the new operator there is no injection. I think what you want to do is inject the current available MyViewPart into your LoadDataHandler.execute()
Re: singleton part [message #1060733 is a reply to message #1060702] Tue, 28 May 2013 09:39 Go to previous messageGo to next message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 39
Registered: April 2013
Member
Thank you for the answer

I am new with the injection mechanism, I read several articles on this, but I did not really understand the behavior.

can you give me an overview with some example based on my above code, how to implement this in my case, so hwo to inject my class and method and retrieve their instances in my loadData handler.

it will allow me to understand the mechanism for my future implementations
Re: singleton part [message #1060738 is a reply to message #1060733] Tue, 28 May 2013 09:54 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Well the easiest thing is to do it like this:

public class LoadDataHandler {

	@Execute
	public void execute(Shell shell, MyViewPart data)
		DirectoryDialog dialog = new DirectoryDialog(shell);
		String fileSelected = dialog.open();
	   
		data.loadData(fileSelected); 
	}

}


Important to know is that if YOU create the object than the container does not know about it until you let him know. This would be done using the ContextInjectionFactory. There are a lot of posts in this forum pointing to that. But for your case the simple code snippet above should work.

You should also notice that this handler will only work if there is an instance of a MyViewPart in the context.
Re: singleton part [message #1060768 is a reply to message #1060738] Tue, 28 May 2013 11:52 Go to previous messageGo to next message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 39
Registered: April 2013
Member
i have tried your example i have not Exception but the @Execute execute methode is not fired,

in my MyViewPart Class il inject a Shell object and constructor like this, something wrong

public class MyViewPart {

@Inject private final Shell shell;
private TableViewer viewer;
  List<satellite> Sate;

@Inject public MyViewPart (Composite parent,Shell shell) {
	   this.shell = shell;
	   
	   
		
	}


@PostConstruct
public void createControls(Composite parent)
  viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
		        | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
 { // my code here }
public void loadData(filename)()
 {
  // my code here
 }
 	
}

[Updated on: Tue, 28 May 2013 11:53]

Report message to a moderator

Re: singleton part [message #1060785 is a reply to message #1060768] Tue, 28 May 2013 12:35 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
As I said before, if there is no MyViewPart instance in your context (which means the part was not opened before) the handler code will not work. And it should because the handler is operating on your part instance.

You don't need to hold a reference to the Shell in your part. You can inject it anywhere, e.g. in the createControls()

To learn about dependency injection in Eclipse you should try to read http://www.vogella.com/articles/EclipseRCP/article.html#dependencyinjectione4

Maybe then things become clearer.
Re: singleton part [message #1060797 is a reply to message #1060785] Tue, 28 May 2013 12:52 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
On 28.05.13 14:35, Dirk Fauth wrote:
> As I said before, if there is no MyViewPart instance in your context
> (which means the part was not opened before) the handler code will not
> work. And it should because the handler is operating on your part instance.
>

This is not 100% true. You can only get injected the part instance if
the Part is the active one! So even if it is created, if the focus is
not on it you won't get it injected = @Execute is not run!

Tom
Re: singleton part [message #1060800 is a reply to message #1060797] Tue, 28 May 2013 13:00 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Quote:
You can only get injected the part instance if the Part is the active one!


I wasn't aware of that one too. Thanks for pointing to it! Smile
Re: singleton part [message #1060825 is a reply to message #1060685] Tue, 28 May 2013 14:31 Go to previous messageGo to next message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 39
Registered: April 2013
Member
I thought that adding to the default context is automatically done when you inject the constructor of the class.

my part is instantiated at initialization of my application(all child object is created and I checked it with System.out.println in the constructor).

what is good practice to add to MyViewPart to context?

@Dirk Fauth

I have already read Lars Vogell tutorial, it gives good basis on the functioning of dependency injection in Eclipse 4,I have not been able to implement it real case, for example how to make my MyViewPart added to context, or giving small a example how to use EclipseContextFactory.

Re: singleton part [message #1060833 is a reply to message #1060825] Tue, 28 May 2013 14:54 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
If there is a MyViewPart created and you didn't do it with the new operator, the Eclipse application platform did that for you and putted it into the context. So everythings fine here.

The solution to your issue seems to be explained by Tom Schindl above, and I wasn't aware of that before too. If your part is not the active one when executing your handler, than the handler will not execute. Try to add the @Optional annotation to the MyViewPart parameter of your handler and look if it is null.

You could also try to inject the EPartService and search for your part. In this case you are not limited to the focus of the part.
Re: singleton part [message #1060845 is a reply to message #1060833] Tue, 28 May 2013 15:54 Go to previous messageGo to next message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 39
Registered: April 2013
Member
Following your suggestion,
when using the @optional anotation on the parameter MyViewPart the handler is fired and Data object is null.

I could get my Part with EPartService (the result object is not null) but when i try to get the class name

MPart todoPart = EPartService.findPart("com.example.e4.rcp.todo.part.todooverview");
// 
System.out.println(todoPart.getClass().getName());


i get this
Quote:
org.eclipse.e4.ui.model.application.ui.basic.impl.PartImpl

this is not the name of the class associated with the Part

[Updated on: Tue, 28 May 2013 15:56]

Report message to a moderator

Re: singleton part [message #1060849 is a reply to message #1060845] Tue, 28 May 2013 16:01 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You only get the model element! You get MPart and there from you can get
the real object by calling getObject()

Tom

On 28.05.13 17:54, AIT YAHIA Idir wrote:
> Following your suggestion,
> when using the @optional anotation on the parameter MyViewPart the
> handler is fired and Data object is null.
>
> I could get my Part with EPartService (the result object is not null)
> but when i try to get the class name
>
> System.out.println(todoPart.getClass().getName());
>
> i get this Quote:
>> org.eclipse.e4.ui.model.application.ui.basic.impl.PartImpl
>
> this is not the name of the class associated with the Part
icon14.gif  Re: singleton part [message #1060865 is a reply to message #1060849] Tue, 28 May 2013 18:20 Go to previous messageGo to next message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 39
Registered: April 2013
Member
Smile it works with the handler below, i thank you for your precious help

@Inject private EPartService service;
private Method somepart;

//
@Execute
   public void execute(Shell shell) {
		
		MPart todoPart = service.findPart("com.example.e4.rcp.todo.part.todooverview");
		
			
	    DirectoryDialog dialog = new DirectoryDialog(shell);
	    String fileSelected = dialog.open();
	    
	    
		try {
			
			Method somepart = todoPart.getObject().getClass().getDeclaredMethod("loadSatelliesData", String.class);
			somepart.invoke( todoPart.getObject(),fileSelected);
			
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	     //Loadsat.loadSatelliesData(fileSelected); 	    
	  }


but I think it is not the best practice to do that, the issues that arise:

  why Shell was well injected and not my ViewPart
  Therefore, why the solution proposed by Dirk Fauth did not work, I would have liked

[Updated on: Tue, 28 May 2013 18:21]

Report message to a moderator

Re: singleton part [message #1060886 is a reply to message #1060865] Tue, 28 May 2013 20:56 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Quote:
but I think it is not the best practice to do that


It is the best practice in your case

Quote:
why Shell was well injected and not my ViewPart


That was explained by Tom Schindl

Quote:
why the solution proposed by Dirk Fauth did not work, I would have liked


Although I don't know about your application I guess you have a command that gets triggered by clicking on a toolbar button or a menu item. In this case you lose the focus of your part. Therefore the injection does not work as Tom explained. The why can also be explained. Guess your part can be instantiated multiple times. How should the container know which part instance to use?
Re: singleton part [message #1060925 is a reply to message #1060886] Wed, 29 May 2013 08:10 Go to previous message
AIT YAHIA Idir is currently offline AIT YAHIA IdirFriend
Messages: 39
Registered: April 2013
Member
I thought as the Shell is injected the probleme of Focus is turned off causes Embarrassed , yes my handler is triggered by a menu action,

in my case i need only one instance of my part, but in my whole project i have other case where i have a parts which will be multi-instantiated, i will see this when the time has comes,

in the training purpose, I would make an example with my part Focused, to see what happens

thank you again

[Updated on: Wed, 29 May 2013 08:16]

Report message to a moderator

Previous Topic:Setting VisibleWhen on Perspective
Next Topic:installing e4 tools on Eclipse 4.3RC2a
Goto Forum:
  


Current Time: Tue Apr 23 07:11:13 GMT 2024

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

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

Back to the top