/*******************************************************************************
 * Copyright (c) 2013 MEDEVIT <office@medevit.at>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     MEDEVIT <office@medevit.at> - initial API and implementation
 ******************************************************************************/
package ch.elexis.core.ui;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.constants.Preferences;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.data.events.ElexisEvent;
import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.elexis.core.data.extension.AbstractCoreOperationAdvisor;
import ch.elexis.core.ui.actions.GlobalActions;
import ch.elexis.core.ui.constants.UiResourceConstants;
import ch.elexis.core.ui.dialogs.ErsterMandantDialog;
import ch.elexis.core.ui.dialogs.LoginDialog;
import ch.elexis.core.ui.util.SWTHelper;
import ch.elexis.core.ui.util.SqlWithUiRunner;
import ch.elexis.core.ui.wizards.DBConnectWizard;
import ch.elexis.data.Anwender;

public class CoreOperationAdvisor extends AbstractCoreOperationAdvisor {
	
	public String initialPerspectiveString;
	private Logger log = LoggerFactory.getLogger(CoreOperationAdvisor.class);
	
	@Override
	public void requestDatabaseConnectionConfiguration(){
		Display disp = new Display();
		Shell shell = new Shell(disp);
		WizardDialog wd = new WizardDialog(shell, new DBConnectWizard());
		wd.create();
		SWTHelper.center(wd.getShell());
		wd.open();
	}
	
	@Override
	public void requestInitialMandatorConfiguration(){
		Display d = Display.getDefault();
		new ErsterMandantDialog(d.getActiveShell()).open();
	}
	
	@Override
	public void adaptForUser(){
		if (CoreHub.actUser != null) {
			initialPerspectiveString =
				CoreHub.localCfg.get(CoreHub.actUser + GlobalActions.DEFAULTPERSPECTIVECFG, null);
		}
		
		UiDesk.updateFont(Preferences.USR_DEFAULTFONT);
		
		ElexisEventDispatcher.getInstance().fire(
			new ElexisEvent(CoreHub.actUser, Anwender.class, ElexisEvent.EVENT_USER_CHANGED));
	}
	
	@Override
	public String getInitialPerspective(){
		return (initialPerspectiveString == null) ? UiResourceConstants.PatientPerspektive_ID
				: initialPerspectiveString;
	}
	
	@Override
	public boolean openQuestion(String title, String message){
		Display d = Display.getDefault();
		return MessageDialog.openQuestion(d.getActiveShell(), title, message);
	}
	
	@Override
	public void performLogin(Object shell){
		String username = System.getProperty("ch.elexis.username");
		String password = System.getProperty("ch.elexis.password");
		if (username != null && password != null) {
			/* Allow bypassing the login dialog, eg. for automated GUI-tests.
			 * Example: when having a demoDB you may login directly by passing
			 * -vmargs -Dch.elexis.username=test -Dch.elexis.password=test 
			 * as command line parameters to elexis.
			 */
			log.error("Bypassing LoginDialog with username " + username);
			if (!Anwender.login(username, password)) {
				log.error("Authentication failed. Exiting");
			}
		}		
		else {
			LoginDialog dlg = new LoginDialog((Shell) shell);
			dlg.create();
			dlg.getShell().setText(Messages.LoginDialog_loginHeader);
			dlg.setTitle(Messages.LoginDialog_notLoggedIn);
			dlg.setMessage(Messages.LoginDialog_enterUsernamePass);
			dlg.open();
		}
	}

	@Override
	public boolean performDatabaseUpdate(String[] array, String pluginId){
		return new SqlWithUiRunner(array, pluginId).runSql();
	}
}

@@ -1699,73 +1699,8 @@
 ()%7B%0A
-%09%09Display disp = new Display();%0A%09%09Shell shell = new Shell(disp);%0A
 %09%09Wi
@@ -1732,21 +1732,36 @@
 dDialog(
-s
+UiDesk.getTopS
 hell
+()
 , new DB
@@ -2977,16 +2977,22 @@
  %7B%0A%09%09%09/*
+%0A%09%09%09 *
  Allow b
@@ -3046,22 +3046,16 @@
 I-tests.
-%0A%09%09%09 *
  Example
@@ -3069,16 +3069,22 @@
 having a
+%0A%09%09%09 *
  demoDB 
@@ -3116,22 +3116,16 @@
  passing
-%0A%09%09%09 *
  -vmargs
@@ -3150,16 +3150,22 @@
 ame=test
+%0A%09%09%09 *
  -Dch.el
@@ -3183,23 +3183,16 @@
 rd=test 
-%0A%09%09%09 * 
 as comma
@@ -3396,21 +3396,17 @@
 %09%09%09%7D%0A%09%09%7D
-%09%09%0A%09%09
+ 
 else %7B%0A%09
@@ -3667,16 +3667,17 @@
 %0A%09%09%7D%0A%09%7D%0A
+%09
 %0A%09@Overr

Fix call of DatabaseConnectionConfiguration in wrong Display context