Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Best way to compare 2 pieces of text
Best way to compare 2 pieces of text [message #294266] Tue, 08 November 2005 09:40 Go to next message
Eclipse UserFriend
Hi,

Can someone tell me which is the best way to compare 2 pieces of text
and display it on a compare editor, like how the "Compare" functionality
of eclipse does, when 2 resources are selected...I have been stuck in this
for 2 days, It can be like comparing 2 pieces of text/files or
anything...i will change the text that needs to be compared into any
resources required for the implementation.

thanks

Vijai
Re: Best way to compare 2 pieces of text [message #294476 is a reply to message #294266] Sat, 12 November 2005 11:57 Go to previous messageGo to next message
Eclipse UserFriend
Hi Vijai,

using the compare functionality is really hard and took me much more than
two days. Methods which expect "Object" as parameter are not really useful.
No documentation which helps you to put the pieces together...

Here's a short description how I succeeded:
- Download eclipse-examples-2.1.3-win32.zip (there may be an updated version
around there)
- Try out the project "org.eclipse.compare.examples"

To get more into, look at the code of the package
"org.eclipse.jdt.internal.ui.refactoring". I don't remember exactly where to
start...

Here's an abstract of my code. Hope that helps:

-- snip --
class PreviewWizardPage extends WizardPage {
public void createControl(Composite parent) {
Composite result= new Composite(parent, SWT.NONE);
...
SashForm sashForm= new SashForm(result, SWT.VERTICAL);
...
ViewerPane pane= new ViewerPane(sashForm, SWT.BORDER | SWT.FLAT);
...
fCheckboxTableViewer = new CheckboxTableViewer(pane);
...
pane.setContent(fCheckboxTableViewer.getControl());
fPreviewContainer = new PageBook(sashForm, SWT.NONE);
fComparePreview = new ComparePreviewer(fPreviewContainer, false,
true);
...
return result;
}

/** Called by a wizard */
public void setInput(ChangeElement changeElement) {
fComparePreview.setInput(changeElement);
fPreviewContainer.showPage(fComparePreview.getControl());
}
}

class ComparePreviewer extends CompareViewerSwitchingPane {
public ComparePreviewer(Composite parent, boolean leftEditable, boolean
rightEditable) {
super(parent, SWT.BORDER | SWT.FLAT, true);

fCompareConfiguration= new CompareConfiguration();
fCompareConfiguration.setLeftEditable(leftEditable);
fCompareConfiguration.setLeftLabel(
CompareMessagesUtil.getString("ComparePreviewer.original_source "));
//$NON-NLS-1$
fCompareConfiguration.setRightEditable(rightEditable);
fCompareConfiguration.setRightLabel(
CompareMessagesUtil.getString("ComparePreviewer.refactored_source "));
//$NON-NLS-1$
}

public Control getControl() {
return this;
}

/**
* Called by {@link
org.eclipse.compare.CompareViewerSwitchingPane#setInput(Obje ct input)}
*
* @see
org.eclipse.compare.CompareViewerSwitchingPane#getViewer(org .eclipse.jface.viewers.Viewer,
java.lang.Object)
*/
protected Viewer getViewer(Viewer oldViewer, Object input) {
Viewer mergeViewer = CompareUI.findContentViewer(oldViewer,
(ICompareInput)input, this, fCompareConfiguration);
if (mergeViewer instanceof ContentMergeViewer) {
//((ContentMergeViewer)mergeViewer).addPropertyChangeListene r(this);
((ContentMergeViewer)mergeViewer).setConfirmSave(true);
}

return mergeViewer;
}

public void setInput(Object input) {
if (input instanceof ChangeElement) {
ChangeElement changeElement= (ChangeElement) input;

try {
changeElement.getOldElement().getContents().reset();
changeElement.getNewElement().getContents().reset();
} catch (IOException e) {
logger.error("setInput(Object) - exception ignored", e);
//$NON-NLS-1$
}

super.setInput(new DiffNode(
Differencer.CHANGE,
null,
changeElement.getOldElement(),
changeElement.getNewElement()));
} else {
super.setInput(input);
}
}
}

class ChangeElement {
private CompareElement oldElement;
private CompareElement newElement;
}

class CompareElement implements ITypedElement, IEditableContent,
IStreamContentAccessor {
private InputStream fContent;

public void setContent(byte[] newContent) {
fContent = new ByteArrayInputStream(newContent);
}

public ITypedElement replace(ITypedElement child, ITypedElement other) {
return child;
}

public String getType() {
ComparePreviewer.JAVA_TYPE;
}

...
}
-- snap --

Good luck,
Jan.

"Vijai" <voodoo_6600@yahoo.com> schrieb im Newsbeitrag
news:a97db6de47f45889cdbf3297fc7f1ce7$1@www.eclipse.org...
> Hi,
>
> Can someone tell me which is the best way to compare 2 pieces of text and
> display it on a compare editor, like how the "Compare" functionality of
> eclipse does, when 2 resources are selected...I have been stuck in this
> for 2 days, It can be like comparing 2 pieces of text/files or
> anything...i will change the text that needs to be compared into any
> resources required for the implementation.
>
> thanks
>
> Vijai
>
Re: Best way to compare 2 pieces of text [message #296126 is a reply to message #294476] Thu, 15 December 2005 09:14 Go to previous message
Eclipse UserFriend
Originally posted by: automatic.javalobby.org

Thanks for this tip, it helped me do a compare editor today. It such a weird but powerful interface, I hardly scratched the surface.
Previous Topic:Unexpected entries in the tool bar
Next Topic:Snippets
Goto Forum:
  


Current Time: Wed Sep 24 17:45:56 EDT 2025

Powered by FUDForum. Page generated in 0.06042 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top