1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import org.eclipse.compare.SharedDocumentAdapter; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.ui.IEditorInput; 18 import org.eclipse.ui.texteditor.IDocumentProvider; 19 import org.eclipse.ui.texteditor.IElementStateListener; 20 21 34 public class EditableSharedDocumentAdapter extends 35 SharedDocumentAdapter implements IElementStateListener { 36 37 private int connectionCount; 38 private final ISharedDocumentAdapterListener listener; 39 private IEditorInput bufferedKey; 40 41 45 public interface ISharedDocumentAdapterListener { 46 47 52 void handleDocumentConnected(); 53 54 59 void handleDocumentDisconnected(); 60 61 64 void handleDocumentFlushed(); 65 66 69 void handleDocumentDeleted(); 70 71 74 void handleDocumentSaved(); 75 } 76 77 81 public EditableSharedDocumentAdapter(ISharedDocumentAdapterListener listener) { 82 super(); 83 this.listener = listener; 84 } 85 86 89 public void connect(IDocumentProvider provider, IEditorInput documentKey) 90 throws CoreException { 91 super.connect(provider, documentKey); 92 connectionCount++; 93 if (connectionCount == 1) { 94 provider.addElementStateListener(this); 95 listener.handleDocumentConnected(); 96 } 97 } 98 99 102 public void disconnect(IDocumentProvider provider, 103 IEditorInput documentKey) { 104 try { 105 super.disconnect(provider, documentKey); 106 } finally { 107 if (connectionCount > 0) 108 connectionCount--; 109 if (connectionCount == 0) { 110 provider.removeElementStateListener(this); 111 listener.handleDocumentDisconnected(); 112 } 113 } 114 } 115 116 120 public boolean isConnected() { 121 return connectionCount > 0; 122 } 123 124 133 public boolean saveDocument(IEditorInput input, boolean overwrite, IProgressMonitor monitor) throws CoreException { 134 if (isConnected()) { 135 IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(input); 136 try { 137 saveDocument(provider, input, provider.getDocument(input), overwrite, monitor); 138 } finally { 139 releaseBuffer(); 141 } 142 return true; 143 } 144 return false; 145 } 146 147 151 public void releaseBuffer() { 152 if (bufferedKey != null) { 153 IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(bufferedKey); 154 provider.disconnect(bufferedKey); 155 bufferedKey = null; 156 } 157 } 158 159 162 public void flushDocument(IDocumentProvider provider, 163 IEditorInput documentKey, IDocument document, 164 boolean overwrite) 165 throws CoreException { 166 if (!hasBufferedContents()) { 167 bufferedKey = documentKey; 170 provider.connect(bufferedKey); 171 } 172 this.listener.handleDocumentFlushed(); 173 } 174 175 178 public void elementContentAboutToBeReplaced(Object element) { 179 } 181 182 185 public void elementContentReplaced(Object element) { 186 } 188 189 192 public void elementDeleted(Object element) { 193 listener.handleDocumentDeleted(); 194 } 195 196 199 public void elementDirtyStateChanged(Object element, boolean isDirty) { 200 if (!isDirty) { 201 this.listener.handleDocumentSaved(); 202 } 203 } 204 205 208 public void elementMoved(Object originalElement, Object movedElement) { 209 } 211 212 219 public boolean hasBufferedContents() { 220 return bufferedKey != null; 221 } 222 } | Popular Tags |