Bug 450320 - Add 2-way compare preview to preference page

Change-Id: I7910a963e0b050602eb70a6b06f69a55c5bbc494
Signed-off-by: Mickael Istria <mistria@redhat.com>
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ComparePreferencePage.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ComparePreferencePage.java
index ac215b2..133adb2 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ComparePreferencePage.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ComparePreferencePage.java
@@ -124,7 +124,7 @@
 
 
 	private IPropertyChangeListener fPreferenceChangeListener;
-	private CompareConfiguration fCompareConfiguration;
+	private List<CompareConfiguration> fCompareConfigurations = new ArrayList<>();
 	private OverlayPreferenceStore fOverlayStore;
 	private Map<Button, String> fCheckBoxes = new HashMap<>();
 	private Text fFilters;
@@ -186,8 +186,8 @@
 			String key= event.getProperty();
 			if (key.equals(INITIALLY_SHOW_ANCESTOR_PANE)) {
 				boolean b= fOverlayStore.getBoolean(INITIALLY_SHOW_ANCESTOR_PANE);
-				if (fCompareConfiguration != null) {
-					fCompareConfiguration.setProperty(INITIALLY_SHOW_ANCESTOR_PANE, Boolean.valueOf(b));
+				for (CompareConfiguration compareConfiguration : fCompareConfigurations) {
+					compareConfiguration.setProperty(INITIALLY_SHOW_ANCESTOR_PANE, Boolean.valueOf(b));
 				}
 			}
 		};
@@ -378,43 +378,57 @@
 		editor.fillIntoGrid(radioGroup, 1);
 		editors.add(editor);
 
+		PreferenceLinkArea area = new PreferenceLinkArea(composite, SWT.NONE,
+				"org.eclipse.ui.preferencePages.ColorsAndFonts", //$NON-NLS-1$
+				Utilities.getString("ComparePreferencePage.colorAndFontLink"), //$NON-NLS-1$
+				(IWorkbenchPreferenceContainer) getContainer(),
+				"selectCategory:org.eclipse.compare.contentmergeviewer.TextMergeViewer"); //$NON-NLS-1$
+
+		GridData data = new GridData(SWT.FILL, SWT.CENTER, false, false);
+		area.getControl().setLayoutData(data);
+
 		// a spacer
 		Label separator= new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
 		separator.setVisible(false);
 
-		Label previewLabel= new Label(composite, SWT.NULL);
-		previewLabel.setText(Utilities.getString("ComparePreferencePage.preview.label"));	//$NON-NLS-1$
+		{
+			Label previewLabel = new Label(composite, SWT.NULL);
+			previewLabel.setText(Utilities.getString("ComparePreferencePage.preview-2way.label")); //$NON-NLS-1$
 
-		Control previewer= createPreviewer(composite);
-		GridData gd= new GridData(GridData.FILL_BOTH);
-		gd.widthHint= convertWidthInCharsToPixels(60);
-		gd.heightHint= convertHeightInCharsToPixels(13);
-		previewer.setLayoutData(gd);
+			Control previewer = create2WayPreviewer(composite);
+			GridData gd = new GridData(GridData.FILL_BOTH);
+			gd.widthHint = convertWidthInCharsToPixels(60);
+			gd.heightHint = convertHeightInCharsToPixels(12);
+			previewer.setLayoutData(gd);
+		}
 
-		PreferenceLinkArea area = new PreferenceLinkArea(composite, SWT.NONE,
-				"org.eclipse.ui.preferencePages.ColorsAndFonts", Utilities.getString("ComparePreferencePage.colorAndFontLink"), //$NON-NLS-1$ //$NON-NLS-2$
-				(IWorkbenchPreferenceContainer)getContainer(), "selectCategory:org.eclipse.compare.contentmergeviewer.TextMergeViewer"); //$NON-NLS-1$
+		{
+			Label previewLabel = new Label(composite, SWT.NULL);
+			previewLabel.setText(Utilities.getString("ComparePreferencePage.preview.label")); //$NON-NLS-1$
 
-		GridData data= new GridData(SWT.FILL, SWT.CENTER, false, false);
-		area.getControl().setLayoutData(data);
+			Control previewer = create3WayPreviewer(composite);
+			GridData gd = new GridData(GridData.FILL_BOTH);
+			gd.widthHint = convertWidthInCharsToPixels(60);
+			gd.heightHint = convertHeightInCharsToPixels(9);
+			previewer.setLayoutData(gd);
+		}
 
 		return composite;
 	}
 
-	private Control createPreviewer(Composite parent) {
+	private Control create3WayPreviewer(Composite parent) {
+		CompareConfiguration compareConfiguration = new CompareConfiguration(fOverlayStore);
+		compareConfiguration.setAncestorLabel(Utilities.getString("ComparePreferencePage.ancestor.label")); //$NON-NLS-1$
 
-		fCompareConfiguration= new CompareConfiguration(fOverlayStore);
-		fCompareConfiguration.setAncestorLabel(Utilities.getString("ComparePreferencePage.ancestor.label"));	//$NON-NLS-1$
+		compareConfiguration.setLeftLabel(Utilities.getString("ComparePreferencePage.left.label")); //$NON-NLS-1$
+		compareConfiguration.setLeftEditable(false);
 
-		fCompareConfiguration.setLeftLabel(Utilities.getString("ComparePreferencePage.left.label"));	//$NON-NLS-1$
-		fCompareConfiguration.setLeftEditable(false);
+		compareConfiguration.setRightLabel(Utilities.getString("ComparePreferencePage.right.label")); //$NON-NLS-1$
+		compareConfiguration.setRightEditable(false);
 
-		fCompareConfiguration.setRightLabel(Utilities.getString("ComparePreferencePage.right.label"));	//$NON-NLS-1$
-		fCompareConfiguration.setRightEditable(false);
+		TextMergeViewer previewViewer = new TextMergeViewer(parent, SWT.BORDER, compareConfiguration);
 
-		TextMergeViewer fPreviewViewer= new TextMergeViewer(parent, SWT.BORDER, fCompareConfiguration);
-
-		fPreviewViewer.setInput(
+		previewViewer.setInput(
 			new DiffNode(Differencer.CONFLICTING,
 				new FakeInput("ComparePreferencePage.previewAncestor"),	//$NON-NLS-1$
 				new FakeInput("ComparePreferencePage.previewLeft"),	//$NON-NLS-1$
@@ -422,15 +436,39 @@
 			)
 		);
 
-		Control c= fPreviewViewer.getControl();
+		Control c = previewViewer.getControl();
 		c.addDisposeListener(e -> {
-			if (fCompareConfiguration != null)
-				fCompareConfiguration.dispose();
+			if (compareConfiguration != null)
+				compareConfiguration.dispose();
 		});
 
+		fCompareConfigurations.add(compareConfiguration);
 		return  c;
 	}
 
+	private Control create2WayPreviewer(Composite parent) {
+		CompareConfiguration compareConfiguration = new CompareConfiguration(fOverlayStore);
+		compareConfiguration.setLeftLabel(Utilities.getString("ComparePreferencePage.workingCopy.label")); //$NON-NLS-1$
+		compareConfiguration.setRightLabel(Utilities.getString("ComparePreferencePage.baseline.label")); //$NON-NLS-1$
+		compareConfiguration.setRightEditable(false);
+
+		TextMergeViewer previewViewer = new TextMergeViewer(parent, SWT.BORDER, compareConfiguration);
+
+		previewViewer.setInput(new DiffNode(
+				new FakeInput("ComparePreferencePage.workingCopy"), //$NON-NLS-1$
+				new FakeInput("ComparePreferencePage.baseline") //$NON-NLS-1$
+		));
+
+		Control c = previewViewer.getControl();
+		c.addDisposeListener(e -> {
+			if (compareConfiguration != null)
+				compareConfiguration.dispose();
+		});
+
+		fCompareConfigurations.add(compareConfiguration);
+		return c;
+	}
+
 	private void initializeFields() {
 
 		Iterator<Button> e = fCheckBoxes.keySet().iterator();
diff --git a/bundles/org.eclipse.compare/plugin.properties b/bundles/org.eclipse.compare/plugin.properties
index 041d520..8bb9f80 100644
--- a/bundles/org.eclipse.compare/plugin.properties
+++ b/bundles/org.eclipse.compare/plugin.properties
@@ -137,18 +137,23 @@
 ComparePreferencePage.disableCapping.label=Disable &capping when comparing large documents
 ComparePreferencePage.swapped.label=S&wap left and right
 
-ComparePreferencePage.preview.label= Pre&view:
+ComparePreferencePage.preview.label= Pre&view 3-way compare:
+ComparePreferencePage.preview-2way.label= Preview 2-w&ay compare:
 ComparePreferencePage.ancestor.label= Common Ancestor
 ComparePreferencePage.left.label= Local
 ComparePreferencePage.right.label= Remote
 ComparePreferencePage.backgroundColor1.label= Bac&kground Color:
 ComparePreferencePage.backgroundColor2.label= S&ystem Default
 ComparePreferencePage.backgroundColor3.label= C&ustom
+ComparePreferencePage.workingCopy.label= Working Copy
+ComparePreferencePage.baseline.label= Baseline
 
 # Compare editor contents for Compare preview page
 ComparePreferencePage.previewAncestor= 1\n2\n3\nconflicting change\n4\n5
 ComparePreferencePage.previewLeft= 1\n2\noutgoing addition\n3\noutgoing change\n4\npseudo conflict\n5
 ComparePreferencePage.previewRight= 1\nincoming addition\nincoming addition\n2\n3\nincoming change\n4\npseudo conflict\n5
+ComparePreferencePage.baseline= \nline 1\nline 2\nremoved line 2a\nremoved line 2b\nline 3\nline 4\nline 5\nline 6\nline to modify 7\nline 8\nremoved words in line 9\n
+ComparePreferencePage.workingCopy= line 1\nline 2\nline 3\nadded words in line 4\nline 5\nadded line 5a\nadded line 6b\nline 6\nmodified line 7\nline 8\nline 9
 
 ComparePreferencePage.colorAndFontLink=See <a>''{0}''</a> preferences for text compare colors and fonts.