KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > actions > GenerateDiffFileAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.mapping.ResourceMapping;
20 import org.eclipse.core.resources.mapping.ResourceTraversal;
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.team.core.subscribers.SubscriberResourceMappingContext;
25 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
26 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
27 import org.eclipse.team.internal.ccvs.ui.ICVSUIConstants;
28 import org.eclipse.team.internal.ccvs.ui.wizards.GenerateDiffFileWizard;
29 import org.eclipse.ui.PlatformUI;
30
31 /**
32  * Action to generate a patch file using the CVS diff command.
33  *
34  * NOTE: This is a temporary action and should eventually be replaced
35  * by a create patch command in the compare viewer.
36  */

37 public class GenerateDiffFileAction extends WorkspaceTraversalAction{
38     
39     /** (Non-javadoc)
40      * Method declared on IActionDelegate.
41      */

42     public void execute(IAction action) {
43
44         try {
45             final IResource [][] resources = new IResource[][] { null };
46             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
47                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
48                     try {
49                         resources[0] = getDeepResourcesToPatch(monitor);
50                     } catch (CoreException e) {
51                         throw new InvocationTargetException JavaDoc(e);
52                     }
53                 }
54             });
55             GenerateDiffFileWizard.run(getTargetPart(), resources[0]);
56         } catch (InvocationTargetException JavaDoc e) {
57             CVSUIPlugin.openError(getShell(), null, null, e, CVSUIPlugin.LOG_NONTEAM_EXCEPTIONS);
58         } catch (InterruptedException JavaDoc e) {
59             // Ignore
60
}
61     }
62
63      private IResource[] getDeepResourcesToPatch(IProgressMonitor monitor) throws CoreException {
64             ResourceMapping[] mappings = getCVSResourceMappings();
65             List JavaDoc roots = new ArrayList JavaDoc();
66             for (int i = 0; i < mappings.length; i++) {
67                 ResourceMapping mapping = mappings[i];
68                 ResourceTraversal[] traversals = mapping.getTraversals(
69                         SubscriberResourceMappingContext.createContext(CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber()),
70                         monitor);
71                 for (int j = 0; j < traversals.length; j++) {
72                     ResourceTraversal traversal = traversals[j];
73                     IResource[] resources = traversal.getResources();
74                     if (traversal.getDepth() == IResource.DEPTH_INFINITE) {
75                         roots.addAll(Arrays.asList(resources));
76                     } else if (traversal.getDepth() == IResource.DEPTH_ZERO) {
77                         collectShallowFiles(resources, roots);
78                     } else if (traversal.getDepth() == IResource.DEPTH_ONE) {
79                         collectShallowFiles(resources, roots);
80                         for (int k = 0; k < resources.length; k++) {
81                             IResource resource = resources[k];
82                             if (resource.getType() != IResource.FILE) {
83                                 collectShallowFiles(members(resource), roots);
84                             }
85                         }
86                     }
87                 }
88             }
89             return (IResource[]) roots.toArray(new IResource[roots.size()]);
90         }
91      
92        private void collectShallowFiles(IResource[] resources, List JavaDoc roots) {
93             for (int k = 0; k < resources.length; k++) {
94                 IResource resource = resources[k];
95                 if (resource.getType() == IResource.FILE)
96                     roots.add(resource);
97             }
98         }
99        
100        private IResource[] members(IResource resource) throws CoreException {
101             return CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().members(resource);
102         }
103     /**
104      * @see org.eclipse.team.internal.ccvs.ui.actions.WorkspaceAction#isEnabledForMultipleResources()
105      */

106     protected boolean isEnabledForMultipleResources() {
107             return true;
108     }
109
110     /**
111      * @see org.eclipse.team.internal.ccvs.ui.actions.WorkspaceAction#isEnabledForUnmanagedResources()
112      */

113     protected boolean isEnabledForUnmanagedResources() {
114         return true;
115     }
116
117     
118     /* (non-Javadoc)
119      * @see org.eclipse.team.internal.ccvs.ui.actions.CVSAction#getId()
120      */

121     public String JavaDoc getId() {
122         return ICVSUIConstants.CMD_CREATEPATCH;
123     }
124 }
125
Popular Tags