1 11 package org.eclipse.team.internal.ccvs.ui.wizards; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.eclipse.core.resources.IFile; 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.IResourceVisitor; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.MultiStatus; 30 import org.eclipse.jface.operation.IRunnableWithProgress; 31 import org.eclipse.jface.util.PropertyChangeEvent; 32 import org.eclipse.swt.custom.BusyIndicator; 33 import org.eclipse.swt.widgets.Shell; 34 import org.eclipse.team.core.RepositoryProvider; 35 import org.eclipse.team.core.TeamException; 36 import org.eclipse.team.internal.ccvs.core.CVSException; 37 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin; 38 import org.eclipse.team.internal.ccvs.core.CVSStatus; 39 import org.eclipse.team.internal.ccvs.core.CVSTeamProvider; 40 import org.eclipse.team.internal.ccvs.core.ICVSFile; 41 import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption; 42 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 43 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo; 44 import org.eclipse.team.internal.ccvs.ui.*; 45 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin; 46 import org.eclipse.team.internal.ccvs.ui.Policy; 47 48 59 public class ModeWizard extends ResizableWizard { 60 61 public static class ModeChange { 62 63 private final IFile fFile; 64 private final KSubstOption fMode; 65 66 private KSubstOption fNewMode; 67 68 public ModeChange(IFile file, KSubstOption mode) { 69 fFile = file; 70 fMode= mode; 71 fNewMode= mode; 72 } 73 74 public IFile getFile() { 75 return fFile; 76 } 77 78 public KSubstOption getMode() { 79 return fMode; 80 } 81 82 public KSubstOption getNewMode() { 83 return fNewMode; 84 } 85 86 public boolean hasChanged() { 87 return !fMode.equals(fNewMode); 88 } 89 90 public void setNewMode(KSubstOption mode) { 91 fNewMode= mode; 92 } 93 94 public int compareTo(Object o) { 95 return fFile.getName().compareTo(((ModeChange)o).getFile().getName()); 96 } 97 } 98 99 protected List fChanges; 100 final ModeWizardSelectionPage fPage; 101 102 public static ModeWizard run(final Shell shell, final IResource [] resources) { 103 104 final ModeWizard [] wizard= new ModeWizard[1]; 105 106 BusyIndicator.showWhile(shell.getDisplay(), new Runnable () { 107 public void run() { 108 wizard[0]= new ModeWizard(shell, resources); 109 } 110 }); 111 112 open(shell, wizard[0]); 113 return wizard[0]; 114 } 115 116 123 124 protected ModeWizard(Shell shell, final IResource[] resources) { 125 super(CVSUIMessages.ModeWizard_0, CVSUIPlugin.getPlugin().getDialogSettings(), 700, 480); 126 setWindowTitle(CVSUIMessages.ModeWizard_1); 127 128 fChanges= getModeChanges(shell, resources); 129 fPage= new ModeWizardSelectionPage(fChanges); 130 132 } 133 134 public void addPages() { 135 addPage(fPage); 136 } 137 138 141 public boolean needsProgressMonitor() { 142 return true; 143 } 144 145 protected static List getModeChanges(Shell shell, IResource [] resources) { 146 147 final ArrayList changes= new ArrayList (); 148 final HashSet visited= new HashSet (); 149 150 for (int i = 0; i < resources.length; i++) { 151 final IResource currentResource = resources[i]; 152 try { 153 currentResource.accept(new IResourceVisitor() { 154 public boolean visit(IResource resource) throws CoreException { 155 try { 156 if (visited.contains(resource) || resource.getType() != IResource.FILE || !resource.exists()) 157 return true; 158 visited.add(resource); 159 IFile file = (IFile) resource; 160 ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(file); 161 if (!cvsFile.isManaged()) 162 return true; 163 final ResourceSyncInfo info = cvsFile.getSyncInfo(); 164 final KSubstOption mode = info.getKeywordMode(); 165 166 changes.add(new ModeChange(file, mode)); 167 168 } catch (TeamException e) { 169 throw new CoreException(e.getStatus()); 170 } 171 return true; 173 } 174 }, IResource.DEPTH_INFINITE, false); 175 } catch (CoreException e) { 176 CVSUIPlugin.openError(shell, CVSUIMessages.ModeWizard_2, null, e); 177 } 178 } 179 return changes; 180 } 181 182 public boolean performFinish() { 183 try { 184 final List messages = new ArrayList (); 185 final List changes= fPage.getChanges(); 186 if (changes.size() == 0) 187 return true; 188 189 final String comment = fPage.getComment(getShell()); 190 if (comment == null) 191 return false; 192 193 getContainer().run(false , true , new IRunnableWithProgress() { 194 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 195 try { 196 final int totalWork= 10000; 197 monitor.beginTask(CVSUIMessages.ModeWizard_3, totalWork); 198 199 final Map changesPerProvider= getProviderMapping(changes); 200 201 final int initialWork= totalWork / 10; 202 monitor.worked(initialWork); 203 204 final int workPerProvider = (totalWork - initialWork) / changesPerProvider.size(); 205 206 for (Iterator iter = changesPerProvider.entrySet().iterator(); iter.hasNext();) { 207 final Map.Entry entry = (Map.Entry ) iter.next(); 208 final CVSTeamProvider provider = (CVSTeamProvider)entry.getKey(); 209 final Map providerFiles = (Map ) entry.getValue(); 210 211 final IStatus status = provider.setKeywordSubstitution(providerFiles, comment, Policy.subMonitorFor(monitor, workPerProvider)); 212 if (status.getCode() != IStatus.OK) { 213 messages.add(status); 214 } 215 } 216 CVSUIPlugin.broadcastPropertyChange(new PropertyChangeEvent(this, CVSUIPlugin.P_DECORATORS_CHANGED, null, null)); 220 } catch (TeamException e) { 221 throw new InvocationTargetException (e); 222 } finally { 223 monitor.done(); 224 } 225 } 226 }); 227 if (!messages.isEmpty()) { 229 boolean error = false; 230 final MultiStatus combinedStatus = new MultiStatus(CVSUIPlugin.ID, 0, CVSUIMessages.ModeWizard_4, null); 231 for (int i = 0; i < messages.size(); i++) { 232 final IStatus status = (IStatus)messages.get(i); 233 if (status.getSeverity() == IStatus.ERROR || status.getCode() == CVSStatus.SERVER_ERROR) { 234 error = true; 235 } 236 combinedStatus.merge(status); 237 } 238 String message = null; 239 IStatus statusToDisplay; 240 if (combinedStatus.getChildren().length == 1) { 241 message = combinedStatus.getMessage(); 242 statusToDisplay = combinedStatus.getChildren()[0]; 243 } else { 244 statusToDisplay = combinedStatus; 245 } 246 final String title= error ? CVSUIMessages.ModeWizard_5 : CVSUIMessages.ModeWizard_6; CVSUIPlugin.openError(getShell(), title, message, new CVSException(statusToDisplay)); 248 } 249 return super.performFinish(); 250 } catch (InterruptedException e) { 251 return true; 252 } catch (InvocationTargetException e) { 253 CVSUIPlugin.openError(getShell(), CVSUIMessages.ModeWizard_4, null, e); 254 return false; 255 } 256 } 257 258 263 static Map getProviderMapping(Collection changes) { 264 265 final Map table = new HashMap (); 266 267 for (Iterator iter = changes.iterator(); iter.hasNext();) { 268 final ModeChange change= (ModeChange)iter.next(); 269 270 if (!change.hasChanged()) 271 continue; 272 273 final IFile file = change.getFile(); 274 final RepositoryProvider provider = RepositoryProvider.getProvider(file.getProject(), CVSProviderPlugin.getTypeId()); 275 276 if (!table.containsKey(provider)) { 277 table.put(provider, new HashMap ()); 278 } 279 final Map providerMap = (Map )table.get(provider); 280 providerMap.put(file, change.getNewMode()); 281 } 282 return table; 283 } 284 285 286 } 287 | Popular Tags |