manohar_b
manohar_b

Reputation: 23

How to open the eclipse compare editor with editing enabled programmatically?

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?

Upvotes: 2

Views: 358

Answers (0)

Related Questions