Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9e9eac71a0c85decedd776b38748c723c53b63b4 (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*******************************************************************************
 * Copyright (c) 2009, 2011 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.team.internal.ui.synchronize;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.compare.*;
import org.eclipse.compare.structuremergeviewer.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.action.*;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.viewers.*;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.internal.ui.*;
import org.eclipse.team.internal.ui.history.CompareFileRevisionEditorInput;
import org.eclipse.team.internal.ui.mapping.AbstractCompareInput;
import org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier;
import org.eclipse.team.internal.ui.synchronize.EditableSharedDocumentAdapter.ISharedDocumentAdapterListener;
import org.eclipse.team.ui.mapping.SaveableComparison;
import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
import org.eclipse.ui.*;
import org.eclipse.ui.actions.*;
import org.eclipse.ui.ide.IGotoMarker;
import org.eclipse.ui.keys.IBindingService;
import org.eclipse.ui.services.IDisposable;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * A compare editor input that makes use of a {@link Saveable} to manage the
 * save lifecycle of the left and right sides of the comparison. The ancestor
 * part of the comparison is not editable.
 */
public class SaveablesCompareEditorInput extends CompareEditorInput implements
		ISaveablesSource {

	private IPropertyListener fLeftPropertyListener;
	private IPropertyListener fRightPropertyListener;

	private Saveable fLeftSaveable;
	private Saveable fRightSaveable;

	private ITypedElement fAncestorElement;
	private ITypedElement fLeftElement;
	private ITypedElement fRightElement;

	private final IWorkbenchPage page;
	private final ListenerList inputChangeListeners = new ListenerList(
			ListenerList.IDENTITY);
	private ICompareInputChangeListener compareInputChangeListener;

	public SaveablesCompareEditorInput(ITypedElement ancestor,
			ITypedElement left, ITypedElement right, IWorkbenchPage page) {
		super(new CompareConfiguration());
		this.page = page;
		this.fAncestorElement = ancestor;
		this.fLeftElement = left;
		this.fRightElement = right;
	}

	private static ITypedElement getFileElement(ITypedElement element,
			CompareEditorInput editorInput) {
		if (element instanceof LocalResourceTypedElement) {
			return element;
		}
		if (editorInput instanceof CompareFileRevisionEditorInput) {
			return ((CompareFileRevisionEditorInput) editorInput)
					.getLocalElement();
		}
		return null;
	}

	/**
	 * Return a typed element that represents a local file. If the element
	 * returned from this method is used as the left contributor of the compare
	 * input for a {@link SaveableCompareEditorInput}, then the file will be
	 * properly saved when the compare editor input or viewers are saved.
	 * 
	 * @param file
	 *            the file
	 * @return a typed element that represents a local file.
	 */
	public static ITypedElement createFileElement(IFile file) {
		return new LocalResourceTypedElement(file);
	}

	private ISaveablesLifecycleListener getSaveablesLifecycleListener(
			IWorkbenchPart part) {
		ISaveablesLifecycleListener listener = (ISaveablesLifecycleListener) Utils
				.getAdapter(part, ISaveablesLifecycleListener.class);
		if (listener == null)
			listener = (ISaveablesLifecycleListener) part.getSite().getService(
					ISaveablesLifecycleListener.class);
		return listener;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.compare.CompareEditorInput#contentsCreated()
	 */
	protected void contentsCreated() {
		super.contentsCreated();
		compareInputChangeListener = new ICompareInputChangeListener() {
			public void compareInputChanged(ICompareInput source) {
				if (source == getCompareResult()) {
					boolean closed = false;
					if (source.getKind() == Differencer.NO_CHANGE) {
						closed = closeEditor(true);
					}
					if (!closed) {
						// The editor was not closed either because the compare
						// input still has changes or because the editor input
						// is dirty. In either case, fire the changes
						// to the registered listeners
						propogateInputChange();
					}
				}
			}
		};
		getCompareInput().addCompareInputChangeListener(
				compareInputChangeListener);

		if (getLeftSaveable() instanceof SaveableComparison) {
			SaveableComparison lscm = (SaveableComparison) fLeftSaveable;
			fLeftPropertyListener = new IPropertyListener() {
				public void propertyChanged(Object source, int propId) {
					if (propId == SaveableComparison.PROP_DIRTY) {
						setLeftDirty(fLeftSaveable.isDirty());
					}
				}
			};
			lscm.addPropertyListener(fLeftPropertyListener);
		}

		if (getRightSaveable() instanceof SaveableComparison) {
			SaveableComparison rscm = (SaveableComparison) fRightSaveable;
			fRightPropertyListener = new IPropertyListener() {
				public void propertyChanged(Object source, int propId) {
					if (propId == SaveableComparison.PROP_DIRTY) {
						setRightDirty(fRightSaveable.isDirty());
					}
				}
			};
			rscm.addPropertyListener(fRightPropertyListener);
		}

		setLeftDirty(fLeftSaveable.isDirty());
		setRightDirty(fRightSaveable.isDirty());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.compare.CompareEditorInput#handleDispose()
	 */
	protected void handleDispose() {
		super.handleDispose();
		ICompareInput compareInput = getCompareInput();
		if (compareInput != null)
			compareInput
					.removeCompareInputChangeListener(compareInputChangeListener);
		compareInputChangeListener = null;
		if (fLeftSaveable instanceof SaveableComparison) {
			SaveableComparison scm = (SaveableComparison) fLeftSaveable;
			scm.removePropertyListener(fLeftPropertyListener);
		}
		if (fLeftSaveable instanceof LocalResourceSaveableComparison) {
			LocalResourceSaveableComparison rsc = (LocalResourceSaveableComparison) fLeftSaveable;
			rsc.dispose();
		}
		if (fRightSaveable instanceof SaveableComparison) {
			SaveableComparison scm = (SaveableComparison) fRightSaveable;
			scm.removePropertyListener(fRightPropertyListener);
		}
		if (fRightSaveable instanceof LocalResourceSaveableComparison) {
			LocalResourceSaveableComparison rsc = (LocalResourceSaveableComparison) fRightSaveable;
			rsc.dispose();
		}

		if (getCompareResult() instanceof IDisposable) {
			((IDisposable) getCompareResult()).dispose();
		}
	}
	
	private String[] getLabels() {
		IResource leftResource = getResource(fLeftElement);
		IResource rightResource = getResource(fRightElement);
		if (leftResource != null && rightResource != null) {
			String leftLabel = leftResource.getFullPath().makeRelative().toString();
			String rightLabel = rightResource.getFullPath().makeRelative().toString();
			if (fAncestorElement != null) {
				IResource ancestorResource = getResource(fAncestorElement);
				if (ancestorResource != null) {
					String ancestorLabel = rightResource.getFullPath().makeRelative().toString();
					return new String[] { ancestorLabel, leftLabel, rightLabel };
				}
			}
			return new String[] { leftLabel, rightLabel };
		}
		if (fAncestorElement != null) {
			return new String[] { fAncestorElement.getName(), fLeftElement.getName(), fRightElement.getName() };
		}
		return new String[] { fLeftElement.getName(), fRightElement.getName() };
	}
	
	public String getToolTipText() {
		String[] labels = getLabels();
		if (labels.length == 3)
			return NLS.bind(TeamUIMessages.SaveablesCompareEditorInput_threeWayTooltip, labels);
		return NLS.bind(TeamUIMessages.SaveablesCompareEditorInput_twoWayTooltip, labels);
	}

	public String getTitle() {
		String[] labels = getLabels();
		if (labels.length == 3)
			return NLS.bind(TeamUIMessages.SaveablesCompareEditorInput_threeWayTitle, labels);
		return NLS.bind(TeamUIMessages.SaveablesCompareEditorInput_twoWayTitle, labels);
	}

	private IWorkbenchPage getPage() {
		if (page == null)
			return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
					.getActivePage();
		return page;
	}

	/**
	 * Return the compare input of this editor input.
	 * 
	 * @return the compare input of this editor input
	 */
	protected final ICompareInput getCompareInput() {
		return (ICompareInput) getCompareResult();
	}

	/**
	 * Callback from the resource saveable that is invoked when the resource is
	 * saved so that this input can fire a change event for its input.
	 * Subclasses only need this method if the left side of their compare input
	 * is an element returned from
	 * {@link SaveableCompareEditorInput#createFileElement(IFile)}.
	 */
	protected void fireInputChange() {
		((MyDiffNode) getCompareResult()).fireChange();
	}

	protected Saveable getLeftSaveable() {
		if (fLeftSaveable == null) {
			fLeftSaveable = createLeftSaveable();
		}
		return fLeftSaveable;
	}

	protected Saveable getRightSaveable() {
		if (fRightSaveable == null) {
			fRightSaveable = createRightSaveable();
		}
		return fRightSaveable;
	}

	protected Saveable createLeftSaveable() {
		Object compareResult = getCompareResult();
		Assert
				.isNotNull(compareResult,
						"This method cannot be called until after prepareInput is called"); //$NON-NLS-1$
		ITypedElement leftFileElement = getFileElement(getCompareInput()
				.getLeft(), this);
		return new InternalResourceSaveableComparison(
				(ICompareInput) compareResult, this, leftFileElement);
	}

	protected Saveable createRightSaveable() {
		Object compareResult = getCompareResult();
		Assert
				.isNotNull(compareResult,
						"This method cannot be called until after prepareInput is called"); //$NON-NLS-1$
		ITypedElement rightFileElement = getFileElement(getCompareInput()
				.getRight(), this);
		return new InternalResourceSaveableComparison(
				(ICompareInput) compareResult, this, rightFileElement);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.ISaveablesSource#getActiveSaveables()
	 */
	public Saveable[] getActiveSaveables() {
		if (getCompareResult() == null)
			return new Saveable[0];
		return new Saveable[] { getLeftSaveable(), getRightSaveable() };
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.ISaveablesSource#getSaveables()
	 */
	public Saveable[] getSaveables() {
		return getActiveSaveables();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.compare.CompareEditorInput#findContentViewer(org.eclipse.
	 * jface.viewers.Viewer,
	 * org.eclipse.compare.structuremergeviewer.ICompareInput,
	 * org.eclipse.swt.widgets.Composite)
	 */
	public Viewer findContentViewer(Viewer pOldViewer, ICompareInput pInput,
			Composite pParent) {
		Viewer newViewer = super.findContentViewer(pOldViewer, pInput, pParent);
		boolean isNewViewer = newViewer != pOldViewer;
		if (isNewViewer && newViewer instanceof IPropertyChangeNotifier
				&& fLeftSaveable instanceof IPropertyChangeListener
				&& fRightSaveable instanceof IPropertyChangeListener) {
			// Register the model for change events if appropriate
			final IPropertyChangeNotifier dsp = (IPropertyChangeNotifier) newViewer;
			final IPropertyChangeListener lpcl = (IPropertyChangeListener) fLeftSaveable;
			final IPropertyChangeListener rpcl = (IPropertyChangeListener) fRightSaveable;
			dsp.addPropertyChangeListener(lpcl);
			dsp.addPropertyChangeListener(rpcl);
			Control c = newViewer.getControl();
			c.addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent e) {
					dsp.removePropertyChangeListener(lpcl);
					dsp.removePropertyChangeListener(rpcl);
				}
			});
		}
		return newViewer;
	}

	public boolean isDirty() {
		if (fLeftSaveable != null && fLeftSaveable.isDirty())
			return true;
		if (fRightSaveable != null && fRightSaveable.isDirty())
			return true;
		return super.isDirty();
	}

	/**
	 * Returns <code>true</code> if the given saveable contains any unsaved
	 * changes. If the saveable doesn't match either left nor right side of the
	 * current editor input {@link CompareEditorInput#isSaveNeeded()} is called.
	 * <p>
	 * This method is preferred to {@link CompareEditorInput#isSaveNeeded()}.
	 * 
	 * @param the
	 *            the saveable to check
	 * @return <code>true</code> if there are changes that need to be saved
	 * @since 3.7
	 */
	boolean isSaveNeeded(Saveable saveable) {
		if (saveable == null) {
			return isSaveNeeded();
		}
		if (saveable.equals(fLeftSaveable)) {
			return isLeftSaveNeeded();
		}
		if (saveable.equals(fRightSaveable)) {
			return isRightSaveNeeded();
		}
		// Fallback call returning true if there are unsaved changes in either
		// left or right side
		return isSaveNeeded();
	}

	void setDirty(boolean dirty, Saveable saveable) {
		if (saveable.equals(fLeftSaveable)) {
			setLeftDirty(dirty);
		}
		if (saveable.equals(fRightSaveable)) {
			setRightDirty(dirty);
		}
	}

	void saveChanges(IProgressMonitor monitor, Saveable saveable)
			throws CoreException {
		if (saveable.equals(fLeftSaveable)) {
			flushLeftViewers(monitor);
			return;
		} else if (saveable.equals(fRightSaveable)) {
			flushRightViewers(monitor);
			return;
		}
		Assert.isTrue(false, "invalid saveable parameter"); //$NON-NLS-1$
	}

	/**
	 * Close the editor if it is not dirty. If it is still dirty, let the
	 * content merge viewer handle the compare input change.
	 * 
	 * @param checkForUnsavedChanges
	 *            whether to check for unsaved changes
	 * @return <code>true</code> if the editor was closed (note that the close
	 *         may be asynchronous)
	 */
	protected boolean closeEditor(boolean checkForUnsavedChanges) {
		if (isSaveNeeded() && checkForUnsavedChanges) {
			return false;
		} else {
			Runnable runnable = new Runnable() {
				public void run() {
					IEditorPart part = getPage().findEditor(
							SaveablesCompareEditorInput.this);
					getPage().closeEditor(part, false);
				}
			};
			if (Display.getCurrent() != null) {
				runnable.run();
			} else {
				Display display = getPage().getWorkbenchWindow().getShell()
						.getDisplay();
				display.asyncExec(runnable);
			}
			return true;
		}
	}

	/**
	 * Prepare the compare input of this editor input. This method is not
	 * intended to be overridden of extended by subclasses (but is not final for
	 * backwards compatibility reasons). The implementation of this method in
	 * this class delegates the creation of the compare input to the
	 * {@link #prepareCompareInput(IProgressMonitor)} method which subclasses
	 * must implement.
	 * 
	 * @see org.eclipse.compare.CompareEditorInput#prepareInput(org.eclipse.core.runtime.IProgressMonitor)
	 */
	protected Object prepareInput(IProgressMonitor monitor)
			throws InvocationTargetException, InterruptedException {
		final ICompareInput input = prepareCompareInput(monitor);
		if (input != null)
			setTitle(NLS.bind(TeamUIMessages.SyncInfoCompareInput_title,
					new String[] { input.getName() }));
		return input;
	}

	/**
	 * Method called from {@link #prepareInput(IProgressMonitor)} to obtain the input. Its purpose
	 * is to ensure that the input is an instance of {@link ICompareInput}.
	 * 
	 * @param monitor a progress monitor
	 * @return the compare input
	 * @throws InvocationTargetException
	 * @throws InterruptedException
	 */
	protected ICompareInput prepareCompareInput(IProgressMonitor monitor)
			throws InvocationTargetException, InterruptedException {
		ICompareInput input = createCompareInput();
		getCompareConfiguration().setLeftEditable(isEditable(input.getLeft()));
		getCompareConfiguration()
				.setRightEditable(isEditable(input.getRight()));
		initLabels();
		return input;
	}

	private boolean isEditable(Object obj) {
		if (obj instanceof IEditableContent) {
			return ((IEditableContent) obj).isEditable();
		}
		return false;
	}

	private void initLabels() {
		CompareConfiguration cc = getCompareConfiguration();

		IResource ancestorResource = getResource(fAncestorElement);
		IResource leftResource = getResource(fLeftElement);
		IResource rightResource = getResource(fRightElement);

		if (ancestorResource != null) {
			String ancestorLabel = ancestorResource.getFullPath()
					.makeRelative().toString();

			cc.setAncestorLabel(ancestorLabel);
		}

		if (leftResource != null && rightResource != null) {
			String leftLabel = leftResource.getFullPath().makeRelative()
					.toString();
			String rightLabel = rightResource.getFullPath().makeRelative()
					.toString();

			cc.setLeftLabel(leftLabel);
			cc.setRightLabel(rightLabel);
		}
	}

	private ICompareInput createCompareInput() {
		return fAncestorElement == null ? new MyDiffNode(fLeftElement,
				fRightElement) : new MyDiffNode(fAncestorElement, fLeftElement,
				fRightElement);
	}

	private CompareInputChangeNotifier notifier = new CompareInputChangeNotifier() {
		protected IResource[] getResources(ICompareInput input) {
			IResource leftResource = getResource(fLeftElement);
			IResource rightResource = getResource(fRightElement);
			if (leftResource == null && rightResource == null)
				return new IResource[0];
			if (leftResource == null && rightResource != null)
				return new IResource[] { rightResource };
			if (leftResource != null && rightResource == null)
				return new IResource[] { leftResource };
			return new IResource[] { leftResource, rightResource };
		}
	};

	private class MyDiffNode extends AbstractCompareInput {
		public MyDiffNode(ITypedElement left, ITypedElement right) {
			super(Differencer.CHANGE, null, left, right);
		}

		public MyDiffNode(ITypedElement ancestor, ITypedElement left,
				ITypedElement right) {
			super(Differencer.CONFLICTING, ancestor, left, right);
		}

		public void fireChange() {
			super.fireChange();
		}

		protected CompareInputChangeNotifier getChangeNotifier() {
			return notifier;
		}

		public boolean needsUpdate() {
			return true;
		}

		public void update() {
			fireChange();
		}
	}

	private IResource getResource(ITypedElement pElement) {
		if (pElement instanceof LocalResourceTypedElement
				&& pElement instanceof IResourceProvider) {
			return ((IResourceProvider) pElement).getResource();
		}
		return null;
	}

	public void registerContextMenu(final MenuManager pMenuManager,
			final ISelectionProvider pSelectionProvider) {
		super.registerContextMenu(pMenuManager, pSelectionProvider);
		final Saveable lLeftSaveable = getLeftSaveable();
		final ITypedElement lLeftElement = getFileElement(getCompareInput()
				.getLeft(), this);
		if (lLeftSaveable instanceof LocalResourceSaveableComparison) {
			pMenuManager.addMenuListener(new IMenuListener() {
				public void menuAboutToShow(IMenuManager manager) {
					handleMenuAboutToShow(manager, getContainer(), lLeftSaveable, lLeftElement, pSelectionProvider);
				}
			});
		}
		final Saveable lRightSaveable = getRightSaveable();
		final ITypedElement lRightElement = getFileElement(getCompareInput()
				.getRight(), this);
		if (lRightSaveable instanceof LocalResourceSaveableComparison) {
			pMenuManager.addMenuListener(new IMenuListener() {
				public void menuAboutToShow(IMenuManager manager) {
					handleMenuAboutToShow(manager, getContainer(), lRightSaveable, lRightElement, pSelectionProvider);
				}
			});
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.compare.CompareEditorInput#addCompareInputChangeListener(
	 * org.eclipse.compare.structuremergeviewer.ICompareInput,
	 * org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener)
	 */
	public void addCompareInputChangeListener(ICompareInput input,
			ICompareInputChangeListener listener) {
		if (input == getCompareResult()) {
			inputChangeListeners.add(listener);
		} else {
			super.addCompareInputChangeListener(input, listener);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.compare.CompareEditorInput#removeCompareInputChangeListener
	 * (org.eclipse.compare.structuremergeviewer.ICompareInput,
	 * org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener)
	 */
	public void removeCompareInputChangeListener(ICompareInput input,
			ICompareInputChangeListener listener) {
		if (input == getCompareResult()) {
			inputChangeListeners.remove(listener);
		} else {
			super.removeCompareInputChangeListener(input, listener);
		}
	}

	private void propogateInputChange() {
		if (!inputChangeListeners.isEmpty()) {
			Object[] allListeners = inputChangeListeners.getListeners();
			final ICompareInput compareResult = (ICompareInput) getCompareResult();
			for (int i = 0; i < allListeners.length; i++) {
				final ICompareInputChangeListener listener = (ICompareInputChangeListener) allListeners[i];
				SafeRunner.run(new ISafeRunnable() {
					public void run() throws Exception {
						listener.compareInputChanged(compareResult);
					}

					public void handleException(Throwable exception) {
						// Logged by the safe runner
					}
				});
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.compare.CompareEditorInput#getTitleImage()
	 */
	public Image getTitleImage() {
		ImageRegistry reg = TeamUIPlugin.getPlugin().getImageRegistry();
		Image image = reg.get(ITeamUIImages.IMG_SYNC_VIEW);
		if (image == null) {
			image = getImageDescriptor().createImage();
			reg.put(ITeamUIImages.IMG_SYNC_VIEW, image);
		}
		return image;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
	 */
	public ImageDescriptor getImageDescriptor() {
		return TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_SYNC_VIEW);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.compare.CompareEditorInput#canRunAsJob()
	 */
	public boolean canRunAsJob() {
		return true;
	}

	private static String getShowInMenuLabel() {
		String keyBinding = null;

		IBindingService bindingService = (IBindingService) PlatformUI
				.getWorkbench().getAdapter(IBindingService.class);
		if (bindingService != null)
			keyBinding = bindingService
					.getBestActiveBindingFormattedFor(IWorkbenchCommandConstants.NAVIGATE_SHOW_IN_QUICK_MENU);

		if (keyBinding == null)
			keyBinding = ""; //$NON-NLS-1$

		return NLS
				.bind(TeamUIMessages.SaveableCompareEditorInput_0, keyBinding);
	}

	// TODO: add getAdapter for IFile[]

	private class InternalResourceSaveableComparison extends
			LocalResourceSaveableComparison implements
			ISharedDocumentAdapterListener {
		private LocalResourceTypedElement lrte;
		private boolean connected = false;

		public InternalResourceSaveableComparison(ICompareInput input,
				CompareEditorInput editorInput, ITypedElement element) {
			super(input, editorInput, element);
			if (element instanceof LocalResourceTypedElement) {
				lrte = (LocalResourceTypedElement) element;
				if (lrte.isConnected()) {
					registerSaveable(true);
				} else {
					lrte.setSharedDocumentListener(this);
				}
			}
		}

		protected void fireInputChange() {
			SaveablesCompareEditorInput.this.fireInputChange();
		}

		public void dispose() {
			super.dispose();
			if (lrte != null)
				lrte.setSharedDocumentListener(null);
		}

		public void handleDocumentConnected() {
			if (connected)
				return;
			connected = true;
			registerSaveable(false);
			if (lrte != null)
				lrte.setSharedDocumentListener(null);
		}

		private void registerSaveable(boolean init) {
			ICompareContainer container = getContainer();
			IWorkbenchPart part = container.getWorkbenchPart();
			if (part != null) {
				ISaveablesLifecycleListener lifecycleListener = getSaveablesLifecycleListener(part);
				// Remove this saveable from the lifecycle listener
				if (!init)
					lifecycleListener
							.handleLifecycleEvent(new SaveablesLifecycleEvent(
									part, SaveablesLifecycleEvent.POST_CLOSE,
									new Saveable[] { this }, false));
				// Now fix the hashing so it uses the connected document
				initializeHashing();
				// Finally, add this saveable back to the listener
				lifecycleListener
						.handleLifecycleEvent(new SaveablesLifecycleEvent(part,
								SaveablesLifecycleEvent.POST_OPEN,
								new Saveable[] { this }, false));
			}
		}

		public void handleDocumentDeleted() {
			// Ignore
		}

		public void handleDocumentDisconnected() {
			// Ignore
		}

		public void handleDocumentFlushed() {
			// Ignore
		}

		public void handleDocumentSaved() {
			// Ignore
		}

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

			if (!(obj instanceof Saveable))
				return false;
			
			Object document = getAdapter(IDocument.class);
			
			if (document != null) {
				Object otherDocument = ((Saveable) obj)
						.getAdapter(IDocument.class);
				return document.equals(otherDocument);
			}

			if (obj instanceof InternalResourceSaveableComparison) {
				InternalResourceSaveableComparison rscm = (InternalResourceSaveableComparison) obj;
				return rscm.getInput().equals(getInput()) && rscm.lrte.equals(lrte);
			}
			return false;
		}
	}

	public static void handleMenuAboutToShow(IMenuManager manager, ICompareContainer container, Saveable saveable, ITypedElement element, ISelectionProvider provider) {
		if (provider instanceof ITextViewer) {
			final ITextViewer v= (ITextViewer)provider;
			IDocument d= v.getDocument();
			IDocument other= (IDocument)Utils.getAdapter(saveable, IDocument.class);
			if (d == other) {
				if (element instanceof IResourceProvider) {
					IResourceProvider rp= (IResourceProvider)element;
					IResource resource= rp.getResource();
					StructuredSelection selection= new StructuredSelection(resource);
					IWorkbenchPart workbenchPart= container.getWorkbenchPart();
					if (workbenchPart != null) {
						final IWorkbenchSite ws= workbenchPart.getSite();

						MenuManager submenu1= new MenuManager(getShowInMenuLabel());
						IContributionItem showInMenu= ContributionItemFactory.VIEWS_SHOW_IN.create(ws.getWorkbenchWindow());
						submenu1.add(showInMenu);
						manager.insertAfter("file", submenu1); //$NON-NLS-1$
						MenuManager submenu2= new MenuManager(TeamUIMessages.OpenWithActionGroup_0);

						// XXX: Internal reference will get fixed during 3.7, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=307026
						submenu2.add(new OpenWithMenu(ws.getPage(), resource) {
							/*
							 * (non-Javadoc)
							 * 
							 * @see org.eclipse.ui.actions.OpenWithMenu#openEditor(org.eclipse.ui.
							 * IEditorDescriptor, boolean)
							 */
							protected void openEditor(IEditorDescriptor editorDescriptor, boolean openUsingDescriptor) {
								super.openEditor(editorDescriptor, openUsingDescriptor);
								IEditorPart editor= ws.getPage().getActiveEditor();
								Point selectedRange= v.getSelectedRange();
								revealInEditor(editor, selectedRange.x, selectedRange.y);
							}
						});
						manager.insertAfter("file", submenu2); //$NON-NLS-1$

						// XXX: Internal reference will get fixed during 3.7, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=307026
						OpenFileAction openFileAction= new OpenFileAction(ws.getPage()) {
							/*
							 * (non-Javadoc)
							 * 
							 * @see org.eclipse.ui.actions.OpenSystemEditorAction#run()
							 */
							public void run() {
								super.run();
								IEditorPart editor= ws.getPage().getActiveEditor();
								Point selectedRange= v.getSelectedRange();
								revealInEditor(editor, selectedRange.x, selectedRange.y);
							}
						};
						openFileAction.selectionChanged(selection);
						manager.insertAfter("file", openFileAction); //$NON-NLS-1$
					}
				}
			}
		}
	}

	/**
	 * Selects and reveals the given offset and length in the given editor part.
	 * 
	 * @param editor the editor part
	 * @param offset the offset
	 * @param length the length
	 * @since 3.6
	 */
	private static void revealInEditor(IEditorPart editor, final int offset, final int length) {
		if (editor instanceof ITextEditor) {
			((ITextEditor)editor).selectAndReveal(offset, length);
			return;
		}

		// Support for non-text editor - try IGotoMarker interface
		final IGotoMarker gotoMarkerTarget;
		if (editor instanceof IGotoMarker)
			gotoMarkerTarget= (IGotoMarker)editor;
		else
			gotoMarkerTarget= editor != null ? (IGotoMarker)editor.getAdapter(IGotoMarker.class) : null;
		if (gotoMarkerTarget != null) {
			final IEditorInput input= editor.getEditorInput();
			if (input instanceof IFileEditorInput) {
				WorkspaceModifyOperation op= new WorkspaceModifyOperation() {
					protected void execute(IProgressMonitor monitor) throws CoreException {
						IMarker marker= null;
						try {
							marker= ((IFileEditorInput)input).getFile().createMarker(IMarker.TEXT);
							marker.setAttribute(IMarker.CHAR_START, offset);
							marker.setAttribute(IMarker.CHAR_END, offset + length);

							gotoMarkerTarget.gotoMarker(marker);

						} finally {
							if (marker != null)
								marker.delete();
						}
					}
				};

				try {
					op.run(null);
				} catch (InvocationTargetException ex) {
					// reveal failed
				} catch (InterruptedException e) {
					Assert.isTrue(false, "this operation can not be canceled"); //$NON-NLS-1$
				}
			}
			return;
		}
	}

}

Back to the top