Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 857ae22c2f534685aec96c98c4a5086677dca7a6 (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
/*******************************************************************************
 * Copyright (c) 2000, 2008 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.ui.internal.texteditor.quickdiff;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.action.Action;

import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelExtension;
import org.eclipse.jface.text.source.IChangeRulerColumn;

import org.eclipse.ui.IEditorInput;

import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension3;
import org.eclipse.ui.texteditor.IUpdate;
import org.eclipse.ui.texteditor.quickdiff.IQuickDiffReferenceProvider;
import org.eclipse.ui.texteditor.quickdiff.ReferenceProviderDescriptor;


/**
 * Action to set the quick diff reference for the document displayed in the editor. An instance of
 * this class is created for every extension to the extension point <code>quickdiff.referenceprovider</code>, and for
 * every editor. It acts as a proxy; its <code>run</code> method installs the reference provider
 * specified by the extension with the quick diff differ on the current document.
 *
 * @since 3.0
 */
public class ReferenceSelectionAction extends Action implements IUpdate {

	/** The editor we get the document from. */
	private ITextEditor fEditor= null;
	/** The descriptor of the managed extension. */
	private final ReferenceProviderDescriptor fDescriptor;
	/** The implementation of the extension, after it has been loaded. */
	private IQuickDiffReferenceProvider fProvider;

	/**
	 * Creates a new instance that will lazily create the implementation provided by the extension.
	 *
	 * @param descriptor describes the extension.
	 * @param editor the editor for which this action is created.
	 */
	public ReferenceSelectionAction(ReferenceProviderDescriptor descriptor, ITextEditor editor) {
		super("", AS_RADIO_BUTTON); //$NON-NLS-1$
		setChecked(false);
		setEnabled(true);
		Assert.isLegal(descriptor != null);
		fDescriptor= descriptor;
		fEditor= editor;
		update();
	}

	/**
	 * Creates an instance of the implementation provided by the extension, if none has been created
	 * before. Otherwise, the cached implementation is returned.
	 * @return The <code>IQuickDiffProviderImplementation</code> instance provided by the extension.
	 */
	private IQuickDiffReferenceProvider getProvider() {
		if (fProvider == null) {
			fProvider= fDescriptor.createProvider();
		}
		return fProvider;
	}

	@Override
	public void run() {

		DocumentLineDiffer differ= getDiffer(true); // create if needed, so the user does not have to toggle display when he selects a reference
		if (differ == null)
			return;

		if (fEditor instanceof ITextEditorExtension3) {
			ITextEditorExtension3 extension= (ITextEditorExtension3) fEditor;
			IQuickDiffReferenceProvider provider= getProvider();
			if (provider != null) {
				provider.setActiveEditor(fEditor);
				if (provider.isEnabled()) {
					differ.setReferenceProvider(provider);
					extension.showChangeInformation(true);
					setEnabled(true);
				} else
					setEnabled(false);
			}
		}
	}

	@Override
	public void update() {
		/* two things happen here:
		 * 1: checked state setting - if a provider is already installed, and its id matches
		 * our id, we are in checked state.
		 * 2: enablement - if the extending plugin has been loaded, we check the provider for
		 * enablement and take it as our own.
		 */
		setText(fDescriptor.getLabel());
		DocumentLineDiffer differ= getDiffer(false); // don't create it if we're not showing
		setChecked(false);
		if (differ != null) {
			IQuickDiffReferenceProvider provider= differ.getReferenceProvider();
			if (provider != null && provider.getId().equals(fDescriptor.getId())) {
				setChecked(true);
			}
		}

		if (fDescriptor.isPluginLoaded()) {
			getProvider();
			if (fProvider == null) {
				setEnabled(false);
			} else {
				fProvider.setActiveEditor(fEditor);
				setEnabled(fProvider.isEnabled());
			}
		} else {
			// optimistically enable it
			setEnabled(true);
		}
	}

	/**
	 * Fetches the differ installed with the current editor's document's annotation model. If none
	 * is installed yet, and <code>createIfNeeded</code> is true, one is created and attached to the
	 * model.
	 *
	 * @param createIfNeeded when set to <code>true</code>, a new differ will be created if needed.
	 * @return the differ installed with the annotation model, or <code>null</code>.
	 */
	private DocumentLineDiffer getDiffer(boolean createIfNeeded) {
		// get annotation model
		if (fEditor == null)
			return null;

		IDocumentProvider provider= fEditor.getDocumentProvider();
		IEditorInput editorInput= fEditor.getEditorInput();
		if (provider == null || editorInput == null)
			return null;

		IAnnotationModel m= provider.getAnnotationModel(editorInput);
		IAnnotationModelExtension model= null;
		if (m instanceof IAnnotationModelExtension) {
			model= (IAnnotationModelExtension)m;
		} else {
			return null;
		}

		// get differ
		DocumentLineDiffer differ= (DocumentLineDiffer)model.getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID);

		// create if needed
		if (differ == null && createIfNeeded) {
			differ= new DocumentLineDiffer();
			model.addAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID, differ);
		}

		return differ;
	}
}

Back to the top