Team->Pull and other menus are missing for most projects
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / selection / SelectionPropertyTester.java
blobd706aa52df6a5b31433dcbd020326f8d66fafbc0
1 /*******************************************************************************
2 * Copyright (C) 2014, 2015 Robin Stocker <robin@nibor.org> and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors:
10 * Andre Bossert <anb0s@anbos.de> - Bug 496356
11 *******************************************************************************/
12 package org.eclipse.egit.ui.internal.selection;
14 import java.util.ArrayList;
15 import java.util.Collection;
17 import org.eclipse.core.expressions.PropertyTester;
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IFolder;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.egit.core.AdapterUtils;
25 import org.eclipse.egit.core.project.RepositoryMapping;
26 import org.eclipse.egit.ui.internal.ResourcePropertyTester;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.jgit.lib.Repository;
31 import org.eclipse.ui.IWorkingSet;
33 /**
34 * Property tester for whole selections.
36 public class SelectionPropertyTester extends PropertyTester {
38 @Override
39 public boolean test(Object receiver, String property, Object[] args,
40 Object expectedValue) {
41 Collection<?> collection = (Collection<?>) receiver;
42 if (collection.isEmpty())
43 return false;
44 if ("projectsSingleRepository".equals(property)) { //$NON-NLS-1$
46 Repository repository = getRepositoryOfProjects(collection, true);
47 return testRepositoryProperties(repository, args);
49 } else if ("projectsWithRepositories".equals(property)) { //$NON-NLS-1$
50 Repository repository = getRepositoryOfProjects(collection, false);
51 return repository != null;
53 } else if ("resourcesSingleRepository".equals(property)) { //$NON-NLS-1$
54 IStructuredSelection selection = getStructuredSelection(collection);
56 // It may seem like we could just use SelectionUtils.getRepository
57 // here. The problem: It would also return a repository for a node
58 // in the repo view. But this property is just for resources.
59 IResource[] resources = SelectionUtils
60 .getSelectedResources(selection);
61 Repository repository = getRepositoryOfResources(resources);
62 return testRepositoryProperties(repository, args);
64 } else if ("fileOrFolderInRepository".equals(property)) { //$NON-NLS-1$
65 if (collection.size() != 1)
66 return false;
68 IStructuredSelection selection = getStructuredSelection(collection);
69 if (selection.size() != 1)
70 return false;
72 Object firstElement = selection.getFirstElement();
74 IResource resource = AdapterUtils.adapt(firstElement,
75 IResource.class);
76 if ((resource != null) && (resource instanceof IFile
77 || resource instanceof IFolder)) {
78 RepositoryMapping m = RepositoryMapping.getMapping(resource);
79 if (m != null) {
80 if ((resource instanceof IFolder)
81 && resource.equals(m.getContainer())) {
82 return false;
83 } else {
84 return testRepositoryProperties(m.getRepository(),
85 args);
89 } else if ("resourcesAllInRepository".equals(property)) { //$NON-NLS-1$
90 IStructuredSelection selection = getStructuredSelection(collection);
92 IResource[] resources = SelectionUtils
93 .getSelectedResources(selection);
94 return haveRepositories(resources);
96 return false;
99 private static IStructuredSelection getStructuredSelection(
100 Collection<?> collection) {
101 Object firstElement = collection.iterator().next();
102 if (collection.size() == 1 && firstElement instanceof ITextSelection)
103 return SelectionUtils
104 .getStructuredSelection((ITextSelection) firstElement);
105 else
106 return new StructuredSelection(new ArrayList<>(collection));
109 private static boolean testRepositoryProperties(Repository repository,
110 Object[] properties) {
111 if (repository == null)
112 return false;
114 for (Object arg : properties) {
115 String s = (String) arg;
116 if (!ResourcePropertyTester.testRepositoryState(repository, s))
117 return false;
119 return true;
123 * @param collection
124 * the selected elements
125 * @param single
126 * <code>true</code> if only a single repository is allowed
127 * @return the repository if any was found, <code>null</code> otherwise
129 private static Repository getRepositoryOfProjects(Collection<?> collection,
130 boolean single) {
131 Repository repo = null;
132 for (Object element : collection) {
133 IContainer container = AdapterUtils.adapt(element,
134 IProject.class);
135 RepositoryMapping mapping = null;
136 if (container != null) {
137 mapping = RepositoryMapping.getMapping(container);
138 } else {
139 container = AdapterUtils.adapt(element, IContainer.class);
140 if (container != null) {
141 mapping = RepositoryMapping.getMapping(container);
144 if (container != null && mapping != null
145 && container.equals(mapping.getContainer())) {
146 Repository r = mapping.getRepository();
147 if (single && r != null && repo != null && r != repo) {
148 return null;
149 } else if (r != null) {
150 repo = r;
152 } else {
153 IWorkingSet workingSet = AdapterUtils.adapt(element,
154 IWorkingSet.class);
155 if (workingSet != null) {
156 for (IAdaptable adaptable : workingSet.getElements()) {
157 Repository r = getRepositoryOfProject(adaptable);
158 if (single && r != null && repo != null && r != repo) {
159 return null;
160 } else if (r != null) {
161 repo = r;
167 return repo;
171 * @param resources
172 * the resources
173 * @return the repository that all the mapped resources map to,
174 * <code>null</code> otherwise
176 private static Repository getRepositoryOfResources(IResource[] resources) {
177 Repository repo = null;
178 for (IResource resource : resources) {
179 Repository r = getRepositoryOfMapping(resource);
180 if (r != null && repo != null && r != repo)
181 return null;
182 else if (r != null)
183 repo = r;
185 return repo;
189 * @param resources
190 * the resources
191 * @return {@code true} when all {@code resources} map to a repository,
192 * {@code false} otherwise.
194 private static boolean haveRepositories(IResource[] resources) {
195 for (IResource resource : resources) {
196 Repository r = getRepositoryOfMapping(resource);
197 if (r == null) {
198 return false;
201 return true;
204 private static Repository getRepositoryOfProject(Object object) {
205 IProject project = AdapterUtils.adapt(object, IProject.class);
206 if (project != null)
207 return getRepositoryOfMapping(project);
208 return null;
211 private static Repository getRepositoryOfMapping(IResource resource) {
212 RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
213 if (mapping != null)
214 return mapping.getRepository();
215 return null;