KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > ModeWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.team.internal.ccvs.ui.wizards;
12
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
49  * A wizard for changing the keyword substitution mode of files.
50  *
51  * 1. Ask the user select to select the desired keyword substitution mode.
52  * 2. Compute the set of possibly affected resources
53  * 3. If the affected resources include existing committed files, warn the user
54  * and provide an option to include them in the operation anyways.
55  * 4. If the affected resources include dirty files, warn the user and provide
56  * an option to include them in the operation anyways.
57  * 5. Perform the operation on Finish.
58  */

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 JavaDoc o) {
95             return fFile.getName().compareTo(((ModeChange)o).getFile().getName());
96         }
97     }
98     
99     protected List JavaDoc 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 JavaDoc() {
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     /**
117      * Creates a wizard to set the keyword substitution mode for the specified resources.
118      *
119      * @param resources the resources to alter
120      * @param depth the recursion depth
121      * @param defaultOption the keyword substitution option to select by default
122      */

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 // Workbench.getInstance().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
131

132     }
133     
134     public void addPages() {
135         addPage(fPage);
136     }
137     
138     /* (Non-javadoc)
139      * Method declared on IWizard.
140      */

141     public boolean needsProgressMonitor() {
142         return true;
143     }
144     
145     protected static List JavaDoc getModeChanges(Shell shell, IResource [] resources) {
146         
147         final ArrayList JavaDoc changes= new ArrayList JavaDoc();
148         final HashSet JavaDoc visited= new HashSet JavaDoc();
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                         // always return true and let the depth determine if children are visited
172
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 JavaDoc messages = new ArrayList JavaDoc();
185             final List JavaDoc changes= fPage.getChanges();
186             if (changes.size() == 0)
187                 return true;
188             
189             final String JavaDoc comment = fPage.getComment(getShell());
190             if (comment == null)
191                 return false;
192             
193             getContainer().run(false /*fork*/, true /*cancelable*/, new IRunnableWithProgress() {
194                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
195                     try {
196                         final int totalWork= 10000;
197                         monitor.beginTask(CVSUIMessages.ModeWizard_3, totalWork);
198                         
199                         final Map JavaDoc 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 JavaDoc iter = changesPerProvider.entrySet().iterator(); iter.hasNext();) {
207                             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
208                             final CVSTeamProvider provider = (CVSTeamProvider)entry.getKey();
209                             final Map JavaDoc providerFiles = (Map JavaDoc) 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                         // Broadcast a decorator change so all interested parties will update their labels.
217
// This is done in particular because the syncview will not see this change
218
// as a change in state for the resources involved
219
CVSUIPlugin.broadcastPropertyChange(new PropertyChangeEvent(this, CVSUIPlugin.P_DECORATORS_CHANGED, null, null));
220                     } catch (TeamException e) {
221                         throw new InvocationTargetException JavaDoc(e);
222                     } finally {
223                         monitor.done();
224                     }
225                 }
226             });
227             // Check for any status messages and display them
228
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 JavaDoc 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 JavaDoc title= error ? CVSUIMessages.ModeWizard_5 : CVSUIMessages.ModeWizard_6; //
247
CVSUIPlugin.openError(getShell(), title, message, new CVSException(statusToDisplay));
248             }
249             return super.performFinish();
250         } catch (InterruptedException JavaDoc e) {
251             return true;
252         } catch (InvocationTargetException JavaDoc e) {
253             CVSUIPlugin.openError(getShell(), CVSUIMessages.ModeWizard_4, null, e);
254             return false;
255         }
256     }
257     
258     /**
259      * Get a map
260      * @param changes
261      * @return
262      */

263     static Map JavaDoc getProviderMapping(Collection JavaDoc changes) {
264         
265         final Map JavaDoc table = new HashMap JavaDoc();
266         
267         for (Iterator JavaDoc 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 JavaDoc());
278             }
279             final Map JavaDoc providerMap = (Map JavaDoc)table.get(provider);
280             providerMap.put(file, change.getNewMode());
281         }
282         return table;
283     }
284
285
286 }
287
Popular Tags