Update org.apache.commons:commons-compress to 1.25.0
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / branch / DebugUIPluginFacade.java
blob75bc52dd955a293dffcc7ca7e12bb71c058456d5
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
11 * Contributors:
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;
21 import java.util.Set;
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 {
43 @Override
44 public String getRunningLaunchConfigurationName(
45 final Collection<Repository> repositories,
46 IProgressMonitor monitor) {
47 ILaunchConfiguration launch = getRunningLaunchConfiguration(
48 repositories, monitor);
49 if (launch != null)
50 return launch.getName();
52 return null;
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() {
62 @Override
63 public void run(IProgressMonitor m)
64 throws InvocationTargetException, InterruptedException {
65 Set<IProject> projects = new HashSet<>();
66 for (Repository repository : repositories) {
67 projects.addAll(
68 Arrays.asList(ProjectUtil.getProjects(repository)));
70 result[0] = findLaunch(projects, m);
73 try {
74 if (ModalContext.isModalContextThread(Thread.currentThread())) {
75 operation.run(progress);
76 } else {
77 ModalContext.run(operation, true, progress,
78 PlatformUI.getWorkbench().getDisplay());
80 } catch (InvocationTargetException | InterruptedException e) {
81 // ignore
83 return result[0];
86 private static ILaunchConfiguration findLaunch(Set<IProject> projects,
87 IProgressMonitor monitor) {
88 ILaunchManager launchManager = DebugPlugin.getDefault()
89 .getLaunchManager();
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()) {
95 break;
97 if (launch.isTerminated()) {
98 progress.worked(1);
99 continue;
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();
109 } else {
110 progress.worked(1);
113 return null;
116 private static boolean isAnyProjectInSourceContainers(
117 ISourceContainer[] containers, Set<IProject> projects,
118 IProgressMonitor monitor) {
119 if (containers == null) {
120 return false;
122 SubMonitor progress = SubMonitor.convert(monitor, containers.length);
123 for (ISourceContainer container : containers) {
124 if (progress.isCanceled()) {
125 break;
127 if (container instanceof ProjectSourceContainer) {
128 ProjectSourceContainer projectContainer = (ProjectSourceContainer) container;
129 if (projects.contains(projectContainer.getProject())) {
130 progress.worked(1);
131 return true;
134 try {
135 boolean found = isAnyProjectInSourceContainers(
136 container.getSourceContainers(), projects,
137 progress.newChild(1));
138 if (found) {
139 return true;
141 } catch (CoreException e) {
142 // Ignore the child source containers, continue search
145 return false;