一 相关知识
org.eclipse.compare 插件项目,用于进行文本、源码比对的一个插件,提供了一个Editor或Dialog可方便调用。
org.eclipse.compare.CompareEditorInput.CompareEditorInput 是用于给Compare Editor 的EditorInput, 需要自己实现。
org.eclipse.compare.CompareConfiguration 对CompareEditor的配置。是否允许左边内容被修改,或是否允许右边内容被修改;左右两边的label,image等。
org.eclipse.compare.ITypedElement 接口,本意为指代有图片、名称、类型的元素。而在Compare中,为一个基本的对比单元。
org.eclipse.compare.IEditableContent 接口,是否可编辑的元素
org.eclipse.compare.IModificationDate 接口,修改时间
org.eclipse.compare.IStreamContentAccessor 获得内容的接口,内容是以InputStream流的方式获得。
org.eclipse.compare.IContentChangeNotifier 内容变化的通知接口
org.eclipse.compare.BufferedContent 抽象类,实现了 org.eclipse.compare.IStreamContentAccessor 和 org.eclipse.compare.IContentChangeNotifier 2个接口
在BufferedContent中持有了一个byte[], 表示缓存有界面上修改的内容,当然,最后需要你在CompareEditorInupt中将修改后的byte[]内容保存(比如保存至文件等)
二 代码
(1)解决项目依赖
添加对org.eclipse.compare 的依赖
(2)定义一个表示比对的元素
可继承与BufferedContent,并实现ITypeElement 等接口。主要代码如下:
class CompareItem extends BufferedContent implements ITypedElement, IModificationDate, IEditableContent {
private String fileName;
private long time;
CompareItem(String fileName) {
this.fileName = fileName;
this.time = System.currentTimeMillis();
}
/**
* @see org.eclipse.compare.BufferedContent#createStream()
*/
protected InputStream createStream() throws CoreException {
try {
return new FileInputStream(new File(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return new ByteArrayInputStream(new byte[0]);
}
/**
* @see org.eclipse.compare.IModificationDate#getModificationDate()
*/
public long getModificationDate() {
return time;
}
/**
* @see org.eclipse.compare.ITypedElement#getImage()
*/
public Image getImage() {
return CompareUI.DESC_CTOOL_NEXT.createImage();
}
/**
* @see org.eclipse.compare.ITypedElement#getName()
*/
public String getName() {
return fileName;
}
/**
* @see org.eclipse.compare.ITypedElement#getType()
*/
public String getType() {
return ITypedElement.TEXT_TYPE;
}
/**
* @see org.eclipse.compare.IEditableContent#isEditable()
*/
public boolean isEditable() {
return true;
}
/**
* @see org.eclipse.compare.IEditableContent#replace(org.eclipse.compare.ITypedElement, org.eclipse.compare.ITypedElement)
*/
public ITypedElement replace(ITypedElement dest, ITypedElement src) {
return null;
}
public void writeFile() {
this.writeFile(this.fileName, this.getContent());
}
private void writeFile(String fileName, byte[] newContent) {
FileOutputStream fos = null;
try {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
file.createNewFile();
fos = new FileOutputStream(file);
fos.write(newContent);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
fos = null;
}
}
}
(3)配置CompareConfiguration
CompareConfiguration config = new CompareConfiguration();
config.setProperty(CompareConfiguration.SHOW_PSEUDO_CONFLICTS, Boolean.FALSE);
// left
config.setLeftEditable(true);
config.setLeftLabel("Left");
// right
config.setRightEditable(true);
config.setRightLabel("Right");
(4)定义CompareEditorInput
CompareEditorInput editorInput = new CompareEditorInput(config) {
CompareItem left = new CompareItem("C:/A.txt");
CompareItem right = new CompareItem("C:/Inject.log");
@Override
protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
return new DiffNode(null, Differencer.CONFLICTING, null, left, right);
}
@Override
public void saveChanges(IProgressMonitor pm) throws CoreException {
super.saveChanges(pm);
left.writeFile();
right.writeFile();
}
};
editorInput.setTitle("文件比较");
(5)弹出Compare Editor或Dialog
CompareUI.openCompareEditor(editorInput); // 打开对比Editor
CompareUI.openCompareDialog(editorInput); // 弹出对比Dialog
三 效果
(1)Editor效果
(2)Dialog效果
四 其他
(1) 代码是基于Eclipse插件项目的email Template 创建的, 可直接使用附件中的源码替换到默认生成的类。
(2) 代码演示了在Eclipse RCP环境下使用Compare功能。