Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4d113a4a3660f2f736e0b08f122a3361ccd3a0ef (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
/*******************************************************************************
 * Copyright (c) 2000, 2010 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.texteditor;


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.PlatformUI;


/**
 * Capable of handling input elements that have an associated status with them.
 * @since 2.0
 */
public class StatusTextEditor extends AbstractTextEditor {

	/** The root composite of this editor */
	private Composite fParent;
	/** The layout used to manage the regular and the status page */
	private StackLayout fStackLayout;
	/** The root composite for the regular page */
	private Composite fDefaultComposite;
	/** The status page */
	private Control fStatusControl;
	/** {@link #setFocus()} is still running */
	private boolean setFocusIsRunning;

	// No .options for plugin yet
	private static final boolean DEBUG = false;

	/*
	 * @see org.eclipse.ui.texteditor.AbstractTextEditor.createPartControl(Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {

		fParent= new Composite(parent, SWT.NONE);
		fStackLayout= new StackLayout();
		fParent.setLayout(fStackLayout);

		fDefaultComposite= new Composite(fParent, SWT.NONE);
		fDefaultComposite.setLayout(new FillLayout());
		super.createPartControl(fDefaultComposite);

		updatePartControl(getEditorInput());
	}

	/**
	 * Checks if the status of the given input is OK. If not the
	 * status control is shown rather than the default control.
	 *
	 * @param input the input whose status is checked
	 */
	public void updatePartControl(IEditorInput input) {
		final String where = "updatePartControl"; //$NON-NLS-1$
		if (setFocusIsRunning) {
			trace(where, "ERROR: trying to call update while processing focus", fStatusControl); //$NON-NLS-1$
		} else {
			trace(where, "START", fStatusControl); //$NON-NLS-1$
		}

		boolean restoreFocus= false;

		if (fStatusControl != null) {
			if (!fStatusControl.isDisposed()) {
				restoreFocus= containsFocus(fStatusControl);
			}
			fStatusControl.dispose();
			trace(where, "status control disposed", fStatusControl); //$NON-NLS-1$
			fStatusControl= null;
		}

		Control front= null;
		if (fParent != null && input != null) {
			if (getDocumentProvider() instanceof IDocumentProviderExtension) {
				IDocumentProviderExtension extension= (IDocumentProviderExtension) getDocumentProvider();
				IStatus status= extension.getStatus(input);
				if (!isErrorStatus(status)) {
					front= fDefaultComposite;
				} else {
					fStatusControl= createStatusControl(fParent, status);
					trace(where, "status control created", fStatusControl); //$NON-NLS-1$
					front= fStatusControl;
				}
			}
		}

		if (fStackLayout.topControl != front) {
			fStackLayout.topControl= front;
			fParent.layout();
			updateStatusFields();
		}

		if (restoreFocus && fStatusControl != null && !containsFocus(fStatusControl)) {
			fParent.setFocus();
		}
		trace(where, "END", fStatusControl); //$NON-NLS-1$
	}

	private boolean containsFocus(Control control) {
		Control focusControl= control.getDisplay().getFocusControl();
		if (focusControl != null) {
			focusControl= focusControl.getParent();
			while (focusControl != fParent && focusControl != null && !(focusControl instanceof Shell)) {
				focusControl= focusControl.getParent();
			}
		}
		return focusControl == fParent;
	}

	@Override
	public void setFocus() {
		final String where = "setFocus"; //$NON-NLS-1$
		if (setFocusIsRunning) {
			trace(where, "ERROR: trying to call setFocus while processing focus", fStatusControl); //$NON-NLS-1$
		} else {
			trace(where, "START", fStatusControl); //$NON-NLS-1$
		}
		setFocusIsRunning = true;

		if (fStatusControl != null && !fStatusControl.isDisposed()) {
			/* even if the control does not really take focus, we still have to set it
			 * to fulfill the contract and to make e.g. Ctrl+PageUp/Down work. */
			fStatusControl.setFocus();
		} else {
			super.setFocus();
		}

		setFocusIsRunning = false;

		trace(where, "END", fStatusControl); //$NON-NLS-1$
	}

	@Override
	public boolean validateEditorInputState() {
		if (!super.validateEditorInputState())
			return false;

		if (getDocumentProvider() instanceof IDocumentProviderExtension) {
			IDocumentProviderExtension extension= (IDocumentProviderExtension)getDocumentProvider();
			IStatus status= extension.getStatus(getEditorInput());
			return !isErrorStatus(status) && status.getSeverity() != IStatus.CANCEL;
		}

		return true;
	}

	/**
	 * Returns whether the given status indicates an error. Subclasses may override.
	 *
	 * @param status the status to be checked
	 * @return <code>true</code> if the status indicates an error, <code>false</code> otherwise\
	 * @since 3.0
	 */
	protected boolean isErrorStatus(IStatus status) {
		return status != null && status.getSeverity() == IStatus.ERROR;
	}

	/**
	 * Creates the status control for the given status.
	 * May be overridden by subclasses.
	 *
	 * @param parent the parent control
	 * @param status the status
	 * @return the new status control
	 */
	protected Control createStatusControl(Composite parent, IStatus status) {
		return createInfoForm(parent, status);
	}

	/**
	 * Helper to get rid of deprecation warnings.
	 *
	 * @param parent the parent
	 * @param status the status
	 * @return the control
	 * @since 3.5
	 * @deprecated As of 3.5
	 */
	@Deprecated
	private Control createInfoForm(Composite parent, IStatus status) {
		InfoForm infoForm= new InfoForm(parent);
		infoForm.setHeaderText(getStatusHeader(status));
		infoForm.setBannerText(getStatusBanner(status));
		infoForm.setInfo(getStatusMessage(status));
		return infoForm.getControl();
	}

	/**
	 * Returns a header for the given status
	 *
	 * @param status the status whose message is returned
	 * @return a header for the given status
	 */
	protected String getStatusHeader(IStatus status) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * Returns a banner for the given status.
	 *
	 * @param status the status whose message is returned
	 * @return a banner for the given status
	 */
	protected String getStatusBanner(IStatus status) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * Returns a message for the given status.
	 *
	 * @param status the status whose message is returned
	 * @return a message for the given status
	 */
	protected String getStatusMessage(IStatus status) {
		return status.getMessage();
	}

	@Override
	protected void updateStatusField(String category) {
		IDocumentProvider provider= getDocumentProvider();
		if (provider instanceof IDocumentProviderExtension) {
			IDocumentProviderExtension extension= (IDocumentProviderExtension) provider;
			IStatus status= extension.getStatus(getEditorInput());
			if (isErrorStatus(status)) {
				IStatusField field= getStatusField(category);
				if (field != null) {
					field.setText(fErrorLabel);
					return;
				}
			}
		}

		super.updateStatusField(category);
	}

	@Override
	protected void doSetInput(IEditorInput input) throws CoreException {
		super.doSetInput(input);
		updatePartControl();
	}

	@Override
	public void doRevertToSaved() {
		// http://dev.eclipse.org/bugs/show_bug.cgi?id=19014
		super.doRevertToSaved();
		updatePartControl();
	}

	@Override
	protected void sanityCheckState(IEditorInput input) {
		// http://dev.eclipse.org/bugs/show_bug.cgi?id=19014
		super.sanityCheckState(input);
		if (!setFocusIsRunning) {
			updatePartControl();
		} else {
			trace("sanityCheck", "delaying update", fStatusControl); //$NON-NLS-1$ //$NON-NLS-2$
			PlatformUI.getWorkbench().getDisplay().asyncExec(() -> {
				trace("sanityCheck", "incoming update", fStatusControl); //$NON-NLS-1$ //$NON-NLS-2$
				updatePartControl();
			});
		}
	}

	@Override
	protected void handleEditorInputChanged() {
		super.handleEditorInputChanged();
		updatePartControl();
	}

	@Override
	protected void handleElementContentReplaced() {
		super.handleElementContentReplaced();
		updatePartControl();
	}

	private void updatePartControl() {
		if (fParent != null && !fParent.isDisposed()) {
			updatePartControl(getEditorInput());
		}
	}

	private static void trace(String where, String what, Control o) {
		if (!DEBUG) {
			return;
		}
		String id;
		if (o == null) {
			id = "null"; //$NON-NLS-1$
		} else {
			id = System.identityHashCode(o) + (o.isDisposed() ? "<disposed!>" : ""); //$NON-NLS-1$ //$NON-NLS-2$
		}
		System.out.println(where + " |" + id + "| " + what); //$NON-NLS-1$//$NON-NLS-2$
	}
}

Back to the top