2

I am trying to use eclipse compare editor(org.eclipse.compare) to compare two files. The compare editor opens and shows the differences. But any editing or merging is not enabled in the compare editor. First i am preparing the input and calling CompareUI.openCompareEditor(input).

public class CompareItem implements IStreamContentAccessor, ITypedElement, IModificationDate {

private File content;
private long modifiedDate;
private String fileName;

public CompareItem(File left, long lastModified, String name) {
    content = left;
    modifiedDate = lastModified;
    fileName = name;
}
@Override
public long getModificationDate() {
    return modifiedDate;
}
@Override
public String getName() {
    return fileName;
}
@Override
public String getType() {
    return "JAVA";
}
@Override
public InputStream getContents() {
    try {
        return new FileInputStream(content);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
@Override
public Image getImage() {
    return null;
}

}


public class CompareInput extends CompareEditorInput {

public CompareInput() {
    super(new CompareConfiguration());
}

protected Object prepareInput(IProgressMonitor pm) {
    File file1 = new File("D:\\hello.txt");
    File file2 = new File("D:\\hello2.txt");
    
    CompareItem ancestor = 
       new CompareItem(file1, file1.lastModified(), file1.getName());
    CompareItem left = 
       new CompareItem(file1, file1.lastModified(), file1.getName());
    CompareItem right = 
       new CompareItem(file2, file2.lastModified(), file2.getName());
    
    return new DiffNode(Differencer.CHANGE, ancestor, left, right);
 }

}


public class SampleHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    
    CompareInput input = new CompareInput();
    CompareConfiguration compareConfiguration = input.getCompareConfiguration();
    compareConfiguration.setLeftEditable(true);
    compareConfiguration.setRightEditable(true);
    
    CompareUI.openCompareEditor(input);
    return null;
}

}

This opens the eclipse compare Editor as in the screenshot (compare editor output) but editing(doesn`t allow adding or deleting any characters) the left side file or the right side file in the compare editor is not possible. The buttons 'copy changes from left to Right' and 'copy changes from Right to Left' are also disabled as can be seen in the screenshot.

How to enable the editing in this editor?

manohar_b
  • 21
  • 4
  • Looking at the source the content viewer has to be something that extends `ContentMergeViewer` rather than just `ContentViewer` for example `TextMergeViewer`. How that is determined is complicated, look at `CompareUIPlugin.findContentViewerDescriptor` – greg-449 Nov 05 '20 at 08:45
  • @greg-449 i am not implementing ContentViewer in my case – manohar_b Nov 06 '20 at 07:17
  • I think a content view is used always internally by the compare editor. The ITypedElements determine which one is used. – greg-449 Nov 06 '20 at 07:56
  • @greg-449 so how do I get this working? what changes can I make? - Thanks – manohar_b Nov 06 '20 at 08:42
  • I don't know, I haven't used a compare editor for a long time. Try debugging the findContentViewerDescriptor call. – greg-449 Nov 06 '20 at 10:12

0 Answers0