KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.mappings;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.mapping.ResourceTraversal;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.team.core.diff.IDiff;
25 import org.eclipse.team.core.diff.IThreeWayDiff;
26 import org.eclipse.team.core.mapping.IResourceDiffTree;
27 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
28 import org.eclipse.team.internal.ccvs.core.CVSException;
29 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
30 import org.eclipse.team.internal.ccvs.ui.wizards.CommitWizard;
31 import org.eclipse.team.internal.ui.Utils;
32 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
33 import org.eclipse.ui.PlatformUI;
34
35 public abstract class AbstractCommitAction extends CVSModelProviderAction {
36
37     public AbstractCommitAction(ISynchronizePageConfiguration configuration) {
38         super(configuration);
39     }
40     
41     /* (non-Javadoc)
42      * @see org.eclipse.jface.action.Action#run()
43      */

44     public void execute() {
45         final List JavaDoc resources = new ArrayList JavaDoc();
46         try {
47             final IStructuredSelection selection = getActualSelection();
48             PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
49                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
50                     try {
51                         ResourceTraversal[] traversals = getCommitTraversals(selection, monitor);
52                         resources.add(getOutgoingChanges(getSynchronizationContext().getDiffTree(), traversals, monitor));
53                     } catch (CoreException e) {
54                         throw new InvocationTargetException JavaDoc(e);
55                     }
56                 }
57             });
58         } catch (InvocationTargetException JavaDoc e) {
59             Utils.handleError(getConfiguration().getSite().getShell(), e, null, null);
60         } catch (InterruptedException JavaDoc e) {
61             // Ignore
62
} catch (CVSException e) {
63             Utils.handleError(getConfiguration().getSite().getShell(), e, null, null);
64         }
65         if (!resources.isEmpty() && ((IResource[])resources.get(0)).length > 0) {
66             Shell shell= getConfiguration().getSite().getShell();
67             try {
68                 CommitWizard.run(getConfiguration().getSite().getPart(), shell, ((IResource[])resources.get(0)));
69             } catch (CVSException e) {
70                 CVSUIPlugin.log(e);
71             }
72         }
73     }
74     
75     protected IStructuredSelection getActualSelection() throws CVSException {
76         return getStructuredSelection();
77     }
78
79     protected abstract ResourceTraversal[] getCommitTraversals(IStructuredSelection selection, IProgressMonitor monitor) throws CoreException;
80     
81     protected IResource[] getOutgoingChanges(final IResourceDiffTree tree, ResourceTraversal[] traversals, IProgressMonitor monitor) {
82         final List JavaDoc resources = new ArrayList JavaDoc();
83         IDiff[] diffs = tree.getDiffs(traversals);
84         for (int i = 0; i < diffs.length; i++) {
85             IDiff diff = diffs[i];
86             if (hasLocalChange(diff)) {
87                 IResource resource = ResourceDiffTree.getResourceFor(diff);
88                 if (resource != null)
89                     resources.add(resource);
90             }
91         }
92         return (IResource[]) resources.toArray(new IResource[resources.size()]);
93     }
94     
95     private boolean hasLocalChange(IDiff diff) {
96         if (diff instanceof IThreeWayDiff) {
97             IThreeWayDiff twd = (IThreeWayDiff) diff;
98             return twd.getDirection() == IThreeWayDiff.OUTGOING
99                 || twd.getDirection() == IThreeWayDiff.CONFLICTING;
100         }
101         return false;
102     }
103
104 }
105
Popular Tags