Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 35e9879fd94983ceec50aaa3fdfadc7ae644710b (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
/*******************************************************************************
 * Copyright (C) 2009, 2019 Yann Simon <yann.simon.fr@gmail.com> and others.
 *
 * All rights reserved. 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
 *******************************************************************************/

package org.eclipse.egit.ui.internal.revision;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.compare.IContentChangeListener;
import org.eclipse.compare.IContentChangeNotifier;
import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.ITypedElement;
import org.eclipse.core.resources.IEncodedStorage;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.egit.core.internal.SafeRunnable;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.widgets.Display;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;

/**
 * Editable revision which supports listening to content changes by adding
 * {@link IContentChangeListener}. Used for comparisons with editable index.
 */
public class EditableRevision extends FileRevisionTypedElement implements
		IEditableContent, IContentChangeNotifier {

	private final CopyOnWriteArrayList<IContentChangeListener> listeners = new CopyOnWriteArrayList<>();

	private byte[] modifiedContent;

	private IStorageEditorInput input;

	/**
	 * @param fileRevision
	 * @param encoding the file encoding
	 */
	public EditableRevision(IFileRevision fileRevision, String encoding) {
		super(fileRevision, encoding);
	}

	@Override
	public boolean isEditable() {
		return true;
	}

	@Override
	public ITypedElement replace(ITypedElement dest, ITypedElement src) {
		return null;
	}

	@Override
	public InputStream getContents() throws CoreException {
		if (modifiedContent != null) {
			return new ByteArrayInputStream(modifiedContent);
		}
		return super.getContents();
	}

	@Override
	public void setContent(byte[] newContent) {
		modifiedContent = newContent;
		fireContentChanged();
	}

	/**
	 * @return The modified content for reading. The data is owned by this
	 *         class, do not modify it.
	 */
	public byte[] getModifiedContent() {
		return modifiedContent;
	}

	@Override
	public IEditorInput getDocumentKey(Object element) {
		if (element == this) {
			if (input == null) {
				input = new IStorageEditorInput() {

					@Override
					public boolean exists() {
						return true;
					}

					@Override
					public ImageDescriptor getImageDescriptor() {
						return null;
					}

					@Override
					public String getName() {
						return EditableRevision.this.getName();
					}

					@Override
					public IPersistableElement getPersistable() {
						return null;
					}

					@Override
					public String getToolTipText() {
						return EditableRevision.this.getName();
					}

					@Override
					public <T> T getAdapter(Class<T> adapter) {
						return null;
					}

					private IStorage storage;

					@Override
					public IStorage getStorage() throws CoreException {
						if (storage == null) {
							storage = new IEncodedStorage() {

								@Override
								public <T> T getAdapter(Class<T> adapter) {
									return null;
								}

								@Override
								public boolean isReadOnly() {
									return false;
								}

								@Override
								public String getName() {
									return EditableRevision.this.getName();
								}

								@Override
								public IPath getFullPath() {
									return null;
								}

								@Override
								public InputStream getContents()
										throws CoreException {
									return EditableRevision.this.getContents();
								}

								@Override
								public String getCharset()
										throws CoreException {
									return EditableRevision.this.getCharset();
								}
							};
						}
						return storage;
					}
				};
			}
			return input;
		}
		return super.getDocumentKey(element);
	}

	@Override
	protected ISharedDocumentAdapter createSharedDocumentAdapter() {
		return new EditableRevisionSharedDocumentAdapter(this);
	}

	@Override
	public void addContentChangeListener(IContentChangeListener listener) {
		listeners.addIfAbsent(listener);
	}

	@Override
	public void removeContentChangeListener(IContentChangeListener listener) {
		listeners.remove(listener);
	}

	/**
	 * Notifies all registered <code>IContentChangeListener</code>s of a content
	 * change.
	 */
	protected void fireContentChanged() {
		if (listeners.isEmpty()) {
			return;
		}
		// Legacy listeners may expect to be notified in the UI thread.
		Runnable runnable = () -> {
			for (IContentChangeListener listener : listeners) {
				SafeRunnable.run(
						() -> listener.contentChanged(EditableRevision.this));
			}
		};
		if (Display.getCurrent() == null) {
			Display.getDefault().syncExec(runnable);
		} else {
			runnable.run();
		}
	}

	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}

	@Override
	public int hashCode() {
		return super.hashCode();
	}

	@SuppressWarnings("restriction")
	private static class EditableRevisionSharedDocumentAdapter
			extends
			org.eclipse.team.internal.ui.synchronize.EditableSharedDocumentAdapter {

		private final EditableRevision editable;

		public EditableRevisionSharedDocumentAdapter(
				EditableRevision editable) {
			super(new ISharedDocumentAdapterListener() {

				@Override
				public void handleDocumentConnected() {
					// Nothing
				}

				@Override
				public void handleDocumentDisconnected() {
					// Nothing
				}

				@Override
				public void handleDocumentFlushed() {
					// Nothing
				}

				@Override
				public void handleDocumentDeleted() {
					// Nothing
				}

				@Override
				public void handleDocumentSaved() {
					// Nothing
				}
			});
			this.editable = editable;
		}

		@Override
		public void flushDocument(IDocumentProvider provider,
				IEditorInput documentKey, IDocument document, boolean overwrite)
				throws CoreException {
			super.flushDocument(provider, documentKey, document, overwrite);
			if (document != null && editable.input != null) {
				try {
					String charset = editable.getCharset();
					if (charset == null) {
						editable.setContent(document.get().getBytes());
					} else {
						editable.setContent(document.get().getBytes(charset));
					}
					// We _know_ that the document provider _cannot_ really save
					// the IStorage. Nevertheless calling its save operation is
					// necessary because otherwise an internal "modified" flag
					// inside the document provider is not re-set. As a
					// consequence the dirty state in the merge viewer would
					// become inconsistent with the dirty state of the document,
					// and subsequent changes would not be saved.
					saveDocument(documentKey, overwrite, null);
				} catch (UnsupportedEncodingException e) {
					throw new CoreException(
							Activator.createErrorStatus(
									MessageFormat.format(
											UIText.EditableRevision_CannotSave,
											editable.getName(),
											editable.getContentIdentifier()),
									e));
				}
			}
		}

		@Override
		public IEditorInput getDocumentKey(Object element) {
			return editable.getDocumentKey(element);
		}
	}

}

Back to the top