1 11 package org.eclipse.team.internal.ccvs.ui.operations; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.window.IShellProvider; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.swt.widgets.Shell; 26 import org.eclipse.team.core.TeamException; 27 import org.eclipse.team.internal.ccvs.core.CVSException; 28 import org.eclipse.team.internal.ccvs.core.CVSStatus; 29 import org.eclipse.team.internal.ccvs.ui.*; 30 import org.eclipse.team.internal.ccvs.ui.console.CVSOutputConsole; 31 import org.eclipse.team.internal.ui.dialogs.MultipleYesNoPrompter; 32 import org.eclipse.team.ui.TeamOperation; 33 import org.eclipse.ui.IWorkbenchPart; 34 35 36 40 public abstract class CVSOperation extends TeamOperation implements IShellProvider { 41 42 private int statusCount; 43 44 private boolean involvesMultipleResources = false; 45 46 private List errors = new ArrayList (); 48 protected static final IStatus OK = Status.OK_STATUS; 49 50 private Shell shell; 51 52 private MultipleYesNoPrompter prompter; 53 54 protected CVSOperation(IWorkbenchPart part) { 55 super(part); 56 } 57 58 61 protected String getJobName() { 62 return getTaskName(); 63 } 64 65 68 protected URL getOperationIcon() { 69 return Platform.find(CVSUIPlugin.getPlugin().getBundle(), new Path(ICVSUIConstants.ICON_PATH + ICVSUIConstants.IMG_CVS_PERSPECTIVE)); 70 } 71 72 75 public final void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 76 startOperation(); 77 try { 78 monitor = Policy.monitorFor(monitor); 79 monitor.beginTask(null, 100); 80 monitor.setTaskName(getTaskName()); 81 execute(Policy.subMonitorFor(monitor, 100)); 82 endOperation(); 83 } catch (CVSException e) { 84 throw new InvocationTargetException (e); 86 } finally { 87 monitor.done(); 88 } 89 } 90 91 protected void startOperation() { 92 statusCount = 0; 93 resetErrors(); 94 } 95 96 protected void endOperation() throws CVSException { 97 handleErrors((IStatus[]) errors.toArray(new IStatus[errors.size()])); 98 } 99 100 108 protected abstract void execute(IProgressMonitor monitor) throws CVSException, InterruptedException ; 109 110 protected void addError(IStatus status) { 111 if (status.isOK()) return; 112 if (isLastError(status)) return; 113 errors.add(status); 114 } 115 116 protected void collectStatus(IStatus status) { 117 if (isLastError(status)) return; 118 statusCount++; 119 if (!status.isOK()) addError(status); 120 } 121 122 protected void resetErrors() { 123 errors.clear(); 124 statusCount = 0; 125 } 126 127 protected IStatus[] getErrors() { 128 return (IStatus[]) errors.toArray(new IStatus[errors.size()]); 129 } 130 131 141 protected IStatus getLastError() { 142 Assert.isTrue(errors.size() > 0); 143 IStatus status = (IStatus)errors.get(errors.size() - 1); 144 return status; 145 } 146 147 private boolean isLastError(IStatus status) { 148 return (errors.size() > 0 && getLastError() == status); 149 } 150 151 156 protected void asException(IStatus[] errors) throws CVSException { 157 if (errors.length == 0) return; 158 if (errors.length == 1 && statusCount == 1) { 159 throw new CVSException(errors[0]); 160 } 161 MultiStatus result = new MultiStatus(CVSUIPlugin.ID, 0, getErrorMessage(errors, statusCount), null); 162 for (int i = 0; i < errors.length; i++) { 163 IStatus s = errors[i]; 164 if (s.isMultiStatus()) { 165 result.add(new CVSStatus(s.getSeverity(), s.getMessage(), s.getException())); 166 result.addAll(s); 167 } else { 168 result.add(s); 169 } 170 } 171 throw new CVSException(result); 172 } 173 174 182 protected final void handleErrors(IStatus[] errors) throws CVSException { 183 List reportableErrors = new ArrayList (); 186 for (int i = 0; i < errors.length; i++) { 187 IStatus status = errors[i]; 188 if (isReportableError(status)) { 189 reportableErrors.add(status); 190 } else if (status.isMultiStatus()) { 191 IStatus[] children = status.getChildren(); 192 for (int j = 0; j < children.length; j++) { 193 IStatus child = children[j]; 194 if (isReportableError(child)) { 195 reportableErrors.add(status); 196 break; 197 } 198 } 199 } 200 } 201 if (!reportableErrors.isEmpty()) 202 asException((IStatus[]) reportableErrors.toArray(new IStatus[reportableErrors.size()])); 203 } 204 205 211 protected boolean isReportableError(IStatus status) { 212 return status.getCode() == CVSStatus.SERVER_ERROR || CVSStatus.isInternalError(status) || status.getCode() == TeamException.UNABLE; 213 } 214 215 protected String getErrorMessage(IStatus[] failures, int totalOperations) { 216 return NLS.bind(CVSUIMessages.CVSOperation_0, new String [] { String.valueOf(failures.length), String.valueOf(totalOperations) }); 217 } 218 219 226 protected boolean promptToOverwrite(final String title, final String message, IResource resource) { 227 if (prompter == null) { 228 prompter = new MultipleYesNoPrompter(this, title, involvesMultipleResources(), false); 229 } else { 230 prompter.setTitle(title); 231 } 232 try { 233 return prompter.shouldInclude(message); 234 } catch (InterruptedException e) { 235 throw new OperationCanceledException(); 236 } 237 } 238 239 245 protected boolean involvesMultipleResources() { 246 return involvesMultipleResources; 247 } 248 249 public void setInvolvesMultipleResources(boolean b) { 250 involvesMultipleResources = b; 251 } 252 253 259 protected abstract String getTaskName(); 260 261 265 protected boolean errorsOccurred() { 266 for (Iterator iter = errors.iterator(); iter.hasNext();) { 267 IStatus status = (IStatus) iter.next(); 268 if (isReportableError(status)) return true; 269 if (status.isMultiStatus()) { 270 IStatus[] children = status.getChildren(); 271 for (int j = 0; j < children.length; j++) { 272 IStatus child = children[j]; 273 if (isReportableError(child)) { 274 return true; 275 } 276 } 277 } 278 } 279 return false; 280 } 281 282 285 public Shell getShell() { 286 if (shell != null && !shell.isDisposed()) { 288 return shell; 289 } 290 return super.getShell(); 291 } 292 293 299 public void setShell(Shell shell) { 300 this.shell = shell; 301 } 302 303 306 protected boolean canRunAsJob() { 307 return true; 309 } 310 311 314 protected boolean isSameFamilyAs(TeamOperation operation) { 315 return operation instanceof CVSOperation; 317 } 318 319 323 protected IAction getShowConsoleAction() { 324 return new Action(CVSUIMessages.CVSOperation_1) { 326 public void run() { 327 CVSOutputConsole console = CVSUIPlugin.getPlugin().getConsole(); 328 if (console != null) 329 console.show(true); 330 } 331 public String getToolTipText() { 332 return CVSUIMessages.CVSOperation_2; 333 } 334 }; 335 } 336 } 337 | Popular Tags |