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
|
/*******************************************************************************
* Copyright (c) 2002 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM - Initial implementation
******************************************************************************/
package org.eclipse.team.internal.ccvs.ui;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
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 {
private IResource[] resources;
private Shell shell;
private String[] buttons;
private boolean confirmOverwrite = true;
private IPromptCondition condition;
private String title;
/**
* Prompt for the given resources using the specific condition. The prompt dialog will
* have the title specified.
*/
public PromptingDialog(Shell shell, IResource[] resources, IPromptCondition condition, String title) {
this.condition = condition;
this.resources = resources;
this.title = title;
this.shell = shell;
if(resources.length == 1) {
buttons = new String[] {IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL};
} else {
buttons = new String[] {
IDialogConstants.YES_LABEL,
IDialogConstants.NO_LABEL,
IDialogConstants.YES_TO_ALL_LABEL,
IDialogConstants.CANCEL_LABEL};
}
}
/**
* 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.
*/
public IResource[] promptForMultiple() throws InterruptedException {
List targetResources = new ArrayList();
for (int i = 0; i < resources.length; i++) {
IResource resource = resources[i];
if (condition.needsPrompt(resource) && confirmOverwrite) {
if (confirmOverwrite(condition.promptMessage(resource))) {
targetResources.add(resource);
}
} else {
targetResources.add(resource);
}
}
return (IResource[]) targetResources.toArray(new IResource[targetResources.size()]);
}
/**
* A helper prompt condition for prompting for CVS dirty state.
*/
public static IPromptCondition getOverwriteLocalChangesPrompt() {
return new IPromptCondition() {
public boolean needsPrompt(IResource resource) {
return CVSDecorator.isDirty(resource);
}
public String promptMessage(IResource resource) {
return Policy.bind("ReplaceWithAction.localChanges", resource.getName());//$NON-NLS-1$
}
};
}
/**
* Opens the confirmation dialog based on the prompt condition settings.
*/
private boolean confirmOverwrite(String msg) throws InterruptedException {
if (!confirmOverwrite) {
return true;
}
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();
}
});
switch (dialog.getReturnCode()) {
case 0://Yes
return true;
case 1://No or Cancel
return false;
case 2://Yes to all
confirmOverwrite = false;
return true;
case 3://Cancel
default:
throw new InterruptedException();
}
}
}
|