KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set 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.dialogs.Dialog;
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.operation.IRunnableWithProgress;
33 import org.eclipse.jface.resource.ImageDescriptor;
34 import org.eclipse.jface.wizard.IWizardPage;
35 import org.eclipse.jface.wizard.Wizard;
36 import org.eclipse.swt.custom.BusyIndicator;
37 import org.eclipse.team.core.RepositoryProvider;
38 import org.eclipse.team.core.TeamException;
39 import org.eclipse.team.internal.ccvs.core.CVSException;
40 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
41 import org.eclipse.team.internal.ccvs.core.CVSStatus;
42 import org.eclipse.team.internal.ccvs.core.CVSTeamProvider;
43 import org.eclipse.team.internal.ccvs.core.ICVSFile;
44 import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption;
45 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
46 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
47 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
48 import org.eclipse.team.internal.ccvs.ui.ICVSUIConstants;
49 import org.eclipse.team.internal.ccvs.ui.Policy;
50
51 /**
52  * A wizard for changing the keyword substitution mode of files.
53  *
54  * 1. Ask the user select to select the desired keyword substitution mode.
55  * 2. Compute the set of possibly affected resources
56  * 3. If the affected resources include existing committed files, warn the user
57  * and provide an option to include them in the operation anyways.
58  * 4. If the affected resources include dirty files, warn the user and provide
59  * an option to include them in the operation anyways.
60  * 5. Perform the operation on Finish.
61  */

62 public class KSubstWizard extends Wizard {
63     private KSubstOption defaultKSubst;
64
65     private final IResource[] resources;
66     private final int depth;
67     private List JavaDoc changeList = null;
68     private KSubstOption changeOption = null;
69
70     private KSubstWizardSelectionPage mainPage;
71     private KSubstWizardSummaryPage summaryPage;
72     private KSubstWizardSharedFilesPage sharedFilesPage;
73     private KSubstWizardDirtyFilesPage dirtyFilesPage;
74     
75     private Dialog parentDialog;
76
77     private KSubstWizardCommitCommentPage commitCommentPage;
78
79     public class KSubstChangeElement {
80         public static final int ADDED_FILE = 1;
81         public static final int CHANGED_FILE = 2;
82         public static final int UNCHANGED_FILE = 4;
83     
84         private IFile file;
85         private int classification;
86         private boolean excluded;
87         private KSubstOption fromKSubst;
88         private KSubstOption toKSubst;
89         
90         private KSubstChangeElement(IFile file, int classification, boolean excluded, KSubstOption fromKSubst, KSubstOption toKSubst) {
91             this.file = file;
92             this.classification = classification;
93             this.excluded = excluded;
94             this.fromKSubst = fromKSubst;
95             this.toKSubst = toKSubst;
96         }
97         public boolean matchesFilter(int filter) {
98             return (classification & filter) != 0;
99         }
100         public boolean isExcluded() {
101             return excluded;
102         }
103         public void setExcluded(boolean excluded) {
104             this.excluded = excluded;
105         }
106         public boolean isNewKSubstMode() {
107             return ! fromKSubst.equals(toKSubst);
108         }
109         public void setKSubst(KSubstOption toKSubst) {
110             this.toKSubst = toKSubst;
111         }
112         public KSubstOption getKSubst() {
113             return toKSubst;
114         }
115         public IFile getFile() {
116             return file;
117         }
118     }
119     
120     /**
121      * Creates a wizard to set the keyword substitution mode for the specified resources.
122      *
123      * @param resources the resources to alter
124      * @param depth the recursion depth
125      * @param defaultOption the keyword substitution option to select by default
126      */

127     public KSubstWizard(IResource[] resources, int depth, KSubstOption defaultOption) {
128         super();
129         this.defaultKSubst = defaultOption;
130         this.resources = resources;
131         this.depth = depth;
132         setWindowTitle(Policy.bind("KSubstWizard.title")); //$NON-NLS-1$
133
}
134
135     /**
136      * Returns the keyword substitution option that was selected at the time
137      * the Finish button was pressed.
138      */

139     public KSubstOption getKSubstOption() {
140         return defaultKSubst;
141     }
142
143     public void addPages() {
144         ImageDescriptor substImage = CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_KEYWORD);
145         
146         // add main page
147
String JavaDoc pageTitle = Policy.bind("KSubstWizardSelectionPage.pageTitle"); //$NON-NLS-1$
148
String JavaDoc pageDescription = Policy.bind("KSubstWizardSelectionPage.pageDescription"); //$NON-NLS-1$
149
mainPage = new KSubstWizardSelectionPage(pageTitle, pageTitle, substImage, defaultKSubst);
150         mainPage.setDescription(pageDescription);
151         mainPage.setTitle(pageTitle);
152         addPage(mainPage);
153         
154         // add summary page
155
pageTitle = Policy.bind("KSubstWizardSummaryPage.pageTitle"); //$NON-NLS-1$
156
pageDescription = Policy.bind("KSubstWizardSummaryPage.pageDescription"); //$NON-NLS-1$
157
summaryPage = new KSubstWizardSummaryPage(pageTitle, pageTitle, substImage, false);
158         summaryPage.setDescription(pageDescription);
159         addPage(summaryPage);
160         
161         // add shared files warning page
162
pageTitle = Policy.bind("KSubstWizardSharedFilesPage.pageTitle"); //$NON-NLS-1$
163
pageDescription = Policy.bind("KSubstWizardSharedFilesPage.pageDescription"); //$NON-NLS-1$
164
sharedFilesPage = new KSubstWizardSharedFilesPage(pageTitle, pageTitle, substImage, false);
165         sharedFilesPage.setDescription(pageDescription);
166         addPage(sharedFilesPage);
167         
168         // add changed files warning page
169
pageTitle = Policy.bind("KSubstWizardDirtyFilesPage.pageTitle"); //$NON-NLS-1$
170
pageDescription = Policy.bind("KSubstWizardDirtyFilesPage.pageDescription"); //$NON-NLS-1$
171
dirtyFilesPage = new KSubstWizardDirtyFilesPage(pageTitle, pageTitle, substImage, false);
172         dirtyFilesPage.setDescription(pageDescription);
173         addPage(dirtyFilesPage);
174         
175         // add commit comment page
176
pageTitle = Policy.bind("KSubstWizardCommitCommentPage.pageTitle"); //$NON-NLS-1$
177
pageDescription = Policy.bind("KSubstWizardCommitCommentPage.pageDescription"); //$NON-NLS-1$
178
commitCommentPage = new KSubstWizardCommitCommentPage(parentDialog, pageTitle, pageTitle, substImage, pageDescription);
179         addPage(commitCommentPage);
180     }
181
182     public IWizardPage getNextPage(IWizardPage page) {
183         if (page == mainPage) {
184             if (prepareSharedFilesPage()) return sharedFilesPage;
185         } else if (page == sharedFilesPage) {
186             if (sharedFilesPage.includeSharedFiles() && prepareDirtyFilesPage()) return dirtyFilesPage;
187         } else if (page == summaryPage) {
188             return null;
189         }
190         prepareSummaryPage();
191         if (page != commitCommentPage) return commitCommentPage;
192         return summaryPage;
193     }
194     
195     public IWizardPage getPreviousPage(IWizardPage page) {
196         if (page == summaryPage) {
197             return commitCommentPage;
198         } else if (page == commitCommentPage) {
199             if (sharedFilesPage.includeSharedFiles() && prepareDirtyFilesPage()) return dirtyFilesPage;
200             if (prepareSharedFilesPage()) return sharedFilesPage;
201             return mainPage;
202         } else if (page == dirtyFilesPage) {
203             if (prepareSharedFilesPage()) return sharedFilesPage;
204             return mainPage;
205         } else if (page == sharedFilesPage) {
206             return mainPage;
207         }
208         return null;
209     }
210     
211     /* (Non-javadoc)
212      * Method declared on IWizard.
213      */

214     public boolean needsProgressMonitor() {
215         return true;
216     }
217
218     /* (Non-javadoc)
219      * Method declared on IWizard.
220      */

221     public boolean needsPreviousAndNextButtons() {
222         return true;
223     }
224     
225     /* (Non-javadoc)
226      * Method declared on IWizard.
227      */

228     public boolean performFinish() {
229         try {
230             if (sharedFilesPage.includeSharedFiles()
231                     && !MessageDialog.openConfirm(getShell(), null, Policy.bind("KSubstWizardSharedFilesPage.contents"))) { //$NON-NLS-1$
232
return false;
233             }
234             defaultKSubst = mainPage.getKSubstOption();
235             final List JavaDoc messages = new ArrayList JavaDoc();
236             getContainer().run(false /*fork*/, true /*cancelable*/, new IRunnableWithProgress() {
237                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
238                     try {
239                         monitor.beginTask("", 10000); //$NON-NLS-1$
240
monitor.setTaskName(Policy.bind("KSubstWizard.working")); //$NON-NLS-1$
241
computeChangeList(mainPage.getKSubstOption());
242                         Map JavaDoc table = getProviderMapping();
243                         
244                         int workPerProvider = 10000 / (table.size() + 1);
245                         monitor.worked(workPerProvider);
246                         for (Iterator JavaDoc it = table.entrySet().iterator(); it.hasNext();) {
247                             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
248                             CVSTeamProvider provider = (CVSTeamProvider) entry.getKey();
249                             Map JavaDoc providerFiles = (Map JavaDoc) entry.getValue();
250
251                             String JavaDoc comment = commitCommentPage.getComment();
252                             IStatus status = provider.setKeywordSubstitution(providerFiles, comment,
253                                 Policy.subMonitorFor(monitor, workPerProvider));
254                             if (status.getCode() != CVSStatus.OK) {
255                                 messages.add(status);
256                             }
257                         }
258                     } catch (TeamException e) {
259                         throw new InvocationTargetException JavaDoc(e);
260                     } finally {
261                         monitor.done();
262                     }
263                 }
264             });
265             // Check for any status messages and display them
266
if ( ! messages.isEmpty()) {
267                 boolean error = false;
268                 MultiStatus combinedStatus = new MultiStatus(CVSUIPlugin.ID, 0,
269                     Policy.bind("KSubstWizard.problemsMessage"), null); //$NON-NLS-1$
270
for (int i = 0; i < messages.size(); i++) {
271                     IStatus status = (IStatus)messages.get(i);
272                     if (status.getSeverity() == IStatus.ERROR || status.getCode() == CVSStatus.SERVER_ERROR) {
273                         error = true;
274                     }
275                     combinedStatus.merge(status);
276                 }
277                 String JavaDoc message = null;
278                 IStatus statusToDisplay;
279                 if (combinedStatus.getChildren().length == 1) {
280                     message = combinedStatus.getMessage();
281                     statusToDisplay = combinedStatus.getChildren()[0];
282                 } else {
283                     statusToDisplay = combinedStatus;
284                 }
285                 String JavaDoc title;
286                 if (error) {
287                     title = Policy.bind("KSubstWizard.errorTitle"); //$NON-NLS-1$
288
} else {
289                     title = Policy.bind("KSubstWizard.warningTitle"); //$NON-NLS-1$
290
}
291                 CVSUIPlugin.openError(getShell(), title, message, new CVSException(statusToDisplay));
292             }
293             return true;
294         } catch (InterruptedException JavaDoc e1) {
295             return true;
296         } catch (InvocationTargetException JavaDoc e2) {
297             CVSUIPlugin.openError(getShell(), Policy.bind("KSubstWizard.problemsMessage"), null, e2); //$NON-NLS-1$
298
return false;
299         }
300     }
301
302     private boolean prepareDirtyFilesPage() {
303         BusyIndicator.showWhile(getContainer().getShell().getDisplay(), new Runnable JavaDoc() {
304             public void run() {
305                 computeChangeList(mainPage.getKSubstOption());
306                 dirtyFilesPage.setChangeList(changeList);
307             }
308         });
309         return ! dirtyFilesPage.isListEmpty();
310     }
311
312     private boolean prepareSharedFilesPage() {
313         BusyIndicator.showWhile(getContainer().getShell().getDisplay(), new Runnable JavaDoc() {
314             public void run() {
315                 computeChangeList(mainPage.getKSubstOption());
316                 sharedFilesPage.setChangeList(changeList);
317             }
318         });
319         return ! sharedFilesPage.isListEmpty();
320     }
321     
322     private void prepareSummaryPage() {
323         BusyIndicator.showWhile(getContainer().getShell().getDisplay(), new Runnable JavaDoc() {
324             public void run() {
325                 computeChangeList(mainPage.getKSubstOption());
326                 summaryPage.setChangeList(changeList, getFilters());
327             }
328         });
329     }
330     
331     /**
332      * @param ksubst the desired keyword substitution mode, if null chooses for each file:
333      * <code>KSubstOption.fromPattern(fileName).isBinary() ? KSUBST_BINARY : KSUBST_TEXT</code>
334      */

335     private void computeChangeList(final KSubstOption ksubst) {
336         if (changeList != null) {
337             if (changeOption == ksubst) return;
338             changeList.clear();
339         } else {
340             changeList = new ArrayList JavaDoc();
341         }
342         changeOption = ksubst;
343         // recurse over all specified resources, considering each exactly once
344
final Set JavaDoc seen = new HashSet JavaDoc();
345         for (int i = 0; i < resources.length; i++) {
346             final IResource currentResource = resources[i];
347             try {
348                 currentResource.accept(new IResourceVisitor() {
349                     public boolean visit(IResource resource) throws CoreException {
350                         try {
351                             if (resource.getType() == IResource.FILE && resource.exists() && ! seen.contains(resource)) {
352                                 seen.add(resource);
353                                 IFile file = (IFile) resource;
354                                 ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(file);
355                                 if (cvsFile.isManaged()) {
356                                     ResourceSyncInfo info = cvsFile.getSyncInfo();
357                                     // classify the change
358
final int classification;
359                                     if (info.isAdded()) {
360                                         classification = KSubstChangeElement.ADDED_FILE;
361                                     } else if (info.isDeleted()) {
362                                         return true;
363                                     } else if (cvsFile.isModified(null)) {
364                                         classification = KSubstChangeElement.CHANGED_FILE;
365                                     } else {
366                                         classification = KSubstChangeElement.UNCHANGED_FILE;
367                                     }
368                                     // determine the to/from substitution modes
369
KSubstOption fromKSubst = info.getKeywordMode();
370                                     KSubstOption toKSubst = ksubst;
371                                     if (ksubst == null) {
372                                         toKSubst = KSubstOption.fromFile(file);
373                                     }
374                                     changeList.add(new KSubstChangeElement(file, classification, false, fromKSubst, toKSubst));
375                                 }
376                             }
377                         } catch (TeamException e) {
378                             throw new CoreException(e.getStatus());
379                         }
380                         // always return true and let the depth determine if children are visited
381
return true;
382                     }
383                 }, depth, false);
384             } catch (CoreException e) {
385                 CVSUIPlugin.openError(getShell(), Policy.bind("KSubstWizard.problemsMessage"), null, e); //$NON-NLS-1$
386
}
387         }
388     }
389
390     private int getFilters() {
391         return KSubstChangeElement.ADDED_FILE |
392             (sharedFilesPage.includeSharedFiles() ? KSubstChangeElement.UNCHANGED_FILE |
393             (dirtyFilesPage.includeDirtyFiles() ? KSubstChangeElement.CHANGED_FILE : 0) : 0);
394     }
395     
396     private Map JavaDoc getProviderMapping() {
397         Map JavaDoc table = new HashMap JavaDoc();
398         int filter = getFilters();
399         for (Iterator JavaDoc it = changeList.iterator(); it.hasNext();) {
400             KSubstChangeElement change = (KSubstChangeElement) it.next();
401             if (! change.isExcluded() && change.isNewKSubstMode() && change.matchesFilter(filter)) {
402                 // classify file according to its provider
403
IFile file = change.getFile();
404                 RepositoryProvider provider = RepositoryProvider.getProvider(file.getProject(), CVSProviderPlugin.getTypeId());
405                 Map JavaDoc providerMap = (Map JavaDoc) table.get(provider);
406                 if (providerMap == null) {
407                     providerMap = new HashMap JavaDoc();
408                     table.put(provider, providerMap);
409                 }
410                 providerMap.put(file, change.toKSubst);
411             }
412         }
413         return table;
414     }
415
416     /**
417      * Method setParentDialog.
418      * @param dialog
419      */

420     public void setParentDialog(Dialog dialog) {
421         this.parentDialog = dialog;
422     }
423 }
424
Popular Tags