Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2017ccd88d589b066f4a652c15d8388a51788d78 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*******************************************************************************
 * Copyright (c) 2006, 2017 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.ui;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.CompareViewerPane;
import org.eclipse.compare.IContentChangeListener;
import org.eclipse.compare.IContentChangeNotifier;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.Splitter;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.progress.IProgressService;

/**
 * Abstract class for hosting a page based structure input view for the purposes
 * of feeding compare viewers.
 * <p>
 * This class is not intended to be subclassed by clients outside of the Team framework.
 *
 * @since 3.3
 */
public abstract class PageCompareEditorInput extends CompareEditorInput implements IContentChangeListener {

	private CompareViewerPane pagePane;
	private ICompareInput hookedInput;

	/**
	 * Create a page compare editor input.
	 * @param configuration the compare configuration
	 */
	protected PageCompareEditorInput(CompareConfiguration configuration) {
		super(configuration);
	}

	@Override
	protected CompareViewerPane createStructureInputPane(Composite parent) {
		pagePane = new CompareViewerPane(parent, SWT.BORDER | SWT.FLAT) {
			@Override
			public void selectionChanged(SelectionChangedEvent ev) {
				ISelection selection = ev.getSelection();
				StructuredSelection newSelection = convertSelection(selection, false);
				SelectionChangedEvent newEv = new SelectionChangedEvent(pagePane, newSelection);
				super.selectionChanged(newEv);
			}
			private StructuredSelection convertSelection(ISelection selection, boolean prepare) {
				ICompareInput ci = asCompareInput(selection);
				StructuredSelection newSelection;
				if (ci != null) {
					if (prepare)
						prepareCompareInput(ci);
					newSelection = new StructuredSelection(ci);
				} else {
					newSelection = StructuredSelection.EMPTY;
				}
				return newSelection;
			}
			@Override
			public ISelection getSelection() {
				return convertSelection(getSelectionProvider().getSelection(), false);
			}
			@Override
			public Object getInput() {
				return PageCompareEditorInput.this.getCompareResult();
			}
			@Override
			public void open(OpenEvent event) {
				ISelection selection = event.getSelection();
				StructuredSelection newSelection = convertSelection(selection, true);
				super.open(new OpenEvent((Viewer)event.getSource(), newSelection));
			}
			@Override
			public void doubleClick(DoubleClickEvent event) {
				ISelection selection = event.getSelection();
				StructuredSelection newSelection = convertSelection(selection, true);
				super.doubleClick(new DoubleClickEvent((Viewer)event.getSource(), newSelection));
			}

			@Override
			public void setInput(Object input) {
				super.setInput(input);
				Composite c = getParent();
				if (c instanceof Splitter)
					((Splitter)c).setVisible(this, true);
				layout(true);
			}
		};
		ToolBarManager toolBarManager = CompareViewerPane.getToolBarManager(pagePane);
		IPage page = createPage(pagePane, toolBarManager);
		pagePane.setContent(page.getControl());
		if (parent instanceof Splitter)
			((Splitter)parent).setVisible(pagePane, false);
		hookupListeners();
		return pagePane;
	}

	/**
	 * Create the page for this part and return the top level control
	 * for the page.
	 * @param parent the parent composite
	 * @param toolBarManager the toolbar manager for the page
	 * @return the top-level control for the page
	 */
	protected abstract IPage createPage(CompareViewerPane parent, IToolBarManager toolBarManager);

	/**
	 * Return the selection provider for the page. This method is
	 * called after the page is created in order to register a
	 * selection listener on the page.
	 * @return the selection provider for the page
	 */
	protected abstract ISelectionProvider getSelectionProvider();

	/**
	 * Set the title of the page's page to the given text. The title
	 * will appear in the header of the pane containing the page.
	 * @param title the page's title
	 */
	protected void setPageDescription(String title) {
		pagePane.setText(title);
	}

	@Override
	protected void handleDispose() {
		super.handleDispose();
		cleanupListeners();
		unhookContentChangeListener();
	}

	private void hookupListeners() {
		ISelectionProvider selectionProvider = getSelectionProvider();
		if (selectionProvider != null)
			selectionProvider.addSelectionChangedListener(pagePane);
		if (selectionProvider instanceof StructuredViewer) {
			StructuredViewer sv = (StructuredViewer) selectionProvider;
			sv.addOpenListener(pagePane);
			sv.addDoubleClickListener(pagePane);
		}
	}

	private void cleanupListeners() {
		ISelectionProvider selectionProvider = getSelectionProvider();
		if (selectionProvider != null)
			selectionProvider.removeSelectionChangedListener(pagePane);
		if (selectionProvider instanceof StructuredViewer) {
			StructuredViewer sv = (StructuredViewer) selectionProvider;
			sv.removeOpenListener(pagePane);
			sv.removeDoubleClickListener(pagePane);
		}
	}

	private void hookContentChangeListener(ICompareInput node) {
		if (hookedInput == node)
			return;
		unhookContentChangeListener();
		hookedInput = node;
		ITypedElement left = node.getLeft();
		if(left instanceof IContentChangeNotifier) {
			((IContentChangeNotifier)left).addContentChangeListener(this);
		}
		ITypedElement right = node.getRight();
		if(right instanceof IContentChangeNotifier) {
			((IContentChangeNotifier)right).addContentChangeListener(this);
		}
	}

	private void unhookContentChangeListener() {
		if (hookedInput != null) {
			ITypedElement left = hookedInput.getLeft();
			if(left instanceof IContentChangeNotifier) {
				((IContentChangeNotifier)left).addContentChangeListener(this);
			}
			ITypedElement right = hookedInput.getRight();
			if(right instanceof IContentChangeNotifier) {
				((IContentChangeNotifier)right).addContentChangeListener(this);
			}
		}
	}

	/**
	 * Return a compare input that represents the selection.
	 * This input is used to feed the structure and content
	 * viewers. By default, a compare input is returned if the selection is
	 * of size 1 and the selected element implements <code>ICompareInput</code>.
	 * Subclasses may override.
	 * @param selection the selection
	 * @return a compare input representing the selection
	 */
	protected ICompareInput asCompareInput(ISelection selection) {
		if (selection != null && selection instanceof IStructuredSelection) {
			IStructuredSelection ss= (IStructuredSelection) selection;
			if (ss.size() == 1) {
				Object o = ss.getFirstElement();
				if(o instanceof ICompareInput) {
					return (ICompareInput)o;
				}
			}
		}
		return null;
	}

	/**
	 * Convenience method that calls {@link #prepareInput(ICompareInput, CompareConfiguration, IProgressMonitor)}
	 * with a progress monitor.
	 * @param input the compare input to be prepared
	 */
	protected final void prepareCompareInput(final ICompareInput input) {
		if (input == null)
			return;
		// Don't allow the use of shared documents with PageSaveableParts
		Object left = input.getLeft();
		if (left instanceof LocalResourceTypedElement) {
			LocalResourceTypedElement lrte = (LocalResourceTypedElement) left;
			lrte.enableSharedDocument(false);
		}
		IProgressService manager = PlatformUI.getWorkbench().getProgressService();
		try {
			// TODO: we need a better progress story here (i.e. support for cancellation) bug 127075
			manager.busyCursorWhile(monitor -> {
				prepareInput(input, getCompareConfiguration(), monitor);
				hookContentChangeListener(input);
			});
		} catch (InvocationTargetException e) {
			Utils.handle(e);
		} catch (InterruptedException e) {
			// Ignore
		}
	}

	@Override
	public void contentChanged(IContentChangeNotifier source) {
		setDirty(true);
	}

	@Override
	public boolean canRunAsJob() {
		return true;
	}

	/**
	 * Prepare the compare input for display in a content viewer. This method is
	 * called from {@link #prepareCompareInput(ICompareInput)} and may be called
	 * from a non-UI thread. This method should not be called by others.
	 * @param input the input
	 * @param configuration the compare configuration
	 * @param monitor a progress monitor
	 * @throws InvocationTargetException
	 */
	protected abstract void prepareInput(ICompareInput input, CompareConfiguration configuration, IProgressMonitor monitor) throws InvocationTargetException;


}

Back to the top