KickJava   Java API By Example, From Geeks To Geeks.

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


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.mappings;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.mapping.ResourceTraversal;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.jface.operation.IRunnableWithProgress;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.RepositoryProvider;
22 import org.eclipse.team.core.TeamException;
23 import org.eclipse.team.core.diff.IDiff;
24 import org.eclipse.team.core.diff.IThreeWayDiff;
25 import org.eclipse.team.core.history.IFileRevision;
26 import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
27 import org.eclipse.team.core.variants.IResourceVariant;
28 import org.eclipse.team.internal.ccvs.core.*;
29 import org.eclipse.team.internal.ccvs.core.mapping.CVSCheckedInChangeSet;
30 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
31 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
32 import org.eclipse.team.internal.ccvs.ui.operations.RemoteCompareOperation.CompareTreeBuilder;
33 import org.eclipse.team.internal.ccvs.ui.subscriber.CVSChangeSetCollector;
34 import org.eclipse.team.internal.core.mapping.SyncInfoToDiffConverter;
35 import org.eclipse.team.internal.core.subscribers.ChangeSet;
36 import org.eclipse.team.internal.core.subscribers.DiffChangeSet;
37 import org.eclipse.team.internal.ui.Utils;
38 import org.eclipse.team.internal.ui.mapping.ResourceModelParticipantAction;
39 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
40 import org.eclipse.ui.PlatformUI;
41
42 import com.ibm.icu.text.DateFormat;
43
44 class OpenChangeSetAction extends ResourceModelParticipantAction {
45
46     protected OpenChangeSetAction(ISynchronizePageConfiguration configuration) {
47         super(CVSUIMessages.OpenCommitSetAction_20, configuration);
48         ISelection selection = configuration.getSite().getSelectionProvider().getSelection();
49         if (selection != null)
50             selectionChanged(selection);
51     }
52     
53     private ChangeSet getChangeSet(IStructuredSelection selection) {
54         // First, check to see if a change set is selected directly
55
if (selection.size() == 1) {
56             Object JavaDoc o = selection.getFirstElement();
57             if (o instanceof IAdaptable) {
58                 ChangeSet set = (ChangeSet)((IAdaptable)o).getAdapter(ChangeSet.class);
59                 if (set != null)
60                     return set;
61             }
62         }
63         // Failing that, check to see if all the selected elements and their children are in the same change set
64
if (selection instanceof TreeSelection) {
65             TreeSelection ts = (TreeSelection) selection;
66             TreePath[] paths = ts.getPaths();
67             if (paths.length > 0) {
68                 ChangeSet set = getChangeSet(paths[0]);
69                 if (set != null) {
70                     for (int i = 1; i < paths.length; i++) {
71                         TreePath path = paths[i];
72                         ChangeSet otherSet = getChangeSet(path);
73                         if (set != otherSet)
74                             return null;
75                     }
76                 }
77                 return set;
78             }
79         }
80         return null;
81     }
82
83     private ChangeSet getChangeSet(TreePath treePath) {
84         Object JavaDoc test = treePath.getFirstSegment();
85         if (test instanceof ChangeSet) {
86             ChangeSet cs = (ChangeSet) test;
87             return cs;
88         }
89         return null;
90     }
91     
92     protected boolean isEnabledForSelection(IStructuredSelection selection) {
93         // The selection only contains appropriate files so
94
// only enable if the selection is contained within a single change set
95
ChangeSet set = getChangeSet(selection);
96         return set != null;
97     }
98
99     public void openEditor(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
100         try {
101             IDiff[] diffs = getSelectedDiffs(monitor);
102             if (diffs.length > 0) {
103                 ICVSRepositoryLocation location = getLocation(diffs[0]);
104                 if (location == null) {
105                     throw new CVSException(CVSUIMessages.OpenCommitSetAction_21);
106                 }
107                 CompareTreeBuilder builder = new CompareTreeBuilder(location, null, null);
108                 if (buildTrees(builder, diffs)) {
109                     builder.cacheContents(monitor);
110                     builder.openCompareEditor(getConfiguration().getSite().getPart().getSite().getPage(), getCompareTitle(), getCompareToolTip());
111                 }
112             }
113         } catch (CoreException e) {
114             throw new InvocationTargetException JavaDoc(e);
115         }
116     }
117     
118     private IDiff[] getSelectedDiffs(IProgressMonitor monitor) throws CoreException {
119         ResourceTraversal[] traversals = getResourceTraversals(getStructuredSelection(), monitor);
120         DiffChangeSet set = (DiffChangeSet)getChangeSet(getStructuredSelection());
121         return set.getDiffTree().getDiffs(traversals);
122     }
123
124     /*
125      * Build the trees that will be compared
126      */

127     private boolean buildTrees(CompareTreeBuilder builder, IDiff[] diffs) {
128         for (int i = 0; i < diffs.length; i++) {
129             IDiff diff = diffs[i];
130             if (isFileChange(diff)) {
131                 IFileRevision remoteRevision = Utils.getRemote(diff);
132                 IResourceVariant remote = SyncInfoToDiffConverter.asResourceVariant(remoteRevision);
133                 if (remote == null) {
134                     IFileRevision predecessorRevision = Utils.getBase(diff);
135                     IResourceVariant predecessor = SyncInfoToDiffConverter.asResourceVariant(predecessorRevision);
136                     if (predecessor instanceof ICVSRemoteFile) {
137                         builder.addToTrees((ICVSRemoteFile)predecessor, null);
138                     }
139                 } else if (remote instanceof ICVSRemoteFile) {
140                     try {
141                         ICVSRemoteFile predecessor = getImmediatePredecessor(remote);
142                         builder.addToTrees(predecessor, (ICVSRemoteFile)remote);
143                     } catch (TeamException e) {
144                         Utils.handle(e);
145                         return false;
146                     }
147                 }
148             }
149         }
150         return true;
151     }
152     
153     private boolean isFileChange(IDiff diff) {
154         IResource resource = ResourceDiffTree.getResourceFor(diff);
155         if (resource.getType() == IResource.FILE) {
156             if (diff instanceof IThreeWayDiff) {
157                 IThreeWayDiff twd = (IThreeWayDiff) diff;
158                 return twd.getDirection() == IThreeWayDiff.INCOMING || twd.getDirection() == IThreeWayDiff.CONFLICTING;
159             }
160             return true;
161         }
162         return false;
163     }
164
165     private ICVSRepositoryLocation getLocation(IDiff diff) {
166         IResource resource = ResourceDiffTree.getResourceFor(diff);
167         RepositoryProvider provider = RepositoryProvider.getProvider(resource.getProject());
168         if (provider instanceof CVSTeamProvider) {
169             CVSTeamProvider ctp = (CVSTeamProvider) provider;
170             try {
171                 return ctp.getCVSWorkspaceRoot().getRemoteLocation();
172             } catch (CVSException e) {
173                 CVSUIPlugin.log(e);
174             }
175         }
176         return null;
177     }
178     
179     private String JavaDoc getCompareTitle() {
180         ChangeSet set = getChangeSet(getStructuredSelection());
181         if (set instanceof CVSCheckedInChangeSet) {
182             CVSCheckedInChangeSet cics = (CVSCheckedInChangeSet)set;
183             String JavaDoc date = DateFormat.getDateTimeInstance().format(cics.getDate());
184             return NLS.bind(CVSUIMessages.OpenChangeSetAction_0, new String JavaDoc[] {cics.getAuthor(), date});
185         }
186         return CVSUIMessages.OpenChangeSetAction_1;
187     }
188     
189     private String JavaDoc getCompareToolTip() {
190         ChangeSet set = getChangeSet(getStructuredSelection());
191         if (set != null)
192             return set.getName();
193         return null;
194     }
195     
196     private ICVSRemoteFile getImmediatePredecessor(IResourceVariant remote) throws TeamException {
197         CheckedInChangeSetCollector changeSetCollector = getChangeSetCollector();
198         if (changeSetCollector != null) {
199             return changeSetCollector.getImmediatePredecessor((ICVSRemoteFile)remote);
200         }
201         return null;
202     }
203
204     private CheckedInChangeSetCollector getChangeSetCollector() {
205         return (CheckedInChangeSetCollector)getConfiguration().getProperty(CVSChangeSetCollector.CVS_CHECKED_IN_COLLECTOR);
206     }
207     
208     public void run() {
209         try {
210             PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
211                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc,
212                         InterruptedException JavaDoc {
213                     openEditor(monitor);
214                 }
215             });
216         } catch (InvocationTargetException JavaDoc e) {
217             Utils.handle(e);
218         } catch (InterruptedException JavaDoc e) {
219             // Ignore
220
}
221     }
222 }
223
Popular Tags