Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e9453943df0028e57b422597a8fa7d4002b7b938 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.dialogs;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.widgets.Shell;

/**
 * A confirmation dialog helper that will either show a 'yes/no/yes to all/cancel'
 * dialog to confirm an action performed on several resources or if only one
 * resource is specified 'ok/cancel' will be shown.
 */
public class PromptingDialog extends MultipleYesNoPrompter {
	private IPromptCondition condition;
	private IResource[] resources;
	/**
	 * Prompt for the given resources using the specific condition. The prompt dialog will
	 * have the title specified.
	 * @param shell
	 * @param resources
	 * @param condition
	 * @param title
	 */
	public PromptingDialog(Shell shell, IResource[] resources, IPromptCondition condition, String title) {
		this(shell, resources, condition, title, false /* all or nothing */);
	}

	public PromptingDialog(final Shell shell, IResource[] resources, IPromptCondition condition, String title, boolean allOrNothing) {
		super(new IShellProvider() {
			@Override
			public Shell getShell() {
				return shell;
			}
		}, title, resources.length > 1, allOrNothing);
		this.resources = resources;
		this.condition = condition;
	}

	/**
	 * Call to calculate and show prompt. If no resources satisfy the prompt
	 * condition a dialog won't be shown. The resources for which the user
	 * confirmed the action are returned.
	 * @return the resources
	 *
	 * @throws InterruptedException
	 *             if the user choose to cancel on the prompt dialog
	 */
	public IResource[] promptForMultiple() throws InterruptedException {
		List<IResource> targetResources = new ArrayList<>();
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			if (!condition.needsPrompt(resource) || shouldInclude(condition.promptMessage(resource))) {
				targetResources.add(resource);
			}
		}
		return targetResources.toArray(new IResource[targetResources.size()]);
	}
}

Back to the top