Eclipse rcp 实现文本内容对比功能
2014-06-19 13:53
232 查看
一 相关知识
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 等接口。主要代码如下:
Java代码
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
Java代码
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
Java代码
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
Java代码
CompareUI.openCompareEditor(editorInput); // 打开对比Editor
CompareUI.openCompareDialog(editorInput); // 弹出对比Dialog
三 效果
选取不要比较的文件路径
(1)Editor效果
(2)Dialog效果
下载:http://download.csdn.net/detail/luoww1/7521193 可运行的elipse rcp 工程
相关文章推荐
- [编程小技巧]Notepad++中如何实现文本对比功能?
- 如何实现 对输入框的文本内容进行限制的功能
- 点击复制按钮复制指定文本内容,实现网页中的复制功能
- Java右键菜单实现文本组件内容的的复制、粘贴、剪切功能
- Java右键菜单实现文本组件内容的的复制、粘贴、剪切功能
- 如何实现 对输入框的文本内容进行限制的功能
- Python编程实现两个文件夹里文件的对比功能示例【包含内容的对比】
- 如何实现 对输入框的文本内容进行限制的功能
- 实现textbox对输入内容的自动检索功能,效果相当于百度首页的文本输入框
- Eclipse plugin开发 —实现文本内容帮助
- Eclipse plugin开发 —实现文本内容帮助
- JS实现选定指定HTML元素对象中指定文本内容功能示例
- java实现查找文本内容替换功能示例
- Android开发之文本内容自动朗读功能实现方法
- 前端html中jQuery实现对文本的搜索功能并把搜索相关内容显示出来
- 文章内容防复制功能的实现
- Eclipse实现超级联想的功能
- 在RCP中实现系统托盘功能
- 实现sharepoint 内容编辑器直接上传图片功能
- js实现表内搜索文本功能