Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f2ddc727c7525e6100072f6eab280e1069a1c03 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*******************************************************************************
 * Copyright (c) 2000, 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.compare;

import org.eclipse.compare.contentmergeviewer.IFlushable;
import org.eclipse.compare.internal.CompareMessages;
import org.eclipse.compare.internal.IFlushable2;
import org.eclipse.compare.internal.NullViewer;
import org.eclipse.compare.internal.Utilities;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;

import com.ibm.icu.text.MessageFormat;

/**
 * A custom <code>CompareViewerPane</code> that supports dynamic viewer switching.
 *
 * <p>
 * Clients must implement the viewer switching strategy by implementing
 * the <code>getViewer(Viewer, Object)</code> method.
 * <p>
 * If a property with the name <code>CompareUI.COMPARE_VIEWER_TITLE</code> is set
 * on the top level SWT control of a viewer, it is used as a title in the <code>CompareViewerPane</code>'s
 * title bar.
 *
 * @since 2.0
 */
public abstract class CompareViewerSwitchingPane extends CompareViewerPane {
	private Viewer fViewer;
	private boolean fControlVisibility;
	private String fTitle;
	private String fTitleArgument;

	/**
	 * Creates a <code>CompareViewerSwitchingPane</code> as a child of the given parent and with the
	 * specified SWT style bits.
	 *
	 * @param parent a widget which will be the parent of the new instance (cannot be null)
	 * @param style the style of widget to construct
	 *
	 * @exception IllegalArgumentException <ul>
	 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
	 * </ul>
	 * @exception org.eclipse.swt.SWTException <ul>
	 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
	 * </ul>
	 */
	public CompareViewerSwitchingPane(Composite parent, int style) {
		this(parent, style, false);
	}

	/**
	 * Creates a <code>CompareViewerSwitchingPane</code> as a child of the given parent and with the
	 * specified SWT style bits.
	 *
	 * @param parent a widget which will be the parent of the new instance (cannot be null)
	 * @param style the style of widget to construct
	 * @param visibility the initial visibility of the CompareViewerSwitchingPane
	 *
	 * @exception IllegalArgumentException <ul>
	 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
	 * </ul>
	 * @exception org.eclipse.swt.SWTException <ul>
	 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
	 * </ul>
	 */
	public CompareViewerSwitchingPane(Composite parent, int style, boolean visibility) {
		super(parent, style);

		fControlVisibility= visibility;

		setViewer(new NullViewer(this));

		addDisposeListener(
			e -> {
				if (fViewer != null)
					fViewer.removeSelectionChangedListener(CompareViewerSwitchingPane.this);
				if (fViewer instanceof StructuredViewer) {
					StructuredViewer sv= (StructuredViewer) fViewer;
					sv.removeDoubleClickListener(CompareViewerSwitchingPane.this);
					sv.removeOpenListener(CompareViewerSwitchingPane.this);
				}
				fViewer= null;
			}
		);
	}

	/**
	 * Returns the current viewer.
	 *
	 * @return the current viewer
	 */
	public Viewer getViewer() {
		return fViewer;
	}

	private void setViewer(Viewer newViewer) {
		if (newViewer == fViewer)
			return;

		boolean oldEmpty= isEmpty();

		if (fViewer != null) {
			fViewer.removeSelectionChangedListener(this);

			if (fViewer instanceof StructuredViewer) {
				StructuredViewer sv= (StructuredViewer) fViewer;
				sv.removeDoubleClickListener(this);
				sv.removeOpenListener(this);
			}

			Control content= getContent();
			setContent(null);

			fViewer.setInput(null);

			if (content != null && !content.isDisposed())
				content.dispose();
		} else {
			oldEmpty= false;
		}

		setContent(null);

		fViewer= newViewer;

		if (fViewer != null) {
			// we have to remember and restore the old visibility of the CustomPane
			// since setContent changes the visibility
			boolean old= getVisible();
			setContent(fViewer.getControl());
			setVisible(old);	// restore old visibility

			boolean newEmpty= isEmpty();

			fViewer.addSelectionChangedListener(this);

			if (fViewer instanceof StructuredViewer) {
				StructuredViewer sv= (StructuredViewer) fViewer;
				sv.addDoubleClickListener(this);
				sv.addOpenListener(this);
			}

			if (oldEmpty != newEmpty) {	// re-layout my container
				Composite parent= getParent();
				if (parent instanceof Splitter)
					((Splitter)parent).setVisible(this, fControlVisibility ? !newEmpty : true);
			}

			layout(true);
		}
	}

	/**
	 * Returns the optional title argument that has been set with
	 * <code>setTitelArgument</code> or <code>null</code> if no optional title
	 * argument has been set.
	 *
	 * @return the optional title argument or <code>null</code>
	 * @noreference This method is for internal use only. Clients should not
	 *              call this method.
	 * @nooverride This method is not intended to be re-implemented or extended
	 *             by clients.
	 */
	public String getTitleArgument() {
		return fTitleArgument;
	}

	/**
	 * Returns <code>true</code> if no viewer is installed or if the current viewer
	 * is a <code>NullViewer</code>.
	 *
	 * @return <code>true</code> if no viewer is installed or if the current viewer is a <code>NullViewer</code>
	 */
	public boolean isEmpty() {
		return fViewer == null || fViewer instanceof NullViewer;
	}

	@Override
	public ISelection getSelection() {
		if (fViewer != null)
			return fViewer.getSelection();
		return super.getSelection();
	}

	@Override
	public void setSelection(ISelection s) {
		if (fViewer != null)
			 fViewer.setSelection(s);
	}

	private boolean hasFocus2() {
		// do we have focus?
		Display display= getDisplay();
		if (display != null)
			for (Control focus= display.getFocusControl(); focus != null; focus= focus.getParent())
				if (focus == this)
					return true;
		return false;
	}

	/**
	 * @param input the input
	 * @return true, if the input is considered as changed
	 * @noreference This method is not intended to be referenced by clients.
	 * @nooverride This method is not intended to be re-implemented or extended
	 *             by clients.
	 */
	protected boolean inputChanged(Object input) {
		return getInput() != input;
	}

	/**
	 * Sets the input object of this pane.
	 * For this input object a suitable viewer is determined by calling the abstract
	 * method <code>getViewer(Viewer, Object)</code>.
	 * If the returned viewer differs from the current one, the old viewer
	 * is disposed and the new one installed. Then the input object is fed
	 * into the newly installed viewer by calling its <code>setInput(Object)</code> method.
	 * If new and old viewer don't differ no new viewer is installed but just
	 * <code>setInput(Object)</code> is called.
	 * If the input is <code>null</code> the pane is cleared,
	 * that is the current viewer is disposed.
	 *
	 * @param input the new input object or <code>null</code>
	 */
	@Override
	public void setInput(Object input) {
		if (!inputChanged(input))
			return;

		boolean hadFocus = hasFocus2();

		super.setInput(input);

		// viewer switching
		Viewer newViewer= null;
		if (input != null)
			newViewer= getViewer(fViewer, input);

		if (newViewer == null) {
			if (fViewer instanceof NullViewer)
				return;
			newViewer= new NullViewer(this);
		}

		setViewer(newViewer);

		// set input
		fViewer.setInput(input);

		if (getViewer() == null || !Utilities.okToUse(getViewer().getControl()))
			return;

		Image image= null;
		if (!(fViewer instanceof NullViewer) && input instanceof ICompareInput)
			image= ((ICompareInput)input).getImage();
		setImage(image);

		String title= null;
		if (fViewer != null) {
			Control c= fViewer.getControl();
			if (c != null) {
				Object data= c.getData(CompareUI.COMPARE_VIEWER_TITLE);
				if (data instanceof String)
					title= (String) data;
				if (hadFocus)
					c.setFocus();
			}
		}

		fTitle= title;
		updateTitle();
	}

	/**
	 * Sets an additional and optional argument for the pane's title.
	 *
	 * @param argument
	 *            an optional argument for the pane's title
	 * @noreference This method is for internal use only. Clients should not
	 *              call this method.
	 * @nooverride This method is not intended to be re-implemented or extended
	 *             by clients.
	 */
	public void setTitleArgument(String argument) {
		fTitleArgument= argument;
		updateTitle();
	}

	private void updateTitle() {
		if (fTitle != null) {
			if (fTitleArgument != null) {
				String format= CompareMessages.CompareViewerSwitchingPane_Titleformat;
				String t= MessageFormat.format(format, fTitle, fTitleArgument);
				setText(t);
			} else
				setText(fTitle);
		} else {
			setText("");	//$NON-NLS-1$
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T getAdapter(Class<T> adapter) {
		if (adapter == INavigatable.class) {
			if (isEmpty())
				return null;
			Viewer viewer= getViewer();
			if (viewer == null)
				return null;
			Control control= viewer.getControl();
			if (control == null)
				return null;
			Object data= control.getData(INavigatable.NAVIGATOR_PROPERTY);
			if (data instanceof INavigatable)
				return (T) data;
		}
		if (adapter == IFlushable.class) {
			Viewer v= getViewer();
			if (v != null) {
				IFlushable flushable = Adapters.adapt(v, IFlushable.class);
				if (flushable != null)
					return (T) flushable;
			}
		}
		if (adapter == IFlushable2.class) {
			Viewer v= getViewer();
			if (v != null) {
				IFlushable2 flushable = Adapters.adapt(v, IFlushable2.class);
				if (flushable != null)
					return (T) flushable;
			}
		}
		return super.getAdapter(adapter);
	}

	@Override
	public boolean setFocus() {
		Viewer v= getViewer();
		if (v != null) {
			Control c= v.getControl();
			if (c != null) {
				if (c.setFocus())
					return true;
			}
		}
		return super.setFocus();
	}

	/**
	 * Returns a viewer which is able to display the given input.
	 * If no viewer can be found, <code>null</code> is returned.
	 * The additional argument oldViewer represents the viewer currently installed
	 * in the pane (or <code>null</code> if no viewer is installed).
	 * It can be returned from this method if the current viewer can deal with the
	 * input (and no new viewer must be created).
	 *
	 * @param oldViewer the currently installed viewer or <code>null</code>
	 * @param input the input object for which a viewer must be determined or <code>null</code>
	 * @return a viewer for the given input, or <code>null</code> if no viewer can be determined
	 */
	abstract protected Viewer getViewer(Viewer oldViewer, Object input);
}

Back to the top