KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > dialogs > MultipleYesNoPrompter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui.dialogs;
12
13 import org.eclipse.jface.dialogs.IDialogConstants;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.window.IShellProvider;
16 import org.eclipse.swt.widgets.Shell;
17
18 /**
19  * This class provides a Yes/No prompter that can be used for multiple questions
20  * during the same operation. It can be used for a single prompt (in which case
21  * OK and Cancel are presented) or multiple (in which case Yes, Yes to All, No
22  * and No to All are presented). It uses the previous selection as appropriate.
23  */

24 public class MultipleYesNoPrompter {
25
26     private static final int ALWAYS_ASK = 0;
27     private static final int YES_TO_ALL = 1;
28     private static final int NO_TO_ALL = 2;
29     private String JavaDoc[] buttons;
30     private int confirmation = ALWAYS_ASK;
31     private String JavaDoc title;
32     private boolean hasMultiple;
33     private boolean allOrNothing;
34     private IShellProvider shellProvider;
35     
36     /**
37      * Prompt for the given resources using the specific condition. The prompt dialog will
38      * have the title specified.
39      */

40     public MultipleYesNoPrompter(IShellProvider provider, String JavaDoc title, boolean hasMultiple, boolean allOrNothing) {
41         this.title = title;
42         this.shellProvider = provider;
43         this.hasMultiple = hasMultiple;
44         this.allOrNothing = allOrNothing;
45         if (hasMultiple) {
46             if (allOrNothing) {
47                 buttons = new String JavaDoc[] {
48                     IDialogConstants.YES_LABEL,
49                     IDialogConstants.YES_TO_ALL_LABEL,
50                     IDialogConstants.CANCEL_LABEL};
51             } else {
52                 buttons = new String JavaDoc[] {
53                     IDialogConstants.YES_LABEL,
54                     IDialogConstants.YES_TO_ALL_LABEL,
55                     IDialogConstants.NO_LABEL,
56                     IDialogConstants.NO_TO_ALL_LABEL,
57                     IDialogConstants.CANCEL_LABEL};
58             }
59         } else {
60             buttons = new String JavaDoc[] {
61                     IDialogConstants.YES_LABEL,
62                     IDialogConstants.NO_LABEL,
63                     IDialogConstants.CANCEL_LABEL
64             };
65         }
66     }
67     
68     /**
69      * Return whether the given resource should be included in the
70      * target set.
71      * @param message the message
72      * @return whether the resource should be included
73      * @throws InterruptedException if the user choose to cancel
74      */

75     public boolean shouldInclude(String JavaDoc message) throws InterruptedException JavaDoc {
76         if (confirmation == YES_TO_ALL) {
77             return true;
78         } else {
79             switch (confirmation) {
80                 case ALWAYS_ASK: {
81                     // This call has the nasty side effect of changing the
82
// instance scoped "confirmation"
83
if (confirmOverwrite(message)) {
84                         return true;
85                     }
86                     break;
87                 }
88                 case YES_TO_ALL: {
89                     return true;
90                 }
91                 case NO_TO_ALL: {
92                     // Don't overwrite
93
break;
94                 }
95             }
96             // If we get here, the user said no or not_to_all.
97
return false;
98         }
99     }
100
101     /**
102      * Opens the confirmation dialog based on the prompt condition settings.
103      */

104     private boolean confirmOverwrite(String JavaDoc msg) throws InterruptedException JavaDoc {
105         Shell shell = shellProvider.getShell();
106         if (shell == null) return false;
107         final MessageDialog dialog =
108             new MessageDialog(shell, title, null, msg, MessageDialog.QUESTION, buttons, 0);
109     
110         // run in syncExec because callback is from an operation,
111
// which is probably not running in the UI thread.
112
shell.getDisplay().syncExec(
113             new Runnable JavaDoc() {
114                 public void run() {
115                     dialog.open();
116                 }
117             });
118         if (hasMultiple) {
119             switch (dialog.getReturnCode()) {
120                 case 0://Yes
121
return true;
122                 case 1://Yes to all
123
confirmation = YES_TO_ALL;
124                     return true;
125                 case 2://No (or CANCEL for all-or-nothing)
126
if (allOrNothing) {
127                         throw new InterruptedException JavaDoc();
128                     }
129                     return false;
130                 case 3://No to all
131
confirmation = NO_TO_ALL;
132                     return false;
133                 case 4://Cancel
134
default:
135                     throw new InterruptedException JavaDoc();
136             }
137         } else {
138             switch (dialog.getReturnCode()) {
139             case 0:// Yes
140
return true;
141             case 1:// No
142
return false;
143             case 2:// Cancel
144
default:
145                 throw new InterruptedException JavaDoc();
146             }
147         }
148     }
149
150     public void setTitle(String JavaDoc title) {
151         this.title = title;
152     }
153
154 }
155
Popular Tags