Search code examples
javaeclipseeclipse-pluginswteclipse-rcp

Eclipse TextMergeViewer not showing content?


The following always shows a blank / empty TextMergeViewer:

public class SSCCE extends Dialog {

protected SSCCE(Shell parentShell) { super(parentShell); }

protected Control createDialogArea(Composite parent) {
    Composite area = (Composite)super.createDialogArea(parent);
    CompareConfiguration cc = new CompareConfiguration();
    TextMergeViewer tmv = new TextMergeViewer(area, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL, cc);
    tmv.setInput(new CompareEditorInput(cc) {
        protected Object prepareInput(IProgressMonitor arg0) throws InvocationTargetException, InterruptedException {
            return new DiffNode(new CompareEntry("lName", "lContent"), new CompareEntry("lName", "lContent"));
        }});
    GridDataFactory.fillDefaults().grab(true, true).applyTo(tmv.getControl());
    return area;
}

protected boolean isResizable() { return true; }    

private class CompareEntry implements IStreamContentAccessor, ITypedElement {

    String contents, name;
    
    public CompareEntry(String _contents,String _name) {                
        contents = _contents; name = _name;
    }
    
    public InputStream getContents() throws CoreException {             
        return new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));             
    }
    
    public String getType() { return ITypedElement.TEXT_TYPE; }
    
    public Image getImage() { return null; }

    public String getName() { return name; }

}
}

... if I replace tmv.setInput() with CompareUI.openCompareDialog() I see EXACTLY what I'm expecting to see, so I'm guessing my CompareEditorInput stuff is OK / there's something I need to do to force the TextMergeViewer to recognize the inputs?


Solution

  • So the problem was that TextMergeViewer wants a DiffNode for input, not a CompareEditorInput (which was what CompareUI.openCompareDialog() wanted for input).

    So changing the following:

    tmv.setInput(new CompareEditorInput(cc) {
        protected Object prepareInput(IProgressMonitor arg0) 
            throws InvocationTargetException, InterruptedException {
            return new DiffNode(
                new CompareEntry("lName", "lContent"), 
                new CompareEntry("lName", "lContent"));
        }});
    

    to the following:

    tmv.setInput(new DiffNode(
        new CompareEntry("lName", "lContent"), 
        new CompareEntry("lName", "lContent")));
    

    makes everything work :-)