1 /*******************************************************************************
2 * Copyright (C) 2019, Peter Severin <peter@wireframesketcher.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * Thomas Wolf <thomas.wolf@paranor.ch>
13 * Peter Severin <peter@wireframesketcher.com> - Bug 546329
14 *******************************************************************************/
15 package org
.eclipse
.egit
.ui
.internal
.branch
;
17 import java
.lang
.reflect
.InvocationTargetException
;
18 import java
.util
.Arrays
;
19 import java
.util
.Collection
;
20 import java
.util
.HashSet
;
23 import org
.eclipse
.core
.resources
.IProject
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.SubMonitor
;
27 import org
.eclipse
.debug
.core
.DebugPlugin
;
28 import org
.eclipse
.debug
.core
.ILaunch
;
29 import org
.eclipse
.debug
.core
.ILaunchConfiguration
;
30 import org
.eclipse
.debug
.core
.ILaunchManager
;
31 import org
.eclipse
.debug
.core
.model
.ISourceLocator
;
32 import org
.eclipse
.debug
.core
.sourcelookup
.ISourceContainer
;
33 import org
.eclipse
.debug
.core
.sourcelookup
.ISourceLookupDirector
;
34 import org
.eclipse
.debug
.core
.sourcelookup
.containers
.ProjectSourceContainer
;
35 import org
.eclipse
.egit
.core
.internal
.util
.ProjectUtil
;
36 import org
.eclipse
.egit
.ui
.internal
.UIText
;
37 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
38 import org
.eclipse
.jface
.operation
.ModalContext
;
39 import org
.eclipse
.jgit
.lib
.Repository
;
40 import org
.eclipse
.ui
.PlatformUI
;
42 class DebugUIPluginFacade
implements IDebugUIPluginFacade
{
44 public String
getRunningLaunchConfigurationName(
45 final Collection
<Repository
> repositories
,
46 IProgressMonitor monitor
) {
47 ILaunchConfiguration launch
= getRunningLaunchConfiguration(
48 repositories
, monitor
);
50 return launch
.getName();
55 public static ILaunchConfiguration
getRunningLaunchConfiguration(
56 final Collection
<Repository
> repositories
,
57 IProgressMonitor monitor
) {
58 SubMonitor progress
= SubMonitor
.convert(monitor
, 1);
59 final ILaunchConfiguration
[] result
= { null };
60 IRunnableWithProgress operation
= new IRunnableWithProgress() {
63 public void run(IProgressMonitor m
)
64 throws InvocationTargetException
, InterruptedException
{
65 Set
<IProject
> projects
= new HashSet
<>();
66 for (Repository repository
: repositories
) {
68 Arrays
.asList(ProjectUtil
.getProjects(repository
)));
70 result
[0] = findLaunch(projects
, m
);
74 if (ModalContext
.isModalContextThread(Thread
.currentThread())) {
75 operation
.run(progress
);
77 ModalContext
.run(operation
, true, progress
,
78 PlatformUI
.getWorkbench().getDisplay());
80 } catch (InvocationTargetException
| InterruptedException e
) {
86 private static ILaunchConfiguration
findLaunch(Set
<IProject
> projects
,
87 IProgressMonitor monitor
) {
88 ILaunchManager launchManager
= DebugPlugin
.getDefault()
90 ILaunch
[] launches
= launchManager
.getLaunches();
91 SubMonitor progress
= SubMonitor
.convert(monitor
,
92 UIText
.LaunchFinder_SearchLaunchConfiguration
, launches
.length
);
93 for (ILaunch launch
: launches
) {
94 if (progress
.isCanceled()) {
97 if (launch
.isTerminated()) {
101 ISourceLocator locator
= launch
.getSourceLocator();
102 if (locator
instanceof ISourceLookupDirector
) {
103 ISourceLookupDirector director
= (ISourceLookupDirector
) locator
;
104 ISourceContainer
[] containers
= director
.getSourceContainers();
105 if (isAnyProjectInSourceContainers(containers
, projects
,
106 progress
.newChild(1))) {
107 return launch
.getLaunchConfiguration();
116 private static boolean isAnyProjectInSourceContainers(
117 ISourceContainer
[] containers
, Set
<IProject
> projects
,
118 IProgressMonitor monitor
) {
119 if (containers
== null) {
122 SubMonitor progress
= SubMonitor
.convert(monitor
, containers
.length
);
123 for (ISourceContainer container
: containers
) {
124 if (progress
.isCanceled()) {
127 if (container
instanceof ProjectSourceContainer
) {
128 ProjectSourceContainer projectContainer
= (ProjectSourceContainer
) container
;
129 if (projects
.contains(projectContainer
.getProject())) {
135 boolean found
= isAnyProjectInSourceContainers(
136 container
.getSourceContainers(), projects
,
137 progress
.newChild(1));
141 } catch (CoreException e
) {
142 // Ignore the child source containers, continue search