]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/reorg/ReorgQueries.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui refactoring / org / eclipse / jdt / internal / ui / refactoring / reorg / ReorgQueries.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring.reorg;
12
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;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.OperationCanceledException;
21
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;
26
27 import org.eclipse.ui.dialogs.ListDialog;
28
29 import org.eclipse.jdt.internal.corext.refactoring.reorg.IConfirmQuery;
30 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgQueries;
31
32 import org.eclipse.jdt.ui.JavaElementLabelProvider;
33
34
35 public class ReorgQueries implements IReorgQueries {
36
37         private final Wizard fWizard;
38         private final Shell fShell;
39
40         public ReorgQueries(Wizard wizard){
41                 Assert.isNotNull(wizard);
42                 fWizard= wizard;
43                 fShell= null;
44         }
45
46         public ReorgQueries(Shell shell){
47                 Assert.isNotNull(shell);
48                 fWizard= null;
49                 fShell= shell;
50         }
51
52         private Shell getShell() {
53                 Assert.isTrue(fShell == null || fWizard == null);
54                 Assert.isTrue(fShell != null || fWizard != null);
55                 if (fWizard != null)
56                         return fWizard.getContainer().getShell();
57                 else
58                         return fShell;
59         }
60
61         /* (non-Javadoc)
62          * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries#createYesYesToAllNoNoToAllQuery(java.lang.String)
63          */
64         public IConfirmQuery createYesYesToAllNoNoToAllQuery(String dialogTitle, boolean allowCancel, int queryID) {
65                 return new YesYesToAllNoNoToAllQuery(getShell(), allowCancel, dialogTitle);
66         }
67
68         /* (non-Javadoc)
69          * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries#createYesNoQuery(java.lang.String)
70          */
71         public IConfirmQuery createYesNoQuery(String dialogTitle, boolean allowCancel, int queryID) {
72                 return new YesNoQuery(getShell(), allowCancel, dialogTitle);
73         }
74
75         public IConfirmQuery createSkipQuery(String dialogTitle, int queryID) {
76                 return new SkipQuery(getShell(), dialogTitle);
77         }
78
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;
85
86                 YesYesToAllNoNoToAllQuery(Shell parent, boolean allowCancel, String dialogTitle){
87                         fShell= parent;
88                         fDialogTitle= dialogTitle;
89                         fAllowCancel= allowCancel;
90                 }
91
92                 /* (non-Javadoc)
93                  * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
94                  */
95                 public boolean confirm(final String question) throws OperationCanceledException {
96                         if (fYesToAll)
97                                 return true;
98
99                         if (fNoToAll)
100                                 return false;
101
102                         final int[] result= new int[1];
103                         fShell.getDisplay().syncExec(createQueryRunnable(question, result));
104                         return getResult(result);
105                 }
106
107                 /* (non-Javadoc)
108                  * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
109                  */
110                 public boolean confirm(String question, Object[] elements) throws OperationCanceledException {
111                         if (fYesToAll)
112                                 return true;
113
114                         if (fNoToAll)
115                                 return false;
116
117                         final int[] result= new int[1];
118                         fShell.getDisplay().syncExec(createQueryRunnable(question, elements, result));
119                         return getResult(result);
120                 }
121
122                 private Runnable createQueryRunnable(final String question, final int[] result) {
123                         return new Runnable() {
124                                 public void run() {
125                                         int[] resultId= getResultIDs();
126
127                                         MessageDialog dialog= new MessageDialog(
128                                                 fShell,
129                                                 fDialogTitle,
130                                                 null,
131                                                 question,
132                                                 MessageDialog.QUESTION,
133                                                 getButtonLabels(),
134                                                 0);
135                                         dialog.open();
136
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;
140                                         } else {
141                                                 result[0]= resultId[dialog.getReturnCode()];
142                                         }
143                                 }
144
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 };
153                                         else
154                                                 return new String[] {
155                                                         IDialogConstants.YES_LABEL,
156                                                         IDialogConstants.YES_TO_ALL_LABEL,
157                                                         IDialogConstants.NO_LABEL,
158                                                         IDialogConstants.NO_TO_ALL_LABEL};
159                                 }
160
161                                 private int[] getResultIDs() {
162                                         if (YesYesToAllNoNoToAllQuery.this.fAllowCancel)
163                                                 return new int[] {
164                                                         IDialogConstants.YES_ID,
165                                                         IDialogConstants.YES_TO_ALL_ID,
166                                                         IDialogConstants.NO_ID,
167                                                         IDialogConstants.NO_TO_ALL_ID,
168                                                         IDialogConstants.CANCEL_ID};
169                                         else
170                                                 return new int[] {
171                                                         IDialogConstants.YES_ID,
172                                                         IDialogConstants.YES_TO_ALL_ID,
173                                                         IDialogConstants.NO_ID,
174                                                         IDialogConstants.NO_TO_ALL_ID};
175                                 }
176                         };
177                 }
178
179                 private Runnable createQueryRunnable(final String question, final Object[] elements, final int[] result) {
180                         return new Runnable() {
181                                 public void run() {
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);
190
191                                         dialog.open();
192                                         result[0]= dialog.getReturnCode();
193                                 }
194                         };
195                 }
196
197                 private boolean getResult(int[] result) throws OperationCanceledException {
198                         switch(result[0]){
199                                 case IDialogConstants.YES_TO_ALL_ID:
200                                         fYesToAll= true;
201                                         return true;
202                                 case IDialogConstants.YES_ID:
203                                         return true;
204                                 case IDialogConstants.CANCEL_ID:
205                                         throw new OperationCanceledException();
206                                 case IDialogConstants.NO_ID:
207                                         return false;
208                                 case IDialogConstants.NO_TO_ALL_ID:
209                                         fNoToAll= true;
210                                         return false;
211                                 default:
212                                         Assert.isTrue(false);
213                                         return false;
214                         }
215                 }
216         }
217
218         private static class YesNoQuery implements IConfirmQuery{
219
220                 private final Shell fShell;
221                 private final String fDialogTitle;
222                 private final boolean fAllowCancel;
223
224                 YesNoQuery(Shell parent, boolean allowCancel, String dialogTitle){
225                         fShell= parent;
226                         fDialogTitle= dialogTitle;
227                         fAllowCancel= allowCancel;
228                 }
229
230                 /* (non-Javadoc)
231                  * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
232                  */
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);
237                 }
238
239                 /* (non-Javadoc)
240                  * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
241                  */
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);
246                 }
247
248                 private Runnable createQueryRunnable(final String question, final int[] result){
249                         return new Runnable() {
250                                 public void run() {
251                                         MessageDialog dialog= new MessageDialog(
252                                                 fShell,
253                                                 fDialogTitle,
254                                                 null,
255                                                 question,
256                                                 MessageDialog.QUESTION,
257                                                 getButtonLabels(),
258                                                 0);
259                                         dialog.open();
260
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;
265                                                         break;
266                                                 case 0 :
267                                                         result[0]= IDialogConstants.YES_ID;
268                                                         break;
269                                                 case 1 :
270                                                         result[0]= IDialogConstants.NO_ID;
271                                                         break;
272                                                 case 2 :
273                                                         if (fAllowCancel)
274                                                                 result[0]= IDialogConstants.CANCEL_ID;
275                                                         else
276                                                                 Assert.isTrue(false);
277                                                         break;
278                                                 default :
279                                                         Assert.isTrue(false);
280                                                         break;
281                                         }
282                                 }
283
284                                 private String[] getButtonLabels() {
285                                         if (fAllowCancel)
286                                                 return new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
287                                         else
288                                                 return new String[] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL};
289                                 }
290                         };
291                 }
292
293                 private Runnable createQueryRunnable(final String question, final Object[] elements, final int[] result) {
294                         return new Runnable() {
295                                 public void run() {
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);
304
305                                         dialog.open();
306                                         result[0]= dialog.getReturnCode();
307                                 }
308                         };
309                 }
310
311                 private boolean getResult(int[] result) throws OperationCanceledException {
312                         switch(result[0]){
313                                 case IDialogConstants.YES_ID:
314                                         return true;
315                                 case IDialogConstants.CANCEL_ID:
316                                         throw new OperationCanceledException();
317                                 case IDialogConstants.NO_ID:
318                                         return false;
319                                 default:
320                                         Assert.isTrue(false);
321                                         return false;
322                         }
323                 }
324         }
325
326         private static class SkipQuery implements IConfirmQuery{
327
328                 private final Shell fShell;
329                 private final String fDialogTitle;
330                 private boolean fSkipAll;
331
332                 SkipQuery(Shell parent, String dialogTitle){
333                         fShell= parent;
334                         fDialogTitle= dialogTitle;
335                         fSkipAll= false;
336                 }
337
338                 /* (non-Javadoc)
339                  * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IConfirmQuery#confirm(java.lang.String)
340                  */
341                 public boolean confirm(String question) throws OperationCanceledException {
342                         if (fSkipAll)
343                                 return false;
344                         final int[] result= new int[1];
345                         fShell.getDisplay().syncExec(createQueryRunnable(question, result));
346                         return getResult(result);
347                 }
348
349                 /* (non-Javadoc)
350                  * @see org.eclipse.jdt.internal.corext.refactoring.reorg2.IReorgQueries.IConfirmQuery#confirm(java.lang.String, java.lang.Object[])
351                  */
352                 public boolean confirm(String question, Object[] elements) throws OperationCanceledException {
353                         throw new UnsupportedOperationException("Not supported for skip queries"); //$NON-NLS-1$
354                 }
355
356                 private Runnable createQueryRunnable(final String question, final int[] result){
357                         return new Runnable() {
358                                 public void run() {
359                                         MessageDialog dialog= new MessageDialog(
360                                                 fShell,
361                                                 fDialogTitle,
362                                                 null,
363                                                 question,
364                                                 MessageDialog.QUESTION,
365                                                 getButtonLabels(),
366                                                 0);
367                                         dialog.open();
368
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;
373                                                         break;
374                                                 default:
375                                                         result[0]= dialog.getReturnCode();
376                                         }
377                                 }
378
379                                 private String[] getButtonLabels() {
380                                         return new String[] {IDialogConstants.SKIP_LABEL, ReorgMessages.ReorgQueries_skip_all, IDialogConstants.CANCEL_LABEL};
381                                 }
382                         };
383                 }
384
385                 private boolean getResult(int[] result) throws OperationCanceledException {
386                         switch(result[0]){
387                                 // skip button
388                                 case 0:
389                                         return false;
390                                 // skip all button
391                                 case 1:
392                                         fSkipAll= true;
393                                         return false;
394                                 // Cancel button
395                                 case 2:
396                                         throw new OperationCanceledException();
397                                 default:
398                                         return false;
399                         }
400                 }
401         }
402
403         private static final class YesNoListDialog extends ListDialog {
404                 private final boolean fYesToAllNoToAll;
405
406                 private YesNoListDialog(Shell parent, boolean includeYesToAllNoToAll) {
407                         super(parent);
408                         setAddCancelButton(false);
409                         fYesToAllNoToAll= includeYesToAllNoToAll;
410                 }
411
412                 @Override
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);
420                         return label;
421                 }
422
423                 @Override
424                 protected void buttonPressed(int buttonId) {
425                         super.buttonPressed(buttonId);
426                         setReturnCode(buttonId);
427                         close();
428                 }
429
430                 @Override
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);
439                 }
440         }
441 }