Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 036770dbd39b61c83a4c88173a673c5e7c0a423f (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ui.dialogs;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.widgets.Shell;

/**
 * This class provides a Yes/No prompter that can be used for multiple questions
 * during the same operation. It can be used for a single prompt (in which case
 * OK and Cancel are presented) or multiple (in which case Yes, Yes to All, No 
 * and No to All are presented). It uses the previous selection as appropriate.
 */
public class MultipleYesNoPrompter {

	private static final int ALWAYS_ASK = 0;
	private static final int YES_TO_ALL = 1;
	private static final int NO_TO_ALL = 2;
	private String[] buttons;
	private int confirmation = ALWAYS_ASK;
	private String title;
	private boolean hasMultiple;
	private boolean allOrNothing;
	private IShellProvider shellProvider;
	
	/**
	 * Prompt for the given resources using the specific condition. The prompt dialog will
	 * have the title specified.
	 */
	public MultipleYesNoPrompter(IShellProvider provider, String title, boolean hasMultiple, boolean allOrNothing) {
		this.title = title;
		this.shellProvider = provider;
		this.hasMultiple = hasMultiple;
		this.allOrNothing = allOrNothing;
		if (hasMultiple) {
			if (allOrNothing) {
				buttons = new String[] {
					IDialogConstants.YES_LABEL, 
					IDialogConstants.YES_TO_ALL_LABEL,
					IDialogConstants.CANCEL_LABEL};
			} else {
				buttons = new String[] {
					IDialogConstants.YES_LABEL, 
					IDialogConstants.YES_TO_ALL_LABEL, 
					IDialogConstants.NO_LABEL, 
					IDialogConstants.NO_TO_ALL_LABEL,
					IDialogConstants.CANCEL_LABEL};
			}
		} else {
			buttons = new String[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL};
		}	 
	}
	
	/**
	 * Return whether the given resource should be included in the
	 * target set.
	 * @param message the message
	 * @return whether the resource should be included
	 * @throws InterruptedException if the user choose to cancel
	 */
	public boolean shouldInclude(String message) throws InterruptedException {
		if (confirmation == YES_TO_ALL) {
			return true;
		} else {
			switch (confirmation) {
				case ALWAYS_ASK: {
					// This call has the nasty side effect of changing the
					// instance scoped "confirmation"
					if (confirmOverwrite(message)) {
						return true;
					}
					break;
				}
				case YES_TO_ALL: {
					return true;
				}
				case NO_TO_ALL: {
					// Don't overwrite
					break;
				}
			}
			// If we get here, the user said no or not_to_all.
			return false;
		}
	}

	/**
	 * Opens the confirmation dialog based on the prompt condition settings.
	 */
	private boolean confirmOverwrite(String msg) throws InterruptedException {
		Shell shell = shellProvider.getShell();
		if (shell == null) return false;
		final MessageDialog dialog = 
			new MessageDialog(shell, title, null, msg, MessageDialog.QUESTION, buttons, 0);
	
		// run in syncExec because callback is from an operation,
		// which is probably not running in the UI thread.
		shell.getDisplay().syncExec(
			new Runnable() {
				public void run() {
					dialog.open();
				}
			});
		if (hasMultiple) {
			switch (dialog.getReturnCode()) {
				case 0://Yes
					return true;
				case 1://Yes to all
					confirmation = YES_TO_ALL; 
					return true;
				case 2://No (or CANCEL for all-or-nothing)
					if (allOrNothing) {
						throw new InterruptedException();
					}
					return false;
				case 3://No to all
					confirmation = NO_TO_ALL;
					return false;
				case 4://Cancel
				default:
					throw new InterruptedException();
			}
		} else {
			return dialog.getReturnCode() == 0;
		}
	}

	public void setTitle(String title) {
		this.title = title;
	}

}

Back to the top