1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.compare.*; 17 import org.eclipse.compare.structuremergeviewer.ICompareInput; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.IWorkspaceRunnable; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.jface.viewers.BaseLabelProvider; 22 import org.eclipse.jface.viewers.LabelProviderChangedEvent; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.widgets.Display; 26 import org.eclipse.team.core.ICache; 27 import org.eclipse.team.core.ICacheListener; 28 import org.eclipse.team.core.diff.*; 29 import org.eclipse.team.core.history.IFileRevision; 30 import org.eclipse.team.core.mapping.ISynchronizationContext; 31 import org.eclipse.team.core.mapping.provider.ResourceDiffTree; 32 import org.eclipse.team.internal.ui.*; 33 import org.eclipse.team.internal.ui.history.FileRevisionTypedElement; 34 35 38 public class ResourceCompareInputChangeNotifier extends CompareInputChangeNotifier implements IDiffChangeListener { 39 40 static final String RESOURCE_CHANGE_NOTIFIER_PROPERTY = "org.eclipse.team.ui.ResourceChangeNotifier"; 42 private ISynchronizationContext context; 43 44 private class CompareInputLabelProvider extends BaseLabelProvider implements ICompareInputLabelProvider { 45 46 public Image getAncestorImage(Object input) { 47 return null; 49 } 50 51 public String getAncestorLabel(Object input) { 52 if (input instanceof ResourceDiffCompareInput) { 53 ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) input; 54 ITypedElement element = rdci.getAncestor(); 55 if (element != null) { 56 final IFileRevision revision = ((FileRevisionTypedElement)element).getFileRevision(); 57 if (revision != null) { 58 if (TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.SHOW_AUTHOR_IN_COMPARE_EDITOR)) { 59 String author = ((FileRevisionTypedElement)element).getAuthor(); 60 if (author != null) { 61 return NLS.bind(TeamUIMessages.Utils_20, new String [] { revision.getContentIdentifier(), author }); 62 } else if (revision.isPropertyMissing()) { 63 fetchAuthors(input); 64 } 65 } 66 return NLS.bind(TeamUIMessages.SyncInfoCompareInput_baseLabelExists, new String [] { revision.getContentIdentifier() }); 67 } else { 68 return TeamUIMessages.SyncInfoCompareInput_baseLabel; 69 } 70 } 71 } 72 return null; 73 } 74 75 public Image getLeftImage(Object input) { 76 return null; 78 } 79 80 public String getLeftLabel(Object input) { 81 if (input instanceof ResourceDiffCompareInput) { 82 ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) input; 83 String localContentId = rdci.getLocalContentId(); 84 if (localContentId != null) { 85 return NLS.bind(TeamUIMessages.SyncInfoCompareInput_localLabelExists, new String [] { localContentId }); 86 } else { 87 return TeamUIMessages.SyncInfoCompareInput_localLabel; 88 } 89 } 90 return null; 91 } 92 93 public Image getRightImage(Object input) { 94 return null; 96 } 97 98 public String getRightLabel(Object input) { 99 if (input instanceof ResourceDiffCompareInput) { 100 ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) input; 101 ITypedElement element = rdci.getRight(); 102 if (element != null) { 103 final IFileRevision revision = ((FileRevisionTypedElement)element).getFileRevision(); 104 if (revision != null) { 105 if (TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.SHOW_AUTHOR_IN_COMPARE_EDITOR)) { 106 String author = ((FileRevisionTypedElement)element).getAuthor(); 107 if (author != null) { 108 return NLS.bind(TeamUIMessages.Utils_21, new String [] { revision.getContentIdentifier(), author }); 109 } else if (revision.isPropertyMissing()) { 110 fetchAuthors(input); 111 } 112 } 113 return NLS.bind(TeamUIMessages.SyncInfoCompareInput_remoteLabelExists, new String [] { revision.getContentIdentifier() }); 114 } else { 115 return TeamUIMessages.SyncInfoCompareInput_remoteLabel; 116 } 117 } 118 } 119 return null; 120 } 121 122 public Image getImage(Object element) { 123 if (element instanceof ICompareInput) { 124 ICompareInput ci = (ICompareInput) element; 125 return ci.getImage(); 126 } 127 return null; 128 } 129 130 public String getText(Object element) { 131 if (element instanceof ICompareInput) { 132 ICompareInput ci = (ICompareInput) element; 133 return ci.getName(); 134 } 135 return null; 136 } 137 138 public void fireChangeEvent(final Object element) { 139 Display.getDefault().asyncExec(new Runnable () { 140 public void run() { 141 fireLabelProviderChanged(new LabelProviderChangedEvent(CompareInputLabelProvider.this, element)); 142 } 143 144 }); 145 } 146 } 147 148 154 public static ResourceCompareInputChangeNotifier getChangeNotifier(ISynchronizationContext context) { 155 ResourceCompareInputChangeNotifier notifier = (ResourceCompareInputChangeNotifier)context.getCache().get(ResourceCompareInputChangeNotifier.RESOURCE_CHANGE_NOTIFIER_PROPERTY); 156 if (notifier == null) { 157 notifier = new ResourceCompareInputChangeNotifier(context); 158 context.getCache().put(ResourceCompareInputChangeNotifier.RESOURCE_CHANGE_NOTIFIER_PROPERTY, notifier); 159 } 160 return notifier; 161 } 162 163 private final CompareInputLabelProvider labelProvider = new CompareInputLabelProvider(); 164 165 169 public ResourceCompareInputChangeNotifier(ISynchronizationContext context) { 170 super(); 171 this.context = context; 172 initialize(); 174 } 175 176 public void initialize() { 177 context.getDiffTree().addDiffChangeListener(this); 178 context.getCache().addCacheListener(new ICacheListener() { 179 public void cacheDisposed(ICache cache) { 180 dispose(); 181 } 182 }); 183 super.initialize(); 184 } 185 186 189 public void dispose() { 190 super.dispose(); 191 context.getDiffTree().removeDiffChangeListener(this); 192 labelProvider.dispose(); 193 } 194 195 198 public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) { 199 Set changedInputs = new HashSet (); 200 IDiff[] added = event.getAdditions(); 201 for (int i = 0; i < added.length; i++) { 202 IDiff diff = added[i]; 203 ICompareInput input = findInput(ResourceDiffTree.getResourceFor(diff)); 204 if (input != null) 205 changedInputs.add(input); 206 } 207 IDiff[] changed = event.getChanges(); 208 for (int i = 0; i < changed.length; i++) { 209 IDiff diff = changed[i]; 210 ICompareInput input = findInput(ResourceDiffTree.getResourceFor(diff)); 211 if (input != null) 212 changedInputs.add(input); 213 } 214 IPath[] paths = event.getRemovals(); 215 for (int i = 0; i < paths.length; i++) { 216 IPath path = paths[i]; 217 ICompareInput input = findInput(path); 218 if (input != null) 219 changedInputs.add(input); 220 } 221 222 if (!changedInputs.isEmpty()) 223 handleInputChanges((ICompareInput[]) changedInputs.toArray(new ICompareInput[changedInputs.size()]), false); 224 } 225 226 229 public void propertyChanged(IDiffTree tree, int property, IPath[] paths) { 230 } 232 233 236 protected IResource[] getResources(ICompareInput input) { 237 IResource resource = getResource(input); 238 if (resource == null) 239 return new IResource[0]; 240 return new IResource[] { resource }; 241 } 242 243 private IResource getResource(ICompareInput input) { 244 if (input instanceof IResourceProvider) { 245 IResourceProvider rp = (IResourceProvider) input; 246 return rp.getResource(); 247 } 248 return Utils.getResource(input); 249 } 250 251 private ICompareInput findInput(IPath path) { 252 ICompareInput[] inputs = getConnectedInputs(); 253 for (int i = 0; i < inputs.length; i++) { 254 ICompareInput input = inputs[i]; 255 IResource inputResource = getResource(input); 256 if (inputResource != null && inputResource.getFullPath().equals(path)) { 257 return input; 258 } 259 } 260 return null; 261 } 262 263 private ICompareInput findInput(IResource resource) { 264 ICompareInput[] inputs = getConnectedInputs(); 265 for (int i = 0; i < inputs.length; i++) { 266 ICompareInput input = inputs[i]; 267 IResource inputResource = getResource(input); 268 if (inputResource != null && inputResource.equals(resource)) { 269 return input; 270 } 271 } 272 return null; 273 } 274 275 278 protected void prepareInput(ICompareInput input, IProgressMonitor monitor) { 279 if (input instanceof ResourceDiffCompareInput) { 280 ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) input; 281 IResource resource = rdci.getResource(); 282 IDiff diff = getContext().getDiffTree().getDiff(resource); 283 try { 284 ResourceDiffCompareInput.ensureContentsCached(diff, monitor); 285 } catch (CoreException e) { 286 TeamUIPlugin.log(e); 288 } 289 } 290 super.prepareInput(input, monitor); 291 } 292 293 297 public ICompareInputLabelProvider getLabelProvider() { 298 return labelProvider; 299 } 300 301 public void fetchAuthors(final Object input) { 302 runInBackground(new IWorkspaceRunnable() { 303 public void run(IProgressMonitor monitor) throws CoreException { 304 fetchAuthors(input, monitor); 305 } 306 }); 307 } 308 309 protected void fetchAuthors(Object input, IProgressMonitor monitor) throws CoreException { 310 if (input instanceof ResourceDiffCompareInput) { 311 ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) input; 312 if (rdci.updateAuthorInfo(monitor)) { 313 fireLabelProviderChange(input); 314 } 315 } 316 } 317 318 private void fireLabelProviderChange(Object input) { 319 labelProvider.fireChangeEvent(input); 320 } 321 322 326 public final ISynchronizationContext getContext() { 327 return context; 328 } 329 330 protected boolean belongsTo(Object family) { 331 return family == getContext(); 332 } 333 } 334 | Popular Tags |