/*******************************************************************************
 * 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.factories;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdapterFactory;
import static org.eclipse.egit.core.project.RepositoryMapping.getMapping;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryNode;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.team.ui.history.IHistoryPage;
import org.eclipse.team.ui.history.IHistoryView;

/**
 * Get JGit repository for element selected in Git Flow UI.
 */
public class GitFlowAdapterFactory implements IAdapterFactory {
	@Override
	public Repository getAdapter(Object adaptableObject, Class adapterType) {
		Repository repository = null;
		if (adaptableObject instanceof IResource) {
			IResource resource = (IResource) adaptableObject;
			repository = getRepository(resource);
		} else if (adaptableObject instanceof IHistoryView) {
			IHistoryView historyView = (IHistoryView) adaptableObject;
			IHistoryPage historyPage = historyView.getHistoryPage();
			Object input = historyPage.getInput();
			if (input instanceof RepositoryNode) {
				RepositoryNode node = (RepositoryNode) input;
				repository = node.getRepository();
			} else if (input instanceof IResource) {
				repository = getRepository((IResource) input);
			}
		} else {
			throw new IllegalStateException();
		}

		return repository;
	}

	private Repository getRepository(IResource resource) {
		return getMapping(resource.getProject()).getRepository();
	}

	@Override
	public Class[] getAdapterList() {
		return new Class[] { IResource.class, IHistoryView.class };
	}
}

/*******************************************************************************
 * 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.factories;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryNode;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.team.ui.history.IHistoryPage;
import org.eclipse.team.ui.history.IHistoryView;

/**
 * Get JGit repository for element selected in Git Flow UI.
 */
public class GitFlowAdapterFactory implements IAdapterFactory {
	@Override
	public Repository getAdapter(Object adaptableObject, Class adapterType) {
		Repository repository = null;
		if (adaptableObject instanceof IResource) {
			IResource resource = (IResource) adaptableObject;
			repository = getRepository(resource);
		} else if (adaptableObject instanceof IHistoryView) {
			IHistoryView historyView = (IHistoryView) adaptableObject;
			IHistoryPage historyPage = historyView.getHistoryPage();
			Object input = historyPage.getInput();
			if (input instanceof RepositoryNode) {
				RepositoryNode node = (RepositoryNode) input;
				repository = node.getRepository();
			} else if (input instanceof IResource) {
				repository = getRepository((IResource) input);
			}
		} else {
			throw new IllegalStateException();
		}

		return repository;
	}

	private Repository getRepository(IResource resource) {
		RepositoryMapping mapping = RepositoryMapping
				.getMapping(resource.getProject());
		return mapping != null ? mapping.getRepository() : null;
	}

	@Override
	public Class[] getAdapterList() {
		return new Class[] { IResource.class, IHistoryView.class };
	}
}

Fix NPE in GitFlowAdapterFactory