Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8050fc51529353b8c5da7f26ee92f6bc741728c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui.subscriber;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.core.variants.IResourceVariant;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
import org.eclipse.team.internal.ccvs.ui.*;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.team.internal.ui.IPreferenceIds;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.team.internal.ui.synchronize.ChangeSetCapability;
import org.eclipse.team.internal.ui.synchronize.IChangeSetProvider;
import org.eclipse.team.ui.synchronize.*;

/**
 * Superclass for all CVS particpants (workspace, merge and compare)
 */
public class CVSParticipant extends SubscriberParticipant implements IChangeSetProvider {
	
	private CVSChangeSetCapability capability;

	@Override
	protected void initializeConfiguration(ISynchronizePageConfiguration configuration) {
		super.initializeConfiguration(configuration);
		// The decorator adds itself to the configuration
		ILabelDecorator labelDecorator = new CVSParticipantLabelDecorator(configuration);
		configuration.addLabelDecorator(labelDecorator);
	}
	
    @Override
	public void prepareCompareInput(ISynchronizeModelElement element, CompareConfiguration config, IProgressMonitor monitor) throws TeamException {
        monitor.beginTask(null, 100);
        deriveBaseContentsFromLocal(element, Policy.subMonitorFor(monitor, 10));
        super.prepareCompareInput(element, config, Policy.subMonitorFor(monitor, 80));
        updateLabelsForCVS(element, config, Policy.subMonitorFor(monitor, 10));
        monitor.done();
    }

    /**
     * Helper method for updating compare editor labels
     */
    protected static void updateLabelsForCVS(ISynchronizeModelElement element, CompareConfiguration config, IProgressMonitor monitor) {
        // Add the author to the remote or base
        if (TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.SHOW_AUTHOR_IN_COMPARE_EDITOR)) {
	        SyncInfo info = getSyncInfo(element);
	        if (info != null) {
	    		final IResourceVariant remote = info.getRemote();
	    		final IResourceVariant base = info.getBase();
	    		String remoteAuthor = null;
	    		if (remote != null && !remote.isContainer()) {
	    		    try {
	                    ILogEntry entry = ((ICVSRemoteFile)remote).getLogEntry(monitor);
	                    remoteAuthor = entry.getAuthor();
	                    config.setRightLabel(NLS.bind(CVSUIMessages.CVSParticipant_0, new String[] { remote.getContentIdentifier(), remoteAuthor })); 
	                } catch (TeamException e) {
	                    CVSUIPlugin.log(e);
	                }
	    		}
	    		if (base != null && !base.isContainer()) {
	    		    try {
                        String baseAuthor;
                        if (remoteAuthor != null && remote.getContentIdentifier().equals(base.getContentIdentifier())) {
                            baseAuthor = remoteAuthor;
                        } else {
                            ILogEntry entry = ((ICVSRemoteFile)base).getLogEntry(monitor);
                            baseAuthor = entry.getAuthor();
                        }
                        config.setAncestorLabel(NLS.bind(CVSUIMessages.CVSParticipant_1, new String[] { base.getContentIdentifier(), baseAuthor })); 
                    } catch (TeamException e) {
                        CVSUIPlugin.log(e);
                    }
	    		}
	        }
        }
    }
    
	protected static SyncInfo getSyncInfo(ISynchronizeModelElement element) {
	    if (element instanceof IAdaptable) {
		    return ((IAdaptable)element).getAdapter(SyncInfo.class);
	    }
	    return null;
	}

    /**
     * If the local is not modified and the base matches the local then 
     * cache the local contents as the contents of the base.
     * @param element
     * @throws CoreException
     * @throws TeamException
     */
    public static void deriveBaseContentsFromLocal(ISynchronizeModelElement element, IProgressMonitor monitor) throws TeamException {
        SyncInfo info = getSyncInfo(element);
        if (info == null) 
            return;
        
        // We need a base that is a file and a local that is a file
        IResource local = info.getLocal();
        IResourceVariant base = info.getBase();
        if (base == null || base.isContainer() || local.getType() != IResource.FILE || !local.exists())
            return;
        
        // We can only use the local contents for incoming changes.
        // Outgoing or conflicting changes imply that the local has changed
        if ((info.getKind() & SyncInfo.DIRECTION_MASK) != SyncInfo.INCOMING)
            return;
        
        try {
            RemoteFile remoteFile = (RemoteFile)base;
            if (!remoteFile.isContentsCached())
                (remoteFile).setContents((IFile)local, monitor);
        } catch (CoreException e) {
            if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND) {
                // The file must have just been deleted
                return;
            }
            throw CVSException.wrapException(e);
        }
    }
    
    @Override
	public PreferencePage[] getPreferencePages() {
        return addCVSPreferencePages(super.getPreferencePages());
    }

    public static PreferencePage[] addCVSPreferencePages(PreferencePage[] inheritedPages) {
        PreferencePage[] pages = new PreferencePage[inheritedPages.length + 1];
        for (int i = 0; i < inheritedPages.length; i++) {
            pages[i] = inheritedPages[i];
        }
        pages[pages.length - 1] = new ComparePreferencePage();
        pages[pages.length - 1].setTitle(CVSUIMessages.CVSParticipant_2); 
        return pages;
    }
    
    @Override
	public ChangeSetCapability getChangeSetCapability() {
        if (capability == null) {
            capability = createChangeSetCapability();
        }
        return capability;
    }

    /**
     * Create the change set capability for this particpant.
     * @return the created capability
     */
    protected CVSChangeSetCapability createChangeSetCapability() {
        return new CVSChangeSetCapability();
    }
    
    @Override
	protected boolean isViewerContributionsSupported() {
        return true;
    }
}

Back to the top