KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > ResourceCompareInputChangeNotifier


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui.mapping;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
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 /**
36  * A change notifier for resource-based compare inputs.
37  */

38 public class ResourceCompareInputChangeNotifier extends CompareInputChangeNotifier implements IDiffChangeListener {
39
40     static final String JavaDoc RESOURCE_CHANGE_NOTIFIER_PROPERTY = "org.eclipse.team.ui.ResourceChangeNotifier"; //$NON-NLS-1$
41

42     private ISynchronizationContext context;
43     
44     private class CompareInputLabelProvider extends BaseLabelProvider implements ICompareInputLabelProvider {
45
46         public Image getAncestorImage(Object JavaDoc input) {
47             // No image desired
48
return null;
49         }
50
51         public String JavaDoc getAncestorLabel(Object JavaDoc 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 JavaDoc author = ((FileRevisionTypedElement)element).getAuthor();
60                             if (author != null) {
61                                 return NLS.bind(TeamUIMessages.Utils_20, new String JavaDoc[] { revision.getContentIdentifier(), author });
62                             } else if (revision.isPropertyMissing()) {
63                                 fetchAuthors(input);
64                             }
65                         }
66                         return NLS.bind(TeamUIMessages.SyncInfoCompareInput_baseLabelExists, new String JavaDoc[] { revision.getContentIdentifier() });
67                     } else {
68                         return TeamUIMessages.SyncInfoCompareInput_baseLabel;
69                     }
70                 }
71             }
72             return null;
73         }
74
75         public Image getLeftImage(Object JavaDoc input) {
76             // No image desired
77
return null;
78         }
79
80         public String JavaDoc getLeftLabel(Object JavaDoc input) {
81             if (input instanceof ResourceDiffCompareInput) {
82                 ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) input;
83                 String JavaDoc localContentId = rdci.getLocalContentId();
84                 if (localContentId != null) {
85                     return NLS.bind(TeamUIMessages.SyncInfoCompareInput_localLabelExists, new String JavaDoc[] { localContentId });
86                 } else {
87                     return TeamUIMessages.SyncInfoCompareInput_localLabel;
88                 }
89             }
90             return null;
91         }
92
93         public Image getRightImage(Object JavaDoc input) {
94             // No image desired
95
return null;
96         }
97
98         public String JavaDoc getRightLabel(Object JavaDoc 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 JavaDoc author = ((FileRevisionTypedElement)element).getAuthor();
107                             if (author != null) {
108                                 return NLS.bind(TeamUIMessages.Utils_21, new String JavaDoc[] { revision.getContentIdentifier(), author });
109                             } else if (revision.isPropertyMissing()) {
110                                 fetchAuthors(input);
111                             }
112                         }
113                         return NLS.bind(TeamUIMessages.SyncInfoCompareInput_remoteLabelExists, new String JavaDoc[] { revision.getContentIdentifier() });
114                     } else {
115                         return TeamUIMessages.SyncInfoCompareInput_remoteLabel;
116                     }
117                 }
118             }
119             return null;
120         }
121
122         public Image getImage(Object JavaDoc element) {
123             if (element instanceof ICompareInput) {
124                 ICompareInput ci = (ICompareInput) element;
125                 return ci.getImage();
126             }
127             return null;
128         }
129
130         public String JavaDoc getText(Object JavaDoc 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 JavaDoc element) {
139             Display.getDefault().asyncExec(new Runnable JavaDoc() {
140                 public void run() {
141                     fireLabelProviderChanged(new LabelProviderChangedEvent(CompareInputLabelProvider.this, element));
142                 }
143             
144             });
145         }
146     }
147     
148     /**
149      * Return a compare input change notifier that will detect changes in the synchronization context and
150      * translate them into compare input change events by calling {@link ResourceDiffCompareInput#update()}.
151      * @param context the synchronization context
152      * @return a compare input change notifier
153      */

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     /**
166      * Create a notifier
167      * @param context a synchronization context
168      */

169     public ResourceCompareInputChangeNotifier(ISynchronizationContext context) {
170         super();
171         this.context = context;
172         // We can initialize in the constructor since the context will allow us to dispose
173
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     /* (non-Javadoc)
187      * @see org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier#dispose()
188      */

189     public void dispose() {
190         super.dispose();
191         context.getDiffTree().removeDiffChangeListener(this);
192         labelProvider.dispose();
193     }
194
195     /* (non-Javadoc)
196      * @see org.eclipse.team.core.diff.IDiffChangeListener#diffsChanged(org.eclipse.team.core.diff.IDiffChangeEvent, org.eclipse.core.runtime.IProgressMonitor)
197      */

198     public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) {
199         Set JavaDoc changedInputs = new HashSet JavaDoc();
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     /* (non-Javadoc)
227      * @see org.eclipse.team.core.diff.IDiffChangeListener#propertyChanged(org.eclipse.team.core.diff.IDiffTree, int, org.eclipse.core.runtime.IPath[])
228      */

229     public void propertyChanged(IDiffTree tree, int property, IPath[] paths) {
230         // Property changes are not interesting w.r.t. state changes
231
}
232     
233     /* (non-Javadoc)
234      * @see org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier#getResources(org.eclipse.compare.structuremergeviewer.ICompareInput)
235      */

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     /* (non-Javadoc)
276      * @see org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier#prepareInput(org.eclipse.compare.structuremergeviewer.ICompareInput, org.eclipse.core.runtime.IProgressMonitor)
277      */

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                 // Log the exception and continue
287
TeamUIPlugin.log(e);
288             }
289         }
290         super.prepareInput(input, monitor);
291     }
292
293     /**
294      * Return the label provider for resource compare inputs.
295      * @return the label provider for resource compare inputs
296      */

297     public ICompareInputLabelProvider getLabelProvider() {
298         return labelProvider;
299     }
300
301     public void fetchAuthors(final Object JavaDoc 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 JavaDoc 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 JavaDoc input) {
319         labelProvider.fireChangeEvent(input);
320     }
321     
322     /**
323      * Return the synchronization context to which this notifier is associated.
324      * @return the synchronization context to which this notifier is associated
325      */

326     public final ISynchronizationContext getContext() {
327         return context;
328     }
329     
330     protected boolean belongsTo(Object JavaDoc family) {
331         return family == getContext();
332     }
333 }
334
Popular Tags