Skip to content

Commit

Permalink
Show number of differences in the Compare editor eclipse-platform#504
Browse files Browse the repository at this point in the history
Review points incorporated

Fixes eclipse-platform#504
  • Loading branch information
lathapatil authored and iloveeclipse committed Dec 21, 2023
1 parent 07198c4 commit af84431
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 10 deletions.
2 changes: 1 addition & 1 deletion team/bundles/org.eclipse.compare/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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.400.qualifier
Bundle-Version: 3.10.0.qualifier
Bundle-Activator: org.eclipse.compare.internal.CompareUIPlugin
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2023 ETAS GmbH and others, 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.core.runtime.Platform.OS;
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.NONE);
this.toolbarLabel = new Label(composite, SWT.NONE);

GridLayout compositeLayout = new GridLayout(1, false);
if (OS.isWindows()) {
compositeLayout.marginTop = -3;
compositeLayout.marginBottom = -6;
}
composite.setLayout(compositeLayout);
GridData labelLayout = new GridData(SWT.LEFT, SWT.CENTER, true, true);

this.toolbarLabel.setLayoutData(labelLayout);
this.toolbarLabel.setText(this.labelName);
return composite;
}

public Label getToolbarLabel() {
return toolbarLabel;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +13,7 @@
* Alex Blewitt <alex.blewitt@gmail.com> - replace new Boolean with Boolean.valueOf - https://bugs.eclipse.org/470344
* Stefan Xenos <sxenos@gmail.com> (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;
Expand Down Expand Up @@ -795,7 +796,7 @@ 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) {
updateToolItems();
tbm.update(true);
tbm.getControl().getParent().layout(true);
Expand Down Expand Up @@ -897,6 +898,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$
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand All @@ -45,6 +46,7 @@
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.LabelContributionItem;
import org.eclipse.compare.SharedDocumentAdapter;
import org.eclipse.compare.internal.ChangeCompareFilterPropertyAction;
import org.eclipse.compare.internal.ChangePropertyAction;
Expand Down Expand Up @@ -175,6 +177,7 @@
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.TypedListener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
Expand Down Expand Up @@ -5408,6 +5411,34 @@ private boolean isIgnoreAncestor() {

updateVScrollBar();
updatePresentation();
updateToolbarLabel();
}

private void updateToolbarLabel() {
final String DIFF_COUNT_ID = "DiffCount"; //$NON-NLS-1$
ToolBarManager tbm =
(ToolBarManager) getToolBarManager(fComposite.getParent());
int differenceCount = fMerger.changesCount();
if (tbm != null) {

String label = differenceCount > 1 ? differenceCount + " Differences" //$NON-NLS-1$
: differenceCount == 1 ? differenceCount + " Difference" : "No Difference"; //$NON-NLS-1$ //$NON-NLS-2$
LabelContributionItem labelContributionItem = new LabelContributionItem(DIFF_COUNT_ID,
label);

if (tbm.find(DIFF_COUNT_ID) != null) {
tbm.replaceItem(DIFF_COUNT_ID, labelContributionItem);
} else {
tbm.appendToGroup("diffLabel", labelContributionItem); //$NON-NLS-1$
}
fComposite.getDisplay().asyncExec(() -> {
// relayout in next tick
ToolBar control = tbm.getControl();
if (control != null && !control.isDisposed()) {
tbm.update(true);
}
});
}
}

private void resetDiffs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}

}
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand All @@ -18,21 +19,46 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.compare.*;
import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareViewerPane;
import org.eclipse.compare.ICompareFilter;
import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.LabelContributionItem;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.compare.internal.*;
import org.eclipse.compare.internal.ChangeCompareFilterPropertyAction;
import org.eclipse.compare.internal.IMergeViewerTestAdapter;
import org.eclipse.compare.internal.MergeViewerContentProvider;
import org.eclipse.compare.internal.Utilities;
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 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.jface.text.Document;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.junit.Test;

Expand Down Expand Up @@ -480,6 +506,59 @@ 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);
}

CompareViewerPane fCompareViewerPane;
private void runInDialogWithToolbarDiffLabel(DiffNode testNode) 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);

processQueuedEvents();

IContributionItem contributionItem = toolbarManager.find("DiffCount");
assertNotNull(contributionItem);
LabelContributionItem labelContributionItem=(LabelContributionItem) contributionItem;
assertTrue(labelContributionItem.getToolbarLabel().getText().equals("7 Differences"));

dialog.close();
viewer = null;
}

private void processQueuedEvents() {
while (Display.getCurrent().readAndDispatch()) {
// Process all the events in the queue
}

}

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

0 comments on commit af84431

Please sign in to comment.