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
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
;
34 * Property tester for whole selections.
36 public class SelectionPropertyTester
extends PropertyTester
{
39 public boolean test(Object receiver
, String property
, Object
[] args
,
40 Object expectedValue
) {
41 Collection
<?
> collection
= (Collection
<?
>) receiver
;
42 if (collection
.isEmpty())
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)
68 IStructuredSelection selection
= getStructuredSelection(collection
);
69 if (selection
.size() != 1)
72 Object firstElement
= selection
.getFirstElement();
74 IResource resource
= AdapterUtils
.adapt(firstElement
,
76 if ((resource
!= null) && (resource
instanceof IFile
77 || resource
instanceof IFolder
)) {
78 RepositoryMapping m
= RepositoryMapping
.getMapping(resource
);
80 if ((resource
instanceof IFolder
)
81 && resource
.equals(m
.getContainer())) {
84 return testRepositoryProperties(m
.getRepository(),
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
);
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
);
106 return new StructuredSelection(new ArrayList
<>(collection
));
109 private static boolean testRepositoryProperties(Repository repository
,
110 Object
[] properties
) {
111 if (repository
== null)
114 for (Object arg
: properties
) {
115 String s
= (String
) arg
;
116 if (!ResourcePropertyTester
.testRepositoryState(repository
, s
))
124 * the selected elements
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
,
131 Repository repo
= null;
132 for (Object element
: collection
) {
133 IContainer container
= AdapterUtils
.adapt(element
,
135 RepositoryMapping mapping
= null;
136 if (container
!= null) {
137 mapping
= RepositoryMapping
.getMapping(container
);
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
) {
149 } else if (r
!= null) {
153 IWorkingSet workingSet
= AdapterUtils
.adapt(element
,
155 if (workingSet
!= null) {
156 for (IAdaptable adaptable
: workingSet
.getElements()) {
157 Repository r
= getRepositoryOfProject(adaptable
);
158 if (single
&& r
!= null && repo
!= null && r
!= repo
) {
160 } else if (r
!= null) {
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
)
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
);
204 private static Repository
getRepositoryOfProject(Object object
) {
205 IProject project
= AdapterUtils
.adapt(object
, IProject
.class);
207 return getRepositoryOfMapping(project
);
211 private static Repository
getRepositoryOfMapping(IResource resource
) {
212 RepositoryMapping mapping
= RepositoryMapping
.getMapping(resource
);
214 return mapping
.getRepository();