1 11 package org.eclipse.team.internal.ui.history; 12 13 import java.io.InputStream ; 14 import java.util.Date ; 15 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.team.core.history.IFileRevision; 21 import org.eclipse.team.internal.ui.TeamUIMessages; 22 import org.eclipse.team.internal.ui.Utils; 23 import org.eclipse.ui.IPersistableElement; 24 import org.eclipse.ui.IStorageEditorInput; 25 import org.eclipse.ui.model.IWorkbenchAdapter; 26 27 import com.ibm.icu.text.DateFormat; 28 29 public class FileRevisionEditorInput extends PlatformObject implements IWorkbenchAdapter, IStorageEditorInput { 30 31 private final Object fileRevision; 32 private final IStorage storage; 33 34 41 public static FileRevisionEditorInput createEditorInputFor(IFileRevision revision, IProgressMonitor monitor) throws CoreException { 42 IStorage storage = revision.getStorage(monitor); 43 return new FileRevisionEditorInput(revision, storage); 44 } 45 46 private static IStorage wrapStorage(final IStorage storage, final String charset) { 47 if (charset == null) 48 return storage; 49 return new IEncodedStorage() { 50 public Object getAdapter(Class adapter) { 51 return storage.getAdapter(adapter); 52 } 53 public boolean isReadOnly() { 54 return storage.isReadOnly(); 55 } 56 public String getName() { 57 return storage.getName(); 58 } 59 public IPath getFullPath() { 60 return storage.getFullPath(); 61 } 62 public InputStream getContents() throws CoreException { 63 return storage.getContents(); 64 } 65 public String getCharset() throws CoreException { 66 return charset; 67 } 68 }; 69 } 70 71 76 public FileRevisionEditorInput(Object revision, IStorage storage) { 77 Assert.isNotNull(revision); 78 Assert.isNotNull(storage); 79 this.fileRevision = revision; 80 this.storage = storage; 81 } 82 83 public FileRevisionEditorInput(IFileState state) { 84 this(state, state); 85 } 86 87 public FileRevisionEditorInput(Object revision, IStorage storage, String charset) { 88 this(revision, wrapStorage(storage, charset)); 89 } 90 91 public IStorage getStorage() throws CoreException { 92 return storage; 93 } 94 95 public boolean exists() { 96 return true; 97 } 98 99 public ImageDescriptor getImageDescriptor() { 100 return null; 101 } 102 103 public String getName() { 104 IFileRevision rev = (IFileRevision)getAdapter(IFileRevision.class); 105 if (rev != null) 106 return NLS.bind(TeamUIMessages.nameAndRevision, new String [] { rev.getName(), rev.getContentIdentifier()}); 107 IFileState state = (IFileState)getAdapter(IFileState.class); 108 if (state != null) 109 return state.getName() + " " + DateFormat.getInstance().format(new Date (state.getModificationTime())) ; return storage.getName(); 111 112 } 113 114 public IPersistableElement getPersistable() { 115 return null; 117 } 118 119 public String getToolTipText() { 120 return storage.getFullPath().toString(); 121 } 122 123 public Object getAdapter(Class adapter) { 124 if (adapter == IWorkbenchAdapter.class) 125 return this; 126 if (adapter == IStorage.class) 127 return storage; 128 Object object = super.getAdapter(adapter); 129 if (object != null) 130 return object; 131 return Utils.getAdapter(fileRevision, adapter); 132 } 133 134 public Object [] getChildren(Object o) { 135 return new Object [0]; 136 } 137 138 public ImageDescriptor getImageDescriptor(Object object) { 139 return null; 140 } 141 142 public String getLabel(Object o) { 143 IFileRevision rev = (IFileRevision)getAdapter(IFileRevision.class); 144 if (rev != null) 145 return rev.getName(); 146 return storage.getName(); 147 } 148 149 public Object getParent(Object o) { 150 return null; 151 } 152 153 public boolean equals(Object obj) { 154 if (obj instanceof FileRevisionEditorInput) { 155 FileRevisionEditorInput other = (FileRevisionEditorInput) obj; 156 return (other.fileRevision.equals(this.fileRevision)); 157 } 158 return false; 159 } 160 161 public int hashCode() { 162 return fileRevision.hashCode(); 163 } 164 165 public IFileRevision getFileRevision() { 166 if (fileRevision instanceof IFileRevision) { 167 return (IFileRevision) fileRevision; 168 } 169 return null; 170 } 171 172 } 173 | Popular Tags |