KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > CVSCompareRevisionsInput


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13  
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17
18 import org.eclipse.compare.*;
19 import org.eclipse.compare.structuremergeviewer.*;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.*;
23 import org.eclipse.jface.action.*;
24 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.jface.viewers.*;
27 import org.eclipse.osgi.util.NLS;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.*;
30 import org.eclipse.team.core.TeamException;
31 import org.eclipse.team.internal.ccvs.core.*;
32 import org.eclipse.team.internal.ccvs.core.client.Command;
33 import org.eclipse.team.internal.ccvs.core.client.Update;
34 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
35 import org.eclipse.team.internal.ccvs.ui.actions.CVSAction;
36 import org.eclipse.team.internal.ccvs.ui.operations.UpdateOperation;
37 import org.eclipse.team.internal.ui.Utils;
38 import org.eclipse.team.ui.ISaveableWorkbenchPart;
39 import org.eclipse.ui.*;
40 import org.eclipse.ui.actions.WorkspaceModifyOperation;
41
42 public class CVSCompareRevisionsInput extends CompareEditorInput implements ISaveableWorkbenchPart {
43     IFile resource;
44     ILogEntry[] editions;
45     TableViewer viewer;
46     Action getRevisionAction;
47     Action getContentsAction;
48     Shell shell;
49     
50     // Provide the widget for the history table
51
private HistoryTableProvider historyTableProvider;
52     
53     /**
54      * Provide a wrapper for a resource node that doesn't buffer. Changes are saved directly to the
55      * underlying file.
56      */

57     class TypedBufferedContent extends ResourceNode {
58         public TypedBufferedContent(IFile resource) {
59             super(resource);
60         }
61         protected InputStream JavaDoc createStream() throws CoreException {
62             return ((IFile)getResource()).getContents();
63         }
64         public void setContent(byte[] contents) {
65             if (contents == null) contents = new byte[0];
66             final InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(contents);
67             IRunnableWithProgress runnable = new IRunnableWithProgress() {
68                 public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
69                     try {
70                         IFile file = resource;
71                         if (is != null) {
72                             if (!file.exists()) {
73                                 file.create(is, false, monitor);
74                             } else {
75                                 file.setContents(is, false, true, monitor);
76                             }
77                         } else {
78                             file.delete(false, true, monitor);
79                         }
80                     } catch (CoreException e) {
81                         throw new InvocationTargetException JavaDoc(e);
82                     }
83                 }
84             };
85             try {
86                 new ProgressMonitorDialog(shell).run(false, false, runnable);
87             } catch (InvocationTargetException JavaDoc e) {
88                 CVSUIPlugin.openError(null, null, null, e);
89             } catch (InterruptedException JavaDoc e) {
90                 // Ignore
91
}
92             fireContentChanged();
93         }
94         public ITypedElement replace(ITypedElement child, ITypedElement other) {
95             return null;
96         }
97         public void fireChange() {
98             fireContentChanged();
99         }
100     }
101     
102     /**
103      * This class is an edition node which knows the log entry it came from.
104      */

105     class ResourceRevisionNode extends ResourceEditionNode {
106         ILogEntry entry;
107         public ResourceRevisionNode(ILogEntry entry) {
108             super(entry.getRemoteFile());
109             this.entry = entry;
110         }
111         public ILogEntry getLogEntry() {
112             return entry;
113         }
114         public String JavaDoc getName() {
115             String JavaDoc revisionName = entry.getRevision();
116             if (revisionName != null) {
117                 IResource resource = CVSCompareRevisionsInput.this.resource;
118                 try {
119                     ICVSRemoteFile currentEdition = (ICVSRemoteFile) CVSWorkspaceRoot.getRemoteResourceFor(resource);
120                     if (currentEdition != null && currentEdition.getRevision().equals(revisionName)) {
121                         NLS.bind(CVSUIMessages.currentRevision, new String JavaDoc[] { revisionName }); //$NON-NLS-1$
122
} else {
123                         return revisionName;
124                     }
125                 } catch (TeamException e) {
126                     handle(e);
127                 }
128             }
129             return super.getName();
130         }
131     }
132     
133     /**
134      * A compare node that gets its label from the right element
135      */

136     class VersionCompareDiffNode extends DiffNode implements IAdaptable {
137         public VersionCompareDiffNode(ITypedElement left, ITypedElement right) {
138             super(left, right);
139         }
140         public String JavaDoc getName() {
141             return getRight().getName();
142         }
143         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
144             if (adapter == ILogEntry.class) {
145                 return ((ResourceRevisionNode)getRight()).getLogEntry();
146             }
147             return null;
148         }
149         public void fireContentChanges() {
150             fireChange();
151         }
152     }
153     /**
154      * A content provider which knows how to get the children of the diff container
155      */

156     class VersionCompareContentProvider implements IStructuredContentProvider {
157         public void dispose() {
158         }
159         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
160         }
161         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
162             if (inputElement instanceof DiffContainer) {
163                 return ((DiffContainer)inputElement).getChildren();
164             }
165             return null;
166         }
167     }
168     
169     public CVSCompareRevisionsInput(IFile resource, ILogEntry[] editions) {
170         super(new CompareConfiguration());
171         this.resource = resource;
172         this.editions = editions;
173         updateCurrentEdition();
174         initializeActions();
175     }
176
177     /* (non-Javadoc)
178      * @see org.eclipse.compare.CompareEditorInput#createContents(org.eclipse.swt.widgets.Composite)
179      */

180     public Control createContents(Composite parent) {
181         Control c = super.createContents(parent);
182         c.setLayoutData(new GridData(GridData.FILL_BOTH));
183         return c;
184     }
185     
186     public Viewer createDiffViewer(Composite parent) {
187         this.shell = parent.getShell();
188         viewer = getHistoryTableProvider().createTable(parent);
189         Table table = viewer.getTable();
190         table.setData(CompareUI.COMPARE_VIEWER_TITLE, getTitle()); //$NON-NLS-1$
191

192         viewer.setContentProvider(new VersionCompareContentProvider());
193
194         MenuManager mm = new MenuManager();
195         mm.setRemoveAllWhenShown(true);
196         mm.addMenuListener(
197             new IMenuListener() {
198                 public void menuAboutToShow(IMenuManager mm) {
199                     mm.add(getContentsAction);
200                     mm.add(getRevisionAction);
201                 }
202             }
203         );
204         table.setMenu(mm.createContextMenu(table));
205         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
206             public void selectionChanged(SelectionChangedEvent event) {
207                 ISelection selection = event.getSelection();
208                 if (!(selection instanceof IStructuredSelection)) {
209                     getRevisionAction.setEnabled(false);
210                     getContentsAction.setEnabled(false);
211                     return;
212                 }
213                 IStructuredSelection ss = (IStructuredSelection)selection;
214                 getRevisionAction.setEnabled(ss.size() == 1);
215                 getContentsAction.setEnabled(ss.size() == 1);
216             }
217         });
218         
219         // Add F1 help.
220
PlatformUI.getWorkbench().getHelpSystem().setHelp(table, IHelpContextIds.COMPARE_REVISIONS_VIEW);
221         return viewer;
222     }
223     
224     private void initLabels() {
225         CompareConfiguration cc = getCompareConfiguration();
226         cc.setLeftEditable(true);
227         cc.setRightEditable(false);
228         String JavaDoc resourceName = resource.getName();
229         String JavaDoc leftLabel = NLS.bind(CVSUIMessages.CVSCompareRevisionsInput_workspace, (new Object JavaDoc[] {resourceName})); //$NON-NLS-1$
230
cc.setLeftLabel(leftLabel);
231         String JavaDoc rightLabel = NLS.bind(CVSUIMessages.CVSCompareRevisionsInput_repository, (new Object JavaDoc[] {resourceName})); //$NON-NLS-1$
232
cc.setRightLabel(rightLabel);
233     }
234     private void initializeActions() {
235         getRevisionAction = new Action(CVSUIMessages.HistoryView_getRevisionAction) { //$NON-NLS-1$
236
public void run() {
237                 try {
238                     final IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
239                     new ProgressMonitorDialog(shell).run(true, true, new WorkspaceModifyOperation(null) {
240                         protected void execute(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
241                             if (selection.size() != 1) return;
242                             VersionCompareDiffNode node = (VersionCompareDiffNode)selection.getFirstElement();
243                             ResourceEditionNode right = (ResourceEditionNode)node.getRight();
244                             ICVSRemoteResource edition = right.getRemoteResource();
245                             // Do the load. This just consists of setting the local contents. We don't
246
// actually want to change the base.
247
try {
248                                 CVSTag revisionTag = new CVSTag(((ICVSRemoteFile)edition).getRevision(), CVSTag.VERSION);
249                                 if(CVSAction.checkForMixingTags(shell, new IResource[] {resource}, revisionTag)) {
250                                     new UpdateOperation(
251                                             null,
252                                             new IResource[] {resource},
253                                             new Command.LocalOption[] {Update.IGNORE_LOCAL_CHANGES},
254                                             revisionTag)
255                                                 .run(monitor);
256                                     getHistoryTableProvider().setFile((ICVSFile)edition);
257                                 }
258                             } catch (TeamException e) {
259                                 throw new InvocationTargetException JavaDoc(e);
260                             }
261                         }
262                     });
263                 } catch (InterruptedException JavaDoc e) {
264                     // Do nothing
265
return;
266                 } catch (InvocationTargetException JavaDoc e) {
267                     handle(e);
268                 }
269                 // fire change
270
IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
271                 if (selection.size() != 1) return;
272                 VersionCompareDiffNode node = (VersionCompareDiffNode)selection.getFirstElement();
273                 TypedBufferedContent left = (TypedBufferedContent)node.getLeft();
274                 left.fireChange();
275                 // recompute the labels on the viewer
276
Display.getCurrent().syncExec(new Runnable JavaDoc() {
277                     public void run() {
278                         viewer.refresh();
279                     }
280                 });
281             }
282         };
283         getContentsAction = new Action(CVSUIMessages.HistoryView_getContentsAction) { //$NON-NLS-1$
284
public void run() {
285                 try {
286                     replaceLocalWithCurrentlySelectedRevision();
287                 } catch (CoreException e) {
288                     Utils.handle(e);
289                 }
290             }
291         };
292     }
293     protected Object JavaDoc prepareInput(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
294         initLabels();
295         DiffNode diffRoot = new DiffNode(Differencer.NO_CHANGE);
296         ITypedElement left = new TypedBufferedContent(resource);
297         for (int i = 0; i < editions.length; i++) {
298             ITypedElement right = new ResourceRevisionNode(editions[i]);
299             diffRoot.add(new VersionCompareDiffNode(left, right));
300         }
301         return diffRoot;
302     }
303     private void updateCurrentEdition() {
304         try {
305             getHistoryTableProvider().setFile((ICVSFile) CVSWorkspaceRoot.getRemoteResourceFor(resource));
306         } catch (TeamException e) {
307             handle(e);
308         }
309     }
310     private void handle(Exception JavaDoc e) {
311         setMessage(CVSUIPlugin.openError(shell, null, null, e, CVSUIPlugin.LOG_NONTEAM_EXCEPTIONS).getMessage());
312     }
313     /**
314      * Returns the historyTableProvider.
315      * @return HistoryTableProvider
316      */

317     public HistoryTableProvider getHistoryTableProvider() {
318         if (historyTableProvider == null) {
319             historyTableProvider = new HistoryTableProvider();
320         }
321         return historyTableProvider;
322     }
323     
324     
325     /* (non-Javadoc)
326      * @see org.eclipse.compare.CompareEditorInput#saveChanges(org.eclipse.core.runtime.IProgressMonitor)
327      */

328     public void saveChanges(IProgressMonitor pm) throws CoreException {
329         super.saveChanges(pm);
330     }
331     
332     public void replaceLocalWithCurrentlySelectedRevision() throws CoreException {
333         IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
334         if (selection.size() != 1) return;
335         VersionCompareDiffNode node = (VersionCompareDiffNode)selection.getFirstElement();
336         ResourceRevisionNode right = (ResourceRevisionNode)node.getRight();
337         TypedBufferedContent left = (TypedBufferedContent)node.getLeft();
338         left.setContent(Utils.readBytes(right.getContents()));
339     }
340     
341     public Viewer getViewer() {
342         return viewer;
343     }
344     
345     
346     /* (non-Javadoc)
347      * @see org.eclipse.compare.CompareEditorInput#getTitle()
348      */

349     public String JavaDoc getTitle() {
350         return NLS.bind(CVSUIMessages.CVSCompareRevisionsInput_compareResourceAndVersions, (new Object JavaDoc[] {resource.getFullPath().toString()})); //$NON-NLS-1$
351
}
352
353     /* (non-Javadoc)
354      * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
355      */

356     public void doSave(IProgressMonitor monitor) {
357         try {
358             saveChanges(monitor);
359         } catch (CoreException e) {
360             Utils.handle(e);
361         }
362     }
363
364     /* (non-Javadoc)
365      * @see org.eclipse.ui.ISaveablePart#doSaveAs()
366      */

367     public void doSaveAs() {
368         // noop
369
}
370
371     /* (non-Javadoc)
372      * @see org.eclipse.ui.ISaveablePart#isDirty()
373      */

374     public boolean isDirty() {
375         return isSaveNeeded();
376     }
377
378     /* (non-Javadoc)
379      * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
380      */

381     public boolean isSaveAsAllowed() {
382         return true;
383     }
384
385     /* (non-Javadoc)
386      * @see org.eclipse.ui.ISaveablePart#isSaveOnCloseNeeded()
387      */

388     public boolean isSaveOnCloseNeeded() {
389         return true;
390     }
391
392     /* (non-Javadoc)
393      * @see org.eclipse.ui.IWorkbenchPart#addPropertyListener(org.eclipse.ui.IPropertyListener)
394      */

395     public void addPropertyListener(IPropertyListener listener) {
396         // noop
397
}
398
399     /* (non-Javadoc)
400      * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
401      */

402     public void createPartControl(Composite parent) {
403         createContents(parent);
404     }
405
406     /* (non-Javadoc)
407      * @see org.eclipse.ui.IWorkbenchPart#dispose()
408      */

409     public void dispose() {
410     }
411
412     /* (non-Javadoc)
413      * @see org.eclipse.ui.IWorkbenchPart#getSite()
414      */

415     public IWorkbenchPartSite getSite() {
416         return null;
417     }
418
419     /* (non-Javadoc)
420      * @see org.eclipse.ui.IWorkbenchPart#getTitleToolTip()
421      */

422     public String JavaDoc getTitleToolTip() {
423         return null;
424     }
425
426     /* (non-Javadoc)
427      * @see org.eclipse.ui.IWorkbenchPart#removePropertyListener(org.eclipse.ui.IPropertyListener)
428      */

429     public void removePropertyListener(IPropertyListener listener) {
430         // noop
431
}
432 }
433
Popular Tags