1 11 package org.eclipse.ui.part; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IResourceChangeEvent; 15 import org.eclipse.core.resources.IResourceChangeListener; 16 import org.eclipse.core.resources.IResourceDelta; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.ui.IInPlaceEditor; 20 import org.eclipse.ui.IInPlaceEditorInput; 21 22 30 public class FileInPlaceEditorInput extends FileEditorInput implements 31 IInPlaceEditorInput { 32 IInPlaceEditor embeddedEditor; 33 34 38 private IResourceChangeListener resourceListener = new IResourceChangeListener() { 39 public void resourceChanged(IResourceChangeEvent event) { 40 IResourceDelta mainDelta = event.getDelta(); 41 if (mainDelta != null && embeddedEditor != null) { 42 IResourceDelta affectedElement = mainDelta.findMember(getFile() 43 .getFullPath()); 44 if (affectedElement != null) { 45 try { 46 processDelta(affectedElement); 47 } catch (CoreException exception) { 48 if (embeddedEditor != null) { 50 embeddedEditor.getSite().getPage().closeEditor( 51 embeddedEditor, true); 52 } 53 } 54 } 55 } 56 } 57 58 private boolean processDelta(final IResourceDelta delta) 59 throws CoreException { 60 Runnable changeRunnable = null; 61 62 switch (delta.getKind()) { 63 case IResourceDelta.REMOVED: 64 if ((IResourceDelta.MOVED_TO & delta.getFlags()) != 0) { 65 changeRunnable = new Runnable () { 66 public void run() { 67 IPath path = delta.getMovedToPath(); 68 IFile newFile = delta.getResource().getWorkspace() 69 .getRoot().getFile(path); 70 if (newFile != null && embeddedEditor != null) { 71 embeddedEditor 72 .sourceChanged(new FileInPlaceEditorInput( 73 newFile)); 74 } 75 } 76 }; 77 } else { 78 changeRunnable = new Runnable () { 79 public void run() { 80 if (embeddedEditor != null) { 81 embeddedEditor.sourceDeleted(); 82 embeddedEditor.getSite().getPage().closeEditor( 83 embeddedEditor, true); 84 } 85 } 86 }; 87 88 } 89 90 break; 91 } 92 93 if (changeRunnable != null && embeddedEditor != null) { 94 embeddedEditor.getSite().getShell().getDisplay().asyncExec( 95 changeRunnable); 96 } 97 98 return true; } 100 }; 101 102 107 public FileInPlaceEditorInput(IFile file) { 108 super(file); 109 } 110 111 114 public void setInPlaceEditor(IInPlaceEditor editor) { 115 if (embeddedEditor != editor) { 116 if (embeddedEditor != null) { 117 getFile().getWorkspace().removeResourceChangeListener( 118 resourceListener); 119 } 120 121 embeddedEditor = editor; 122 123 if (embeddedEditor != null) { 124 getFile().getWorkspace().addResourceChangeListener( 125 resourceListener); 126 } 127 } 128 } 129 } 130 | Popular Tags |