KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > actions > CompareRevisionAction


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
12 package org.eclipse.team.internal.ui.actions;
13
14 import org.eclipse.compare.*;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.history.IFileRevision;
22 import org.eclipse.team.core.history.provider.FileRevision;
23 import org.eclipse.team.internal.core.history.LocalFileRevision;
24 import org.eclipse.team.internal.ui.TeamUIMessages;
25 import org.eclipse.team.internal.ui.TeamUIPlugin;
26 import org.eclipse.team.internal.ui.history.*;
27 import org.eclipse.team.ui.history.HistoryPage;
28 import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
29 import org.eclipse.ui.*;
30 import org.eclipse.ui.actions.BaseSelectionListenerAction;
31
32 public class CompareRevisionAction extends BaseSelectionListenerAction {
33     
34     HistoryPage page;
35     IStructuredSelection selection;
36     IFileRevision currentFileRevision;
37     
38     public CompareRevisionAction(String JavaDoc text) {
39         super(text);
40     }
41     
42     public CompareRevisionAction() {
43         this(TeamUIMessages.LocalHistoryPage_CompareAction);
44     }
45
46     public void run() {
47         IStructuredSelection structSel = selection;
48         Object JavaDoc[] objArray = structSel.toArray();
49
50         IFileRevision file1 = null;
51         IFileRevision file2 = null;
52         
53         switch (structSel.size()){
54             case 1:
55                 file1 = getCurrentFileRevision();
56                 Object JavaDoc tempRevision = objArray[0];
57                 if (tempRevision instanceof IFileRevision)
58                     file2 = (IFileRevision) tempRevision;
59                 else
60                     return;
61             break;
62             
63             case 2:
64                 Object JavaDoc tempRevision2 = objArray[0];
65                 Object JavaDoc tempRevision3 = objArray[1];
66                 
67                 if (tempRevision2 instanceof IFileRevision &&
68                     tempRevision3 instanceof IFileRevision){
69                     file1 = (IFileRevision) objArray[0];
70                     file2 = (IFileRevision) objArray[1];
71                 } else
72                     return;
73             break;
74         }
75
76         if (file1 == null || file2 == null ||
77            !file1.exists() || !file2.exists()){
78             MessageDialog.openError(page.getSite().getShell(), TeamUIMessages.OpenRevisionAction_DeletedRevTitle, TeamUIMessages.CompareRevisionAction_DeleteCompareMessage);
79             return;
80         }
81         
82         IResource resource = getResource(file2);
83         if (resource != null) {
84             IFileRevision temp = file1;
85             file1 = file2;
86             file2 = temp;
87         }
88         ITypedElement left;
89         resource = getResource(file1);
90         if (resource != null) {
91             left = getElementFor(resource);
92         } else {
93             left = new FileRevisionTypedElement(file1, getLocalEncoding());
94         }
95         ITypedElement right = new FileRevisionTypedElement(file2, getLocalEncoding());
96         
97         openInCompare(left, right);
98     }
99
100     private String JavaDoc getLocalEncoding() {
101         IResource resource = getResource(getCurrentFileRevision());
102         if (resource instanceof IFile) {
103             IFile file = (IFile) resource;
104             try {
105                 return file.getCharset();
106             } catch (CoreException e) {
107                 TeamUIPlugin.log(e);
108             }
109         }
110         return null;
111     }
112
113     protected ITypedElement getElementFor(IResource resource) {
114         return SaveableCompareEditorInput.createFileElement((IFile)resource);
115     }
116
117     private void openInCompare(ITypedElement left, ITypedElement right) {
118         CompareEditorInput input = createCompareEditorInput(left, right, page.getSite().getPage());
119         IWorkbenchPage workBenchPage = page.getSite().getPage();
120         IEditorPart editor = findReusableCompareEditor(workBenchPage);
121         if (editor != null) {
122             IEditorInput otherInput = editor.getEditorInput();
123             if (otherInput.equals(input)) {
124                 // simply provide focus to editor
125
workBenchPage.activate(editor);
126             } else {
127                 // if editor is currently not open on that input either re-use
128
// existing
129
CompareUI.reuseCompareEditor(input, (IReusableEditor) editor);
130                 workBenchPage.activate(editor);
131             }
132         } else {
133             CompareUI.openCompareEditor(input);
134         }
135     }
136
137     protected CompareFileRevisionEditorInput createCompareEditorInput(
138             ITypedElement left, ITypedElement right, IWorkbenchPage page) {
139         return new CompareFileRevisionEditorInput(left,
140                 right, page);
141     }
142
143     private IResource getResource(IFileRevision revision) {
144         if (revision instanceof LocalFileRevision) {
145             LocalFileRevision local = (LocalFileRevision) revision;
146             return local.getFile();
147         }
148         return null;
149     }
150
151     private IFileRevision getCurrentFileRevision() {
152         return currentFileRevision;
153     }
154     
155     public void setCurrentFileRevision(IFileRevision fileRevision){
156         this.currentFileRevision = fileRevision;
157     }
158
159     /**
160      * Returns an editor that can be re-used. An open compare editor that
161      * has un-saved changes cannot be re-used.
162      * @param page
163      * @return an EditorPart or <code>null</code> if none can be found
164      */

165     public static IEditorPart findReusableCompareEditor(IWorkbenchPage page) {
166         IEditorReference[] editorRefs = page.getEditorReferences();
167         for (int i = 0; i < editorRefs.length; i++) {
168             IEditorPart part = editorRefs[i].getEditor(false);
169             if(part != null
170                     && (part.getEditorInput() instanceof CompareFileRevisionEditorInput)
171                     && part instanceof IReusableEditor) {
172                 if(! part.isDirty()) {
173                     return part;
174                 }
175             }
176         }
177         return null;
178     }
179     
180     protected boolean updateSelection(IStructuredSelection selection) {
181         this.selection = selection;
182         if (selection.size() == 1){
183             Object JavaDoc el = selection.getFirstElement();
184             if (el instanceof LocalFileRevision)
185                 this.setText(TeamUIMessages.CompareRevisionAction_Local);
186             else if (el instanceof FileRevision){
187                 FileRevision tempFileRevision = (FileRevision) el;
188                 this.setText(NLS.bind(TeamUIMessages.CompareRevisionAction_Revision, new String JavaDoc[]{tempFileRevision.getContentIdentifier()}));
189             }
190             else
191                 this.setText(TeamUIMessages.CompareRevisionAction_CompareWithCurrent);
192             return shouldShow();
193         }
194         else if (selection.size() == 2){
195             this.setText(TeamUIMessages.CompareRevisionAction_CompareWithOther);
196             return shouldShow();
197         }
198
199         return false;
200     }
201     public void setPage(HistoryPage page) {
202         this.page = page;
203     }
204
205     private boolean shouldShow() {
206         IStructuredSelection structSel = selection;
207         Object JavaDoc[] objArray = structSel.toArray();
208         
209         if (objArray.length == 0)
210             return false;
211         
212         for (int i = 0; i < objArray.length; i++) {
213             
214             //Don't bother showing if this a category
215
if (objArray[i] instanceof AbstractHistoryCategory)
216                 return false;
217             
218             IFileRevision revision = (IFileRevision) objArray[i];
219             //check to see if any of the selected revisions are deleted revisions
220
if (revision != null && !revision.exists())
221                 return false;
222         }
223         
224         return true;
225     }
226
227 }
228
Popular Tags