/*******************************************************************************
 * Copyright (C) 2015, Max Hohenegger <eclipse@hohenegger.eu>
 *
 * 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.gitflow.ui.internal.menu;

import java.io.IOException;

import org.eclipse.egit.core.internal.Utils;
import org.eclipse.egit.gitflow.GitFlowRepository;
import org.eclipse.egit.gitflow.ui.Activator;
import org.eclipse.egit.gitflow.ui.internal.UIText;
import org.eclipse.egit.gitflow.ui.internal.actions.ReleaseStartFromCommitHandler;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

/**
 * Start GitFlow release from a specified commit
 */
public class DynamicHistoryMenu extends ContributionItem {
	@Override
	public void fill(Menu menu, int index) {
		GitFlowRepository gfRepo = getRepository();
		try {
			if (gfRepo == null || !gfRepo.isDevelop()) {
				return;
			}
		} catch (IOException e) {
			Activator.getDefault().getLog().log(Activator.error(e.getMessage()));
			return;
		}

		RevCommit selectedCommit = getSelectedCommit();
		if (selectedCommit == null) {
			return;
		}
		String startCommitSha1 = selectedCommit.getName();
		Shell activeShell = getActiveShell();

		ReleaseStartFromCommitHandler listener = new ReleaseStartFromCommitHandler(
				gfRepo, startCommitSha1, activeShell);
		MenuItem menuItem = new MenuItem(menu, SWT.PUSH, index);
		menuItem.setText(NLS.bind(
				UIText.DynamicHistoryMenu_startGitflowReleaseFrom,
				abbreviate(selectedCommit)));
		menuItem.addSelectionListener(listener);
	}

	private String abbreviate(RevCommit selectedCommit) {
		return selectedCommit.getId().abbreviate(7).name();
	}

	/**
	 * @return Selected commit
	 */
	private RevCommit getSelectedCommit() {
		ISelection selection = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage().getActivePart()
				.getSite().getSelectionProvider().getSelection();
		if (!(selection instanceof IStructuredSelection)) {
			return null;
		}
		IStructuredSelection structSelection = (IStructuredSelection) selection;
		Object firstElement = structSelection.getFirstElement();
		if (!(firstElement instanceof RevCommit)) {
			return null;
		}
		return (RevCommit) firstElement;
	}

	private Shell getActiveShell() {
		return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
	}

	private GitFlowRepository getRepository() {
		IWorkbenchPart activePart = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage().getActivePart();
		Repository repository = Utils.getAdapter(activePart, Repository.class);
		if (repository == null) {
			return null;
		}
		return new GitFlowRepository(repository);
	}
}

@@ -1528,17 +1528,8 @@
 ();%0A
-%09%09try %7B%0A%09
 %09%09if
@@ -1548,164 +1548,25 @@
 null
- %7C%7C !gfRepo.isDevelop()) %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09Activator.getDefault().getLog().log(Activator.error(e.getMessage()));%0A%09%09%09return;%0A
+) %7B%0A%09%09%09%09return;%0A%09
 %09%09%7D%0A
@@ -2097,16 +2097,192 @@
 stener);
+%0A%0A%09%09try %7B%0A%09%09%09menuItem.setEnabled(!gfRepo.isDevelop());%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09Activator.getDefault().getLog()%0A%09%09%09%09%09.log(Activator.error(e.getMessage()));%0A%09%09%09return;%0A%09%09%7D
 %0A%09%7D%0A%0A%09pr

Disable Gitflow menu in History view already on first access