KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > history > CompareFileRevisionEditorInput


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 package org.eclipse.team.internal.ui.history;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.compare.*;
16 import org.eclipse.compare.structuremergeviewer.Differencer;
17 import org.eclipse.compare.structuremergeviewer.ICompareInput;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.internal.core.history.LocalFileRevision;
22 import org.eclipse.team.internal.ui.*;
23 import org.eclipse.team.internal.ui.mapping.AbstractCompareInput;
24 import org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier;
25 import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;
26 import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
27 import org.eclipse.ui.IWorkbenchPage;
28
29 public class CompareFileRevisionEditorInput extends SaveableCompareEditorInput {
30     
31     private ITypedElement left;
32     private ITypedElement right;
33     
34     CompareInputChangeNotifier notifier = new CompareInputChangeNotifier() {
35         protected IResource[] getResources(ICompareInput input) {
36             IResource resource = getResource(input);
37             if (resource == null)
38                 return new IResource[0];
39             return new IResource[] { resource };
40         }
41     };
42     
43     public class MyDiffNode extends AbstractCompareInput {
44         public MyDiffNode(ITypedElement left, ITypedElement right) {
45             super(Differencer.CHANGE, null, left, right);
46         }
47         public void fireChange() {
48             super.fireChange();
49         }
50         protected CompareInputChangeNotifier getChangeNotifier() {
51             return notifier;
52         }
53         public boolean needsUpdate() {
54             // The remote never changes
55
return false;
56         }
57         public void update() {
58             fireChange();
59         }
60     }
61     
62     /**
63      * Creates a new CompareFileRevisionEditorInput.
64      * @param left
65      * @param right
66      * @param page
67      */

68     public CompareFileRevisionEditorInput(ITypedElement left, ITypedElement right, IWorkbenchPage page) {
69         super(new CompareConfiguration(), page);
70         this.left = left;
71         this.right = right;
72         setTitle(NLS.bind(TeamUIMessages.SyncInfoCompareInput_title, new String JavaDoc[] { left.getName() }));
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.team.ui.synchronize.LocalResourceCompareEditorInput#internalPrepareInput(org.eclipse.core.runtime.IProgressMonitor)
77      */

78     protected ICompareInput prepareCompareInput(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
79         ICompareInput input = createCompareInput();
80         getCompareConfiguration().setLeftEditable(isLeftEditable(input));
81         getCompareConfiguration().setRightEditable(false);
82         ensureContentsCached(getLeftRevision(), getRightRevision(), monitor);
83         initLabels(input);
84         return input;
85     }
86
87     protected FileRevisionTypedElement getRightRevision() {
88         if (right instanceof FileRevisionTypedElement) {
89             return (FileRevisionTypedElement) right;
90         }
91         return null;
92     }
93
94     protected FileRevisionTypedElement getLeftRevision() {
95         if (left instanceof FileRevisionTypedElement) {
96             return (FileRevisionTypedElement) left;
97         }
98         return null;
99     }
100
101     private static void ensureContentsCached(FileRevisionTypedElement left, FileRevisionTypedElement right,
102             IProgressMonitor monitor) {
103         if (left != null) {
104             try {
105                 left.cacheContents(monitor);
106             } catch (CoreException e) {
107                 TeamUIPlugin.log(e);
108             }
109         }
110         if (right != null) {
111             try {
112                 right.cacheContents(monitor);
113             } catch (CoreException e) {
114                 TeamUIPlugin.log(e);
115             }
116         }
117     }
118     
119     private boolean isLeftEditable(ICompareInput input) {
120         Object JavaDoc left = input.getLeft();
121         if (left instanceof IEditableContent) {
122             return ((IEditableContent) left).isEditable();
123         }
124         return false;
125     }
126
127     private IResource getResource(ICompareInput input) {
128         if (getLocalElement() instanceof IResourceProvider) {
129             return ((IResourceProvider) getLocalElement()).getResource();
130         }
131         return null;
132     }
133
134     private ICompareInput createCompareInput() {
135         MyDiffNode input = new MyDiffNode(left,right);
136         return input;
137     }
138
139     private void initLabels(ICompareInput input) {
140         CompareConfiguration cc = getCompareConfiguration();
141         if (getLeftRevision() != null) {
142             String JavaDoc leftLabel = getFileRevisionLabel(getLeftRevision());
143             cc.setLeftLabel(leftLabel);
144         } else if (getResource(input) != null) {
145             String JavaDoc label = NLS.bind(TeamUIMessages.CompareFileRevisionEditorInput_workspace, new Object JavaDoc[]{ input.getLeft().getName() });
146             cc.setLeftLabel(label);
147         }
148         if (getRightRevision() != null) {
149             String JavaDoc rightLabel = getFileRevisionLabel(getRightRevision());
150             cc.setRightLabel(rightLabel);
151         }
152     }
153
154     private String JavaDoc getFileRevisionLabel(FileRevisionTypedElement element) {
155         Object JavaDoc fileObject = element.getFileRevision();
156         if (fileObject instanceof LocalFileRevision){
157             IFileState state = ((LocalFileRevision) fileObject).getState();
158             if (state != null) {
159                 return NLS.bind(TeamUIMessages.CompareFileRevisionEditorInput_localRevision, new Object JavaDoc[]{element.getName(), element.getTimestamp()});
160             }
161         } else {
162             return NLS.bind(TeamUIMessages.CompareFileRevisionEditorInput_repository, new Object JavaDoc[]{ element.getName(), element.getContentIdentifier(), element.getAuthor()});
163         }
164         return ""; //$NON-NLS-1$
165
}
166
167     /* (non-Javadoc)
168      * @see org.eclipse.compare.CompareEditorInput#getToolTipText()
169      */

170     public String JavaDoc getToolTipText() {
171         Object JavaDoc[] titleObject = new Object JavaDoc[3];
172         titleObject[0] = getLongName(left);
173         titleObject[1] = getContentIdentifier(getLeftRevision());
174         titleObject[2] = getContentIdentifier(getRightRevision());
175         return NLS.bind(TeamUIMessages.CompareFileRevisionEditorInput_compareResourceAndVersions, titleObject);
176     }
177     
178     /* (non-Javadoc)
179      * @see org.eclipse.compare.CompareEditorInput#getTitle()
180      */

181     public String JavaDoc getTitle() {
182         Object JavaDoc[] titleObject = new Object JavaDoc[3];
183         titleObject[0] = getShortName(left);
184         titleObject[1] = getContentIdentifier(getLeftRevision());
185         titleObject[2] = getContentIdentifier(getRightRevision());
186         return NLS.bind(TeamUIMessages.CompareFileRevisionEditorInput_compareResourceAndVersions, titleObject);
187     }
188     
189     /* (non-Javadoc)
190      * @see org.eclipse.compare.CompareEditorInput#getAdapter(java.lang.Class)
191      */

192     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
193         if (adapter == IFile.class || adapter == IResource.class) {
194             if (getLocalElement() != null) {
195                 return getLocalElement().getResource();
196             }
197             return null;
198         }
199         return super.getAdapter(adapter);
200     }
201     
202     private String JavaDoc getShortName(ITypedElement element) {
203         if (element instanceof FileRevisionTypedElement){
204             FileRevisionTypedElement fileRevisionElement = (FileRevisionTypedElement) element;
205             return fileRevisionElement.getName();
206         }
207         else if (element instanceof LocalResourceTypedElement){
208             LocalResourceTypedElement typedContent = (LocalResourceTypedElement) element;
209             return typedContent.getResource().getName();
210         }
211         return element.getName();
212     }
213     
214     private String JavaDoc getLongName(ITypedElement element) {
215         if (element instanceof FileRevisionTypedElement){
216             FileRevisionTypedElement fileRevisionElement = (FileRevisionTypedElement) element;
217             return fileRevisionElement.getPath();
218         }
219         else if (element instanceof LocalResourceTypedElement){
220             LocalResourceTypedElement typedContent = (LocalResourceTypedElement) element;
221             return typedContent.getResource().getFullPath().toString();
222         }
223         return element.getName();
224     }
225     
226     private String JavaDoc getContentIdentifier(ITypedElement element){
227         if (element instanceof FileRevisionTypedElement){
228             FileRevisionTypedElement fileRevisionElement = (FileRevisionTypedElement) element;
229             Object JavaDoc fileObject = fileRevisionElement.getFileRevision();
230             if (fileObject instanceof LocalFileRevision){
231                 try {
232                     IStorage storage = ((LocalFileRevision) fileObject).getStorage(new NullProgressMonitor());
233                     if (Utils.getAdapter(storage, IFileState.class) != null){
234                         //local revision
235
return TeamUIMessages.CompareFileRevisionEditorInput_0;
236                     } else if (Utils.getAdapter(storage, IFile.class) != null) {
237                         //current revision
238
return TeamUIMessages.CompareFileRevisionEditorInput_1;
239                     }
240                 } catch (CoreException e) {
241                 }
242             } else {
243                 return fileRevisionElement.getContentIdentifier();
244             }
245         }
246         return TeamUIMessages.CompareFileRevisionEditorInput_2;
247     }
248
249     /* (non-Javadoc)
250      * @see org.eclipse.team.ui.synchronize.LocalResourceCompareEditorInput#fireInputChange()
251      */

252     protected void fireInputChange() {
253         ((MyDiffNode)getCompareResult()).fireChange();
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.team.ui.synchronize.SaveableCompareEditorInput#contentsCreated()
258      */

259     protected void contentsCreated() {
260         super.contentsCreated();
261         notifier.initialize();
262     }
263     
264     /* (non-Javadoc)
265      * @see org.eclipse.team.ui.synchronize.SaveableCompareEditorInput#handleDispose()
266      */

267     protected void handleDispose() {
268         super.handleDispose();
269         notifier.dispose();
270         if (getLocalElement() != null) {
271             getLocalElement().discardBuffer();
272         }
273     }
274
275     public LocalResourceTypedElement getLocalElement() {
276         if (left instanceof LocalResourceTypedElement) {
277             return (LocalResourceTypedElement) left;
278         }
279         return null;
280     }
281
282     public ITypedElement getLeft() {
283         return left;
284     }
285 }
286
Popular Tags