Update org.apache.commons:commons-compress to 1.25.0
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / branch / LaunchFinder.java
blob1a681d20e64167cdf1b40df4b55c4c7f3a7f2652
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
10 * Contributors:
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;
33 /**
34 * Utility class for finding launch configurations.
36 public final class LaunchFinder {
37 private static final IDebugUIPluginFacade debugPluginFacade;
39 static {
40 if (hasDebugUiBundle()) {
41 debugPluginFacade = new DebugUIPluginFacade();
42 } else {
43 debugPluginFacade = new NoopDebugUIPluginFacade();
47 private static final boolean hasDebugUiBundle() {
48 try {
49 return Class.forName(
50 "org.eclipse.debug.core.ILaunchConfiguration") != null; //$NON-NLS-1$
51 } catch (ClassNotFoundException e) {
52 return false;
56 private LaunchFinder() {
57 // Utility class shall not be instantiated
60 /**
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.
64 * @param repositories
65 * to determine projects to be checked whether they are used in
66 * running launches
67 * @param monitor
68 * for progress reporting and cancellation
69 * @return the launch name, or {@code null} if none found.
71 @Nullable
72 public static String getRunningLaunchConfiguration(
73 final Collection<Repository> repositories,
74 IProgressMonitor monitor) {
75 return debugPluginFacade.getRunningLaunchConfigurationName(repositories,
76 monitor);
79 /**
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.
86 * @param repository
87 * to determine projects to be checked whether they are used in
88 * running launches
89 * @param monitor
90 * for progress reporting and cancellation
91 * @return {@code true} if the operation should be canceled, {@code false}
92 * otherwise
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
109 * running launches
110 * @param monitor
111 * for progress reporting and cancellation
112 * @return {@code true} if the operation should be canceled, {@code false}
113 * otherwise
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)) {
121 return false;
123 SubMonitor progress = SubMonitor.convert(monitor);
124 final String launchConfiguration = getRunningLaunchConfiguration(
125 repositories,
126 progress);
127 if (launchConfiguration != null) {
128 final boolean[] dialogResult = new boolean[1];
129 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
130 @Override
131 public void run() {
132 dialogResult[0] = showContinueDialogInUI(store,
133 launchConfiguration);
136 return dialogResult[0];
138 return false;
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()
150 .getShell(),
151 UIText.LaunchFinder_RunningLaunchTitle, null,
152 message, MessageDialog.NONE, buttons, 0,
153 UIText.LaunchFinder_RunningLaunchDontShowAgain, false);
154 int result = continueDialog.open();
155 // cancel
156 if (result == IDialogConstants.CANCEL_ID || result == SWT.DEFAULT)
157 return true;
158 boolean dontWarnAgain = continueDialog.getToggleState();
159 if (dontWarnAgain)
160 store.setValue(
161 UIPreferences.SHOW_RUNNING_LAUNCH_ON_CHECKOUT_WARNING,
162 false);
163 return false;