admin管理员组

文章数量:1305671

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?

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?

Share Improve this question edited Feb 4 at 17:16 Dave Carpeneto asked Feb 3 at 16:53 Dave CarpenetoDave Carpeneto 1,0803 gold badges12 silver badges25 bronze badges 3
  • Note that I've tried adding tmv.invalidateTextPresentation() & tmv.refresh(), neither of which added any value :-/ – Dave Carpeneto Commented Feb 3 at 17:03
  • You haven't set a GridLayout on your "area" Composite. – nitind Commented Feb 3 at 19:48
  • @nitind : this doesn't change the outcome (just confirmed). The layout itself seems to be 100% fine. The SSCCE above does compile (once you import the dependencies); feel free to fire it up to see the outcome I'm talking about :-) – Dave Carpeneto Commented Feb 3 at 20:17
Add a comment  | 

1 Answer 1

Reset to default 0

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 :-)

本文标签: javaEclipse TextMergeViewer not showing contentStack Overflow