KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > subscriber > 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.subscriber;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import com.ibm.icu.text.DateFormat;
15
16 import org.eclipse.compare.structuremergeviewer.IDiffElement;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.team.core.TeamException;
23 import org.eclipse.team.core.synchronize.*;
24 import org.eclipse.team.core.synchronize.FastSyncInfoFilter.*;
25 import org.eclipse.team.core.variants.IResourceVariant;
26 import org.eclipse.team.internal.ccvs.core.*;
27 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
28 import org.eclipse.team.internal.ccvs.ui.operations.RemoteCompareOperation.CompareTreeBuilder;
29 import org.eclipse.team.internal.core.subscribers.ChangeSet;
30 import org.eclipse.team.internal.core.subscribers.CheckedInChangeSet;
31 import org.eclipse.team.internal.ui.synchronize.ChangeSetDiffNode;
32 import org.eclipse.team.ui.synchronize.*;
33
34 class OpenChangeSetAction extends SynchronizeModelAction {
35
36     protected OpenChangeSetAction(ISynchronizePageConfiguration configuration) {
37         super(CVSUIMessages.OpenCommitSetAction_20, configuration);
38     }
39     
40     /* (non-Javadoc)
41      * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSyncInfoFilter()
42      */

43     protected FastSyncInfoFilter getSyncInfoFilter() {
44         return new AndSyncInfoFilter(new FastSyncInfoFilter[] {
45                 new FastSyncInfoFilter() {
46                     public boolean select(SyncInfo info) {
47                         return info.getLocal().getType() == IResource.FILE;
48                     }
49                 },
50                 new OrSyncInfoFilter(new FastSyncInfoFilter[] {
51                     new SyncInfoDirectionFilter(new int[] { SyncInfo.INCOMING, SyncInfo.CONFLICTING }),
52                     new FastSyncInfoFilter() {
53                         public boolean select(SyncInfo info) {
54                             return !info.getComparator().isThreeWay();
55                         }
56                     }
57                 })
58         });
59     }
60     
61     private ChangeSet getChangeSet(IStructuredSelection selection) {
62         // First, check to see if a change set is selected directly
63
if (selection.size() == 1) {
64             Object JavaDoc o = selection.getFirstElement();
65             if (o instanceof IAdaptable) {
66                 ChangeSet set = (ChangeSet)((IAdaptable)o).getAdapter(ChangeSet.class);
67                 if (set != null)
68                     return set;
69             }
70         }
71         // Failing that, check to see if all the selected elements and their childen are in the same change set
72
return getChangeSet(selection.toArray());
73     }
74     
75     private ChangeSet getChangeSet(Object JavaDoc[] elements) {
76         ChangeSet foundSet = null;
77         for (int i = 0; i < elements.length; i++) {
78             Object JavaDoc object = elements[i];
79             ChangeSet set = getChangeSet((ISynchronizeModelElement)object);
80             if (set == null) return null;
81             if (foundSet == null) {
82                 foundSet = set;
83             } else if (foundSet != set) {
84                 return null;
85             }
86         }
87         return foundSet;
88     }
89     
90     private ChangeSet getChangeSet(ISynchronizeModelElement element) {
91         if (element == null) return null;
92         if (element instanceof IAdaptable) {
93             ChangeSet set = (ChangeSet)((IAdaptable)element).getAdapter(ChangeSet.class);
94             if (set != null)
95                 return set;
96         }
97         return getChangeSet((ISynchronizeModelElement)element.getParent());
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
102      */

103     protected boolean updateSelection(IStructuredSelection selection) {
104         boolean enabled = super.updateSelection(selection);
105         if (enabled) {
106             // The selection only contains appropriate files so
107
// only enable if the selection is contained within a single change set
108
ChangeSet set = getChangeSet(selection);
109             return set != null;
110         }
111         return false;
112     }
113
114     /* (non-Javadoc)
115      * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSubscriberOperation(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration, org.eclipse.compare.structuremergeviewer.IDiffElement[])
116      */

117     protected SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
118         return new SynchronizeModelOperation(configuration, elements) {
119             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
120                 SyncInfoSet set = getSyncInfoSet();
121                 SyncInfo[] infos = set.getSyncInfos();
122                 if (infos.length > 0) {
123                     ICVSRepositoryLocation location = getLocation(infos[0]);
124                     if (location == null) {
125                         handle(new CVSException(CVSUIMessages.OpenCommitSetAction_21));
126                         return;
127                     }
128                     CompareTreeBuilder builder = new CompareTreeBuilder(location, null, null);
129                     if (buildTrees(builder, infos)) {
130                         try {
131                             builder.cacheContents(monitor);
132                             builder.openCompareEditor(getConfiguration().getSite().getPart().getSite().getPage(), getCompareTitle(), getCompareToolTip());
133                         } catch (CVSException e) {
134                             handle(e);
135                             return;
136                         }
137                     }
138                 }
139             }
140
141             private String JavaDoc getCompareToolTip() {
142                 IDiffElement[] elements = getSelectedDiffElements();
143                 for (int i = 0; i < elements.length; i++) {
144                     IDiffElement element = elements[i];
145                     while (element != null) {
146                         if (element instanceof ChangeSetDiffNode) {
147                             return ((ChangeSetDiffNode)element).getName();
148                         }
149                         element = element.getParent();
150                     }
151                 }
152                 return null;
153             }
154             
155             private String JavaDoc getCompareTitle() {
156                 IDiffElement[] elements = getSelectedDiffElements();
157                 ChangeSet set = getChangeSet(elements);
158                 if (set instanceof CheckedInChangeSet) {
159                     CheckedInChangeSet cics = (CheckedInChangeSet)set;
160                     String JavaDoc date = DateFormat.getDateTimeInstance().format(cics.getDate());
161                     return NLS.bind(CVSUIMessages.OpenChangeSetAction_0, new String JavaDoc[] {cics.getAuthor(), date});
162                 }
163                 return CVSUIMessages.OpenChangeSetAction_1;
164             }
165
166             private ICVSRepositoryLocation getLocation(SyncInfo info) {
167                 IResourceVariant remote = info.getRemote();
168                 if (remote == null) {
169                     remote = info.getBase();
170                 }
171                 if (remote != null) {
172                     return ((ICVSRemoteResource)remote).getRepository();
173                 }
174                 return null;
175             }
176
177             /*
178              * Build the trees that will be compared
179              */

180             private boolean buildTrees(CompareTreeBuilder builder, SyncInfo[] infos) {
181                 for (int i = 0; i < infos.length; i++) {
182                     SyncInfo info = infos[i];
183                     IResourceVariant remote = info.getRemote();
184                     if (remote == null) {
185                         IResourceVariant predecessor = info.getBase();
186                         if (predecessor instanceof ICVSRemoteFile) {
187                             builder.addToTrees((ICVSRemoteFile)predecessor, null);
188                         }
189                     } else if (remote instanceof ICVSRemoteFile) {
190                         try {
191                             ICVSRemoteFile predecessor = getImmediatePredecessor(remote);
192                             builder.addToTrees(predecessor, (ICVSRemoteFile)remote);
193                         } catch (TeamException e) {
194                             handle(e);
195                             return false;
196                         }
197                     }
198                 }
199                 return true;
200             }
201         };
202     }
203
204     private ICVSRemoteFile getImmediatePredecessor(IResourceVariant remote) throws TeamException {
205         CVSChangeSetCollector changeSetCollector = getChangeSetCollector();
206         if (changeSetCollector != null) {
207             return changeSetCollector.getImmediatePredecessor((ICVSRemoteFile)remote);
208         }
209         return null;
210     }
211
212     private CVSChangeSetCollector getChangeSetCollector() {
213         return (CVSChangeSetCollector)getConfiguration().getProperty(CVSChangeSetCollector.CVS_CHECKED_IN_COLLECTOR);
214     }
215
216 }
217
Popular Tags