Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d80d5c1533e7ad20a107369514a1dbd7bf99b09d (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 * William Chen (Wind River)- [345552] Edit the remote files with a proper editor
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.ui.internal.compare;

import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.ICompareContainer;
import org.eclipse.compare.IContentChangeListener;
import org.eclipse.compare.IContentChangeNotifier;
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.SharedDocumentAdapter;
import org.eclipse.compare.contentmergeviewer.ContentMergeViewer;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.swt.graphics.Image;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.OpCacheCommit;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.compare.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.operations.IOpExecutor;
import org.eclipse.tcf.te.tcf.filesystem.ui.internal.operations.UiExecutor;
import org.eclipse.tcf.te.tcf.filesystem.ui.nls.Messages;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.ISaveablesLifecycleListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.Saveable;
import org.eclipse.ui.SaveablesLifecycleEvent;
import org.eclipse.ui.texteditor.IDocumentProvider;

/**
 * A <code>LocalFileSaveable</code> is used as the saveable object that provides
 * the save behavior for the merge editor input(<code>MergeEditorInput</code>).
 */
public class LocalFileSaveable extends Saveable implements
		IPropertyChangeListener, ISharedDocumentAdapterListener,
		IContentChangeListener {
	// The property listener list.
	private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);

	// The merge input that provides the left and the right compared elements.
	private final MergeInput input;

	// The input of the editor that provides the comparing view.
	private final MergeEditorInput editorInput;

	// If the current document of the file is being saved.
	private boolean saving;

	// The local file element.
	private LocalTypedElement fileElement;

	// The document provided by the local file.
	private IDocument document;

	// If the current document has been connected.
	private boolean connected;

	/**
	 * Create the file-based saveable comparison.
	 *
	 * @param input
	 *            the compare input to be save
	 * @param editorInput
	 *            the editor input containing the comparison
	 * @param fileElement
	 *            the file element that handles the saving and change
	 *            notification
	 */
	public LocalFileSaveable(MergeInput input, MergeEditorInput editorInput, LocalTypedElement fileElement) {
		Assert.isNotNull(input);

		this.input = input;
		this.editorInput = editorInput;
		this.fileElement = fileElement;
		this.fileElement.addContentChangeListener(this);
		this.fileElement.setDocumentListener(this);
	}

	/**
	 * Dispose of the saveable.
	 */
	public void dispose() {
		fileElement.removeContentChangeListener(this);
		fileElement.discardBuffer();
		fileElement.setDocumentListener(null);
	}

	/**
	 * Performs the save.
	 *
	 * @param monitor The progress monitor.
	 *
	 * @throws CoreException If the save operation fails.
	 */
	protected void performSave(IProgressMonitor monitor) throws CoreException {
		try {
			saving = true;
			monitor.beginTask(null, 100);
			// First, we need to flush the viewers so the changes get buffered
			// in the input
			flushViewers(monitor);
			// Then we tell the input to commit its changes
			// Only the left is ever saveable
			if (fileElement.isDirty()) {
				if (fileElement.isConnected()) {
					fileElement.store2Document(monitor);
				} else {
					fileElement.store2Cache(monitor);
				}
			}
		} finally {
			// Make sure we fire a change for the compare input to update the
			// viewers
			fireInputChange();
			setDirty(false);
			saving = false;
			monitor.done();
			//Trigger upload action
			FSTreeNode node = fileElement.getFSTreeNode();
			IOpExecutor executor = new UiExecutor(Messages.CacheManager_UploadingProgress);
			executor.execute(new OpCacheCommit(new FSTreeNode[]{node}, true));
		}
	}

	/**
	 * Flush the contents of any viewers into the compare input.
	 *
	 * @param monitor
	 *            a progress monitor
	 * @throws CoreException
	 */
	protected void flushViewers(IProgressMonitor monitor) throws CoreException {
		editorInput.saveChanges(monitor);
	}

	/**
	 * Fire an input change for the compare input after it has been saved.
	 */
	protected void fireInputChange() {
		editorInput.getCompareResult().fireInputChanged();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.Saveable#isDirty()
	 */
	@Override
	public boolean isDirty() {
		return editorInput.isSaveNeeded();
	}

	/**
	 * Marks the editor input dirty.
	 *
	 * @param dirty <code>True</code> if the editor is dirty, <code>false</code> if not.
	 */
	protected void setDirty(boolean dirty) {
		if (isDirty() != dirty) {
			editorInput.setDirty(dirty);
			firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.Saveable#getName()
	 */
	@Override
	public String getName() {
		// Return the name of the file element as held in the compare input
		if (fileElement.equals(input.getLeft())) {
			return input.getLeft().getName();
		}
		if (fileElement.equals(input.getRight())) {
			return input.getRight().getName();
		}

		// Fallback call returning name of the main non-null element of the input
		//
		// see org.eclipse.team.internal.ui.mapping.AbstractCompareInput#getMainElement()
		return input.getName();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.Saveable#getToolTipText()
	 */
	@Override
	public String getToolTipText() {
		return editorInput.getToolTipText();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.Saveable#getImageDescriptor()
	 */
	@Override
	public ImageDescriptor getImageDescriptor() {
		Image image = input.getImage();
		if (image != null)
			return ImageDescriptor.createFromImage(image);
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
	 */
	@Override
	public void propertyChange(PropertyChangeEvent e) {
		String propertyName = e.getProperty();
		if (CompareEditorInput.DIRTY_STATE.equals(propertyName)) {
			boolean changed = false;
			Object newValue = e.getNewValue();
			if (newValue instanceof Boolean)
				changed = ((Boolean) newValue).booleanValue();

			ContentMergeViewer cmv = (ContentMergeViewer) e.getSource();

			if (fileElement.equals(input.getLeft())) {
				if (changed && cmv.internalIsLeftDirty())
					setDirty(changed);
				else if (!changed && !cmv.internalIsLeftDirty()) {
					setDirty(changed);
				}
			}
			if (fileElement.equals(input.getRight())) {
				if (changed && cmv.internalIsRightDirty())
					setDirty(changed);
				else if (!changed && !cmv.internalIsRightDirty()) {
					setDirty(changed);
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.Saveable#hashCode()
	 */
	@Override
	public int hashCode() {
		if (document != null) {
			return document.hashCode();
		}
		return input.hashCode();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.Saveable#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;

		if (!(obj instanceof Saveable))
			return false;

		if (document != null) {
			Object otherDocument = ((Saveable) obj).getAdapter(IDocument.class);

			if (otherDocument == null)
				return false;

			return document.equals(otherDocument);
		}

		if (obj instanceof LocalFileSaveable) {
			LocalFileSaveable saveable = (LocalFileSaveable) obj;
			return saveable.input.equals(input);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.Saveable#getAdapter(java.lang.Class)
	 */
	@Override
	public Object getAdapter(Class adapter) {
		if (adapter == IDocument.class) {
			if (document != null)
				return document;
			if (fileElement.isConnected()) {
				ISharedDocumentAdapter sda = (ISharedDocumentAdapter) fileElement
						.getAdapter(ISharedDocumentAdapter.class);
				if (sda != null) {
					IEditorInput input = sda.getDocumentKey(fileElement);
					if (input != null) {
						IDocumentProvider provider = SharedDocumentAdapter
								.getDocumentProvider(input);
						if (provider != null)
							return provider.getDocument(input);
					}
				}
			}
		}
		if (adapter == IEditorInput.class) {
			return fileElement.getEditorInput();
		}
		return super.getAdapter(adapter);
	}

	/**
	 * Get an ISaveablesLifecycleListener from the specified workbench
	 * part.
	 * @param part The workbench part.
	 * @return The listener.
	 */
	private ISaveablesLifecycleListener getSaveablesLifecycleListener(
			IWorkbenchPart part) {
		if (part instanceof ISaveablesLifecycleListener)
			return (ISaveablesLifecycleListener) part;

		Object adapted = part.getAdapter(ISaveablesLifecycleListener.class);
		if (adapted instanceof ISaveablesLifecycleListener)
			return (ISaveablesLifecycleListener) adapted;

		adapted = Platform.getAdapterManager().loadAdapter(part,
				ISaveablesLifecycleListener.class.getName());
		if (adapted instanceof ISaveablesLifecycleListener)
			return (ISaveablesLifecycleListener) adapted;

		return (ISaveablesLifecycleListener) part.getSite().getService(
				ISaveablesLifecycleListener.class);
	}

	/**
	 * When the document of the local file is connected, register this saveable.
	 */
	private void registerSaveable() {
		ICompareContainer container = editorInput.getContainer();
		IWorkbenchPart part = container.getWorkbenchPart();
		if (part != null) {
			ISaveablesLifecycleListener lifecycleListener = getSaveablesLifecycleListener(part);
			// Remove this saveable from the lifecycle listener
			lifecycleListener.handleLifecycleEvent(new SaveablesLifecycleEvent(
					part, SaveablesLifecycleEvent.POST_CLOSE,
					new Saveable[] { this }, false));
			// Now fix the hashing so it uses the fConnected fDocument
			IDocument document = (IDocument) getAdapter(IDocument.class);
			if (document != null) {
				this.document = document;
			}
			// Finally, add this saveable back to the listener
			lifecycleListener.handleLifecycleEvent(new SaveablesLifecycleEvent(
					part, SaveablesLifecycleEvent.POST_OPEN,
					new Saveable[] { this }, false));
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.internal.compare.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener#handleDocumentConnected()
	 */
	@Override
	public void handleDocumentConnected() {
		if (connected)
			return;
		connected = true;
		registerSaveable();
		fileElement.setDocumentListener(null);
	}

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

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

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

	@Override
	public void handleDocumentSaved() {
		// Ignore
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.compare.IContentChangeListener#contentChanged(org.eclipse.compare.IContentChangeNotifier)
	 */
	@Override
	public void contentChanged(IContentChangeNotifier source) {
		if (!saving) {
			try {
				performSave(new NullProgressMonitor());
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void doSave(IProgressMonitor monitor) throws CoreException {
		if (isDirty()) {
			performSave(monitor);
			setDirty(false);
		}
	}

	/**
	 * Revert any changes in the buffer back to the last saved state.
	 *
	 * @param monitor
	 *            a progress monitor on <code>null</code> if progress feedback
	 *            is not required
	 */
	public void doRevert(IProgressMonitor monitor) {
		if (!isDirty())
			return;
		fileElement.discardBuffer();
		setDirty(false);
	}

	/**
	 * Add a property change listener. Adding a listener that is already
	 * registered has no effect.
	 *
	 * @param listener
	 *            the listener
	 */
	public void addPropertyListener(IPropertyListener listener) {
		listeners.add(listener);
	}

	/**
	 * Remove a property change listener. Removing a listener that is not
	 * registered has no effect.
	 *
	 * @param listener
	 *            the listener
	 */
	public void removePropertyListener(IPropertyListener listener) {
		listeners.remove(listener);
	}

	/**
	 * Fire a property change event for this buffer.
	 *
	 * @param property
	 *            the property that changed
	 */
	protected void firePropertyChange(final int property) {
		Object[] allListeners = listeners.getListeners();
		for (int i = 0; i < allListeners.length; i++) {
			final Object object = allListeners[i];
			SafeRunner.run(new SafeRunnable() {
				@Override
                public void handleException(Throwable e) {
					// Ignore exception
                }
				@Override
				public void run() throws Exception {
					((IPropertyListener) object).propertyChanged(
							LocalFileSaveable.this, property);
				}
			});
		}
	}
}

Back to the top