1 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 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 [] buttons; 30 private int confirmation = ALWAYS_ASK; 31 private String title; 32 private boolean hasMultiple; 33 private boolean allOrNothing; 34 private IShellProvider shellProvider; 35 36 40 public MultipleYesNoPrompter(IShellProvider provider, String 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 [] { 48 IDialogConstants.YES_LABEL, 49 IDialogConstants.YES_TO_ALL_LABEL, 50 IDialogConstants.CANCEL_LABEL}; 51 } else { 52 buttons = new String [] { 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 [] { 61 IDialogConstants.YES_LABEL, 62 IDialogConstants.NO_LABEL, 63 IDialogConstants.CANCEL_LABEL 64 }; 65 } 66 } 67 68 75 public boolean shouldInclude(String message) throws InterruptedException { 76 if (confirmation == YES_TO_ALL) { 77 return true; 78 } else { 79 switch (confirmation) { 80 case ALWAYS_ASK: { 81 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 break; 94 } 95 } 96 return false; 98 } 99 } 100 101 104 private boolean confirmOverwrite(String msg) throws InterruptedException { 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 shell.getDisplay().syncExec( 113 new Runnable () { 114 public void run() { 115 dialog.open(); 116 } 117 }); 118 if (hasMultiple) { 119 switch (dialog.getReturnCode()) { 120 case 0: return true; 122 case 1: confirmation = YES_TO_ALL; 124 return true; 125 case 2: if (allOrNothing) { 127 throw new InterruptedException (); 128 } 129 return false; 130 case 3: confirmation = NO_TO_ALL; 132 return false; 133 case 4: default: 135 throw new InterruptedException (); 136 } 137 } else { 138 switch (dialog.getReturnCode()) { 139 case 0: return true; 141 case 1: return false; 143 case 2: default: 145 throw new InterruptedException (); 146 } 147 } 148 } 149 150 public void setTitle(String title) { 151 this.title = title; 152 } 153 154 } 155 | Popular Tags |