Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4fa93c9ce194d7458dffac5296d234e3eff68542 (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
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.editors.text;

import java.io.CharConversionException;
import java.io.UnsupportedEncodingException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.InstanceScope;

import org.eclipse.core.resources.ResourcesPlugin;

import org.eclipse.jface.action.IAction;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.editors.text.NLSUtility;

import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.StatusTextEditor;
import org.eclipse.ui.texteditor.TextEditorAction;


/**
 * The standard implementation of <code>IEncodingSupport</code>.
 * @since 2.0
 */
public class DefaultEncodingSupport implements IEncodingSupport {

	/** Internal preference change listener. */
	private IPreferenceChangeListener fPreferenceChangeListener;
	/** The editor this support is associated with. */
	private StatusTextEditor fTextEditor;

	/**
	 * Creates a new encoding support.
	 */
	public DefaultEncodingSupport() {
		super();
	}

	/**
	 * Associates this encoding support to the given text editor and initializes this encoding.
	 *
	 * @param textEditor the editor
	 */
	public void initialize(StatusTextEditor textEditor) {

		fTextEditor= textEditor;

		IEclipsePreferences prefs= InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES);

		fPreferenceChangeListener= event -> {
			if (ResourcesPlugin.PREF_ENCODING.equals(event.getKey())) {
				// null means: use default
				Runnable runnable= () -> setEncoding(null, false);
				if (Display.getCurrent() != null)
					runnable.run();
				else {
					// Post runnable into UI thread
					Shell shell;
					if (fTextEditor != null)
						shell= fTextEditor.getSite().getShell();
					else
						shell= getActiveWorkbenchShell();
					Display display;
					if (shell != null)
						display= shell.getDisplay();
					else
						display= Display.getDefault();
					display.asyncExec(runnable);
				}
			}
		};
		
		prefs.addPreferenceChangeListener(fPreferenceChangeListener);
	}

	/**
	 * Disposes this encoding support.
	 */
	public void dispose() {
		IEclipsePreferences prefs= InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES);
		prefs.removePreferenceChangeListener(fPreferenceChangeListener);
		fTextEditor= null;
	}

	/**
	 * Resets this encoding support. Should be called if, e.g., the input element of the
	 * associated editor changed.
	 */
	public void reset() {
	}

	/**
	 * Sets the encoding of the editor's input to the given value. If <code>overwrite</code> is
	 * <code>true</code> the value is set even if the encoding is already set.
	 *
	 * @param encoding the new encoding
	 * @param overwrite <code>true</code> if current encoding should be overwritten
	 */
	protected void setEncoding(String encoding, boolean overwrite) {
		IDocumentProvider p= fTextEditor.getDocumentProvider();
		if (p instanceof IStorageDocumentProvider) {
			final IEditorInput input= fTextEditor.getEditorInput();
			IStorageDocumentProvider provider= (IStorageDocumentProvider)p;
			String current= provider.getEncoding(input);
			if (!fTextEditor.isDirty()) {
				String internal= encoding == null ? "" : encoding; //$NON-NLS-1$
				boolean apply= (overwrite || current == null) && !internal.equals(current);
				if (apply) {
					provider.setEncoding(input, encoding);
					Runnable encodingSetter=
							() -> fTextEditor.doRevertToSaved();
					Display display= fTextEditor.getSite().getShell().getDisplay();
					if (display != null && !display.isDisposed())
						BusyIndicator.showWhile(display, encodingSetter);
					else
						encodingSetter.run();
				}
			}
		}
	}

	@Override
	public void setEncoding(String encoding) {
		setEncoding(encoding, true);
	}

	@Override
	public String getEncoding() {
		IDocumentProvider p= fTextEditor.getDocumentProvider();
		if (p instanceof IStorageDocumentProvider) {
			IStorageDocumentProvider provider= (IStorageDocumentProvider) p;
			return provider.getEncoding(fTextEditor.getEditorInput());
		}
		return null;
	}

	@Override
	public String getDefaultEncoding() {
		IDocumentProvider p= fTextEditor.getDocumentProvider();
		if (p instanceof IStorageDocumentProvider) {
			IStorageDocumentProvider provider= (IStorageDocumentProvider) p;
			return provider.getDefaultEncoding();
		}
		return null;
	}

	/**
	 * Returns a status header for the given status.
	 *
	 * @param status the status
	 * @return a status header for the given status.
	 */
	public String getStatusHeader(IStatus status) {
		Throwable t= status.getException();

		if (t instanceof CharConversionException)
			return TextEditorMessages.Editor_error_unreadable_encoding_header;

		if (t instanceof UnsupportedEncodingException)
			return TextEditorMessages.Editor_error_unsupported_encoding_header;

		return null;
	}

	/**
	 * Returns a banner for the given status.
	 *
	 * @param status the status
	 * @return a banner for the given status.
	 */
	public String getStatusBanner(IStatus status) {
		Throwable t= status.getException();

		if (t instanceof CharConversionException)
			return TextEditorMessages.Editor_error_unreadable_encoding_banner;

		if (t instanceof UnsupportedEncodingException)
			return TextEditorMessages.Editor_error_unsupported_encoding_banner;

		return null;

	}

	/**
	 * Returns a status message if any.
	 *
	 * @param status the status
	 * @return a status message indicating encoding problems or <code>null</code> otherwise
	 */
	public String getStatusMessage(IStatus status) {
		Throwable t= status.getException();
		if (t instanceof CharConversionException || t instanceof UnsupportedEncodingException) {

			String encoding= getEncoding();
			if (encoding == null)
				encoding= getDefaultEncoding();

			if (t instanceof CharConversionException) {
				if (encoding != null)
					return NLSUtility.format(TextEditorMessages.Editor_error_unreadable_encoding_message_arg, encoding);
				return TextEditorMessages.Editor_error_unreadable_encoding_message;
			}

			if (t instanceof UnsupportedEncodingException) {
				if (encoding != null)
					return NLSUtility.format(TextEditorMessages.Editor_error_unsupported_encoding_message_arg, encoding);
				return TextEditorMessages.Editor_error_unsupported_encoding_message;
			}
		}

		return null;
	}

	/**
	 * Returns <code>true</code> if the given status is an
	 * encoding error.
	 *
	 * @param status the status to check
	 * @return <code>true</code> if the given status is an encoding error
	 * @since 3.1
	 */
	public boolean isEncodingError(IStatus status) {
		if (status == null || status.getSeverity() != IStatus.ERROR)
			return false;

		Throwable t= status.getException();
		return t instanceof CharConversionException || t instanceof UnsupportedEncodingException;
	}

	/**
	 * Creates the control which allows to change the encoding.
	 * In case of encoding errors this control will be placed below
	 * the status of the status editor.
	 *
	 * @param parent the parent control
	 * @param status the status
	 * @since 3.1
	 */
	public void createStatusEncodingChangeControl(Composite parent, final IStatus status) {
		final IAction action= fTextEditor.getAction(ITextEditorActionConstants.CHANGE_ENCODING);
		if (action instanceof TextEditorAction)
			((TextEditorAction)action).update();

		if (action == null || !action.isEnabled())
			return;

		Shell shell= parent.getShell();
		Display display= shell.getDisplay();
		Color bgColor= display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);

		Button button= new Button(parent, SWT.PUSH | SWT.FLAT);
		button.setText(action.getText());
		button.addSelectionListener(new SelectionAdapter() {
			/*
			 * @see org.eclipse.swt.events.SelectionAdapter#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {
				action.run();
			}
		});
		button.setFocus();

		Label filler= new Label(parent, SWT.NONE);
		filler.setLayoutData(new GridData(GridData.FILL_BOTH));
		filler.setBackground(bgColor);
	}

	/**
	 * Returns the shell of the active workbench window.
	 *
	 * @return the shell of the active workbench window or <code>null</code> if none
	 * @since 3.2
	 */
	private static Shell getActiveWorkbenchShell() {
		 IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		 if (window != null)
		 	return window.getShell();

		 return null;
	}

}

Back to the top