From c611ac8e88382cdb25e2d644588b37ff77f5ffd0 Mon Sep 17 00:00:00 2001 From: "APAC\\LPT5KOR" Date: Fri, 6 Oct 2023 14:53:33 +0530 Subject: [PATCH] Show number of differences in the Compare editor #504 Show number of differences (Ex: 6 Differences) in the toolbar of compare editor which matches "Change markers" next to the scroll bar in compare editor Fixes https://github.com/eclipse-platform/eclipse.platform/issues/504 --- .../org.eclipse.compare/META-INF/MANIFEST.MF | 2 +- .../compare/LabelContributionItem.java | 66 +++++++++++++++++++ .../ContentMergeViewer.java | 26 +++++++- .../contentmergeviewer/TextMergeViewer.java | 7 +- .../labelContributionData/file1.java | 23 +++++++ .../labelContributionData/file2.java | 31 +++++++++ .../compare/tests/TextMergeViewerTest.java | 58 +++++++++++++++- 7 files changed, 207 insertions(+), 6 deletions(-) create mode 100644 team/bundles/org.eclipse.compare/compare/org/eclipse/compare/LabelContributionItem.java create mode 100644 team/tests/org.eclipse.compare.tests/labelContributionData/file1.java create mode 100644 team/tests/org.eclipse.compare.tests/labelContributionData/file2.java diff --git a/team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF b/team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF index 118eb00377d..a7d1aa49b0e 100644 --- a/team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF +++ b/team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.compare; singleton:=true -Bundle-Version: 3.9.300.qualifier +Bundle-Version: 3.10.0.qualifier Bundle-Activator: org.eclipse.compare.internal.CompareUIPlugin Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/LabelContributionItem.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/LabelContributionItem.java new file mode 100644 index 00000000000..a3c5f99d3f2 --- /dev/null +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/LabelContributionItem.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) ETAS GmbH 2023, 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 + * + * Contributors: + * ETAS GmbH - initial API and implementation + *******************************************************************************/ + +package org.eclipse.compare; + +import org.eclipse.jface.action.ControlContribution; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; + + +/** + * @since 3.10 + * + * A contribution item which delegates to a label on the tool bar. + * + */ +public class LabelContributionItem extends ControlContribution { + + private Label toolbarLabel; + private String labelName; + + /** + * @param id + * @param name + */ + public LabelContributionItem(String id, String name) { + super(id); + this.labelName = name; + } + + @Override + protected Control createControl(Composite parent) { + Composite composite = new Composite(parent, SWT.LEFT); + this.toolbarLabel = new Label(composite, SWT.LEFT); + + GridLayout compositeLayout = new GridLayout(1, false); + compositeLayout.marginTop = -3; + compositeLayout.marginBottom = -3; + composite.setLayout(compositeLayout); + GridData labelLayout = new GridData(SWT.LEFT, SWT.BOTTOM, true, true); + + this.toolbarLabel.setLayoutData(labelLayout); + this.toolbarLabel.setText(this.labelName); + + return composite; + } + + public Label getToolbarLabel() { + return toolbarLabel; + } +} diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java index e0a0c2064da..b7565473dc9 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corporation and others. + * Copyright (c) 2000, 2023 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -13,6 +13,7 @@ * Alex Blewitt - replace new Boolean with Boolean.valueOf - https://bugs.eclipse.org/470344 * Stefan Xenos (Google) - bug 448968 - Add diagnostic logging * Conrad Groth - Bug 213780 - Compare With direction should be configurable + * Latha Patil (ETAS GmbH) - Issue #504 Show number of differences in the Compare editor *******************************************************************************/ package org.eclipse.compare.contentmergeviewer; @@ -27,6 +28,7 @@ import org.eclipse.compare.ICompareContainer; import org.eclipse.compare.ICompareInputLabelProvider; import org.eclipse.compare.IPropertyChangeNotifier; +import org.eclipse.compare.LabelContributionItem; import org.eclipse.compare.internal.ChangePropertyAction; import org.eclipse.compare.internal.CompareEditor; import org.eclipse.compare.internal.CompareHandlerService; @@ -364,6 +366,7 @@ private void resize(MouseEvent e) { private Cursor fHSashCursor; private Cursor fVSashCursor; private Cursor fHVSashCursor; + private int documentDiffCount; private final ILabelProviderListener labelChangeListener = event -> { Object[] elements = event.getElements(); @@ -778,6 +781,7 @@ public void refresh() { } private void internalRefresh(Object input) { + final String DIFF_COUNT_ID = "DiffCount"; //$NON-NLS-1$ IMergeViewerContentProvider content= getMergeContentProvider(); if (content != null) { Object ancestor= content.getAncestorContent(input); @@ -803,7 +807,16 @@ private void internalRefresh(Object input) { updateHeader(); if (Utilities.okToUse(fComposite) && Utilities.okToUse(fComposite.getParent())) { ToolBarManager tbm = (ToolBarManager) getToolBarManager(fComposite.getParent()); - if (tbm != null ) { + if (tbm != null) { + String label = documentDiffCount > 1 ? " Differences" : " Difference"; //$NON-NLS-1$ //$NON-NLS-2$ + LabelContributionItem labelContributionItem = new LabelContributionItem(DIFF_COUNT_ID, + documentDiffCount + label); + + if (tbm.find(DIFF_COUNT_ID) != null) { + tbm.replaceItem(DIFF_COUNT_ID, labelContributionItem); + } else { + tbm.appendToGroup("diffLabel", labelContributionItem); //$NON-NLS-1$ + } updateToolItems(); Display.getDefault().asyncExec(() -> { // relayout in next tick @@ -909,6 +922,7 @@ private void initializeToolbars(Composite parent) { tbm.removeAll(); // Define groups. + tbm.add(new Separator("diffLabel")); //$NON-NLS-1$ tbm.add(new Separator("modes")); //$NON-NLS-1$ tbm.add(new Separator("merge")); //$NON-NLS-1$ tbm.add(new Separator("navigation")); //$NON-NLS-1$ @@ -1451,4 +1465,12 @@ protected boolean isLeftEditable() { protected boolean isRightEditable() { return fCompareConfiguration.isMirrored() ? fCompareConfiguration.isLeftEditable() : fCompareConfiguration.isRightEditable(); } + + /** + * @param docDiffCount - current number of differences in the compare editor + * @since 3.10 + */ + protected void setDocumentDiffCount(int docDiffCount) { + documentDiffCount = docDiffCount; + } } diff --git a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java index d6a96bdd8a6..696b06d0eaf 100644 --- a/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java +++ b/team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2019 IBM Corporation and others. + * Copyright (c) 2000, 2023 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -21,6 +21,7 @@ * Robin Stocker (robin@nibor.org) - Bug 399960: [Edit] Make merge arrow buttons easier to hit * John Hendrikx (hjohn@xs4all.nl) - Bug 541401 - [regression] Vertical scrollbar thumb size is wrong in compare view * Stefan Dirix (sdirix@eclipsesource.com) - Bug 473847: Minimum E4 Compatibility of Compare + * Latha Patil (ETAS GmbH) - Issue #504 Show number of differences in the Compare editor *******************************************************************************/ package org.eclipse.compare.contentmergeviewer; @@ -3059,7 +3060,9 @@ protected void updateContent(Object ancestor, Object left, Object right) { setSyncScrolling(fPreferenceStore.getBoolean(ComparePreferencePage.SYNCHRONIZE_SCROLLING)); update(false); - + if (fMerger!=null) { + setDocumentDiffCount(fMerger.changesCount()); + } if (!fHasErrors && !emptyInput && !fComposite.isDisposed()) { if (isRefreshing()) { fLeftContributor.updateSelection(fLeft, !fSynchronizedScrolling); diff --git a/team/tests/org.eclipse.compare.tests/labelContributionData/file1.java b/team/tests/org.eclipse.compare.tests/labelContributionData/file1.java new file mode 100644 index 00000000000..148d2929486 --- /dev/null +++ b/team/tests/org.eclipse.compare.tests/labelContributionData/file1.java @@ -0,0 +1,23 @@ +package testPackage; +import java.math.*; +public class Javaclass1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + int a=0; + + System.out.println(""); + + call_me(); + + + } + + private static void call_me() { + // TODO Auto-generated method stub + System.out.println(); + + } + +} diff --git a/team/tests/org.eclipse.compare.tests/labelContributionData/file2.java b/team/tests/org.eclipse.compare.tests/labelContributionData/file2.java new file mode 100644 index 00000000000..47ea67a80b9 --- /dev/null +++ b/team/tests/org.eclipse.compare.tests/labelContributionData/file2.java @@ -0,0 +1,31 @@ +package testPackage; + +import java.io.File; + +public class Javaclass1 { + + public static void main(String[] args) { + + + int a=0; + + System.out.println(""); + + call_me(); + + callMe(a); + + } + + private static void callMe(int a) { + // TODO Auto-generated method stub + System.out.println(); + + } + + private static void call_me() { + File f= new File(""); + System.out.println("I am calledJavaclass1.java"); + } + +} diff --git a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java index 08ceb89438b..a5135938a80 100644 --- a/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java +++ b/team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2018 IBM Corporation and others. + * Copyright (c) 2006, 2023 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Latha Patil (ETAS GmbH) - Issue #504 Show number of differences in the Compare editor *******************************************************************************/ package org.eclipse.compare.tests; @@ -28,13 +29,18 @@ import org.eclipse.compare.structuremergeviewer.DiffNode; import org.eclipse.compare.structuremergeviewer.Differencer; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import java.net.URL; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jface.action.IContributionItem; +import org.eclipse.jface.action.ToolBarManager; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.text.*; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.*; import org.eclipse.ui.PlatformUI; import org.junit.Test; +import org.eclipse.swt.SWT; public class TextMergeViewerTest { @@ -480,6 +486,56 @@ public String getType() { assertNotNull(rightDoc.getDocumentPartitioner()); } + @Test + public void testToolbarLabelContribution() throws Exception { + + IPath path = IPath.fromOSString("labelContributionData/" + "file1.java"); + URL url = new URL(CompareTestPlugin.getDefault().getBundle().getEntry("/"), path.toString()); + + IPath path1= IPath.fromOSString("labelContributionData/" + "file2.java"); + URL url1 = new URL(CompareTestPlugin.getDefault().getBundle().getEntry("/"), path1.toString()); + + DiffNode parentNode = new DiffNode(new ParentTestElement(), new ParentTestElement()); + DiffNode testNode = new DiffNode(parentNode, Differencer.CHANGE, null, new EditableTestElement(url.openStream().readAllBytes()), new EditableTestElement(url1.openStream().readAllBytes())); + + runInDialogWithToolbarDiffLabel(testNode, () -> { + //Not required + }); + } + + CompareViewerPane fCompareViewerPane; + private void runInDialogWithToolbarDiffLabel(DiffNode testNode, Runnable runnable) throws Exception { + + CompareConfiguration compareConfig = new CompareConfiguration(); + Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); + Dialog dialog = new Dialog(shell) { + @Override + protected Control createDialogArea(Composite parent) { + Composite composite = (Composite) super.createDialogArea(parent); + fCompareViewerPane = new CompareViewerPane(composite, SWT.BORDER | SWT.FLAT); + composite.getChildren(); + viewer = new TestMergeViewer(fCompareViewerPane, compareConfig); + return composite; + } + }; + dialog.setBlockOnOpen(false); + dialog.open(); + viewer.setInput(testNode); + fCompareViewerPane.setContent(viewer.getControl()); + ToolBarManager toolbarManager = CompareViewerPane.getToolBarManager(fCompareViewerPane); + + IContributionItem contributionItem = toolbarManager.find("DiffCount"); + assertNotNull(contributionItem); + LabelContributionItem labelContributionItem=(LabelContributionItem) contributionItem; + assertTrue(labelContributionItem.getToolbarLabel().getText().equals("7 Differences")); + try { + runnable.run(); + } catch (WrappedException e) { + e.throwException(); + } + dialog.close(); + viewer = null; + } private void runInDialogWithPartioner(Object input, Runnable runnable, final CompareConfiguration cc) throws Exception { Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();