1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import org.eclipse.compare.*; 14 import org.eclipse.compare.internal.Utilities; 15 import org.eclipse.compare.structuremergeviewer.ICompareInput; 16 import org.eclipse.core.resources.IFile; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.jface.dialogs.IDialogConstants; 19 import org.eclipse.jface.dialogs.MessageDialog; 20 import org.eclipse.jface.resource.ImageDescriptor; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.util.IPropertyChangeListener; 23 import org.eclipse.jface.util.PropertyChangeEvent; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.team.internal.ui.*; 26 import org.eclipse.team.ui.mapping.SaveableComparison; 27 import org.eclipse.ui.IEditorInput; 28 import org.eclipse.ui.Saveable; 29 import org.eclipse.ui.part.FileEditorInput; 30 import org.eclipse.ui.texteditor.IDocumentProvider; 31 32 39 public abstract class LocalResourceSaveableComparison extends SaveableComparison implements IPropertyChangeListener { 40 41 private final ICompareInput input; 42 private final CompareEditorInput editorInput; 43 private boolean isSaving; 44 private IContentChangeListener contentChangeListener; 45 private ITypedElement fileElement; 46 private IDocument document; 47 48 53 public LocalResourceSaveableComparison(ICompareInput input, CompareEditorInput editorInput) { 54 this(input, editorInput, input.getLeft()); 55 } 56 57 63 public LocalResourceSaveableComparison(ICompareInput input, CompareEditorInput editorInput, ITypedElement fileElement) { 64 this.input = input; 65 this.editorInput = editorInput; 66 this.fileElement = fileElement; 67 initializeContentChangeListeners(); 68 } 69 70 protected void initializeHashing() { 71 Object document= getAdapter(IDocument.class); 72 if (document != null) { 73 this.document = (IDocument)document; 74 } 75 } 76 77 private void initializeContentChangeListeners() { 78 ITypedElement te = getFileElement(); 81 if (te instanceof IContentChangeNotifier) { 82 if (contentChangeListener == null) { 83 contentChangeListener = new IContentChangeListener() { 84 public void contentChanged(IContentChangeNotifier source) { 85 try { 86 if(! isSaving) { 87 performSave(new NullProgressMonitor()); 88 } 89 } catch (CoreException e) { 90 TeamUIPlugin.log(e); 91 } 92 } 93 }; 94 } 95 ((IContentChangeNotifier) te).addContentChangeListener(contentChangeListener); 96 } 97 } 98 99 102 public void dispose() { 103 if (contentChangeListener != null) { 104 ITypedElement te = getFileElement(); 105 if (te instanceof IContentChangeNotifier) { 106 ((IContentChangeNotifier) te).removeContentChangeListener(contentChangeListener); 107 } 108 } 109 ITypedElement left = getFileElement(); 111 if (left instanceof LocalResourceTypedElement) 112 ((LocalResourceTypedElement) left).discardBuffer(); 113 } 114 115 private ITypedElement getFileElement() { 116 return fileElement; 117 } 118 119 122 protected void performSave(IProgressMonitor monitor) throws CoreException { 123 if (checkForUpdateConflicts()) { 124 return; 125 } 126 try { 127 isSaving = true; 128 monitor.beginTask(null, 100); 129 flushViewers(Policy.subMonitorFor(monitor, 40)); 132 ITypedElement left = getFileElement(); 135 if (left instanceof LocalResourceTypedElement) { 136 LocalResourceTypedElement te = (LocalResourceTypedElement) left; 137 te.commit(Policy.subMonitorFor(monitor, 60)); 138 } 139 } finally { 140 fireInputChange(); 142 setDirty(false); 143 isSaving = false; 144 monitor.done(); 145 } 146 } 147 148 153 protected void flushViewers(IProgressMonitor monitor) throws CoreException { 154 editorInput.saveChanges(monitor); 155 } 156 157 161 protected abstract void fireInputChange(); 162 163 167 private boolean checkForUpdateConflicts() { 168 if(hasSaveConflict()) { 169 if (Utilities.RUNNING_TESTS) 170 return !Utilities.TESTING_FLUSH_ON_COMPARE_INPUT_CHANGE; 171 final MessageDialog dialog = 172 new MessageDialog(TeamUIPlugin.getStandardDisplay().getActiveShell(), 173 TeamUIMessages.SyncInfoCompareInput_0, 174 null, 175 TeamUIMessages.SyncInfoCompareInput_1, 176 MessageDialog.QUESTION, 177 new String [] { 178 TeamUIMessages.SyncInfoCompareInput_2, 179 IDialogConstants.CANCEL_LABEL}, 180 0); 181 182 int retval = dialog.open(); 183 switch(retval) { 184 case 0: 186 return false; 187 case 1: 189 return true; 190 } 191 } 192 return false; 193 } 194 195 private boolean hasSaveConflict() { 196 ITypedElement left = getFileElement(); 197 if (left instanceof LocalResourceTypedElement) { 198 LocalResourceTypedElement te = (LocalResourceTypedElement) left; 199 return !te.isSynchronized(); 200 } 201 return false; 202 } 203 204 207 public boolean isDirty() { 208 return editorInput.isSaveNeeded(); 211 } 212 213 216 protected void setDirty(boolean dirty) { 217 editorInput.setDirty(dirty); 220 } 221 222 225 protected void performRevert(IProgressMonitor monitor) { 226 ITypedElement left = getFileElement(); 228 if (left instanceof LocalResourceTypedElement) 229 ((LocalResourceTypedElement) left).discardBuffer(); 230 } 231 232 235 public String getName() { 236 return input.getName(); 237 } 238 239 242 public String getToolTipText() { 243 return editorInput.getToolTipText(); 244 } 245 246 249 public ImageDescriptor getImageDescriptor() { 250 Image image = input.getImage(); 251 if (image != null) 252 return ImageDescriptor.createFromImage(image); 253 return TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_SYNC_VIEW); 254 } 255 256 259 public void propertyChange(PropertyChangeEvent e) { 260 String propertyName= e.getProperty(); 261 if (CompareEditorInput.DIRTY_STATE.equals(propertyName)) { 262 boolean changed= false; 263 Object newValue= e.getNewValue(); 264 if (newValue instanceof Boolean ) 265 changed= ((Boolean )newValue).booleanValue(); 266 setDirty(changed); 267 } 268 } 269 270 273 public int hashCode() { 274 if (document != null) { 275 return document.hashCode(); 276 } 277 return input.hashCode(); 278 } 279 280 283 public boolean equals(Object obj) { 284 if (this == obj) 285 return true; 286 287 if (!(obj instanceof Saveable)) 288 return false; 289 290 if (document != null) { 291 Object otherDocument= ((Saveable)obj).getAdapter(IDocument.class); 292 293 if (document == null && otherDocument == null) 294 return false; 295 296 return document != null && document.equals(otherDocument); 297 } 298 299 if (obj instanceof LocalResourceSaveableComparison) { 300 LocalResourceSaveableComparison rscm = (LocalResourceSaveableComparison) obj; 301 return rscm.input.equals(input); 302 } 303 return false; 304 } 305 306 public Object getAdapter(Class adapter) { 307 if (adapter == IDocument.class) { 308 if (document != null) 309 return document; 310 if (fileElement instanceof LocalResourceTypedElement) { 311 LocalResourceTypedElement lrte = (LocalResourceTypedElement) fileElement; 312 if (lrte.isConnected()) { 313 ISharedDocumentAdapter sda = (ISharedDocumentAdapter)Utils.getAdapter(lrte, ISharedDocumentAdapter.class); 314 if (sda != null) { 315 IEditorInput input = sda.getDocumentKey(lrte); 316 if (input != null) { 317 IDocumentProvider provider = SharedDocumentAdapter.getDocumentProvider(input); 318 if (provider != null) 319 return provider.getDocument(input); 320 } 321 } 322 } 323 } 324 } 325 if (adapter == IEditorInput.class) { 326 if (fileElement instanceof LocalResourceTypedElement) { 327 LocalResourceTypedElement lrte = (LocalResourceTypedElement) fileElement; 328 return new FileEditorInput((IFile)lrte.getResource()); 329 } 330 } 331 return super.getAdapter(adapter); 332 } 333 334 338 public ICompareInput getInput() { 339 return input; 340 } 341 342 public boolean isConnectedToSharedDocument() { 343 if (fileElement instanceof LocalResourceTypedElement) { 344 LocalResourceTypedElement lrte = (LocalResourceTypedElement) fileElement; 345 return lrte.isConnected(); 346 } 347 return false; 348 } 349 } 350 | Popular Tags |