1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring.reorg;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.layout.GridData;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.OperationCanceledException;
22 import org.eclipse.jface.dialogs.IDialogConstants;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.viewers.ArrayContentProvider;
25 import org.eclipse.jface.wizard.Wizard;
27 import org.eclipse.ui.dialogs.ListDialog;
29 import org.eclipse.jdt.internal.corext.refactoring.reorg.IConfirmQuery;
30 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgQueries;
32 import org.eclipse.jdt.ui.JavaElementLabelProvider;
35 public class ReorgQueries implements IReorgQueries {
37 private final Wizard fWizard;
38 private final Shell fShell;
40 public ReorgQueries(Wizard wizard){
41 Assert.isNotNull(wizard);
46 public ReorgQueries(Shell shell){
47 Assert.isNotNull(shell);
52 private Shell getShell() {
53 Assert.isTrue(fShell == null || fWizard == null);
54 Assert.isTrue(fShell != null || fWizard != null);
56 return fWizard.getContainer().getShell();
62 * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries#createYesYesToAllNoNoToAllQuery(java.lang.String)
64 public IConfirmQuery createYesYesToAllNoNoToAllQuery(String dialogTitle, boolean allowCancel, int queryID) {
65 return new YesYesToAllNoNoToAllQuery(getShell(), allowCancel, dialogTitle);
69 * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries#createYesNoQuery(java.lang.String)
71 public IConfirmQuery createYesNoQuery(String dialogTitle, boolean allowCancel, int queryID) {
72 return new YesNoQuery(getShell(), allowCancel, dialogTitle);
75 public IConfirmQuery createSkipQuery(String dialogTitle, int queryID) {
76 return new SkipQuery(getShell(), dialogTitle);
79 private static class YesYesToAllNoNoToAllQuery implements IConfirmQuery{
80 private final boolean fAllowCancel;
81 private boolean fYesToAll= false;
82 private boolean fNoToAll= false;
83 private final Shell fShell;
84 private final String fDialogTitle;
86 YesYesToAllNoNoToAllQuery(Shell parent, boolean allowCancel, String dialogTitle){
88 fDialogTitle= dialogTitle;
89 fAllowCancel= allowCancel;
93 * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
95 public boolean confirm(final String question) throws OperationCanceledException {
102 final int[] result= new int[1];
103 fShell.getDisplay().syncExec(createQueryRunnable(question, result));
104 return getResult(result);
108 * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
110 public boolean confirm(String question, Object[] elements) throws OperationCanceledException {
117 final int[] result= new int[1];
118 fShell.getDisplay().syncExec(createQueryRunnable(question, elements, result));
119 return getResult(result);
122 private Runnable createQueryRunnable(final String question, final int[] result) {
123 return new Runnable() {
125 int[] resultId= getResultIDs();
127 MessageDialog dialog= new MessageDialog(
132 MessageDialog.QUESTION,
137 if (dialog.getReturnCode() == -1) { //MessageDialog closed without choice => cancel | no
138 //see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=48400
139 result[0]= fAllowCancel ? IDialogConstants.CANCEL_ID : IDialogConstants.NO_ID;
141 result[0]= resultId[dialog.getReturnCode()];
145 private String[] getButtonLabels() {
146 if (YesYesToAllNoNoToAllQuery.this.fAllowCancel)
147 return new String[] {
148 IDialogConstants.YES_LABEL,
149 IDialogConstants.YES_TO_ALL_LABEL,
150 IDialogConstants.NO_LABEL,
151 IDialogConstants.NO_TO_ALL_LABEL,
152 IDialogConstants.CANCEL_LABEL };
154 return new String[] {
155 IDialogConstants.YES_LABEL,
156 IDialogConstants.YES_TO_ALL_LABEL,
157 IDialogConstants.NO_LABEL,
158 IDialogConstants.NO_TO_ALL_LABEL};
161 private int[] getResultIDs() {
162 if (YesYesToAllNoNoToAllQuery.this.fAllowCancel)
164 IDialogConstants.YES_ID,
165 IDialogConstants.YES_TO_ALL_ID,
166 IDialogConstants.NO_ID,
167 IDialogConstants.NO_TO_ALL_ID,
168 IDialogConstants.CANCEL_ID};
171 IDialogConstants.YES_ID,
172 IDialogConstants.YES_TO_ALL_ID,
173 IDialogConstants.NO_ID,
174 IDialogConstants.NO_TO_ALL_ID};
179 private Runnable createQueryRunnable(final String question, final Object[] elements, final int[] result) {
180 return new Runnable() {
182 ListDialog dialog= new YesNoListDialog(fShell, true);
183 dialog.setAddCancelButton(false);
184 dialog.setBlockOnOpen(true);
185 dialog.setContentProvider(new ArrayContentProvider());
186 dialog.setLabelProvider(new JavaElementLabelProvider());
187 dialog.setTitle(fDialogTitle);
188 dialog.setMessage(question);
189 dialog.setInput(elements);
192 result[0]= dialog.getReturnCode();
197 private boolean getResult(int[] result) throws OperationCanceledException {
199 case IDialogConstants.YES_TO_ALL_ID:
202 case IDialogConstants.YES_ID:
204 case IDialogConstants.CANCEL_ID:
205 throw new OperationCanceledException();
206 case IDialogConstants.NO_ID:
208 case IDialogConstants.NO_TO_ALL_ID:
212 Assert.isTrue(false);
218 private static class YesNoQuery implements IConfirmQuery{
220 private final Shell fShell;
221 private final String fDialogTitle;
222 private final boolean fAllowCancel;
224 YesNoQuery(Shell parent, boolean allowCancel, String dialogTitle){
226 fDialogTitle= dialogTitle;
227 fAllowCancel= allowCancel;
231 * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
233 public boolean confirm(String question) throws OperationCanceledException {
234 final int[] result= new int[1];
235 fShell.getDisplay().syncExec(createQueryRunnable(question, result));
236 return getResult(result);
240 * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
242 public boolean confirm(String question, Object[] elements) throws OperationCanceledException {
243 final int[] result= new int[1];
244 fShell.getDisplay().syncExec(createQueryRunnable(question, elements, result));
245 return getResult(result);
248 private Runnable createQueryRunnable(final String question, final int[] result){
249 return new Runnable() {
251 MessageDialog dialog= new MessageDialog(
256 MessageDialog.QUESTION,
261 switch (dialog.getReturnCode()) {
262 case -1 : //MessageDialog closed without choice => cancel | no
263 //see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=48400
264 result[0]= fAllowCancel ? IDialogConstants.CANCEL_ID : IDialogConstants.NO_ID;
267 result[0]= IDialogConstants.YES_ID;
270 result[0]= IDialogConstants.NO_ID;
274 result[0]= IDialogConstants.CANCEL_ID;
276 Assert.isTrue(false);
279 Assert.isTrue(false);
284 private String[] getButtonLabels() {
286 return new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
288 return new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL};
293 private Runnable createQueryRunnable(final String question, final Object[] elements, final int[] result) {
294 return new Runnable() {
296 ListDialog dialog= new YesNoListDialog(fShell, false);
297 dialog.setAddCancelButton(false);
298 dialog.setBlockOnOpen(true);
299 dialog.setContentProvider(new ArrayContentProvider());
300 dialog.setLabelProvider(new JavaElementLabelProvider());
301 dialog.setTitle(fDialogTitle);
302 dialog.setMessage(question);
303 dialog.setInput(elements);
306 result[0]= dialog.getReturnCode();
311 private boolean getResult(int[] result) throws OperationCanceledException {
313 case IDialogConstants.YES_ID:
315 case IDialogConstants.CANCEL_ID:
316 throw new OperationCanceledException();
317 case IDialogConstants.NO_ID:
320 Assert.isTrue(false);
326 private static class SkipQuery implements IConfirmQuery{
328 private final Shell fShell;
329 private final String fDialogTitle;
330 private boolean fSkipAll;
332 SkipQuery(Shell parent, String dialogTitle){
334 fDialogTitle= dialogTitle;
339 * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
341 public boolean confirm(String question) throws OperationCanceledException {
344 final int[] result= new int[1];
345 fShell.getDisplay().syncExec(createQueryRunnable(question, result));
346 return getResult(result);
350 * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
352 public boolean confirm(String question, Object[] elements) throws OperationCanceledException {
353 throw new UnsupportedOperationException("Not supported for skip queries"); //$NON-NLS-1$
356 private Runnable createQueryRunnable(final String question, final int[] result){
357 return new Runnable() {
359 MessageDialog dialog= new MessageDialog(
364 MessageDialog.QUESTION,
369 switch (dialog.getReturnCode()) {
370 case -1 : //MessageDialog closed without choice => cancel | no
371 //see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=48400
372 result[0]= IDialogConstants.CANCEL_ID;
375 result[0]= dialog.getReturnCode();
379 private String[] getButtonLabels() {
380 return new String[] {IDialogConstants.SKIP_LABEL, ReorgMessages.ReorgQueries_skip_all, IDialogConstants.CANCEL_LABEL};
385 private boolean getResult(int[] result) throws OperationCanceledException {
396 throw new OperationCanceledException();
403 private static final class YesNoListDialog extends ListDialog {
404 private final boolean fYesToAllNoToAll;
406 private YesNoListDialog(Shell parent, boolean includeYesToAllNoToAll) {
408 setAddCancelButton(false);
409 fYesToAllNoToAll= includeYesToAllNoToAll;
413 protected Label createMessageArea(Composite composite) {
414 Label label= new Label(composite, SWT.WRAP);
415 label.setText(getMessage());
416 GridData gd= new GridData(SWT.FILL, SWT.CENTER, true, false);
417 gd.widthHint= convertWidthInCharsToPixels(55);
418 label.setLayoutData(gd);
419 applyDialogFont(label);
424 protected void buttonPressed(int buttonId) {
425 super.buttonPressed(buttonId);
426 setReturnCode(buttonId);
431 protected void createButtonsForButtonBar(Composite parent) {
432 createButton(parent, IDialogConstants.YES_ID, IDialogConstants.YES_LABEL, true);
433 if (fYesToAllNoToAll)
434 createButton(parent, IDialogConstants.YES_TO_ALL_ID, IDialogConstants.YES_TO_ALL_LABEL, false);
435 createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL, false);
436 if (fYesToAllNoToAll)
437 createButton(parent, IDialogConstants.NO_TO_ALL_ID, IDialogConstants.NO_TO_ALL_LABEL, false);
438 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);