1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 Robin Stocker and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
11 * Thomas Wolf <thomas.wolf@paranor.ch>
12 *******************************************************************************/
13 package org
.eclipse
.egit
.ui
.internal
.branch
;
15 import java
.util
.Collection
;
16 import java
.util
.Collections
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.SubMonitor
;
20 import org
.eclipse
.egit
.ui
.Activator
;
21 import org
.eclipse
.egit
.ui
.UIPreferences
;
22 import org
.eclipse
.egit
.ui
.internal
.UIText
;
23 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
25 import org
.eclipse
.jface
.dialogs
.MessageDialogWithToggle
;
26 import org
.eclipse
.jface
.preference
.IPreferenceStore
;
27 import org
.eclipse
.jgit
.annotations
.Nullable
;
28 import org
.eclipse
.jgit
.lib
.Repository
;
29 import org
.eclipse
.osgi
.util
.NLS
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.ui
.PlatformUI
;
34 * Utility class for finding launch configurations.
36 public final class LaunchFinder
{
37 private static final IDebugUIPluginFacade debugPluginFacade
;
40 if (hasDebugUiBundle()) {
41 debugPluginFacade
= new DebugUIPluginFacade();
43 debugPluginFacade
= new NoopDebugUIPluginFacade();
47 private static final boolean hasDebugUiBundle() {
50 "org.eclipse.debug.core.ILaunchConfiguration") != null; //$NON-NLS-1$
51 } catch (ClassNotFoundException e
) {
56 private LaunchFinder() {
57 // Utility class shall not be instantiated
61 * If there is a running launch covering at least one project from the given
62 * repositories, return the name of the first such launch configuration.
65 * to determine projects to be checked whether they are used in
68 * for progress reporting and cancellation
69 * @return the launch name, or {@code null} if none found.
72 public static String
getRunningLaunchConfiguration(
73 final Collection
<Repository
> repositories
,
74 IProgressMonitor monitor
) {
75 return debugPluginFacade
.getRunningLaunchConfigurationName(repositories
,
80 * Checks whether there are any running launches based on projects belonging
81 * to the given repository. If so, asks the user whether to cancel, and
82 * returns the user's choice. The user has the possibility to suppress the
83 * dialog, in which case this method returns {@code false} without checking
84 * for running launches.
87 * to determine projects to be checked whether they are used in
90 * for progress reporting and cancellation
91 * @return {@code true} if the operation should be canceled, {@code false}
94 public static boolean shouldCancelBecauseOfRunningLaunches(
95 Repository repository
, IProgressMonitor monitor
) {
96 return shouldCancelBecauseOfRunningLaunches(
97 Collections
.singleton(repository
), monitor
);
101 * Checks whether there are any running launches based on projects belonging
102 * to the given repositories. If so, asks the user whether to cancel, and
103 * returns the user's choice. The user has the possibility to suppress the
104 * dialog, in which case this method returns {@code false} without checking
105 * for running launches.
107 * @param repositories
108 * to determine projects to be checked whether they are used in
111 * for progress reporting and cancellation
112 * @return {@code true} if the operation should be canceled, {@code false}
115 public static boolean shouldCancelBecauseOfRunningLaunches(
116 Collection
<Repository
> repositories
, IProgressMonitor monitor
) {
117 final IPreferenceStore store
= Activator
.getDefault()
118 .getPreferenceStore();
119 if (!store
.getBoolean(
120 UIPreferences
.SHOW_RUNNING_LAUNCH_ON_CHECKOUT_WARNING
)) {
123 SubMonitor progress
= SubMonitor
.convert(monitor
);
124 final String launchConfiguration
= getRunningLaunchConfiguration(
127 if (launchConfiguration
!= null) {
128 final boolean[] dialogResult
= new boolean[1];
129 PlatformUI
.getWorkbench().getDisplay().syncExec(new Runnable() {
132 dialogResult
[0] = showContinueDialogInUI(store
,
133 launchConfiguration
);
136 return dialogResult
[0];
141 private static boolean showContinueDialogInUI(final IPreferenceStore store
,
142 final String launchConfiguration
) {
143 String
[] buttons
= new String
[] { UIText
.BranchOperationUI_Continue
,
144 IDialogConstants
.CANCEL_LABEL
};
145 String message
= NLS
.bind(UIText
.LaunchFinder_RunningLaunchMessage
,
146 launchConfiguration
) + ' '
147 + UIText
.LaunchFinder_ContinueQuestion
;
148 MessageDialogWithToggle continueDialog
= new MessageDialogWithToggle(
149 PlatformUI
.getWorkbench().getModalDialogShellProvider()
151 UIText
.LaunchFinder_RunningLaunchTitle
, null,
152 message
, MessageDialog
.NONE
, buttons
, 0,
153 UIText
.LaunchFinder_RunningLaunchDontShowAgain
, false);
154 int result
= continueDialog
.open();
156 if (result
== IDialogConstants
.CANCEL_ID
|| result
== SWT
.DEFAULT
)
158 boolean dontWarnAgain
= continueDialog
.getToggleState();
161 UIPreferences
.SHOW_RUNNING_LAUNCH_ON_CHECKOUT_WARNING
,