001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring.reorg;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.OperationCanceledException;
014:
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Shell;
018:
019: import org.eclipse.jface.dialogs.IDialogConstants;
020: import org.eclipse.jface.dialogs.MessageDialog;
021: import org.eclipse.jface.viewers.ArrayContentProvider;
022: import org.eclipse.jface.wizard.Wizard;
023:
024: import org.eclipse.jdt.internal.corext.refactoring.reorg.IConfirmQuery;
025: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgQueries;
026:
027: import org.eclipse.jdt.internal.ui.dialogs.ListDialog;
028:
029: import org.eclipse.jdt.ui.JavaElementLabelProvider;
030:
031: public class ReorgQueries implements IReorgQueries {
032:
033: private final Wizard fWizard;
034: private final Shell fShell;
035:
036: public ReorgQueries(Wizard wizard) {
037: Assert.isNotNull(wizard);
038: fWizard = wizard;
039: fShell = null;
040: }
041:
042: public ReorgQueries(Shell shell) {
043: Assert.isNotNull(shell);
044: fWizard = null;
045: fShell = shell;
046: }
047:
048: private Shell getShell() {
049: Assert.isTrue(fShell == null || fWizard == null);
050: Assert.isTrue(fShell != null || fWizard != null);
051: if (fWizard != null)
052: return fWizard.getContainer().getShell();
053: else
054: return fShell;
055: }
056:
057: /* (non-Javadoc)
058: * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries#createYesYesToAllNoNoToAllQuery(java.lang.String)
059: */
060: public IConfirmQuery createYesYesToAllNoNoToAllQuery(
061: String dialogTitle, boolean allowCancel, int queryID) {
062: return new YesYesToAllNoNoToAllQuery(getShell(), allowCancel,
063: dialogTitle);
064: }
065:
066: /* (non-Javadoc)
067: * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries#createYesNoQuery(java.lang.String)
068: */
069: public IConfirmQuery createYesNoQuery(String dialogTitle,
070: boolean allowCancel, int queryID) {
071: return new YesNoQuery(getShell(), allowCancel, dialogTitle);
072: }
073:
074: public IConfirmQuery createSkipQuery(String dialogTitle, int queryID) {
075: return new SkipQuery(getShell(), dialogTitle);
076: }
077:
078: private static class YesYesToAllNoNoToAllQuery implements
079: IConfirmQuery {
080: private final boolean fAllowCancel;
081: private boolean fYesToAll = false;
082: private boolean fNoToAll = false;
083: private final Shell fShell;
084: private final String fDialogTitle;
085:
086: YesYesToAllNoNoToAllQuery(Shell parent, boolean allowCancel,
087: String dialogTitle) {
088: fShell = parent;
089: fDialogTitle = dialogTitle;
090: fAllowCancel = allowCancel;
091: }
092:
093: /* (non-Javadoc)
094: * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
095: */
096: public boolean confirm(final String question)
097: throws OperationCanceledException {
098: if (fYesToAll)
099: return true;
100:
101: if (fNoToAll)
102: return false;
103:
104: final int[] result = new int[1];
105: fShell.getDisplay().syncExec(
106: createQueryRunnable(question, result));
107: return getResult(result);
108: }
109:
110: /* (non-Javadoc)
111: * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
112: */
113: public boolean confirm(String question, Object[] elements)
114: throws OperationCanceledException {
115: if (fYesToAll)
116: return true;
117:
118: if (fNoToAll)
119: return false;
120:
121: final int[] result = new int[1];
122: fShell.getDisplay().syncExec(
123: createQueryRunnable(question, elements, result));
124: return getResult(result);
125: }
126:
127: private Runnable createQueryRunnable(final String question,
128: final int[] result) {
129: return new Runnable() {
130: public void run() {
131: int[] resultId = getResultIDs();
132:
133: MessageDialog dialog = new MessageDialog(fShell,
134: fDialogTitle, null, question,
135: MessageDialog.QUESTION, getButtonLabels(),
136: 0);
137: dialog.open();
138:
139: if (dialog.getReturnCode() == -1) { //MessageDialog closed without choice => cancel | no
140: //see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=48400
141: result[0] = fAllowCancel ? IDialogConstants.CANCEL_ID
142: : IDialogConstants.NO_ID;
143: } else {
144: result[0] = resultId[dialog.getReturnCode()];
145: }
146: }
147:
148: private String[] getButtonLabels() {
149: if (YesYesToAllNoNoToAllQuery.this .fAllowCancel)
150: return new String[] {
151: IDialogConstants.YES_LABEL,
152: IDialogConstants.YES_TO_ALL_LABEL,
153: IDialogConstants.NO_LABEL,
154: IDialogConstants.NO_TO_ALL_LABEL,
155: IDialogConstants.CANCEL_LABEL };
156: else
157: return new String[] {
158: IDialogConstants.YES_LABEL,
159: IDialogConstants.YES_TO_ALL_LABEL,
160: IDialogConstants.NO_LABEL,
161: IDialogConstants.NO_TO_ALL_LABEL };
162: }
163:
164: private int[] getResultIDs() {
165: if (YesYesToAllNoNoToAllQuery.this .fAllowCancel)
166: return new int[] { IDialogConstants.YES_ID,
167: IDialogConstants.YES_TO_ALL_ID,
168: IDialogConstants.NO_ID,
169: IDialogConstants.NO_TO_ALL_ID,
170: IDialogConstants.CANCEL_ID };
171: else
172: return new int[] { IDialogConstants.YES_ID,
173: IDialogConstants.YES_TO_ALL_ID,
174: IDialogConstants.NO_ID,
175: IDialogConstants.NO_TO_ALL_ID };
176: }
177: };
178: }
179:
180: private Runnable createQueryRunnable(final String question,
181: final Object[] elements, final int[] result) {
182: return new Runnable() {
183: public void run() {
184: ListDialog dialog = new YesNoListDialog(fShell,
185: true);
186: dialog.setAddCancelButton(false);
187: dialog.setBlockOnOpen(true);
188: dialog
189: .setContentProvider(new ArrayContentProvider());
190: dialog
191: .setLabelProvider(new JavaElementLabelProvider());
192: dialog.setTitle(fDialogTitle);
193: dialog.setMessage(question);
194: dialog.setInput(elements);
195:
196: dialog.open();
197: result[0] = dialog.getReturnCode();
198: }
199: };
200: }
201:
202: private boolean getResult(int[] result)
203: throws OperationCanceledException {
204: switch (result[0]) {
205: case IDialogConstants.YES_TO_ALL_ID:
206: fYesToAll = true;
207: return true;
208: case IDialogConstants.YES_ID:
209: return true;
210: case IDialogConstants.CANCEL_ID:
211: throw new OperationCanceledException();
212: case IDialogConstants.NO_ID:
213: return false;
214: case IDialogConstants.NO_TO_ALL_ID:
215: fNoToAll = true;
216: return false;
217: default:
218: Assert.isTrue(false);
219: return false;
220: }
221: }
222: }
223:
224: private static class YesNoQuery implements IConfirmQuery {
225:
226: private final Shell fShell;
227: private final String fDialogTitle;
228: private final boolean fAllowCancel;
229:
230: YesNoQuery(Shell parent, boolean allowCancel, String dialogTitle) {
231: fShell = parent;
232: fDialogTitle = dialogTitle;
233: fAllowCancel = allowCancel;
234: }
235:
236: /* (non-Javadoc)
237: * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
238: */
239: public boolean confirm(String question)
240: throws OperationCanceledException {
241: final int[] result = new int[1];
242: fShell.getDisplay().syncExec(
243: createQueryRunnable(question, result));
244: return getResult(result);
245: }
246:
247: /* (non-Javadoc)
248: * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
249: */
250: public boolean confirm(String question, Object[] elements)
251: throws OperationCanceledException {
252: final int[] result = new int[1];
253: fShell.getDisplay().syncExec(
254: createQueryRunnable(question, elements, result));
255: return getResult(result);
256: }
257:
258: private Runnable createQueryRunnable(final String question,
259: final int[] result) {
260: return new Runnable() {
261: public void run() {
262: MessageDialog dialog = new MessageDialog(fShell,
263: fDialogTitle, null, question,
264: MessageDialog.QUESTION, getButtonLabels(),
265: 0);
266: dialog.open();
267:
268: switch (dialog.getReturnCode()) {
269: case -1: //MessageDialog closed without choice => cancel | no
270: //see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=48400
271: result[0] = fAllowCancel ? IDialogConstants.CANCEL_ID
272: : IDialogConstants.NO_ID;
273: break;
274: case 0:
275: result[0] = IDialogConstants.YES_ID;
276: break;
277: case 1:
278: result[0] = IDialogConstants.NO_ID;
279: break;
280: case 2:
281: if (fAllowCancel)
282: result[0] = IDialogConstants.CANCEL_ID;
283: else
284: Assert.isTrue(false);
285: break;
286: default:
287: Assert.isTrue(false);
288: break;
289: }
290: }
291:
292: private String[] getButtonLabels() {
293: if (fAllowCancel)
294: return new String[] {
295: IDialogConstants.YES_LABEL,
296: IDialogConstants.NO_LABEL,
297: IDialogConstants.CANCEL_LABEL };
298: else
299: return new String[] {
300: IDialogConstants.YES_LABEL,
301: IDialogConstants.NO_LABEL };
302: }
303: };
304: }
305:
306: private Runnable createQueryRunnable(final String question,
307: final Object[] elements, final int[] result) {
308: return new Runnable() {
309: public void run() {
310: ListDialog dialog = new YesNoListDialog(fShell,
311: false);
312: dialog.setAddCancelButton(false);
313: dialog.setBlockOnOpen(true);
314: dialog
315: .setContentProvider(new ArrayContentProvider());
316: dialog
317: .setLabelProvider(new JavaElementLabelProvider());
318: dialog.setTitle(fDialogTitle);
319: dialog.setMessage(question);
320: dialog.setInput(elements);
321:
322: dialog.open();
323: result[0] = dialog.getReturnCode();
324: }
325: };
326: }
327:
328: private boolean getResult(int[] result)
329: throws OperationCanceledException {
330: switch (result[0]) {
331: case IDialogConstants.YES_ID:
332: return true;
333: case IDialogConstants.CANCEL_ID:
334: throw new OperationCanceledException();
335: case IDialogConstants.NO_ID:
336: return false;
337: default:
338: Assert.isTrue(false);
339: return false;
340: }
341: }
342: }
343:
344: private static class SkipQuery implements IConfirmQuery {
345:
346: private final Shell fShell;
347: private final String fDialogTitle;
348: private boolean fSkipAll;
349:
350: SkipQuery(Shell parent, String dialogTitle) {
351: fShell = parent;
352: fDialogTitle = dialogTitle;
353: fSkipAll = false;
354: }
355:
356: /* (non-Javadoc)
357: * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
358: */
359: public boolean confirm(String question)
360: throws OperationCanceledException {
361: if (fSkipAll)
362: return false;
363: final int[] result = new int[1];
364: fShell.getDisplay().syncExec(
365: createQueryRunnable(question, result));
366: return getResult(result);
367: }
368:
369: /* (non-Javadoc)
370: * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
371: */
372: public boolean confirm(String question, Object[] elements)
373: throws OperationCanceledException {
374: throw new UnsupportedOperationException(
375: "Not supported for skip queries"); //$NON-NLS-1$
376: }
377:
378: private Runnable createQueryRunnable(final String question,
379: final int[] result) {
380: return new Runnable() {
381: public void run() {
382: MessageDialog dialog = new MessageDialog(fShell,
383: fDialogTitle, null, question,
384: MessageDialog.QUESTION, getButtonLabels(),
385: 0);
386: dialog.open();
387:
388: switch (dialog.getReturnCode()) {
389: case -1: //MessageDialog closed without choice => cancel | no
390: //see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=48400
391: result[0] = IDialogConstants.CANCEL_ID;
392: break;
393: default:
394: result[0] = dialog.getReturnCode();
395: }
396: }
397:
398: private String[] getButtonLabels() {
399: return new String[] { IDialogConstants.SKIP_LABEL,
400: ReorgMessages.ReorgQueries_skip_all,
401: IDialogConstants.CANCEL_LABEL };
402: }
403: };
404: }
405:
406: private boolean getResult(int[] result)
407: throws OperationCanceledException {
408: switch (result[0]) {
409: // skip button
410: case 0:
411: return false;
412: // skip all button
413: case 1:
414: fSkipAll = true;
415: return false;
416: // Cancel button
417: case 2:
418: throw new OperationCanceledException();
419: default:
420: return false;
421: }
422: }
423: }
424:
425: private static final class YesNoListDialog extends ListDialog {
426: private final boolean fYesToAllNoToAll;
427:
428: private YesNoListDialog(Shell parent,
429: boolean includeYesToAllNoToAll) {
430: super (parent, SWT.TITLE | SWT.BORDER | SWT.RESIZE
431: | SWT.APPLICATION_MODAL);
432: fYesToAllNoToAll = includeYesToAllNoToAll;
433: }
434:
435: protected void buttonPressed(int buttonId) {
436: super .buttonPressed(buttonId);
437: setReturnCode(buttonId);
438: close();
439: }
440:
441: protected void createButtonsForButtonBar(Composite parent) {
442: createButton(parent, IDialogConstants.YES_ID,
443: IDialogConstants.YES_LABEL, true);
444: if (fYesToAllNoToAll)
445: createButton(parent, IDialogConstants.YES_TO_ALL_ID,
446: IDialogConstants.YES_TO_ALL_LABEL, false);
447: createButton(parent, IDialogConstants.NO_ID,
448: IDialogConstants.NO_LABEL, false);
449: if (fYesToAllNoToAll)
450: createButton(parent, IDialogConstants.NO_TO_ALL_ID,
451: IDialogConstants.NO_TO_ALL_LABEL, false);
452: createButton(parent, IDialogConstants.CANCEL_ID,
453: IDialogConstants.CANCEL_LABEL, false);
454: }
455: }
456: }
|