KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Brock Janiczak <brockj@tpg.com.au> - Bug 161536 Warn user when committing resources with problem markers
11  * Brock Janiczak <brockj@tpg.com.au> - Bug 177519 [Wizards] Adopt new IResource.findMaxProblemSeverity API
12  *******************************************************************************/

13
14 package org.eclipse.team.internal.ccvs.ui.wizards;
15
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.eclipse.core.resources.*;
28 import org.eclipse.core.resources.mapping.ResourceTraversal;
29 import org.eclipse.core.runtime.*;
30 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
31 import org.eclipse.core.runtime.jobs.IJobChangeListener;
32 import org.eclipse.jface.dialogs.*;
33 import org.eclipse.jface.operation.IRunnableWithProgress;
34 import org.eclipse.jface.preference.IPreferenceStore;
35 import org.eclipse.jface.wizard.IWizardPage;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.team.core.IFileContentManager;
38 import org.eclipse.team.core.Team;
39 import org.eclipse.team.core.synchronize.*;
40 import org.eclipse.team.internal.ccvs.core.*;
41 import org.eclipse.team.internal.ccvs.core.client.Command;
42 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
43 import org.eclipse.team.internal.ccvs.ui.*;
44 import org.eclipse.team.internal.ccvs.ui.operations.*;
45 import org.eclipse.team.internal.core.subscribers.SubscriberSyncInfoCollector;
46 import org.eclipse.team.internal.ui.Policy;
47 import org.eclipse.team.ui.synchronize.ResourceScope;
48 import org.eclipse.ui.IWorkbenchPart;
49 import org.eclipse.ui.PlatformUI;
50
51 /**
52  * A wizard to commit the resources whose synchronization state is given in form
53  * of a set of <code>SyncInfo</code>.
54  */

55 public class CommitWizard extends ResizableWizard {
56     
57     /**
58      * An operation to add and commit resources to a CVS repository.
59      */

60     public static class AddAndCommitOperation extends CVSOperation {
61         
62         private final IResource[] fAllResources;
63         private final String JavaDoc fComment;
64         
65         private Map JavaDoc fModesForExtensionsForOneTime;
66         private Map JavaDoc fModesForNamesForOneTime;
67         
68         private IResource[] fNewResources;
69         private IJobChangeListener jobListener;
70         
71         public AddAndCommitOperation(IWorkbenchPart part, IResource[] allResources, IResource[] newResources, String JavaDoc comment) {
72             super(part);
73             fAllResources = allResources;
74             fNewResources = newResources;
75             fModesForExtensionsForOneTime = Collections.EMPTY_MAP;
76             fModesForNamesForOneTime= Collections.EMPTY_MAP;
77             fComment = comment;
78         }
79         
80         public void setModesForExtensionsForOneTime(Map JavaDoc modes) {
81             if (modes != null)
82                 fModesForExtensionsForOneTime= modes;
83         }
84         
85         public void setModesForNamesForOneTime(Map JavaDoc modes) {
86             if (modes != null)
87                 fModesForNamesForOneTime= modes;
88         }
89         
90         protected void execute(IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
91             try {
92                 monitor.beginTask(null, (fNewResources.length + fAllResources.length) * 100);
93                 if (fNewResources.length > 0) {
94                     final AddOperation op= new AddOperation(getPart(), RepositoryProviderOperation.asResourceMappers(fNewResources));
95                     op.addModesForExtensions(fModesForExtensionsForOneTime);
96                     op.addModesForNames(fModesForNamesForOneTime);
97                     op.run(Policy.subMonitorFor(monitor, fNewResources.length * 100));
98                 }
99                 if (fAllResources.length > 0) {
100                     CommitOperation commitOperation = new CommitOperation(getPart(), RepositoryProviderOperation.asResourceMappers(fAllResources), new Command.LocalOption[0], fComment) {
101                         public boolean consultModelsForMappings() {
102                             // Do not consult models from the commit wizard
103
return false;
104                         }
105                     };
106                     commitOperation.run(Policy.subMonitorFor(monitor, fAllResources.length * 100));
107                 }
108             } catch (InvocationTargetException JavaDoc e) {
109                 throw CVSException.wrapException(e);
110             } finally {
111                 monitor.done();
112             }
113         }
114         
115         protected String JavaDoc getJobName() {
116             return CVSUIMessages.CommitWizard_0;
117         }
118         
119         protected String JavaDoc getTaskName() {
120             return CVSUIMessages.CommitWizard_1;
121         }
122
123         /*
124          * Set the job listener. It will only recieve scheduled and done
125          * events as these are what are used by a sync model operation
126          * to show busy state in the sync view.
127          */

128         protected void setJobChangeListener(IJobChangeListener jobListener) {
129             this.jobListener = jobListener;
130         }
131         
132         
133         /* (non-Javadoc)
134          * @see org.eclipse.core.runtime.jobs.IJobChangeListener#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
135          */

136         public void done(IJobChangeEvent event) {
137             super.done(event);
138             if (jobListener != null)
139                 jobListener.done(event);
140         }
141         
142         /* (non-Javadoc)
143          * @see org.eclipse.core.runtime.jobs.IJobChangeListener#scheduled(org.eclipse.core.runtime.jobs.IJobChangeEvent)
144          */

145         public void scheduled(IJobChangeEvent event) {
146             super.scheduled(event);
147             if (jobListener != null)
148                 jobListener.scheduled(event);
149         }
150     }
151     
152     private final IResource[] fResources;
153     private final SyncInfoSet fOutOfSyncInfos;
154     private final SyncInfoSet fUnaddedInfos;
155     private final CommitWizardParticipant fParticipant;
156     
157     private CommitWizardFileTypePage fFileTypePage;
158     private CommitWizardCommitPage fCommitPage;
159     private IJobChangeListener jobListener;
160     private IWorkbenchPart part;
161     
162     public CommitWizard(SyncInfoSet infos) throws CVSException {
163         this(infos.getResources());
164     }
165     
166     public CommitWizard(final IResource [] resources) throws CVSException {
167         
168         super(CVSUIMessages.CommitWizard_3, CVSUIPlugin.getPlugin().getDialogSettings());
169         
170         setWindowTitle(CVSUIMessages.CommitWizard_2);
171         setDefaultPageImageDescriptor(CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_NEW_LOCATION));
172         
173         fResources= resources;
174         fParticipant= new CommitWizardParticipant(new ResourceScope(fResources), this);
175         
176         SyncInfoSet infos = getAllOutOfSync();
177         fOutOfSyncInfos= new SyncInfoSet(infos.getNodes(new FastSyncInfoFilter.SyncInfoDirectionFilter(new int [] { SyncInfo.OUTGOING, SyncInfo.CONFLICTING })));
178         fUnaddedInfos= getUnaddedInfos(fOutOfSyncInfos);
179     }
180
181     public CommitWizard(SyncInfoSet infos, IJobChangeListener jobListener) throws CVSException {
182         this(infos);
183         this.jobListener = jobListener;
184     }
185
186     private SyncInfoSet getAllOutOfSync() throws CVSException {
187         final SubscriberSyncInfoCollector syncInfoCollector = fParticipant.getSubscriberSyncInfoCollector();
188             try {
189                 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
190                     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
191                         monitor.beginTask(CVSUIMessages.CommitWizard_4, IProgressMonitor.UNKNOWN);
192                         syncInfoCollector.waitForCollector(monitor);
193                         monitor.done();
194                     }
195                 });
196             } catch (InvocationTargetException JavaDoc e) {
197                 throw CVSException.wrapException(e);
198             } catch (InterruptedException JavaDoc e) {
199                 throw new OperationCanceledException();
200             }
201         return fParticipant.getSyncInfoSet();
202     }
203     
204     public boolean hasOutgoingChanges() {
205         return fOutOfSyncInfos.size() > 0;
206     }
207     
208     public int getHighestProblemSeverity() {
209         IResource[] resources = fOutOfSyncInfos.getResources();
210         int mostSeriousSeverity = -1;
211         
212         for (int i = 0; i < resources.length; i++) {
213             IResource resource = resources[i];
214             try {
215                 int severity = resource.findMaxProblemSeverity(IMarker.PROBLEM, true, IResource.DEPTH_ZERO);
216                 if (severity > mostSeriousSeverity) {
217                     mostSeriousSeverity = severity;
218                 }
219             } catch (CoreException e) {
220             }
221         }
222         
223         return mostSeriousSeverity;
224     }
225     
226     public CommitWizardFileTypePage getFileTypePage() {
227         return fFileTypePage;
228     }
229
230     public CommitWizardCommitPage getCommitPage() {
231         return fCommitPage;
232     }
233
234     public CommitWizardParticipant getParticipant() {
235         return fParticipant;
236     }
237
238     public boolean canFinish() {
239         final IWizardPage current= getContainer().getCurrentPage();
240         if (current == fFileTypePage && fCommitPage != null)
241             return false;
242         return super.canFinish();
243     }
244
245     public boolean performFinish() {
246         
247         final String JavaDoc comment= fCommitPage.getComment(getShell());
248         if (comment == null)
249             return false;
250         
251         final SyncInfoSet infos= fCommitPage.getInfosToCommit();
252         if (infos.size() == 0)
253             return true;
254         
255         final SyncInfoSet unadded;
256         try {
257             unadded = getUnaddedInfos(infos);
258         } catch (CVSException e1) {
259             return false;
260         }
261         
262         final SyncInfoSet files;
263         try {
264             files = getFiles(infos);
265         } catch (CVSException e1) {
266             return false;
267         }
268         
269         final AddAndCommitOperation operation= new AddAndCommitOperation(getPart(), files.getResources(), unadded.getResources(), comment);
270         if (jobListener != null)
271             operation.setJobChangeListener(jobListener);
272         
273         if (fFileTypePage != null) {
274             final Map JavaDoc extensionsToSave= new HashMap JavaDoc();
275             final Map JavaDoc extensionsNotToSave= new HashMap JavaDoc();
276             
277             fFileTypePage.getModesForExtensions(extensionsToSave, extensionsNotToSave);
278             CommitWizardFileTypePage.saveExtensionMappings(extensionsToSave);
279             operation.setModesForExtensionsForOneTime(extensionsNotToSave);
280             
281             final Map JavaDoc namesToSave= new HashMap JavaDoc();
282             final Map JavaDoc namesNotToSave= new HashMap JavaDoc();
283             
284             fFileTypePage.getModesForNames(namesToSave, namesNotToSave);
285             CommitWizardFileTypePage.saveNameMappings(namesToSave);
286             operation.setModesForNamesForOneTime(namesNotToSave);
287         }
288         
289         try {
290             operation.run();
291         } catch (InvocationTargetException JavaDoc e) {
292             return false;
293         } catch (InterruptedException JavaDoc e) {
294             return false;
295         }
296         
297         return super.performFinish();
298     }
299
300     public void addPages() {
301         
302         final Collection JavaDoc names= new HashSet JavaDoc();
303         final Collection JavaDoc extensions= new HashSet JavaDoc();
304         getUnknownNamesAndExtension(fUnaddedInfos, names, extensions);
305         
306         if (names.size() + extensions.size() > 0) {
307             fFileTypePage= new CommitWizardFileTypePage(extensions, names);
308             addPage(fFileTypePage);
309         }
310         
311         fCommitPage= new CommitWizardCommitPage(fResources, this);
312         addPage(fCommitPage);
313         
314         super.addPages();
315     }
316
317     public void dispose() {
318         fParticipant.dispose();
319         super.dispose();
320     }
321     
322     public static void run(IWorkbenchPart part, Shell shell, IResource [] resources) throws CVSException {
323         try {
324             CommitWizard commitWizard = new CommitWizard(resources);
325             commitWizard.setPart(part);
326             run(shell, commitWizard);
327         } catch (OperationCanceledException e) {
328             // Ignore
329
}
330     }
331     
332     private void setPart(IWorkbenchPart part) {
333         this.part = part;
334     }
335
336     public static void run(Shell shell, SyncInfoSet infos, IJobChangeListener jobListener) throws CVSException {
337         try {
338             run(shell, new CommitWizard(infos, jobListener));
339         } catch (OperationCanceledException e) {
340             // Ignore
341
}
342     }
343     
344     public static void run(IWorkbenchPart part, Shell shell, final ResourceTraversal[] traversals) throws CVSException {
345         try {
346             final IResource [][] resources = new IResource[][] { null };
347             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
348                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
349                     try {
350                         resources[0] = getDeepResourcesToCommit(traversals, monitor);
351                     } catch (CoreException e) {
352                         throw new InvocationTargetException JavaDoc(e);
353                     }
354                 }
355             });
356             run(part, shell, resources[0]);
357         } catch (OperationCanceledException e) {
358             // Ignore
359
} catch (InvocationTargetException JavaDoc e) {
360             throw CVSException.wrapException(e);
361         } catch (InterruptedException JavaDoc e) {
362             // Ignore
363
}
364     }
365
366     private IWorkbenchPart getPart() {
367         if (part != null)
368             return part;
369         return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService().getActivePart();
370     }
371     
372     private static void run(Shell shell, CommitWizard wizard) {
373         if (!wizard.hasOutgoingChanges()) {
374             MessageDialog.openInformation(shell, CVSUIMessages.CommitWizard_6, CVSUIMessages.CommitWizard_7); //
375
} else {
376             int highestProblemSeverity = wizard.getHighestProblemSeverity();
377             IPreferenceStore preferenceStore = CVSUIPlugin.getPlugin().getPreferenceStore();
378             switch (highestProblemSeverity) {
379             case IMarker.SEVERITY_WARNING:
380                 String JavaDoc allowCommitsWithWarnings = preferenceStore.getString(ICVSUIConstants.PREF_ALLOW_COMMIT_WITH_WARNINGS);
381                 if (MessageDialogWithToggle.PROMPT.equals(allowCommitsWithWarnings) || MessageDialogWithToggle.NEVER.equals(allowCommitsWithWarnings)) {
382                     MessageDialogWithToggle warningDialog = MessageDialogWithToggle.openYesNoQuestion(shell, CVSUIMessages.CommitWizard_8, CVSUIMessages.CommitWizard_9, CVSUIMessages.CommitWizard_10, false, preferenceStore, ICVSUIConstants.PREF_ALLOW_COMMIT_WITH_WARNINGS);
383                     if (IDialogConstants.YES_ID != warningDialog.getReturnCode()) {
384                         return;
385                     }
386                 }
387                 break;
388             case IMarker.SEVERITY_ERROR:
389                 String JavaDoc allowCommitsWithErrors = preferenceStore.getString(ICVSUIConstants.PREF_ALLOW_COMMIT_WITH_ERRORS);
390                 if (MessageDialogWithToggle.PROMPT.equals(allowCommitsWithErrors) || MessageDialogWithToggle.NEVER.equals(allowCommitsWithErrors)) {
391                     MessageDialogWithToggle errorDialog = MessageDialogWithToggle.openYesNoQuestion(shell, CVSUIMessages.CommitWizard_11, CVSUIMessages.CommitWizard_12, CVSUIMessages.CommitWizard_13, false, preferenceStore, ICVSUIConstants.PREF_ALLOW_COMMIT_WITH_ERRORS);
392                     if (IDialogConstants.YES_ID != errorDialog.getReturnCode()) {
393                         return;
394                     }
395                 }
396                 break;
397             }
398             open(shell, wizard);
399         }
400     }
401
402     
403     private static void getUnknownNamesAndExtension(SyncInfoSet infos, Collection JavaDoc names, Collection JavaDoc extensions) {
404         
405         final IFileContentManager manager= Team.getFileContentManager();
406         
407         for (final Iterator JavaDoc iter = infos.iterator(); iter.hasNext();) {
408             
409             final SyncInfo info = (SyncInfo)iter.next();
410             
411             IResource local = info.getLocal();
412             if (local instanceof IFile && manager.getType((IFile)local) == Team.UNKNOWN) {
413                 final String JavaDoc extension= local.getFileExtension();
414                 if (extension != null && !manager.isKnownExtension(extension)) {
415                     extensions.add(extension);
416                 }
417                 
418                 final String JavaDoc name= local.getName();
419                 if (extension == null && name != null && !manager.isKnownFilename(name))
420                     names.add(name);
421             }
422         }
423     }
424     
425     private static SyncInfoSet getUnaddedInfos(SyncInfoSet infos) throws CVSException {
426         final SyncInfoSet unadded= new SyncInfoSet();
427         for (final Iterator JavaDoc iter = infos.iterator(); iter.hasNext();) {
428             final SyncInfo info = (SyncInfo) iter.next();
429             final IResource resource= info.getLocal();
430             if (!isAdded(resource))
431                 unadded.add(info);
432         }
433         return unadded;
434     }
435     
436     private static SyncInfoSet getFiles(SyncInfoSet infos) throws CVSException {
437         final SyncInfoSet files= new SyncInfoSet();
438         for (final Iterator JavaDoc iter = infos.iterator(); iter.hasNext();) {
439             final SyncInfo info = (SyncInfo) iter.next();
440             final IResource resource= info.getLocal();
441             if (resource.getType() == IResource.FILE)
442                 files.add(info);
443         }
444         return files;
445     }
446     
447     private static boolean isAdded(IResource resource) throws CVSException {
448         final ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
449         if (cvsResource.isFolder()) {
450             return ((ICVSFolder)cvsResource).isCVSFolder();
451         }
452         return cvsResource.isManaged();
453     }
454
455     private static IResource[] getDeepResourcesToCommit(ResourceTraversal[] traversals, IProgressMonitor monitor) throws CoreException {
456         List JavaDoc roots = new ArrayList JavaDoc();
457         for (int j = 0; j < traversals.length; j++) {
458             ResourceTraversal traversal = traversals[j];
459             IResource[] resources = traversal.getResources();
460             if (traversal.getDepth() == IResource.DEPTH_INFINITE) {
461                 roots.addAll(Arrays.asList(resources));
462             } else if (traversal.getDepth() == IResource.DEPTH_ZERO) {
463                 collectShallowFiles(resources, roots);
464             } else if (traversal.getDepth() == IResource.DEPTH_ONE) {
465                 collectShallowFiles(resources, roots);
466                 for (int k = 0; k < resources.length; k++) {
467                     IResource resource = resources[k];
468                     if (resource.getType() != IResource.FILE) {
469                         collectShallowFiles(members(resource), roots);
470                     }
471                 }
472             }
473         }
474         return (IResource[]) roots.toArray(new IResource[roots.size()]);
475     }
476
477     private static IResource[] members(IResource resource) throws CoreException {
478         return CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().members(resource);
479     }
480
481     private static void collectShallowFiles(IResource[] resources, List JavaDoc roots) {
482         for (int k = 0; k < resources.length; k++) {
483             IResource resource = resources[k];
484             if (resource.getType() == IResource.FILE)
485                 roots.add(resource);
486         }
487     }
488 }
489
490
Popular Tags