/*******************************************************************************
 * Copyright (C) 2009, Stefan Lay <stefan.lay@sap.com>
 *
 * 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
 *******************************************************************************/
package org.eclipse.egit.ui.internal.actions;

import java.io.IOException;

import org.eclipse.compare.CompareUI;
import org.eclipse.compare.ITypedElement;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.CompareUtils;
import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput;
import org.eclipse.egit.ui.internal.dialogs.CompareTreeView;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * Compares the working tree content of a file with the version of the file in
 * the HEAD commit.
 */
public class CompareWithHeadActionHandler extends RepositoryActionHandler {
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final Repository repository = getRepository(true, event);
		// assert all resources map to the same repository
		if (repository == null)
			return null;
		final IResource[] resources = getSelectedResources(event);

		if (resources.length == 1 && resources[0] instanceof IFile) {
			final IFile baseFile = (IFile) resources[0];
			final String gitPath = RepositoryMapping.getMapping(
					baseFile.getProject()).getRepoRelativePath(baseFile);
			final ITypedElement base = SaveableCompareEditorInput
					.createFileElement(baseFile);

			ITypedElement next;
			try {
				Ref head = repository.getRef(Constants.HEAD);
				RevWalk rw = new RevWalk(repository);
				RevCommit commit = rw.parseCommit(head.getObjectId());

				next = CompareUtils.getFileRevisionTypedElement(gitPath,
						commit, repository);
			} catch (IOException e) {
				Activator.handleError(e.getMessage(), e, true);
				return null;
			}

			final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
					base, next, null);
			CompareUI.openCompareEditor(in);
			return null;

		} else {
			CompareTreeView view;
			try {
				view = (CompareTreeView) PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getActivePage().showView(
								CompareTreeView.ID);
				try {
					view.setInput(resources, repository.resolve(Constants.HEAD)
							.name());
				} catch (IOException e) {
					Activator.handleError(e.getMessage(), e, true);
					return null;
				}
			} catch (PartInitException e) {
				Activator.handleError(e.getMessage(), e, true);
				return null;
			}
			return null;
		}
	}

	@Override
	public boolean isEnabled() {
		return getRepository() != null;
	}
}

@@ -540,88 +540,8 @@
 n;%0A%0A
-import org.eclipse.compare.CompareUI;%0Aimport org.eclipse.compare.ITypedElement;%0A
 impo
@@ -585,16 +585,16 @@
 nEvent;%0A
+
 import o
@@ -728,64 +728,8 @@
 ce;%0A
-import org.eclipse.egit.core.project.RepositoryMapping;%0A
 impo
@@ -816,79 +816,8 @@
 ls;%0A
-import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput;%0A
 impo
@@ -916,41 +916,8 @@
 ts;%0A
-import org.eclipse.jgit.lib.Ref;%0A
 impo
@@ -956,159 +956,8 @@
 ry;%0A
-import org.eclipse.jgit.revwalk.RevCommit;%0Aimport org.eclipse.jgit.revwalk.RevWalk;%0Aimport org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;%0A
 impo
@@ -1580,21 +1580,17 @@
 l IFile 
-baseF
+f
 ile = (I
@@ -1616,733 +1616,62 @@
 %0A%09%09%09
-final String gitPath = RepositoryMapping.getMapping(%0A%09%09%09%09%09baseFile.getProject()).getRepoRelativePath(baseFile);%0A%09%09%09final ITypedElement base = SaveableCompareEditorInput%0A%09%09%09%09%09.createFileElement(baseFile);%0A%0A%09%09%09ITypedElement next;%0A%09%09%09try %7B%0A%09%09%09%09Ref head = repository.getRef(Constants.HEAD);%0A%09%09%09%09RevWalk rw = new RevWalk(repository);%0A%09%09%09%09RevCommit commit = rw.parseCommit(head.getObjectId());%0A%0A%09%09%09%09next = CompareUtils.getFileRevisionTypedElement(gitPath,%0A%09%09%09%09%09%09commit, repository);%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09Activator.handleError(e.getMessage(), e, true);%0A%09%09%09%09return null;%0A%09%09%09%7D%0A%0A%09%09%09final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(%0A%09%09%09%09%09base, next, null);%0A%09%09%09CompareUI.openCompareEditor(in
+CompareUtils.compareHeadWithWorkspace(repository, file
 );%0A%09

Use CompareUtils.compareHeadWithWorkspace