Skip to content

Commit af84431

Browse files
lathapatililoveeclipse
authored andcommitted
Show number of differences in the Compare editor #504
Review points incorporated Fixes #504
1 parent 07198c4 commit af84431

File tree

7 files changed

+244
-10
lines changed

7 files changed

+244
-10
lines changed

team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.compare; singleton:=true
5-
Bundle-Version: 3.9.400.qualifier
5+
Bundle-Version: 3.10.0.qualifier
66
Bundle-Activator: org.eclipse.compare.internal.CompareUIPlugin
77
Bundle-Vendor: %providerName
88
Bundle-Localization: plugin
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 ETAS GmbH and others, all rights reserved.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* ETAS GmbH - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.compare;
16+
17+
import org.eclipse.core.runtime.Platform.OS;
18+
import org.eclipse.jface.action.ControlContribution;
19+
import org.eclipse.swt.SWT;
20+
import org.eclipse.swt.layout.GridData;
21+
import org.eclipse.swt.layout.GridLayout;
22+
import org.eclipse.swt.widgets.Composite;
23+
import org.eclipse.swt.widgets.Control;
24+
import org.eclipse.swt.widgets.Label;
25+
26+
27+
/**
28+
* @since 3.10
29+
*
30+
* A contribution item which delegates to a label on the tool bar.
31+
*
32+
*/
33+
public class LabelContributionItem extends ControlContribution {
34+
35+
private Label toolbarLabel;
36+
private String labelName;
37+
38+
/**
39+
* @param id
40+
* @param name
41+
*/
42+
public LabelContributionItem(String id, String name) {
43+
super(id);
44+
this.labelName = name;
45+
}
46+
47+
@Override
48+
protected Control createControl(Composite parent) {
49+
Composite composite = new Composite(parent, SWT.NONE);
50+
this.toolbarLabel = new Label(composite, SWT.NONE);
51+
52+
GridLayout compositeLayout = new GridLayout(1, false);
53+
if (OS.isWindows()) {
54+
compositeLayout.marginTop = -3;
55+
compositeLayout.marginBottom = -6;
56+
}
57+
composite.setLayout(compositeLayout);
58+
GridData labelLayout = new GridData(SWT.LEFT, SWT.CENTER, true, true);
59+
60+
this.toolbarLabel.setLayoutData(labelLayout);
61+
this.toolbarLabel.setText(this.labelName);
62+
return composite;
63+
}
64+
65+
public Label getToolbarLabel() {
66+
return toolbarLabel;
67+
}
68+
}

team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/ContentMergeViewer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 IBM Corporation and others.
2+
* Copyright (c) 2000, 2023 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -13,6 +13,7 @@
1313
* Alex Blewitt <[email protected]> - replace new Boolean with Boolean.valueOf - https://bugs.eclipse.org/470344
1414
* Stefan Xenos <[email protected]> (Google) - bug 448968 - Add diagnostic logging
1515
* Conrad Groth - Bug 213780 - Compare With direction should be configurable
16+
* Latha Patil (ETAS GmbH) - Issue #504 Show number of differences in the Compare editor
1617
*******************************************************************************/
1718

1819
package org.eclipse.compare.contentmergeviewer;
@@ -795,7 +796,7 @@ private void internalRefresh(Object input) {
795796
updateHeader();
796797
if (Utilities.okToUse(fComposite) && Utilities.okToUse(fComposite.getParent())) {
797798
ToolBarManager tbm = (ToolBarManager) getToolBarManager(fComposite.getParent());
798-
if (tbm != null ) {
799+
if (tbm != null) {
799800
updateToolItems();
800801
tbm.update(true);
801802
tbm.getControl().getParent().layout(true);
@@ -897,6 +898,7 @@ private void initializeToolbars(Composite parent) {
897898
tbm.removeAll();
898899

899900
// Define groups.
901+
tbm.add(new Separator("diffLabel")); //$NON-NLS-1$
900902
tbm.add(new Separator("modes")); //$NON-NLS-1$
901903
tbm.add(new Separator("merge")); //$NON-NLS-1$
902904
tbm.add(new Separator("navigation")); //$NON-NLS-1$

team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2019 IBM Corporation and others.
2+
* Copyright (c) 2000, 2023 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -21,6 +21,7 @@
2121
* Robin Stocker ([email protected]) - Bug 399960: [Edit] Make merge arrow buttons easier to hit
2222
* John Hendrikx ([email protected]) - Bug 541401 - [regression] Vertical scrollbar thumb size is wrong in compare view
2323
* Stefan Dirix ([email protected]) - Bug 473847: Minimum E4 Compatibility of Compare
24+
* Latha Patil (ETAS GmbH) - Issue #504 Show number of differences in the Compare editor
2425
*******************************************************************************/
2526
package org.eclipse.compare.contentmergeviewer;
2627

@@ -45,6 +46,7 @@
4546
import org.eclipse.compare.ISharedDocumentAdapter;
4647
import org.eclipse.compare.IStreamContentAccessor;
4748
import org.eclipse.compare.ITypedElement;
49+
import org.eclipse.compare.LabelContributionItem;
4850
import org.eclipse.compare.SharedDocumentAdapter;
4951
import org.eclipse.compare.internal.ChangeCompareFilterPropertyAction;
5052
import org.eclipse.compare.internal.ChangePropertyAction;
@@ -175,6 +177,7 @@
175177
import org.eclipse.swt.widgets.Listener;
176178
import org.eclipse.swt.widgets.ScrollBar;
177179
import org.eclipse.swt.widgets.Shell;
180+
import org.eclipse.swt.widgets.ToolBar;
178181
import org.eclipse.swt.widgets.TypedListener;
179182
import org.eclipse.ui.IEditorInput;
180183
import org.eclipse.ui.IEditorSite;
@@ -5408,6 +5411,34 @@ private boolean isIgnoreAncestor() {
54085411

54095412
updateVScrollBar();
54105413
updatePresentation();
5414+
updateToolbarLabel();
5415+
}
5416+
5417+
private void updateToolbarLabel() {
5418+
final String DIFF_COUNT_ID = "DiffCount"; //$NON-NLS-1$
5419+
ToolBarManager tbm =
5420+
(ToolBarManager) getToolBarManager(fComposite.getParent());
5421+
int differenceCount = fMerger.changesCount();
5422+
if (tbm != null) {
5423+
5424+
String label = differenceCount > 1 ? differenceCount + " Differences" //$NON-NLS-1$
5425+
: differenceCount == 1 ? differenceCount + " Difference" : "No Difference"; //$NON-NLS-1$ //$NON-NLS-2$
5426+
LabelContributionItem labelContributionItem = new LabelContributionItem(DIFF_COUNT_ID,
5427+
label);
5428+
5429+
if (tbm.find(DIFF_COUNT_ID) != null) {
5430+
tbm.replaceItem(DIFF_COUNT_ID, labelContributionItem);
5431+
} else {
5432+
tbm.appendToGroup("diffLabel", labelContributionItem); //$NON-NLS-1$
5433+
}
5434+
fComposite.getDisplay().asyncExec(() -> {
5435+
// relayout in next tick
5436+
ToolBar control = tbm.getControl();
5437+
if (control != null && !control.isDisposed()) {
5438+
tbm.update(true);
5439+
}
5440+
});
5441+
}
54115442
}
54125443

54135444
private void resetDiffs() {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package testPackage;
2+
import java.math.*;
3+
public class Javaclass1 {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
8+
int a=0;
9+
10+
System.out.println("");
11+
12+
call_me();
13+
14+
15+
}
16+
17+
private static void call_me() {
18+
// TODO Auto-generated method stub
19+
System.out.println();
20+
21+
}
22+
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package testPackage;
2+
3+
import java.io.File;
4+
5+
public class Javaclass1 {
6+
7+
public static void main(String[] args) {
8+
9+
10+
int a=0;
11+
12+
System.out.println("");
13+
14+
call_me();
15+
16+
callMe(a);
17+
18+
}
19+
20+
private static void callMe(int a) {
21+
// TODO Auto-generated method stub
22+
System.out.println();
23+
24+
}
25+
26+
private static void call_me() {
27+
File f= new File("");
28+
System.out.println("I am calledJavaclass1.java");
29+
}
30+
31+
}

team/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/TextMergeViewerTest.java

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2006, 2018 IBM Corporation and others.
2+
* Copyright (c) 2006, 2023 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
1010
*
1111
* Contributors:
1212
* IBM Corporation - initial API and implementation
13+
* Latha Patil (ETAS GmbH) - Issue #504 Show number of differences in the Compare editor
1314
*******************************************************************************/
1415
package org.eclipse.compare.tests;
1516

@@ -18,21 +19,46 @@
1819
import static org.junit.Assert.assertNull;
1920
import static org.junit.Assert.assertTrue;
2021

21-
import java.io.*;
22+
import java.io.ByteArrayInputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.net.URL;
2226
import java.util.HashMap;
2327
import java.util.Map;
2428

25-
import org.eclipse.compare.*;
29+
import org.eclipse.compare.CompareConfiguration;
30+
import org.eclipse.compare.CompareViewerPane;
31+
import org.eclipse.compare.ICompareFilter;
32+
import org.eclipse.compare.IEditableContent;
33+
import org.eclipse.compare.IStreamContentAccessor;
34+
import org.eclipse.compare.ITypedElement;
35+
import org.eclipse.compare.LabelContributionItem;
2636
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
27-
import org.eclipse.compare.internal.*;
37+
import org.eclipse.compare.internal.ChangeCompareFilterPropertyAction;
38+
import org.eclipse.compare.internal.IMergeViewerTestAdapter;
39+
import org.eclipse.compare.internal.MergeViewerContentProvider;
40+
import org.eclipse.compare.internal.Utilities;
2841
import org.eclipse.compare.structuremergeviewer.DiffNode;
2942
import org.eclipse.compare.structuremergeviewer.Differencer;
3043
import org.eclipse.core.runtime.CoreException;
44+
import org.eclipse.core.runtime.IPath;
3145
import org.eclipse.core.runtime.NullProgressMonitor;
46+
import org.eclipse.jface.action.IContributionItem;
47+
import org.eclipse.jface.action.ToolBarManager;
3248
import org.eclipse.jface.dialogs.Dialog;
33-
import org.eclipse.jface.text.*;
49+
import org.eclipse.jface.text.Document;
50+
import org.eclipse.jface.text.DocumentEvent;
51+
import org.eclipse.jface.text.IDocument;
52+
import org.eclipse.jface.text.IDocumentPartitioner;
53+
import org.eclipse.jface.text.IRegion;
54+
import org.eclipse.jface.text.ITypedRegion;
55+
import org.eclipse.jface.text.Region;
56+
import org.eclipse.swt.SWT;
3457
import org.eclipse.swt.graphics.Image;
35-
import org.eclipse.swt.widgets.*;
58+
import org.eclipse.swt.widgets.Composite;
59+
import org.eclipse.swt.widgets.Control;
60+
import org.eclipse.swt.widgets.Display;
61+
import org.eclipse.swt.widgets.Shell;
3662
import org.eclipse.ui.PlatformUI;
3763
import org.junit.Test;
3864

@@ -480,6 +506,59 @@ public String getType() {
480506
assertNotNull(rightDoc.getDocumentPartitioner());
481507
}
482508

509+
@Test
510+
public void testToolbarLabelContribution() throws Exception {
511+
512+
IPath path = IPath.fromOSString("labelContributionData/" + "file1.java");
513+
URL url = new URL(CompareTestPlugin.getDefault().getBundle().getEntry("/"), path.toString());
514+
515+
IPath path1= IPath.fromOSString("labelContributionData/" + "file2.java");
516+
URL url1 = new URL(CompareTestPlugin.getDefault().getBundle().getEntry("/"), path1.toString());
517+
518+
DiffNode parentNode = new DiffNode(new ParentTestElement(), new ParentTestElement());
519+
DiffNode testNode = new DiffNode(parentNode, Differencer.CHANGE, null, new EditableTestElement(url.openStream().readAllBytes()), new EditableTestElement(url1.openStream().readAllBytes()));
520+
521+
runInDialogWithToolbarDiffLabel(testNode);
522+
}
523+
524+
CompareViewerPane fCompareViewerPane;
525+
private void runInDialogWithToolbarDiffLabel(DiffNode testNode) throws Exception {
526+
527+
CompareConfiguration compareConfig = new CompareConfiguration();
528+
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
529+
Dialog dialog = new Dialog(shell) {
530+
@Override
531+
protected Control createDialogArea(Composite parent) {
532+
Composite composite = (Composite) super.createDialogArea(parent);
533+
fCompareViewerPane = new CompareViewerPane(composite, SWT.BORDER | SWT.FLAT);
534+
composite.getChildren();
535+
viewer = new TestMergeViewer(fCompareViewerPane, compareConfig);
536+
return composite;
537+
}
538+
};
539+
dialog.setBlockOnOpen(false);
540+
dialog.open();
541+
viewer.setInput(testNode);
542+
fCompareViewerPane.setContent(viewer.getControl());
543+
ToolBarManager toolbarManager = CompareViewerPane.getToolBarManager(fCompareViewerPane);
544+
545+
processQueuedEvents();
546+
547+
IContributionItem contributionItem = toolbarManager.find("DiffCount");
548+
assertNotNull(contributionItem);
549+
LabelContributionItem labelContributionItem=(LabelContributionItem) contributionItem;
550+
assertTrue(labelContributionItem.getToolbarLabel().getText().equals("7 Differences"));
551+
552+
dialog.close();
553+
viewer = null;
554+
}
555+
556+
private void processQueuedEvents() {
557+
while (Display.getCurrent().readAndDispatch()) {
558+
// Process all the events in the queue
559+
}
560+
561+
}
483562

484563
private void runInDialogWithPartioner(Object input, Runnable runnable, final CompareConfiguration cc) throws Exception {
485564
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

0 commit comments

Comments
 (0)