Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 57e1a77949dac2285446068d261f1b4f30ca8e01 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************************
 * 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 java.lang.reflect.InvocationTargetException;

import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.*;
import org.eclipse.team.core.synchronize.FastSyncInfoFilter.*;
import org.eclipse.team.core.variants.IResourceVariant;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
import org.eclipse.team.internal.ccvs.ui.operations.RemoteCompareOperation.CompareTreeBuilder;
import org.eclipse.team.internal.core.subscribers.ChangeSet;
import org.eclipse.team.internal.core.subscribers.CheckedInChangeSet;
import org.eclipse.team.internal.ui.synchronize.ChangeSetDiffNode;
import org.eclipse.team.ui.synchronize.*;

import com.ibm.icu.text.DateFormat;

class OpenChangeSetAction extends SynchronizeModelAction {

    protected OpenChangeSetAction(ISynchronizePageConfiguration configuration) {
        super(CVSUIMessages.OpenCommitSetAction_20, configuration); 
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSyncInfoFilter()
     */
    @Override
	protected FastSyncInfoFilter getSyncInfoFilter() {
        return new AndSyncInfoFilter(new FastSyncInfoFilter[] {
                new FastSyncInfoFilter() {
                    @Override
					public boolean select(SyncInfo info) {
                        return info.getLocal().getType() == IResource.FILE;
                    }
                },
                new OrSyncInfoFilter(new FastSyncInfoFilter[] {
                    new SyncInfoDirectionFilter(new int[] { SyncInfo.INCOMING, SyncInfo.CONFLICTING }),
                    new FastSyncInfoFilter() {
                        @Override
						public boolean select(SyncInfo info) {
                            return !info.getComparator().isThreeWay();
                        }
                    }
                })
        });
    }
    
    private ChangeSet getChangeSet(IStructuredSelection selection) {
        // First, check to see if a change set is selected directly
        if (selection.size() == 1) {
            Object o = selection.getFirstElement();
            if (o instanceof IAdaptable) {
                ChangeSet set = ((IAdaptable)o).getAdapter(ChangeSet.class);
                if (set != null)
                    return set;
            }
        }
        // Failing that, check to see if all the selected elements and their childen are in the same change set
        return getChangeSet(selection.toArray());
    }
    
    private ChangeSet getChangeSet(Object[] elements) {
        ChangeSet foundSet = null;
        for (int i = 0; i < elements.length; i++) {
            Object object = elements[i];
            ChangeSet set = getChangeSet((ISynchronizeModelElement)object);
            if (set == null) return null;
            if (foundSet == null) {
                foundSet = set;
            } else if (foundSet != set) {
                return null;
            }
        }
        return foundSet;
    }
    
    private ChangeSet getChangeSet(ISynchronizeModelElement element) {
        if (element == null) return null;
        if (element instanceof IAdaptable) {
            ChangeSet set = ((IAdaptable)element).getAdapter(ChangeSet.class);
            if (set != null)
                return set;
        }
        return getChangeSet((ISynchronizeModelElement)element.getParent());
    }

    /* (non-Javadoc)
     * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
     */
    @Override
	protected boolean updateSelection(IStructuredSelection selection) {
        boolean enabled = super.updateSelection(selection);
        if (enabled) {
            // The selection only contains appropriate files so
            // only enable if the selection is contained within a single change set
            ChangeSet set = getChangeSet(selection);
            return set != null;
        }
        return false;
    }

    /* (non-Javadoc)
     * @see org.eclipse.team.ui.synchronize.SynchronizeModelAction#getSubscriberOperation(org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration, org.eclipse.compare.structuremergeviewer.IDiffElement[])
     */
    @Override
	protected SynchronizeModelOperation getSubscriberOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements) {
        return new SynchronizeModelOperation(configuration, elements) {
            @Override
			public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                SyncInfoSet set = getSyncInfoSet();
                SyncInfo[] infos = set.getSyncInfos();
                if (infos.length > 0) {
                    ICVSRepositoryLocation location = getLocation(infos[0]);
                    if (location == null) {
                        handle(new CVSException(CVSUIMessages.OpenCommitSetAction_21)); 
                        return;
                    }
                    CompareTreeBuilder builder = new CompareTreeBuilder(location, null, null);
                    if (buildTrees(builder, infos)) {
                        try {
                            builder.cacheContents(monitor);
	                        builder.openCompareEditor(getConfiguration().getSite().getPart().getSite().getPage(), getCompareTitle(), getCompareToolTip());
                        } catch (CVSException e) {
                            handle(e);
                            return;
                        }
                    }
                }
            }

            private String getCompareToolTip() {
                IDiffElement[] elements = getSelectedDiffElements();
                for (int i = 0; i < elements.length; i++) {
                    IDiffElement element = elements[i];
                    while (element != null) {
                        if (element instanceof ChangeSetDiffNode) {
                            return ((ChangeSetDiffNode)element).getName();
                        }
                        element = element.getParent();
                    }
                }
                return null;
            }
            
            private String getCompareTitle() {
                IDiffElement[] elements = getSelectedDiffElements();
                ChangeSet set = getChangeSet(elements);
                if (set instanceof CheckedInChangeSet) {
                    CheckedInChangeSet cics = (CheckedInChangeSet)set;
                    String date = DateFormat.getDateTimeInstance().format(cics.getDate());
                    return NLS.bind(CVSUIMessages.OpenChangeSetAction_0, new String[] {cics.getAuthor(), date});
                }
                return CVSUIMessages.OpenChangeSetAction_1;
            }

            private ICVSRepositoryLocation getLocation(SyncInfo info) {
                IResourceVariant remote = info.getRemote();
                if (remote == null) {
                    remote = info.getBase();
                }
                if (remote != null) {
                    return ((ICVSRemoteResource)remote).getRepository();
                }
                return null;
            }

            /*
             * Build the trees that will be compared
             */
            private boolean buildTrees(CompareTreeBuilder builder, SyncInfo[] infos) {
                for (int i = 0; i < infos.length; i++) {
                    SyncInfo info = infos[i];
                    IResourceVariant remote = info.getRemote();
                    if (remote == null) {
                        IResourceVariant predecessor = info.getBase();
                        if (predecessor instanceof ICVSRemoteFile) {
                            builder.addToTrees((ICVSRemoteFile)predecessor, null);
                        }
                    } else if (remote instanceof ICVSRemoteFile) {
                        try {
                            ICVSRemoteFile predecessor = getImmediatePredecessor(remote);
                            builder.addToTrees(predecessor, (ICVSRemoteFile)remote);
                        } catch (TeamException e) {
                            handle(e);
                            return false;
                        }
                    }
                }
                return true;
            }
        };
    }

    private ICVSRemoteFile getImmediatePredecessor(IResourceVariant remote) throws TeamException {
        CVSChangeSetCollector changeSetCollector = getChangeSetCollector();
        if (changeSetCollector != null) {
	        return changeSetCollector.getImmediatePredecessor((ICVSRemoteFile)remote);
        }
        return null;
    }

    private CVSChangeSetCollector getChangeSetCollector() {
        return (CVSChangeSetCollector)getConfiguration().getProperty(CVSChangeSetCollector.CVS_CHECKED_IN_COLLECTOR);
    }

}

Back to the top