package info.novatec.inspectit.rcp.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import info.novatec.inspectit.rcp.preferences.PreferencesUtils;
import info.novatec.inspectit.rcp.provider.ICmrRepositoryAndAgentProvider;
import info.novatec.inspectit.rcp.provider.ICmrRepositoryProvider;
import info.novatec.inspectit.rcp.repository.CmrRepositoryDefinition;
import info.novatec.inspectit.rcp.repository.CmrRepositoryDefinition.LoginStatus;

/**
 * Handler for CMR Logout.
 * 
 * @author Clemens Geibel
 *
 */

public class CmrLogoutHandler extends AbstractHandler implements IHandler {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		CmrRepositoryDefinition cmrRepositoryDefinition = null;
		ISelection selection = HandlerUtil.getCurrentSelection(event);

		if (selection instanceof StructuredSelection) {
			Object selectedObject = ((StructuredSelection) selection).getFirstElement();
			if (selectedObject instanceof ICmrRepositoryProvider) {
				cmrRepositoryDefinition = ((ICmrRepositoryProvider) selectedObject).getCmrRepositoryDefinition();
			} else if (selectedObject instanceof ICmrRepositoryAndAgentProvider) {
				cmrRepositoryDefinition = ((ICmrRepositoryAndAgentProvider) selectedObject)
						.getCmrRepositoryDefinition();
			}
		}

		if (null != cmrRepositoryDefinition) {
			cmrRepositoryDefinition.refreshLoginStatus();
			if (LoginStatus.LOGGEDIN == cmrRepositoryDefinition.getLoginStatus()) {
				cmrRepositoryDefinition.logout();
				
				//Delete stored login data
				PreferencesUtils.saveStringValue(
						cmrRepositoryDefinition.getIp() + ":" + cmrRepositoryDefinition.getPort() + "EMAIL", "",
						false);
				PreferencesUtils.saveStringValue(
						cmrRepositoryDefinition.getIp() + ":" + cmrRepositoryDefinition.getPort() + "PW", "", false);

				MessageDialog.openError(null, "Warning", "You logged out successfully.");
			} else {
				MessageDialog.openError(null, "Logout failed", "You are not logged in.");
			}
		} else {
			throw new ExecutionException("No CMR");
		}

		return null;
	}

}
@@ -1611,23 +1611,16 @@
 dObject)
-%0A%09%09%09%09%09%09
 .getCmrR
@@ -1638,24 +1638,24 @@
 finition();%0A
+
 %09%09%09%7D%0A%09%09%7D%0A%0A%09%09
@@ -1920,39 +1920,32 @@
 saveStringValue(
-%0A%09%09%09%09%09%09
 cmrRepositoryDef
@@ -2016,23 +2016,17 @@
 IL%22, %22%22,
-%0A%09%09%09%09%09%09
+ 
 false);%0A
@@ -2066,15 +2066,8 @@
 lue(
-%0A%09%09%09%09%09%09
 cmrR
@@ -2408,9 +2408,10 @@
 ll;%0A%09%7D%0A%0A
-
 %7D
+%0A

Update CmrLogoutHandler.java