KickJava   Java API By Example, From Geeks To Geeks.

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


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.compare.CompareConfiguration;
14 import org.eclipse.core.resources.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.preference.PreferencePage;
17 import org.eclipse.jface.viewers.ILabelDecorator;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.team.core.TeamException;
20 import org.eclipse.team.core.synchronize.SyncInfo;
21 import org.eclipse.team.core.variants.IResourceVariant;
22 import org.eclipse.team.internal.ccvs.core.*;
23 import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
24 import org.eclipse.team.internal.ccvs.ui.*;
25 import org.eclipse.team.internal.ccvs.ui.Policy;
26 import org.eclipse.team.internal.ui.IPreferenceIds;
27 import org.eclipse.team.internal.ui.TeamUIPlugin;
28 import org.eclipse.team.internal.ui.synchronize.ChangeSetCapability;
29 import org.eclipse.team.internal.ui.synchronize.IChangeSetProvider;
30 import org.eclipse.team.ui.synchronize.*;
31
32 /**
33  * Superclass for all CVS particpants (workspace, merge and compare)
34  */

35 public class CVSParticipant extends SubscriberParticipant implements IChangeSetProvider {
36     
37     private CVSChangeSetCapability capability;
38
39     /* (non-Javadoc)
40      * @see org.eclipse.team.ui.synchronize.subscribers.SubscriberParticipant#initializeConfiguration(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration)
41      */

42     protected void initializeConfiguration(ISynchronizePageConfiguration configuration) {
43         super.initializeConfiguration(configuration);
44         // The decorator adds itself to the configuration
45
ILabelDecorator labelDecorator = new CVSParticipantLabelDecorator(configuration);
46         configuration.addLabelDecorator(labelDecorator);
47     }
48     
49     /* (non-Javadoc)
50      * @see org.eclipse.team.ui.synchronize.SubscriberParticipant#updateLabels(org.eclipse.team.ui.synchronize.ISynchronizeModelElement, org.eclipse.compare.CompareConfiguration, org.eclipse.core.runtime.IProgressMonitor)
51      */

52     public void prepareCompareInput(ISynchronizeModelElement element, CompareConfiguration config, IProgressMonitor monitor) throws TeamException {
53         monitor.beginTask(null, 100);
54         deriveBaseContentsFromLocal(element, Policy.subMonitorFor(monitor, 10));
55         super.prepareCompareInput(element, config, Policy.subMonitorFor(monitor, 80));
56         updateLabelsForCVS(element, config, Policy.subMonitorFor(monitor, 10));
57         monitor.done();
58     }
59
60     /**
61      * Helper method for updating compare editor labels
62      */

63     protected static void updateLabelsForCVS(ISynchronizeModelElement element, CompareConfiguration config, IProgressMonitor monitor) {
64         // Add the author to the remote or base
65
if (TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.SHOW_AUTHOR_IN_COMPARE_EDITOR)) {
66             SyncInfo info = getSyncInfo(element);
67             if (info != null) {
68                 final IResourceVariant remote = info.getRemote();
69                 final IResourceVariant base = info.getBase();
70                 String JavaDoc remoteAuthor = null;
71                 if (remote != null && !remote.isContainer()) {
72                     try {
73                         ILogEntry entry = ((ICVSRemoteFile)remote).getLogEntry(monitor);
74                         remoteAuthor = entry.getAuthor();
75                         config.setRightLabel(NLS.bind(CVSUIMessages.CVSParticipant_0, new String JavaDoc[] { remote.getContentIdentifier(), remoteAuthor }));
76                     } catch (TeamException e) {
77                         CVSUIPlugin.log(e);
78                     }
79                 }
80                 if (base != null && !base.isContainer()) {
81                     try {
82                         String JavaDoc baseAuthor;
83                         if (remoteAuthor != null && remote.getContentIdentifier().equals(base.getContentIdentifier())) {
84                             baseAuthor = remoteAuthor;
85                         } else {
86                             ILogEntry entry = ((ICVSRemoteFile)base).getLogEntry(monitor);
87                             baseAuthor = entry.getAuthor();
88                         }
89                         config.setAncestorLabel(NLS.bind(CVSUIMessages.CVSParticipant_1, new String JavaDoc[] { base.getContentIdentifier(), baseAuthor }));
90                     } catch (TeamException e) {
91                         CVSUIPlugin.log(e);
92                     }
93                 }
94             }
95         }
96     }
97     
98     protected static SyncInfo getSyncInfo(ISynchronizeModelElement element) {
99         if (element instanceof IAdaptable) {
100             return (SyncInfo)((IAdaptable)element).getAdapter(SyncInfo.class);
101         }
102         return null;
103     }
104
105     /**
106      * If the local is not modified and the base matches the local then
107      * cache the local contents as the contents of the base.
108      * @param element
109      * @throws CoreException
110      * @throws TeamException
111      */

112     public static void deriveBaseContentsFromLocal(ISynchronizeModelElement element, IProgressMonitor monitor) throws TeamException {
113         SyncInfo info = getSyncInfo(element);
114         if (info == null)
115             return;
116         
117         // We need a base that is a file and a local that is a file
118
IResource local = info.getLocal();
119         IResourceVariant base = info.getBase();
120         if (base == null || base.isContainer() || local.getType() != IResource.FILE || !local.exists())
121             return;
122         
123         // We can only use the local contents for incoming changes.
124
// Outgoing or conflicting changes imply that the local has changed
125
if ((info.getKind() & SyncInfo.DIRECTION_MASK) != SyncInfo.INCOMING)
126             return;
127         
128         try {
129             RemoteFile remoteFile = (RemoteFile)base;
130             if (!remoteFile.isContentsCached())
131                 (remoteFile).setContents((IFile)local, monitor);
132         } catch (CoreException e) {
133             if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND) {
134                 // The file must have just been deleted
135
return;
136             }
137             throw CVSException.wrapException(e);
138         }
139     }
140     
141     /* (non-Javadoc)
142      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeParticipant#getPreferencePages()
143      */

144     public PreferencePage[] getPreferencePages() {
145         return addCVSPreferencePages(super.getPreferencePages());
146     }
147
148     public static PreferencePage[] addCVSPreferencePages(PreferencePage[] inheritedPages) {
149         PreferencePage[] pages = new PreferencePage[inheritedPages.length + 1];
150         for (int i = 0; i < inheritedPages.length; i++) {
151             pages[i] = inheritedPages[i];
152         }
153         pages[pages.length - 1] = new ComparePreferencePage();
154         pages[pages.length - 1].setTitle(CVSUIMessages.CVSParticipant_2);
155         return pages;
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeParticipant#getChangeSetCapability()
160      */

161     public ChangeSetCapability getChangeSetCapability() {
162         if (capability == null) {
163             capability = createChangeSetCapability();
164         }
165         return capability;
166     }
167
168     /**
169      * Create the change set capability for this particpant.
170      * @return the created capability
171      */

172     protected CVSChangeSetCapability createChangeSetCapability() {
173         return new CVSChangeSetCapability();
174     }
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.team.ui.synchronize.AbstractSynchronizeParticipant#isViewerContributionsSupported()
178      */

179     protected boolean isViewerContributionsSupported() {
180         return true;
181     }
182 }
183
Popular Tags