Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » LoginDialog in LifeCycleManager
LoginDialog in LifeCycleManager [message #1096368] Wed, 28 August 2013 09:40 Go to next message
Arjan Huijzer is currently offline Arjan Huijzer
Messages: 9
Registered: October 2011
Junior Member
Hello all,

I am writing an E4 application from scratch and I am struggling to get the following working:

Before the application's main window is shown, I want to display a login dialog. When successful, the main window is displayed, otherwise the system exits. I have implemented this using a life cycle manager as described here:
http://www.vogella.com/articles/Eclipse4LifeCycle/article.html

public class LifeCycleManager {

	@PostContextCreate
	public void postContextCreate() {
		
		final Shell shell = new Shell(SWT.TOOL | SWT.NO_TRIM);
		LoginDialog dialog = new LoginDialog(shell);
		if (dialog.open() != Window.OK) {
			// close the application
			System.exit(-1);
		}
	}

}


This works and my LoginDialog is shown. However, I would like my application to remember the values in this dialog. In Eclipse 3 I achieved this using IDialogSettings, but it looks like I cannot use this in E4.

Any suggestions how to implements this in E4?

Thanks in advance,

Arjan
Re: LoginDialog in LifeCycleManager [message #1097339 is a reply to message #1096368] Thu, 29 August 2013 15:20 Go to previous messageGo to next message
Eric Moffatt is currently offline Eric Moffatt
Messages: 118
Registered: July 2009
Senior Member
You can either persist them in the MApplication's 'persistedData' map (If you want to fill in the dialog on the next start...) or in its 'transientData' field if not. These are both maps with String 'keys', how you choose to save the info is up to you...

You can @Inject the MApplication...
Re: LoginDialog in LifeCycleManager [message #1097487 is a reply to message #1097339] Thu, 29 August 2013 20:03 Go to previous messageGo to next message
Arjan Huijzer is currently offline Arjan Huijzer
Messages: 9
Registered: October 2011
Junior Member
Hello Eric,

Thanks for your help!

I tried injecting the MApplication into the LifeCycleManager class:

public class LifeCycleManager {

	@Inject
	MApplication application;
	
	@PostContextCreate
	public void postContextCreate(IEclipseContext context) {

		final Shell shell = new Shell(SWT.TOOL | SWT.NO_TRIM);
		LoginDialog dialog = new LoginDialog(shell);
		
		if (dialog.open() != Window.OK) {
			// close the application
			System.exit(-1);
		}

	}

}


However, this results in an error:

org.eclipse.e4.core.di.InjectionException: Unable to process "LifeCycleManager.application": no actual value was found for the argument "MApplication".


Next, I tried injecting it in my LoginDialog class, but all I get is a null value.

public class LoginDialog extends TitleAreaDialog {

	// Widgets
	private Text textUsername;
	private Text textPassword;

	@Inject
	MApplication application;
	
	public LoginDialog(Shell parent) {
		super(parent);
	}
	
	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle("Credentials");
		setMessage("Please provide the credentials.");
		return contents;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {

		Composite area = (Composite) super.createDialogArea(parent);
		
		System.out.println(application);
		
		Composite container = new Composite(area, SWT.NULL);
		container.setLayout(new GridLayout(2, false));
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		new Label(container, SWT.NULL).setText("Username");
		textUsername = new Text(container, SWT.BORDER);
		textUsername.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		new Label(container, SWT.NULL).setText("Password");
		textPassword = new Text(container, SWT.PASSWORD | SWT.BORDER);
		textPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		return area;
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(500, 300);
	}
	
	
}


Any idea what I am missing?

Thanks,
Arjan

[Updated on: Thu, 29 August 2013 20:04]

Report message to a moderator

Re: LoginDialog in LifeCycleManager [message #1097523 is a reply to message #1097487] Thu, 29 August 2013 21:10 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5331
Registered: July 2009
Senior Member
Nothing - the model has not been loaded at this point you need to use
another point in time e.g. @ProcessAddons

Tom

On 29.08.13 22:03, Arjan Huijzer wrote:
> Hello Eric,
>
> Thanks for your help!
>
> I tried injecting the MApplication into the LifeCycleManager class:
>
> public class LifeCycleManager {
>
> @Inject
> MApplication application;
>
> @PostContextCreate
> public void postContextCreate(IEclipseContext context) {
>
> final Shell shell = new Shell(SWT.TOOL | SWT.NO_TRIM);
> LoginDialog dialog = new LoginDialog(shell);
>
> if (dialog.open() != Window.OK) {
> // close the application
> System.exit(-1);
> }
>
> }
>
> }
>
>
> However, this results in an error:
>
> org.eclipse.e4.core.di.InjectionException: Unable to process
> "LifeCycleManager.application": no actual value was found for the
> argument "MApplication".
>
> Next, I tried injecting it in my LoginDialog class, but all I get is a
> null value.
>
> public class LoginDialog extends TitleAreaDialog {
>
> // Widgets
> private Text textUsername;
> private Text textPassword;
>
> @Inject
> MApplication application;
>
> public CredentialsDialog(Shell parent) {
> super(parent);
> }
>
> @Override
> protected Control createContents(Composite parent) {
> Control contents = super.createContents(parent);
> setTitle("Credentials");
> setMessage("Please provide the credentials.");
> return contents;
> }
>
> @Override
> protected Control createDialogArea(Composite parent) {
>
> Composite area = (Composite) super.createDialogArea(parent);
>
> System.out.println(application);
>
> Composite container = new Composite(area, SWT.NULL);
> container.setLayout(new GridLayout(2, false));
> container.setLayoutData(new GridData(GridData.FILL_BOTH));
>
> new Label(container, SWT.NULL).setText("Username");
> textUsername = new Text(container, SWT.BORDER);
> textUsername.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
>
> new Label(container, SWT.NULL).setText("Password");
> textPassword = new Text(container, SWT.PASSWORD | SWT.BORDER);
> textPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
>
> return area;
> }
>
> @Override
> protected Point getInitialSize() {
> return new Point(500, 300);
> }
>
>
> }
>
> Any idea what I am missing?
>
> Thanks,
> Arjan
Re: LoginDialog in LifeCycleManager [message #1097737 is a reply to message #1097523] Fri, 30 August 2013 05:22 Go to previous messageGo to next message
Arjan Huijzer is currently offline Arjan Huijzer
Messages: 9
Registered: October 2011
Junior Member
Hello Tom,

Thanks for your help. I am one step closer, but I still have not got it working......

I now use @ProcessAdditions in my LIfeCycleManager. Furthermore, I used ContextInjectionFactory to inject the context into the login dialog.

public class LifeCycleManager {
	
	@ProcessAdditions
	public void processAdditions(IEclipseContext context) {

		final Shell shell = new Shell(SWT.TOOL | SWT.NO_TRIM);
		LoginDialog dialog = new LoginDialog(shell);
		ContextInjectionFactory.inject(dialog, context);
		
		if (dialog.open() != Window.OK) {
			// close the application
			System.exit(-1);
		}

	}

}


In the LoginDialog the state is persisted in the following method:

@Override
protected void okPressed() {
	application.getPersistedState().put(CREDENTIALS_USERNAME, textUsername.getText());
	super.okPressed();
}



When I run the application, the state seems to be persisted after I close it. My workbench.xmi file has the following entry:

<persistedState key="credentials.username" value="Arjan"/>


However, when I start the application again, the code to read the persisted state in the login dialog does not seem to work:

String persistedUsername = application.getPersistedState().get(CREDENTIALS_USERNAME);


persistedUsername is NULL Sad

Note: I did remove the clearPersistedState property from the plugin.xml.


Thanks,
Arjan

Re: LoginDialog in LifeCycleManager [message #1098167 is a reply to message #1097737] Fri, 30 August 2013 18:18 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5331
Registered: July 2009
Senior Member
Most likely you launch with -clearPersistedState

Tom

On 30.08.13 07:22, Arjan Huijzer wrote:
> Hello Tom,
>
> Thanks for your help. I am one step closer, but I still have not got it
> working......
>
> I now use @ProcessAdditions in my LIfeCycleManager. Furthermore, I used
> ContextInjectionFactory to inject the context into the login dialog.
>
> public class LifeCycleManager {
>
> @ProcessAdditions
> public void processAdditions(IEclipseContext context) {
>
> final Shell shell = new Shell(SWT.TOOL | SWT.NO_TRIM);
> LoginDialog dialog = new LoginDialog(shell);
> ContextInjectionFactory.inject(dialog, context);
>
> if (dialog.open() != Window.OK) {
> // close the application
> System.exit(-1);
> }
>
> }
>
> }
>
> In the LoginDialog the state is persisted in the following method:
>
> @Override
> protected void okPressed() {
> application.getPersistedState().put(CREDENTIALS_USERNAME,
> textUsername.getText());
> super.okPressed();
> }
>
>
> When I run the application, the state seems to be persisted after I
> close it. My workbench.xmi file has the following entry:
>
> <persistedState key="credentials.username" value="Arjan"/>
>
> However, when I start the application again, the code to read the
> persisted state in the login dialog does not seem to work:
>
> String persistedUsername =
> application.getPersistedState().get(CREDENTIALS_USERNAME);
>
> persistedUsername is NULL :(
>
> Note: I did remove the clearPersistedState property from the plugin.xml.
>
>
> Thanks,
> Arjan
>
>
Re: LoginDialog in LifeCycleManager [message #1098239 is a reply to message #1098167] Fri, 30 August 2013 20:45 Go to previous message
Arjan Huijzer is currently offline Arjan Huijzer
Messages: 9
Registered: October 2011
Junior Member
You were right, I launched the application with -clearPersistedState. Once I removed it, all was working as intended Smile

Thanks for the help!!

Arjan
Previous Topic:Mixed Mode and E4 Command/Handler Error
Next Topic:How to handle closeable button of part
Goto Forum:
  


Current Time: Mon Sep 22 20:32:19 GMT 2014

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

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