1 11 package org.eclipse.jdt.internal.ui.refactoring.reorg; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.OperationCanceledException; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.Shell; 19 20 import org.eclipse.jface.dialogs.IDialogConstants; 21 import org.eclipse.jface.dialogs.MessageDialog; 22 import org.eclipse.jface.viewers.ArrayContentProvider; 23 import org.eclipse.jface.wizard.Wizard; 24 25 import org.eclipse.jdt.internal.corext.refactoring.reorg.IConfirmQuery; 26 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgQueries; 27 28 import org.eclipse.jdt.internal.ui.dialogs.ListDialog; 29 30 import org.eclipse.jdt.ui.JavaElementLabelProvider; 31 32 public class ReorgQueries implements IReorgQueries { 33 34 private final Wizard fWizard; 35 private final Shell fShell; 36 37 public ReorgQueries(Wizard wizard){ 38 Assert.isNotNull(wizard); 39 fWizard= wizard; 40 fShell= null; 41 } 42 43 public ReorgQueries(Shell shell){ 44 Assert.isNotNull(shell); 45 fWizard= null; 46 fShell= shell; 47 } 48 49 private Shell getShell() { 50 Assert.isTrue(fShell == null || fWizard == null); 51 Assert.isTrue(fShell != null || fWizard != null); 52 if (fWizard != null) 53 return fWizard.getContainer().getShell(); 54 else 55 return fShell; 56 } 57 58 61 public IConfirmQuery createYesYesToAllNoNoToAllQuery(String dialogTitle, boolean allowCancel, int queryID) { 62 return new YesYesToAllNoNoToAllQuery(getShell(), allowCancel, dialogTitle); 63 } 64 65 68 public IConfirmQuery createYesNoQuery(String dialogTitle, boolean allowCancel, int queryID) { 69 return new YesNoQuery(getShell(), allowCancel, dialogTitle); 70 } 71 72 public IConfirmQuery createSkipQuery(String dialogTitle, int queryID) { 73 return new SkipQuery(getShell(), dialogTitle); 74 } 75 76 private static class YesYesToAllNoNoToAllQuery implements IConfirmQuery{ 77 private final boolean fAllowCancel; 78 private boolean fYesToAll= false; 79 private boolean fNoToAll= false; 80 private final Shell fShell; 81 private final String fDialogTitle; 82 83 YesYesToAllNoNoToAllQuery(Shell parent, boolean allowCancel, String dialogTitle){ 84 fShell= parent; 85 fDialogTitle= dialogTitle; 86 fAllowCancel= allowCancel; 87 } 88 89 92 public boolean confirm(final String question) throws OperationCanceledException { 93 if (fYesToAll) 94 return true; 95 96 if (fNoToAll) 97 return false; 98 99 final int[] result= new int[1]; 100 fShell.getDisplay().syncExec(createQueryRunnable(question, result)); 101 return getResult(result); 102 } 103 104 107 public boolean confirm(String question, Object [] elements) throws OperationCanceledException { 108 if (fYesToAll) 109 return true; 110 111 if (fNoToAll) 112 return false; 113 114 final int[] result= new int[1]; 115 fShell.getDisplay().syncExec(createQueryRunnable(question, elements, result)); 116 return getResult(result); 117 } 118 119 private Runnable createQueryRunnable(final String question, final int[] result) { 120 return new Runnable () { 121 public void run() { 122 int[] resultId= getResultIDs(); 123 124 MessageDialog dialog= new MessageDialog( 125 fShell, 126 fDialogTitle, 127 null, 128 question, 129 MessageDialog.QUESTION, 130 getButtonLabels(), 131 0); 132 dialog.open(); 133 134 if (dialog.getReturnCode() == -1) { result[0]= fAllowCancel ? IDialogConstants.CANCEL_ID : IDialogConstants.NO_ID; 137 } else { 138 result[0]= resultId[dialog.getReturnCode()]; 139 } 140 } 141 142 private String [] getButtonLabels() { 143 if (YesYesToAllNoNoToAllQuery.this.fAllowCancel) 144 return new String [] { 145 IDialogConstants.YES_LABEL, 146 IDialogConstants.YES_TO_ALL_LABEL, 147 IDialogConstants.NO_LABEL, 148 IDialogConstants.NO_TO_ALL_LABEL, 149 IDialogConstants.CANCEL_LABEL }; 150 else 151 return new String [] { 152 IDialogConstants.YES_LABEL, 153 IDialogConstants.YES_TO_ALL_LABEL, 154 IDialogConstants.NO_LABEL, 155 IDialogConstants.NO_TO_ALL_LABEL}; 156 } 157 158 private int[] getResultIDs() { 159 if (YesYesToAllNoNoToAllQuery.this.fAllowCancel) 160 return new int[] { 161 IDialogConstants.YES_ID, 162 IDialogConstants.YES_TO_ALL_ID, 163 IDialogConstants.NO_ID, 164 IDialogConstants.NO_TO_ALL_ID, 165 IDialogConstants.CANCEL_ID}; 166 else 167 return new int[] { 168 IDialogConstants.YES_ID, 169 IDialogConstants.YES_TO_ALL_ID, 170 IDialogConstants.NO_ID, 171 IDialogConstants.NO_TO_ALL_ID}; 172 } 173 }; 174 } 175 176 private Runnable createQueryRunnable(final String question, final Object [] elements, final int[] result) { 177 return new Runnable () { 178 public void run() { 179 ListDialog dialog= new YesNoListDialog(fShell, true); 180 dialog.setAddCancelButton(false); 181 dialog.setBlockOnOpen(true); 182 dialog.setContentProvider(new ArrayContentProvider()); 183 dialog.setLabelProvider(new JavaElementLabelProvider()); 184 dialog.setTitle(fDialogTitle); 185 dialog.setMessage(question); 186 dialog.setInput(elements); 187 188 dialog.open(); 189 result[0]= dialog.getReturnCode(); 190 } 191 }; 192 } 193 194 private boolean getResult(int[] result) throws OperationCanceledException { 195 switch(result[0]){ 196 case IDialogConstants.YES_TO_ALL_ID: 197 fYesToAll= true; 198 return true; 199 case IDialogConstants.YES_ID: 200 return true; 201 case IDialogConstants.CANCEL_ID: 202 throw new OperationCanceledException(); 203 case IDialogConstants.NO_ID: 204 return false; 205 case IDialogConstants.NO_TO_ALL_ID: 206 fNoToAll= true; 207 return false; 208 default: 209 Assert.isTrue(false); 210 return false; 211 } 212 } 213 } 214 215 private static class YesNoQuery implements IConfirmQuery{ 216 217 private final Shell fShell; 218 private final String fDialogTitle; 219 private final boolean fAllowCancel; 220 221 YesNoQuery(Shell parent, boolean allowCancel, String dialogTitle){ 222 fShell= parent; 223 fDialogTitle= dialogTitle; 224 fAllowCancel= allowCancel; 225 } 226 227 230 public boolean confirm(String question) throws OperationCanceledException { 231 final int[] result= new int[1]; 232 fShell.getDisplay().syncExec(createQueryRunnable(question, result)); 233 return getResult(result); 234 } 235 236 239 public boolean confirm(String question, Object [] elements) throws OperationCanceledException { 240 final int[] result= new int[1]; 241 fShell.getDisplay().syncExec(createQueryRunnable(question, elements, result)); 242 return getResult(result); 243 } 244 245 private Runnable createQueryRunnable(final String question, final int[] result){ 246 return new Runnable () { 247 public void run() { 248 MessageDialog dialog= new MessageDialog( 249 fShell, 250 fDialogTitle, 251 null, 252 question, 253 MessageDialog.QUESTION, 254 getButtonLabels(), 255 0); 256 dialog.open(); 257 258 switch (dialog.getReturnCode()) { 259 case -1 : result[0]= fAllowCancel ? IDialogConstants.CANCEL_ID : IDialogConstants.NO_ID; 262 break; 263 case 0 : 264 result[0]= IDialogConstants.YES_ID; 265 break; 266 case 1 : 267 result[0]= IDialogConstants.NO_ID; 268 break; 269 case 2 : 270 if (fAllowCancel) 271 result[0]= IDialogConstants.CANCEL_ID; 272 else 273 Assert.isTrue(false); 274 break; 275 default : 276 Assert.isTrue(false); 277 break; 278 } 279 } 280 281 private String [] getButtonLabels() { 282 if (fAllowCancel) 283 return new String [] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }; 284 else 285 return new String [] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL}; 286 } 287 }; 288 } 289 290 private Runnable createQueryRunnable(final String question, final Object [] elements, final int[] result) { 291 return new Runnable () { 292 public void run() { 293 ListDialog dialog= new YesNoListDialog(fShell, false); 294 dialog.setAddCancelButton(false); 295 dialog.setBlockOnOpen(true); 296 dialog.setContentProvider(new ArrayContentProvider()); 297 dialog.setLabelProvider(new JavaElementLabelProvider()); 298 dialog.setTitle(fDialogTitle); 299 dialog.setMessage(question); 300 dialog.setInput(elements); 301 302 dialog.open(); 303 result[0]= dialog.getReturnCode(); 304 } 305 }; 306 } 307 308 private boolean getResult(int[] result) throws OperationCanceledException { 309 switch(result[0]){ 310 case IDialogConstants.YES_ID: 311 return true; 312 case IDialogConstants.CANCEL_ID: 313 throw new OperationCanceledException(); 314 case IDialogConstants.NO_ID: 315 return false; 316 default: 317 Assert.isTrue(false); 318 return false; 319 } 320 } 321 } 322 323 private static class SkipQuery implements IConfirmQuery{ 324 325 private final Shell fShell; 326 private final String fDialogTitle; 327 private boolean fSkipAll; 328 329 SkipQuery(Shell parent, String dialogTitle){ 330 fShell= parent; 331 fDialogTitle= dialogTitle; 332 fSkipAll= false; 333 } 334 335 338 public boolean confirm(String question) throws OperationCanceledException { 339 if (fSkipAll) 340 return false; 341 final int[] result= new int[1]; 342 fShell.getDisplay().syncExec(createQueryRunnable(question, result)); 343 return getResult(result); 344 } 345 346 349 public boolean confirm(String question, Object [] elements) throws OperationCanceledException { 350 throw new UnsupportedOperationException ("Not supported for skip queries"); } 352 353 private Runnable createQueryRunnable(final String question, final int[] result){ 354 return new Runnable () { 355 public void run() { 356 MessageDialog dialog= new MessageDialog( 357 fShell, 358 fDialogTitle, 359 null, 360 question, 361 MessageDialog.QUESTION, 362 getButtonLabels(), 363 0); 364 dialog.open(); 365 366 switch (dialog.getReturnCode()) { 367 case -1 : result[0]= IDialogConstants.CANCEL_ID; 370 break; 371 default: 372 result[0]= dialog.getReturnCode(); 373 } 374 } 375 376 private String [] getButtonLabels() { 377 return new String [] {IDialogConstants.SKIP_LABEL, ReorgMessages.ReorgQueries_skip_all, IDialogConstants.CANCEL_LABEL}; 378 } 379 }; 380 } 381 382 private boolean getResult(int[] result) throws OperationCanceledException { 383 switch(result[0]){ 384 case 0: 386 return false; 387 case 1: 389 fSkipAll= true; 390 return false; 391 case 2: 393 throw new OperationCanceledException(); 394 default: 395 return false; 396 } 397 } 398 } 399 400 private static final class YesNoListDialog extends ListDialog { 401 private final boolean fYesToAllNoToAll; 402 private YesNoListDialog(Shell parent, boolean includeYesToAllNoToAll) { 403 super(parent, SWT.TITLE | SWT.BORDER | SWT.RESIZE | SWT.APPLICATION_MODAL); 404 fYesToAllNoToAll= includeYesToAllNoToAll; 405 } 406 407 protected void buttonPressed(int buttonId) { 408 super.buttonPressed(buttonId); 409 setReturnCode(buttonId); 410 close(); 411 } 412 413 protected void createButtonsForButtonBar(Composite parent) { 414 createButton(parent, IDialogConstants.YES_ID, IDialogConstants.YES_LABEL, true); 415 if (fYesToAllNoToAll) 416 createButton(parent, IDialogConstants.YES_TO_ALL_ID, IDialogConstants.YES_TO_ALL_LABEL, false); 417 createButton(parent, IDialogConstants.NO_ID, IDialogConstants.NO_LABEL, false); 418 if (fYesToAllNoToAll) 419 createButton(parent, IDialogConstants.NO_TO_ALL_ID, IDialogConstants.NO_TO_ALL_LABEL, false); 420 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 421 } 422 } 423 } 424 | Popular Tags |