1 11 package org.eclipse.team.internal.ui.history; 12 13 import java.lang.reflect.InvocationTargetException ; 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 return false; 56 } 57 public void update() { 58 fireChange(); 59 } 60 } 61 62 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 [] { left.getName() })); 73 } 74 75 78 protected ICompareInput prepareCompareInput(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 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 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 leftLabel = getFileRevisionLabel(getLeftRevision()); 143 cc.setLeftLabel(leftLabel); 144 } else if (getResource(input) != null) { 145 String label = NLS.bind(TeamUIMessages.CompareFileRevisionEditorInput_workspace, new Object []{ input.getLeft().getName() }); 146 cc.setLeftLabel(label); 147 } 148 if (getRightRevision() != null) { 149 String rightLabel = getFileRevisionLabel(getRightRevision()); 150 cc.setRightLabel(rightLabel); 151 } 152 } 153 154 private String getFileRevisionLabel(FileRevisionTypedElement element) { 155 Object 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 []{element.getName(), element.getTimestamp()}); 160 } 161 } else { 162 return NLS.bind(TeamUIMessages.CompareFileRevisionEditorInput_repository, new Object []{ element.getName(), element.getContentIdentifier(), element.getAuthor()}); 163 } 164 return ""; } 166 167 170 public String getToolTipText() { 171 Object [] titleObject = new Object [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 181 public String getTitle() { 182 Object [] titleObject = new Object [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 192 public Object getAdapter(Class 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 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 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 getContentIdentifier(ITypedElement element){ 227 if (element instanceof FileRevisionTypedElement){ 228 FileRevisionTypedElement fileRevisionElement = (FileRevisionTypedElement) element; 229 Object 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 return TeamUIMessages.CompareFileRevisionEditorInput_0; 236 } else if (Utils.getAdapter(storage, IFile.class) != null) { 237 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 252 protected void fireInputChange() { 253 ((MyDiffNode)getCompareResult()).fireChange(); 254 } 255 256 259 protected void contentsCreated() { 260 super.contentsCreated(); 261 notifier.initialize(); 262 } 263 264 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 |