Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 18cdf0fc0b20762cc88bc13d7713d548c1b00312 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*******************************************************************************
 *  Copyright (c) 2000, 2011 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 java.util.ResourceBundle;

import org.eclipse.compare.internal.CompareUIPlugin;
import org.eclipse.compare.internal.DocumentManager;
import org.eclipse.compare.internal.ICompareUIConstants;
import org.eclipse.compare.internal.StructureCreatorDescriptor;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.compare.structuremergeviewer.IStructureCreator;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IReusableEditor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.plugin.AbstractUIPlugin;


/**
 * The class <code>CompareUI</code> defines the entry point to initiate a configurable
 * compare operation on arbitrary resources. The result of the compare
 * is opened into a compare editor where the details can be browsed and
 * edited in dynamically selected structure and content viewers.
 * <p>
 * The Compare UI provides a registry for content and structure compare viewers,
 * which is initialized from extensions contributed to extension points
 * declared by this plug-in.
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public final class CompareUI {

	/**
	 * Compare Plug-in ID (value <code>"org.eclipse.compare"</code>).
	 * @since 2.0
	 */
	public static final String PLUGIN_ID= "org.eclipse.compare"; //$NON-NLS-1$

	/**
	 * The id of the Compare Preference Page
	 * (value <code>"org.eclipse.compare.internal.ComparePreferencePage"</code>).
	 *
	 * @since 3.1
	 */
	public static final String PREFERENCE_PAGE_ID= "org.eclipse.compare.internal.ComparePreferencePage"; //$NON-NLS-1$

	/**
	 * Image descriptor for the disabled icon of the 'Next' tool bar button.
	 * @since 2.0
	 */
	public static final ImageDescriptor DESC_DTOOL_NEXT= CompareUIPlugin.getImageDescriptor(ICompareUIConstants.DTOOL_NEXT);
	/**
	 * Image descriptor for the normal icon of the 'Next' tool bar button.
	 * @since 2.0
	 */
	public static final ImageDescriptor DESC_CTOOL_NEXT= CompareUIPlugin.getImageDescriptor(ICompareUIConstants.CTOOL_NEXT);
	/**
	 * Image descriptor for the roll-over icon of the 'Next' tool bar button.
	 * @since 2.0
	 */
	public static final ImageDescriptor DESC_ETOOL_NEXT= CompareUIPlugin.getImageDescriptor(ICompareUIConstants.ETOOL_NEXT);

	/**
	 * Image descriptor for the disabled icon of the 'Previous' tool bar button.
	 * @since 2.0
	 */
	public static final ImageDescriptor DESC_DTOOL_PREV= CompareUIPlugin.getImageDescriptor(ICompareUIConstants.DTOOL_PREV);
	/**
	 * Image descriptor for the normal icon of the 'Previous' tool bar button.
	 * @since 2.0
	 */
	public static final ImageDescriptor DESC_CTOOL_PREV= CompareUIPlugin.getImageDescriptor(ICompareUIConstants.CTOOL_PREV);
	/**
	 * Image descriptor for the roll-over icon of the 'Previous' tool bar button.
	 * @since 2.0
	 */
	public static final ImageDescriptor DESC_ETOOL_PREV= CompareUIPlugin.getImageDescriptor(ICompareUIConstants.ETOOL_PREV);

	/**
	 * Name of the title property of a compare viewer.
	 * If a property with this name is set
	 * on the top level SWT control of a viewer, it is used as a title in the pane's
	 * title bar.
	 */
	public static final String COMPARE_VIEWER_TITLE= "org.eclipse.compare.CompareUI.CompareViewerTitle"; //$NON-NLS-1$

	private CompareUI() {
		// empty implementation
	}

	public static AbstractUIPlugin getPlugin() {
		return CompareUIPlugin.getDefault();
	}

	/**
	 * Returns this plug-in's resource bundle.
	 *
	 * @return the plugin's resource bundle
	 */
	public static ResourceBundle getResourceBundle() {
		return CompareUIPlugin.getDefault().getResourceBundle();
	}

	/**
	 * Performs the comparison described by the given input and opens a
	 * compare editor on the result in the currently active workbench page.
	 *
	 * @param input the input on which to open the compare editor
	 */
	public static void openCompareEditor(CompareEditorInput input) {
		openCompareEditor(input, true);
	}

	/**
	 * Performs the comparison described by the given input and opens a compare
	 * editor on the result in the currently active workbench page.
	 *
	 * @param input
	 *            the input on which to open the compare editor
	 * @param activate
	 *            if <code>true</code> the editor will be activated
	 * @see IWorkbenchPage#openEditor(org.eclipse.ui.IEditorInput, String,
	 *      boolean)
	 * @since 3.5
	 */
	public static void openCompareEditor(CompareEditorInput input, boolean activate) {
		openCompareEditorOnPage(input, null, activate);
	}

	/**
	 * Performs the comparison described by the given input and opens a
	 * compare editor on the result in the given workbench page.
	 *
	 * @param input the input on which to open the compare editor
	 * @param page the workbench page in which to open the compare editor
	 * @since 2.1
	 */
	public static void openCompareEditorOnPage(CompareEditorInput input, IWorkbenchPage page) {
		openCompareEditorOnPage(input, page, true);
	}

	/**
	 * Performs the comparison described by the given input and opens a compare
	 * editor on the result in the given workbench page.
	 *
	 * @param input
	 *            the input on which to open the compare editor
	 * @param page
	 *            the workbench page in which to open the compare editor
	 * @param activate
	 *            if <code>true</code> the editor will be activated
	 * @see IWorkbenchPage#openEditor(org.eclipse.ui.IEditorInput, String,
	 *      boolean)
	 */
	private static void openCompareEditorOnPage(CompareEditorInput input, IWorkbenchPage page, boolean activate) {
		CompareUIPlugin plugin= CompareUIPlugin.getDefault();
		if (plugin != null)
			plugin.openCompareEditor(input, page, null, activate);
	}

	/**
	 * Performs the comparison described by the given input and
	 * shows the result in the given editor.
	 *
	 * @param input the input on which to open the compare editor
	 * @param editor the compare editor to reuse or null to create a new one
	 * @since 3.0
	 */
	public static void reuseCompareEditor(CompareEditorInput input, IReusableEditor editor) {
		reuseCompareEditor(input, editor, true);
	}

	/**
	 * Performs the comparison described by the given input and shows the result
	 * in the given editor.
	 *
	 * @param input
	 *            the input on which to open the compare editor
	 * @param editor
	 *            the compare editor to reuse or null to create a new one
	 * @param activate
	 *            if <code>true</code> the editor will be activated
	 * @see IWorkbenchPage#openEditor(org.eclipse.ui.IEditorInput, String,
	 *      boolean)
	 */
	private static void reuseCompareEditor(CompareEditorInput input, IReusableEditor editor, boolean activate) {
		CompareUIPlugin plugin= CompareUIPlugin.getDefault();
		if (plugin != null)
			plugin.openCompareEditor(input, null, editor, activate);
	}

	/**
	 * Performs the comparison described by the given input and opens a
	 * modal compare dialog on the result.
	 *
	 * @param input the input on which to open the compare dialog
	 */
	public static void openCompareDialog(CompareEditorInput input) {
		CompareUIPlugin plugin= CompareUIPlugin.getDefault();
		if (plugin != null)
			plugin.openCompareDialog(input);
	}

	/**
	 * Registers an image descriptor for the given type.
	 *
	 * @param type the type
	 * @param descriptor the image descriptor
	 */
	public static void registerImageDescriptor(String type, ImageDescriptor descriptor) {
		CompareUIPlugin.registerImageDescriptor(type, descriptor);
	}

	/**
	 * Returns a shared image for the given type, or a generic image if none
	 * has been registered for the given type.
	 * <p>
	 * Note: Images returned from this method will be automatically disposed
	 * of when this plug-in shuts down. Callers must not dispose of these
	 * images themselves.
	 * </p>
	 *
	 * @param type the type
	 * @return the image
	 */
	public static Image getImage(String type) {
		return CompareUIPlugin.getImage(type);
	}

	/**
	 * Registers the given image for being disposed when this plug-in is shutdown.
	 *
	 * @param image the image to register for disposal
	 */
	public static void disposeOnShutdown(Image image) {
		CompareUIPlugin.disposeOnShutdown(image);
	}

	/**
	 * Returns a shared image for the given adaptable.
	 * This convenience method queries the given adaptable
	 * for its <code>IWorkbenchAdapter.getImageDescriptor</code>, which it
	 * uses to create an image if it does not already have one.
	 * <p>
	 * Note: Images returned from this method will be automatically disposed
	 * of when this plug-in shuts down. Callers must not dispose of these
	 * images themselves.
	 * </p>
	 *
	 * @param adaptable the adaptable for which to find an image
	 * @return an image
	 */
	public static Image getImage(IAdaptable adaptable) {
		return CompareUIPlugin.getImage(adaptable);
	}


	/**
	 * Creates a stream merger for the given content type.
	 * If no stream merger is registered for the given content type <code>null</code> is returned.
	 *
	 * @param type the type for which to find a stream merger
	 * @return a stream merger for the given type, or <code>null</code> if no
	 *   stream merger has been registered
	 * @deprecated Clients should obtain an <code>org.eclipse.team.core.mapping.IStorageMerger</code> from the
	 * <code>org.eclipse.team.core.Team#createMerger(IContentType)</code> method.
	 */
	@Deprecated
	public static IStreamMerger createStreamMerger(IContentType type) {
		return CompareUIPlugin.getDefault().createStreamMerger(type);
	}

	/**
	 * Creates a stream merger for the given file extension.
	 * If no stream merger is registered for the file extension <code>null</code> is returned.
	 *
	 * @param type the type for which to find a stream merger
	 * @return a stream merger for the given type, or <code>null</code> if no
	 *   stream merger has been registered
	 * @deprecated Clients should obtain an <code>org.eclipse.team.core.mapping.IStorageMerger</code> from the
	 * <code>org.eclipse.team.core.Team#createMerger(String)</code> method.
	 */
	@Deprecated
	public static IStreamMerger createStreamMerger(String type) {
		return CompareUIPlugin.getDefault().createStreamMerger(type);
	}

	/**
	 * Returns a structure compare viewer based on an old viewer and an input object.
	 * If the old viewer is suitable for showing the input, the old viewer
	 * is returned. Otherwise, the input's type is used to find a viewer descriptor in the registry
	 * which in turn is used to create a structure compare viewer under the given parent composite.
	 * If no viewer descriptor can be found <code>null</code> is returned.
	 *
	 * @param oldViewer a new viewer is only created if this old viewer cannot show the given input
	 * @param input the input object for which to find a structure viewer
	 * @param parent the SWT parent composite under which the new viewer is created
	 * @param configuration a configuration which is passed to a newly created viewer
	 * @return the compare viewer which is suitable for the given input object or <code>null</code>
	 */
	public static Viewer findStructureViewer(Viewer oldViewer, ICompareInput input, Composite parent,
				CompareConfiguration configuration) {

		return CompareUIPlugin.getDefault().findStructureViewer(oldViewer, input, parent, configuration);
	}

	/**
	 * Returns a content compare viewer based on an old viewer and an input object.
	 * If the old viewer is suitable for showing the input the old viewer
	 * is returned. Otherwise the input's type is used to find a viewer descriptor in the registry
	 * which in turn is used to create a content compare viewer under the given parent composite.
	 * If no viewer descriptor can be found <code>null</code> is returned.
	 *
	 * @param oldViewer a new viewer is only created if this old viewer cannot show the given input
	 * @param input the input object for which to find a content viewer
	 * @param parent the SWT parent composite under which the new viewer is created
	 * @param configuration a configuration which is passed to a newly created viewer
	 * @return the compare viewer which is suitable for the given input object or <code>null</code>
	 */
	public static Viewer findContentViewer(Viewer oldViewer, ICompareInput input, Composite parent,
			CompareConfiguration configuration) {
		return CompareUIPlugin.getDefault().findContentViewer(oldViewer, input, parent, configuration);
	}

	/**
	 * Returns a content compare viewer based on an old viewer and an input
	 * object. If the old viewer is suitable for showing the input the old
	 * viewer is returned. Otherwise the input's type is used to find a viewer
	 * descriptor in the registry which in turn is used to create a content
	 * compare viewer under the given parent composite. In order to determine
	 * the input's type, the input must either implement IStreamContentAccessor
	 * and ITypedElement or ICompareInput. If no viewer descriptor can be found
	 * <code>null</code> is returned.
	 *
	 * @param oldViewer a new viewer is only created if this old viewer cannot show the given input
	 * @param input the input object for which to find a content viewer. Must
	 * implement either <code>IStreamContentAccessor</code> and<code>
	 * ITypedElement</code> or <code>ICompareInput</code>.
	 * @param parent the SWT parent composite under which the new viewer is created
	 * @param configuration a configuration which is passed to a newly created viewer
	 * @return the compare viewer which is suitable for the given input object or <code>null</code>
	 */
	public static Viewer findContentViewer(Viewer oldViewer, Object input, Composite parent,
			CompareConfiguration configuration) {

		return CompareUIPlugin.getDefault().findContentViewer(oldViewer, input, parent, configuration);
	}

	/**
	 * Adds an alias for the given type. Subsequent calls to
	 * <code>findStructureViewer</code> treat alias as a synonym for type and
	 * return the same viewer.
	 *
	 * @param type
	 *            a type name for which a viewer has been registered
	 * @param alias
	 *            a type name which should be treated as a synonym of type
	 * @since 2.0
	 * @noreference This method is for internal use only. Clients should not
	 *              call this method.
	 */
	public static void addStructureViewerAlias(String type, String alias) {
		CompareUIPlugin.getDefault().addStructureViewerAlias(type, alias);
	}

	/**
	 * Remove all aliases for the given type. This method does not affect the
	 * initial binding between type and viewer. If no aliases exist for the
	 * given type this method does nothing.
	 *
	 * @param type
	 *            the type name for which all synonyms are removed.
	 * @since 2.0
	 * @noreference This method is for internal use only. Clients should not
	 *              call this method.
	 */
	public static void removeAllStructureViewerAliases(String type) {
		CompareUIPlugin.getDefault().removeAllStructureViewerAliases(type);
	}

	/**
	 * Retrieve a document for the given input or return <code>null</code> if
	 * no document has been registered for the input.
	 * @param input the object for which to retrieve a document
	 * @return a document or <code>null</code> if no document was registered for the input
	 * @since 3.1
	 */
	public static IDocument getDocument(Object input) {
		return DocumentManager.get(input);
	}

	/**
	 * Register a document for the given input.
	 * @param input the object for which to register a document
	 * @param document the document to register
	 * @since 3.1
	 */
	public static void registerDocument(Object input, IDocument document) {
		DocumentManager.put(input, document);
	}

	/**
	 * Unregister the given document.
	 * @param document the document to unregister
	 * @since 3.1
	 */
	public static void unregisterDocument(IDocument document) {
		DocumentManager.remove(document);
	}

	/**
	 * Create and return a structure creator for the given typed element.
	 * Return <code>null</code> if an appropriate structure creator could
	 * not be obtained.
	 * @param element the typed element
	 * @return  structure creator for the given typed element or <code>null</code>
	 * @since 3.4
	 */
	public static IStructureCreator createStructureCreator(ITypedElement element) {
		StructureCreatorDescriptor scd= CompareUIPlugin.getDefault().getStructureCreator(element.getType());
		if (scd != null) {
			return scd.createStructureCreator();
		}
		return null;
	}

}

Back to the top