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 HuijzerFriend
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 MoffattFriend
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 HuijzerFriend
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 SchindlFriend
Messages: 5874
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 HuijzerFriend
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 SchindlFriend
Messages: 5874
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 messageGo to next message
Arjan Huijzer is currently offline Arjan HuijzerFriend
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
Re: LoginDialog in LifeCycleManager [message #1706133 is a reply to message #1098239] Mon, 24 August 2015 05:12 Go to previous message
Lalit Solanki is currently offline Lalit SolankiFriend
Messages: 103
Registered: April 2015
Senior Member
But friend I am try to check login validation and false time focus login screen and correct to open application, how and where i'm apply code and i am post my code also....
Login Dialog
import javax.inject.Inject;

import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginDialog extends TitleAreaDialog {

	// Widgets
	public static Text textUsername;
	public static Text textPassword;

	@Inject
	MApplication application;
	
//	@Inject
//	EMenuService menuService;

	/**
	 * @wbp.parser.constructor
	 */
	public LoginDialog() {
		super(null);
	}
	
	@Inject
	public LoginDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle("Login");
		setMessage("Please provide credentials");
		return contents;
	}

	@Override
	protected Control  createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);


		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));
		
		
		checkLoginValidation(textUsername.getText(),textPassword.getText());
		
		
		return area;
	}
	
	private void checkLoginValidation(String text, String text2) {

		
	}


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

}


Lifecycle manager class

package secdemo;



import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.lifecycle.PostContextCreate;
import org.eclipse.e4.ui.workbench.lifecycle.PreSave;
import org.eclipse.e4.ui.workbench.lifecycle.ProcessAdditions;
import org.eclipse.e4.ui.workbench.lifecycle.ProcessRemovals;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

import javax.inject.Inject;

import org.eclipse.e4.core.di.annotations.Optional;


import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

import login.LoginDialog;

@SuppressWarnings("restriction")
public class Manager {
	
	@PostContextCreate
	void postContextCreate(IEclipseContext workbenchContext) {
	}
	@PreSave
	void preSave(IEclipseContext workbenchContext) {
	}
	
	
	@ProcessAdditions
	void processAdditions(final IEclipseContext workbenchContext, IEventBroker eventBroker) {
		LoginDialog dialog = ContextInjectionFactory.make(LoginDialog.class, workbenchContext);
		
		if(dialog.open() != Window.OK){
			// close application
			System.exit(-1);
		}
		
		eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE, new EventHandler() {
			
//			@Override
			public void handleEvent(Event event) {
				
//				if(LoginDialog.textUsername.getText().equals("123") && LoginDialog.textPassword.getText().equals("123") ){
					appStartupComplete(workbenchContext);
//				}
					
				
			}

		});
		// continue to the initial window
	}
	
	@ProcessRemovals
	void processRemovals(IEclipseContext workbenchContext) {
	}
	
	@Optional
	public void applicationStartupComplete(@UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) Event event)
	{
	}
	
	private void appStartupComplete(IEclipseContext workbenchContext) {
	}
	
	

		@PostContextCreate
	  void postContextCreate(IApplicationContext appContext, Display display) {
	    final Shell shell = new Shell(SWT.LEFT);
	    LoginDialog dialog = new LoginDialog(shell);
	    // close the static splash screen
	    appContext.applicationRunning();

	    // position the shell
	    setLocation(display, shell);

	    if (dialog.open() != Window.OK) {
	      // close the application
	      System.exit(-1);
	    }
	  }

	  private void setLocation(Display display, Shell shell) {
	    Monitor monitor = display.getPrimaryMonitor();
	    Rectangle monitorRect = monitor.getBounds();
	    Rectangle shellRect = shell.getBounds();
	    int x = monitorRect.x + (monitorRect.width - shellRect.width) / 2;
	    int y = monitorRect.y + (monitorRect.height - shellRect.height) / 2;
	    shell.setLocation(x, y);
	  }
	}



apply plugin.xml file in lifecycleURI....

Pleas help me friend .....


Lalit
Previous Topic:Create a common delta pack of an RCP application that runs in all the OS ?
Next Topic:Eclipse 4 RCP with ibm rational Doors Database
Goto Forum:
  


Current Time: Thu Sep 03 08:54:47 GMT 2015

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

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