KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > mappings > CreatePatchAction


1 /*******************************************************************************
2  * Copyright (c) 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.mappings;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.mapping.ResourceTraversal;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.team.core.diff.*;
24 import org.eclipse.team.core.mapping.IResourceDiffTree;
25 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
26 import org.eclipse.team.internal.ccvs.ui.wizards.GenerateDiffFileWizard;
27 import org.eclipse.team.internal.ui.Utils;
28 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
29 import org.eclipse.ui.PlatformUI;
30
31 public class CreatePatchAction extends CVSModelProviderAction implements IDiffChangeListener {
32
33     public CreatePatchAction(ISynchronizePageConfiguration configuration) {
34         super(configuration);
35         getSynchronizationContext().getDiffTree().addDiffChangeListener(this);
36     }
37
38     /* (non-Javadoc)
39      * @see org.eclipse.team.internal.ui.mapping.ModelProviderAction#isEnabledForSelection(org.eclipse.jface.viewers.IStructuredSelection)
40      */

41     protected boolean isEnabledForSelection(IStructuredSelection selection) {
42         return internalIsEnabled(selection);
43     }
44     
45     private boolean internalIsEnabled(IStructuredSelection selection) {
46         // Only enable commit in outgoing or both modes
47
int mode = getConfiguration().getMode();
48         if (mode == ISynchronizePageConfiguration.OUTGOING_MODE || mode == ISynchronizePageConfiguration.BOTH_MODE) {
49             return getResourceMappings(selection).length > 0;
50         }
51         return getSynchronizationContext().getDiffTree().countFor(IThreeWayDiff.CONFLICTING, IThreeWayDiff.DIRECTION_MASK) > 0;
52     }
53     
54     private IResource[] getVisibleResources(ResourceTraversal[] traversals) {
55         final Set JavaDoc resources = new HashSet JavaDoc();
56         final IResourceDiffTree diffTree = getSynchronizationContext().getDiffTree();
57         IDiff[] diffs = diffTree.getDiffs(traversals);
58         for (int i = 0; i < diffs.length; i++) {
59             IDiff diff = diffs[i];
60             IResource child = diffTree.getResource(diff);
61             if (child.getType() == IResource.FILE && diff instanceof IThreeWayDiff) {
62                 IThreeWayDiff twd = (IThreeWayDiff) diff;
63                 IDiff local = twd.getLocalChange();
64                 if (local != null && local.getKind() != IDiff.NO_CHANGE) {
65                     resources.add(child);
66                 }
67             }
68         }
69         return (IResource[]) resources.toArray(new IResource[resources.size()]);
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.team.internal.ccvs.ui.mappings.CVSModelProviderAction#getBundleKeyPrefix()
74      */

75     protected String JavaDoc getBundleKeyPrefix() {
76         return "GenerateDiffFileAction."; //$NON-NLS-1$
77
}
78     
79     public void execute() {
80         final ResourceTraversal [][] traversals = new ResourceTraversal[][] { null };
81         try {
82             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
83                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
84                     try {
85                         traversals[0] = getResourceTraversals(getStructuredSelection(), monitor);
86                     } catch (CoreException e) {
87                         throw new InvocationTargetException JavaDoc(e);
88                     }
89                 }
90             });
91         } catch (InvocationTargetException JavaDoc e) {
92             Utils.handleError(getConfiguration().getSite().getShell(), e, null, null);
93         } catch (InterruptedException JavaDoc e) {
94             // Ignore
95
}
96         if (traversals[0] != null) {
97             IResource[] resources = getVisibleResources(traversals[0]);
98             if (resources.length == 0) {
99                 MessageDialog.openInformation(getConfiguration().getSite().getShell(), CVSUIMessages.CreatePatchAction_0, CVSUIMessages.CreatePatchAction_1);
100             } else {
101                 GenerateDiffFileWizard.run(getConfiguration().getSite().getPart(), resources, false);
102             }
103         }
104     }
105
106     public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) {
107         updateEnablement();
108     }
109
110     public void propertyChanged(IDiffTree tree, int property, IPath[] paths) {
111         // Nothing to do
112
}
113
114 }
115
Popular Tags