2
* org.vcs.bazaar.eclipse (c) Vectrace Jan 31, 2006
5
package org.vcs.bazaar.eclipse.team;
7
import java.util.ArrayList;
8
import java.util.Arrays;
9
import java.util.Iterator;
10
import java.util.List;
12
import org.eclipse.core.resources.IContainer;
13
import org.eclipse.core.resources.IProject;
14
import org.eclipse.core.resources.IResource;
15
import org.eclipse.core.resources.IStorage;
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.jface.action.IAction;
19
import org.eclipse.jface.viewers.ISelection;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.team.core.TeamException;
22
import org.eclipse.team.core.subscribers.Subscriber;
23
import org.eclipse.team.core.synchronize.SyncInfo;
24
import org.eclipse.team.core.variants.IResourceVariant;
25
import org.eclipse.team.core.variants.IResourceVariantComparator;
26
import org.eclipse.team.ui.synchronize.SyncInfoCompareInput;
27
import org.eclipse.ui.IWorkbenchWindow;
28
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
29
import org.eclipse.compare.CompareUI;
34
public class ActionDiff implements IWorkbenchWindowActionDelegate {
37
public class FileHistoryVariant implements IResourceVariant {
38
private final IStorage myIStorage;
40
public FileHistoryVariant(IStorage res)
42
this.myIStorage = res;
43
// System.out.println("FileHistoryVariant(" + myIStorage.getName() +")::FileHistoryVariant()" );
47
public String getName()
49
// System.out.println("FileHistoryVariant(" + myIStorage.getName() +")::getName()" );
50
return myIStorage.getName();
53
public boolean isContainer()
55
// System.out.println("FileHistoryVariant(" + myIStorage.getName() +")::isContainer()" );
59
public IStorage getStorage(IProgressMonitor monitor) throws TeamException
61
// System.out.println("FileHistoryVariant(" + myIStorage.getName() +")::getStorage()" );
65
public String getContentIdentifier()
67
// System.out.println("FileHistoryVariant(" + myIStorage.getName() +")::getContentIdentifier()" );
68
return myIStorage.getFullPath().toString();
71
public byte[] asBytes()
73
// System.out.println("FileHistoryVariant(" + myIStorage.getName() +")::asBytes() what is this for?" );
79
public class MyRepositorySubscriber extends Subscriber
82
public class LocalHistoryVariantComparator implements IResourceVariantComparator {
83
public boolean compare(IResource local, IResourceVariant remote) {
87
public boolean compare(IResourceVariant base, IResourceVariant remote) {
91
public boolean isThreeWay() {
97
LocalHistoryVariantComparator comparatorObj;
99
public MyRepositorySubscriber()
101
comparatorObj = new LocalHistoryVariantComparator();
104
public String getName()
106
// TODO Auto-generated method stub
107
return "MyRepositorySubscriber";
110
public boolean isSupervised(IResource resource) throws TeamException
112
// TODO Auto-generated method stub
116
public IResource[] members(IResource resource) throws TeamException {
118
if(resource.getType() == IResource.FILE)
119
return new IResource[0];
120
IContainer container = (IContainer)resource;
121
List existingChildren = new ArrayList(Arrays.asList(container.members()));
122
existingChildren.addAll(Arrays.asList(container.findDeletedMembersWithHistory(IResource.DEPTH_INFINITE, null)));
123
return (IResource[]) existingChildren.toArray(new IResource[existingChildren.size()]);
124
} catch (CoreException e) {
125
throw TeamException.asTeamException(e);
129
public IResource[] roots()
131
// TODO Auto-generated method stub
136
* @see org.eclipse.team.core.subscribers.Subscriber#getSyncInfo(org.eclipse.core.resources.IResource)
138
public SyncInfo getSyncInfo(IResource resource) throws TeamException
140
// TODO Auto-generated method stub
141
return getSyncInfo(resource,null,null);
144
public SyncInfo getSyncInfo(IResource resource, IStorage r1, IStorage r2) throws TeamException {
147
IResourceVariant variant = null;
148
if(resource.getType() == IResource.FILE) {
149
IFile file = (IFile)resource;
150
IFileState[] states = file.getHistory(null);
151
if(states.length > 0) {
153
variant = new LocalHistoryVariant(states[0]);
157
FileHistoryVariant fileHist1=null;
158
FileHistoryVariant fileHist2=null;
161
fileHist1 = new FileHistoryVariant(r1);
165
fileHist2 = new FileHistoryVariant(r2);
168
SyncInfo info = new SyncInfo(resource, fileHist1,fileHist2, comparatorObj);
171
} catch (CoreException e) {
172
throw TeamException.asTeamException(e);
176
public IResourceVariantComparator getResourceComparator()
178
// TODO Auto-generated method stub
179
return comparatorObj;
182
public void refresh(IResource[] resources, int depth, IProgressMonitor monitor) throws TeamException
184
// TODO Auto-generated method stub
190
private IWorkbenchWindow window;
191
// private IWorkbenchPart targetPart;
192
private IStructuredSelection selection;
194
public ActionDiff() {
199
* We can use this method to dispose of any system
200
* resources we previously allocated.
201
* @see IWorkbenchWindowActionDelegate#dispose
203
public void dispose() {
209
* We will cache window object in order to
210
* be able to provide parent shell for the message dialog.
211
* @see IWorkbenchWindowActionDelegate#init
213
public void init(IWorkbenchWindow window) {
214
// System.out.println("ActionDiff:init(window)");
215
this.window = window;
219
* The action has been activated. The argument of the
220
* method represents the 'real' action sitting
221
* in the workbench UI.
222
* @see IWorkbenchWindowActionDelegate#run
226
public void run(IAction action)
231
proj=BazaarUtilities.getProject(selection);
232
Repository=BazaarUtilities.getRepositoryPath(proj);
235
Repository="."; //never leave this empty add a . to point to current path
240
// the last argument will be replaced with a path
241
itr=selection.iterator();
245
if (obj instanceof IResource)
248
//Setup and run command identify, this us used to get the base changeset to diff against
249
//tip cant be used since work can be done in older reviison ( hg up <old rev> )
250
String FullPath = ( ((IResource) obj).getLocation() ).toString();
251
// TODO: Ver como lo hace bzr
252
String launchCmd[] = { BazaarUtilities.getBzrExecutable(),"revno", FullPath};
253
String revno = BazaarUtilities.ExecuteCommand(launchCmd,false).trim();
254
// It consists of the revision id (hash), optionally a '+' sign
255
// if the working tree has been modified, followed by a list of tags.
256
// => we need to strip it ...
257
System.out.println(revno);
258
// if(changeset.indexOf(" ") != -1) //is there a space?
260
// changeset = changeset.substring(0,changeset.indexOf(" ")); //take the begining until the first space
262
// if(changeset.indexOf("+") != -1) //is there a +?
264
// changeset = changeset.substring(0,changeset.indexOf("+")); //take the begining until the first +
267
//Setup and run command diff
269
MyRepositorySubscriber subscriber = new MyRepositorySubscriber();
272
SyncInfo syncInfo = subscriber.getSyncInfo((IResource)obj,(IStorage)obj,new IStorageBazaarRevision(proj,(IResource)obj,revno));
273
SyncInfoCompareInput comparedialog = new SyncInfoCompareInput("diffelidiff",syncInfo);
274
CompareUI.openCompareEditor( comparedialog );
276
catch (TeamException e)
286
* Selection in the workbench has been changed. We
287
* can change the state of the 'real' action here
288
* if we want, but this can only happen after
289
* the delegate has been created.
290
* @see IWorkbenchWindowActionDelegate#selectionChanged
292
public void selectionChanged(IAction action, ISelection in_selection)
294
if( in_selection != null && in_selection instanceof IStructuredSelection )
296
selection = ( IStructuredSelection )in_selection;