1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import java.io.*; 14 15 import org.eclipse.core.resources.IFile; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.core.runtime.jobs.Job; 19 import org.eclipse.jface.text.Document; 20 import org.eclipse.jface.text.IDocument; 21 import org.eclipse.team.core.TeamException; 22 import org.eclipse.team.core.subscribers.ISubscriberChangeEvent; 23 import org.eclipse.team.core.subscribers.ISubscriberChangeListener; 24 import org.eclipse.team.core.synchronize.SyncInfo; 25 import org.eclipse.team.internal.ccvs.core.*; 26 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 27 import org.eclipse.ui.IEditorInput; 28 import org.eclipse.ui.editors.text.IStorageDocumentProvider; 29 import org.eclipse.ui.ide.ResourceUtil; 30 import org.eclipse.ui.texteditor.*; 31 import org.eclipse.ui.texteditor.quickdiff.IQuickDiffReferenceProvider; 32 33 56 public class RemoteRevisionQuickDiffProvider implements IQuickDiffReferenceProvider { 57 private ITextEditor fEditor = null; 60 61 private IDocument fReference = null; 64 65 private boolean fReferenceInitialized = false; 67 68 private IDocumentProvider fDocumentProvider = null; 70 71 private String fId; 73 74 private SyncInfo fLastSyncState; 76 77 private Job fUpdateJob; 79 80 private boolean DEBUG = false; 81 82 85 private ISubscriberChangeListener teamChangeListener = new ISubscriberChangeListener() { 86 public void subscriberResourceChanged(ISubscriberChangeEvent[] deltas) { 87 if(fReferenceInitialized) { 88 for (int i = 0; i < deltas.length; i++) { 89 ISubscriberChangeEvent delta = deltas[i]; 90 IResource resource = delta.getResource(); 91 if(resource.getType() == IResource.FILE && 92 fLastSyncState != null && resource.equals(fLastSyncState.getLocal())) { 93 if(delta.getFlags() == ISubscriberChangeEvent.SYNC_CHANGED) { 94 fetchContentsInJob(); 95 } 96 } 97 } 98 } 99 } 100 }; 101 102 105 private IElementStateListener documentListener = new IElementStateListener() { 106 public void elementDirtyStateChanged(Object element, boolean isDirty) { 107 } 108 109 public void elementContentAboutToBeReplaced(Object element) { 110 } 111 112 public void elementContentReplaced(Object element) { 113 if(fEditor != null && fEditor.getEditorInput() == element) { 114 fetchContentsInJob(); 115 } 116 } 117 118 public void elementDeleted(Object element) { 119 } 120 121 public void elementMoved(Object originalElement, Object movedElement) { 122 } 123 }; 124 125 128 public IDocument getReference(IProgressMonitor monitor) throws CoreException { 129 if(! fReferenceInitialized) return null; 130 if (fReference == null) { 131 readDocument(monitor); 132 } 133 return fReference; 134 } 135 136 139 public void setActiveEditor(ITextEditor targetEditor) { 140 IEditorInput editorInput = targetEditor.getEditorInput(); 141 if (editorInput == null || ResourceUtil.getFile(editorInput) == null) return; 142 fEditor = targetEditor; 143 fDocumentProvider= fEditor.getDocumentProvider(); 144 145 if(fDocumentProvider != null) { 146 CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().addListener(teamChangeListener); 147 fDocumentProvider.addElementStateListener(documentListener); 148 } 149 fReferenceInitialized= true; 150 } 151 152 155 public boolean isEnabled() { 156 if (! fReferenceInitialized) 157 return false; 158 try { 159 return getManagedCVSFile() != null; 160 } catch (CVSException e) { 161 return false; 162 } 163 } 164 165 168 public void dispose() { 169 fReferenceInitialized = false; 170 if(fUpdateJob != null && fUpdateJob.getState() != Job.NONE) { 172 fUpdateJob.cancel(); 173 } 174 175 if(fDocumentProvider != null) { 177 fDocumentProvider.removeElementStateListener(documentListener); 178 } 179 CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().removeListener(teamChangeListener); 180 } 181 182 185 public void setId(String id) { 186 fId= id; 187 } 188 189 192 public String getId() { 193 return fId; 194 } 195 196 201 private boolean computeChange(IProgressMonitor monitor) throws TeamException { 202 boolean needToUpdateReferenceDocument = false; 203 if(fReferenceInitialized) { 204 SyncInfo info = getSyncState(getFileFromEditor()); 205 if(info == null && fLastSyncState != null) { 206 return true; 207 } else if(info == null) { 208 return false; 209 } 210 211 if(fLastSyncState == null) { 212 needToUpdateReferenceDocument = true; 213 } else if(! fLastSyncState.equals(info)) { 214 needToUpdateReferenceDocument = true; 215 } 216 if(DEBUG) debug(fLastSyncState, info); 217 fLastSyncState = info; 218 } 219 return needToUpdateReferenceDocument; 220 } 221 222 private void debug(SyncInfo lastSyncState, SyncInfo info) { 223 String last = "[none]"; if(lastSyncState != null) { 225 last = lastSyncState.toString(); 226 } 227 System.out.println("+ CVSQuickDiff: was " + last + " is " + info.toString()); } 229 230 private SyncInfo getSyncState(IResource resource) throws TeamException { 231 if (resource == null) return null; 232 return CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber().getSyncInfo(resource); 233 } 234 235 241 private void readDocument(IProgressMonitor monitor) throws CoreException { 242 if(! fReferenceInitialized) return; 243 if(fReference == null) 244 fReference = new Document(); 245 if(computeChange(monitor)) { 246 ICVSRemoteFile remoteFile = (ICVSRemoteFile)fLastSyncState.getRemote(); 247 if (fLastSyncState.getRemote() != null && fDocumentProvider instanceof IStorageDocumentProvider) { 248 IStorageDocumentProvider provider= (IStorageDocumentProvider) fDocumentProvider; 249 String encoding= provider.getEncoding(fEditor.getEditorInput()); 250 if (encoding == null) { 251 encoding= provider.getDefaultEncoding(); 252 } 253 if(monitor.isCanceled()) return; 254 InputStream stream= remoteFile.getContents(monitor); 255 if (stream == null || monitor.isCanceled() || ! fReferenceInitialized) { 256 return; 257 } 258 setDocumentContent(fReference, stream, encoding); 259 } else { 260 if(monitor.isCanceled()) return; 262 fReference.set(""); } 264 if(DEBUG) System.out.println("+ CVSQuickDiff: updating document " + (fReference!=null ? "remote found" : "remote empty")); } 266 } 267 268 276 private static void setDocumentContent(IDocument document, InputStream contentStream, String encoding) throws CoreException { 277 Reader in= null; 278 try { 279 final int DEFAULT_FILE_SIZE= 15 * 1024; 280 281 in= new BufferedReader(new InputStreamReader(contentStream, encoding), DEFAULT_FILE_SIZE); 282 CharArrayWriter caw= new CharArrayWriter(DEFAULT_FILE_SIZE); 283 char[] readBuffer= new char[2048]; 284 int n= in.read(readBuffer); 285 while (n > 0) { 286 caw.write(readBuffer, 0, n); 287 n= in.read(readBuffer); 288 } 289 document.set(caw.toString()); 290 } catch (IOException x) { 292 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, CVSUIMessages.RemoteRevisionQuickDiffProvider_readingFile, x); 293 throw new CVSException(status); 294 } finally { 295 if (in != null) { 296 try { 297 in.close(); 298 } catch (IOException x) { 299 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, CVSUIMessages.RemoteRevisionQuickDiffProvider_closingFile, x); 300 throw new CVSException(status); 301 } 302 } 303 } 304 } 305 306 311 private ICVSFile getManagedCVSFile() throws CVSException { 312 if(fEditor != null) { 313 IFile file = getFileFromEditor(); 314 if(file != null && CVSWorkspaceRoot.isSharedWithCVS(file)) { 315 return CVSWorkspaceRoot.getCVSFileFor(file); 316 } 317 } 318 return null; 319 } 320 321 private IFile getFileFromEditor() { 322 if(fEditor != null) { 323 IEditorInput input= fEditor.getEditorInput(); 324 if (input != null) { 325 IFile file = ResourceUtil.getFile(input); 326 return file; 327 } 328 } 329 return null; 330 } 331 332 336 private void fetchContentsInJob() { 337 if(! fReferenceInitialized) return; 338 if(fUpdateJob != null && fUpdateJob.getState() != Job.NONE) { 339 fUpdateJob.cancel(); 340 } 341 fUpdateJob = new Job(CVSUIMessages.RemoteRevisionQuickDiffProvider_fetchingFile) { 342 protected IStatus run(IProgressMonitor monitor) { 343 try { 344 readDocument(monitor); 345 } catch (CoreException e) { 346 } 350 return Status.OK_STATUS; 351 } 352 }; 353 fUpdateJob.schedule(); 354 } 355 } 356 | Popular Tags |