Search in sources :

Example 86 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class SelectionPropertyTester method test.

@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
    Collection<?> collection = (Collection<?>) receiver;
    if (collection.isEmpty())
        return false;
    if ("projectsSingleRepository".equals(property)) {
        // $NON-NLS-1$
        Repository repository = getRepositoryOfProjects(collection, true);
        return testRepositoryProperties(repository, args);
    } else if ("projectsWithRepositories".equals(property)) {
        // $NON-NLS-1$
        Repository repository = getRepositoryOfProjects(collection, false);
        return repository != null;
    } else if ("selectionSingleRepository".equals(property)) {
        // $NON-NLS-1$
        return SelectionUtils.getRepository(getStructuredSelection(collection)) != null;
    } else if ("resourcesSingleRepository".equals(property)) {
        // $NON-NLS-1$
        IStructuredSelection selection = getStructuredSelection(collection);
        // It may seem like we could just use SelectionUtils.getRepository
        // here. The problem: It would also return a repository for a node
        // in the repo view. But this property is just for resources.
        IResource[] resources = SelectionUtils.getSelectedResources(selection);
        Repository repository = getRepositoryOfResources(resources);
        return testRepositoryProperties(repository, args);
    } else if ("fileOrFolderInRepository".equals(property)) {
        // $NON-NLS-1$
        if (collection.size() != 1)
            return false;
        IStructuredSelection selection = getStructuredSelection(collection);
        if (selection.size() != 1)
            return false;
        Object firstElement = selection.getFirstElement();
        IResource resource = AdapterUtils.adaptToAnyResource(firstElement);
        if ((resource != null) && (resource instanceof IFile || resource instanceof IFolder)) {
            RepositoryMapping m = RepositoryMapping.getMapping(resource);
            if (m != null) {
                if ((resource instanceof IFolder) && resource.equals(m.getContainer())) {
                    return false;
                } else {
                    return testRepositoryProperties(m.getRepository(), args);
                }
            }
        }
    } else if ("resourcesAllInRepository".equals(property)) {
        // $NON-NLS-1$
        IStructuredSelection selection = getStructuredSelection(collection);
        IResource[] resources = SelectionUtils.getSelectedResources(selection);
        Collection<Repository> repositories = getRepositories(resources);
        if (repositories.isEmpty()) {
            return false;
        }
        if (args != null && args.length > 0) {
            for (Repository repository : repositories) {
                if (!testRepositoryProperties(repository, args)) {
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) IFile(org.eclipse.core.resources.IFile) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) Collection(java.util.Collection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 87 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class SelectionPropertyTester method getRepositoryOfProjects.

/**
 * @param collection
 *            the selected elements
 * @param single
 *            <code>true</code> if only a single repository is allowed
 * @return the repository if any was found, <code>null</code> otherwise
 */
private static Repository getRepositoryOfProjects(Collection<?> collection, boolean single) {
    Repository repo = null;
    for (Object element : collection) {
        IContainer container = AdapterUtils.adapt(element, IProject.class);
        RepositoryMapping mapping = null;
        if (container != null) {
            mapping = RepositoryMapping.getMapping(container);
        } else {
            container = AdapterUtils.adapt(element, IContainer.class);
            if (container != null) {
                mapping = RepositoryMapping.getMapping(container);
            }
        }
        if (container != null && mapping != null && container.equals(mapping.getContainer())) {
            Repository r = mapping.getRepository();
            if (single && r != null && repo != null && r != repo) {
                return null;
            } else if (r != null) {
                repo = r;
            }
        } else {
            IWorkingSet workingSet = AdapterUtils.adapt(element, IWorkingSet.class);
            if (workingSet != null) {
                for (IAdaptable adaptable : workingSet.getElements()) {
                    Repository r = getRepositoryOfProject(adaptable);
                    if (single && r != null && repo != null && r != repo) {
                        return null;
                    } else if (r != null) {
                        repo = r;
                    }
                }
            }
        }
    }
    return repo;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IContainer(org.eclipse.core.resources.IContainer) IWorkingSet(org.eclipse.ui.IWorkingSet)

Aggregations

RepositoryMapping (org.eclipse.egit.core.project.RepositoryMapping)87 Repository (org.eclipse.jgit.lib.Repository)40 IResource (org.eclipse.core.resources.IResource)31 IProject (org.eclipse.core.resources.IProject)23 IPath (org.eclipse.core.runtime.IPath)20 IOException (java.io.IOException)18 File (java.io.File)17 CoreException (org.eclipse.core.runtime.CoreException)15 IFile (org.eclipse.core.resources.IFile)12 Path (org.eclipse.core.runtime.Path)12 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)10 GitProjectData (org.eclipse.egit.core.project.GitProjectData)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 HashSet (java.util.HashSet)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 RevWalk (org.eclipse.jgit.revwalk.RevWalk)6 HashMap (java.util.HashMap)5