KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > subscriber > CVSParticipantLabelDecorator


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ccvs.ui.subscriber;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.team.core.TeamException;
21 import org.eclipse.team.core.diff.IDiff;
22 import org.eclipse.team.core.history.IFileRevision;
23 import org.eclipse.team.core.mapping.ISynchronizationContext;
24 import org.eclipse.team.core.subscribers.Subscriber;
25 import org.eclipse.team.core.subscribers.SubscriberMergeContext;
26 import org.eclipse.team.core.synchronize.SyncInfo;
27 import org.eclipse.team.core.variants.IResourceVariant;
28 import org.eclipse.team.internal.ccvs.core.CVSException;
29 import org.eclipse.team.internal.ccvs.core.mapping.ChangeSetModelProvider;
30 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
31 import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
32 import org.eclipse.team.internal.ccvs.ui.*;
33 import org.eclipse.team.internal.ccvs.ui.mappings.ModelCompareParticipant;
34 import org.eclipse.team.internal.core.mapping.SyncInfoToDiffConverter;
35 import org.eclipse.team.internal.core.subscribers.DiffChangeSet;
36 import org.eclipse.team.internal.ui.Utils;
37 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
38 import org.eclipse.team.ui.TeamUI;
39 import org.eclipse.team.ui.mapping.SynchronizationStateTester;
40 import org.eclipse.team.ui.synchronize.*;
41
42
43 public class CVSParticipantLabelDecorator extends LabelProvider implements IPropertyChangeListener, ILabelDecorator {
44     private ISynchronizePageConfiguration configuration;
45     private SynchronizationStateTester tester = new SynchronizationStateTester() {
46         public int getState(Object JavaDoc element, int stateMask, IProgressMonitor monitor) throws CoreException {
47             // Disable state decoration
48
return IDiff.NO_CHANGE;
49         }
50     };
51     
52     public CVSParticipantLabelDecorator(ISynchronizePageConfiguration configuration) {
53         this.configuration = configuration;
54         // Listen for decorator changed to refresh the viewer's labels.
55
CVSUIPlugin.addPropertyChangeListener(this);
56         TeamUI.addPropertyChangeListener(this);
57     }
58     
59     public String JavaDoc decorateText(String JavaDoc input, Object JavaDoc elementOrPath) {
60         if (!isEnabledFor(elementOrPath))
61             return input;
62         try {
63             String JavaDoc text = input;
64             IResource resource = getResource(elementOrPath);
65             if (resource != null && resource.getType() != IResource.ROOT) {
66                 // Prepare the decoration but substitute revision and hide dirty indicator
67
CVSDecoration decoration = getDecoration(resource);
68                 decoration.setRevision(getRevisionNumber(elementOrPath));
69                 decoration.compute();
70                 // Update label
71
StringBuffer JavaDoc output = new StringBuffer JavaDoc(25);
72                 if (decoration.getPrefix() != null) {
73                     output.append(decoration.getPrefix());
74                 }
75                 output.append(text);
76                 if (decoration.getSuffix() != null) {
77                     output.append(decoration.getSuffix());
78                 }
79                 return output.toString();
80             }
81             return text;
82         } catch (CoreException e) {
83             return input;
84         }
85     }
86     
87     protected IResource getResource(Object JavaDoc element) {
88         if (element instanceof ISynchronizeModelElement)
89             return ((ISynchronizeModelElement) element).getResource();
90         return Utils.getResource(internalGetElement(element));
91     }
92
93     protected CVSDecoration getDecoration(IResource resource) throws CoreException {
94         return CVSLightweightDecorator.decorate(resource, tester);
95     }
96
97     public Image decorateImage(Image base, Object JavaDoc element) {
98         return base;
99     }
100     public void propertyChange(PropertyChangeEvent event) {
101         if (needsRefresh(event)) {
102             Viewer viewer = configuration.getPage().getViewer();
103             if(viewer instanceof StructuredViewer && !viewer.getControl().isDisposed()) {
104                 ((StructuredViewer)viewer).refresh(true);
105             }
106         }
107     }
108     
109     protected boolean needsRefresh(PropertyChangeEvent event) {
110         final String JavaDoc property= event.getProperty();
111         return property.equals(CVSUIPlugin.P_DECORATORS_CHANGED) || property.equals(TeamUI.GLOBAL_FILE_TYPES_CHANGED);
112     }
113     public void dispose() {
114         CVSUIPlugin.removePropertyChangeListener(this);
115         TeamUI.removePropertyChangeListener(this);
116     }
117     
118     protected String JavaDoc getRevisionNumber(Object JavaDoc elementOrPath) {
119         IResource resource = getResource(elementOrPath);
120         if (resource != null && resource.getType() == IResource.FILE) {
121             IResourceVariant local;
122             try {
123                 local = (IResourceVariant) CVSWorkspaceRoot.getRemoteResourceFor(resource);
124             } catch (CVSException e) {
125                 local = null;
126             }
127             if(local == null) {
128                 local = getBase(elementOrPath);
129             }
130             String JavaDoc localRevision = getRevisionString(local);
131             StringBuffer JavaDoc revisionString = new StringBuffer JavaDoc();
132             IResourceVariant remote = getRemote(elementOrPath);
133             String JavaDoc remoteRevision = getRevisionString(remote);
134             if(localRevision != null) {
135                 revisionString.append(localRevision);
136             }
137             if(remoteRevision != null) {
138                 revisionString.append( (localRevision != null ? " - " : "") + remoteRevision); //$NON-NLS-1$ //$NON-NLS-2$
139
}
140             return revisionString.toString();
141         }
142         return null;
143     }
144
145     private boolean isEnabledFor(Object JavaDoc elementOrPath) {
146         return !isCompareWithChangeSets() || elementOrPath instanceof TreePath;
147     }
148
149     private boolean isCompareWithChangeSets() {
150         String JavaDoc id = (String JavaDoc)configuration.getProperty(ModelSynchronizeParticipant.P_VISIBLE_MODEL_PROVIDER);
151         return configuration.getParticipant() instanceof ModelCompareParticipant && id.equals(ChangeSetModelProvider.ID);
152     }
153
154     private SyncInfo getSyncInfo(Object JavaDoc element) {
155         if (element instanceof SyncInfoModelElement) {
156             SyncInfoModelElement sime = (SyncInfoModelElement) element;
157             return sime.getSyncInfo();
158         }
159         IResource resource = getResource(element);
160         if (resource != null) {
161             ISynchronizeParticipant participant = configuration.getParticipant();
162             if (participant instanceof ModelSynchronizeParticipant) {
163                 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
164                 ISynchronizationContext context = msp.getContext();
165                 if (context instanceof SubscriberMergeContext) {
166                     SubscriberMergeContext smc = (SubscriberMergeContext) context;
167                     Subscriber subscriber = smc.getSubscriber();
168                     try {
169                         return subscriber.getSyncInfo(resource);
170                     } catch (TeamException e) {
171                         CVSUIPlugin.log(e);
172                     }
173                 }
174             }
175         }
176         return null;
177     }
178     
179     private IResourceVariant getBase(Object JavaDoc element) {
180         if (element instanceof TreePath) {
181             TreePath tp = (TreePath) element;
182             IDiff diff = getDiff(tp);
183             IFileRevision revision = Utils.getBase(diff);
184             return SyncInfoToDiffConverter.asResourceVariant(revision);
185         }
186         SyncInfo info = getSyncInfo(element);
187         if (info != null)
188             return info.getBase();
189         return null;
190     }
191     
192     private IResourceVariant getRemote(Object JavaDoc element) {
193         if (element instanceof TreePath) {
194             TreePath tp = (TreePath) element;
195             IDiff diff = getDiff(tp);
196             IFileRevision revision = Utils.getRemote(diff);
197             return SyncInfoToDiffConverter.asResourceVariant(revision);
198         }
199         SyncInfo info = getSyncInfo(element);
200         if (info != null)
201             return info.getRemote();
202         return null;
203     }
204
205     private IDiff getDiff(TreePath tp) {
206         Object JavaDoc first = tp.getFirstSegment();
207         Object JavaDoc last = tp.getLastSegment();
208         IResource resource = Utils.getResource(last);
209         if (first instanceof DiffChangeSet && resource != null) {
210             DiffChangeSet dcs = (DiffChangeSet) first;
211             return dcs.getDiffTree().getDiff(resource);
212         }
213         return null;
214     }
215
216     private String JavaDoc getRevisionString(IResourceVariant remoteFile) {
217         if(remoteFile instanceof RemoteFile) {
218             return ((RemoteFile)remoteFile).getRevision();
219         }
220         return null;
221     }
222     
223     private Object JavaDoc internalGetElement(Object JavaDoc elementOrPath) {
224         if (elementOrPath instanceof TreePath) {
225             TreePath tp = (TreePath) elementOrPath;
226             return tp.getLastSegment();
227         }
228         return elementOrPath;
229     }
230 }
231
Popular Tags