Eclipse rcp 实现文本内容对比功能

来源:互联网 发布:js 比较数值相等 编辑:程序博客网 时间:2024/06/14 15:20

一 相关知识

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代码  收藏代码
  1. class CompareItem extends BufferedContent implements ITypedElement, IModificationDate, IEditableContent {  
  2.     private String fileName;  
  3.     private long time;  
  4.   
  5.     CompareItem(String fileName) {  
  6.         this.fileName = fileName;  
  7.         this.time = System.currentTimeMillis();  
  8.     }  
  9.   
  10.     /** 
  11.      * @see org.eclipse.compare.BufferedContent#createStream() 
  12.      */  
  13.     protected InputStream createStream() throws CoreException {  
  14.         try {  
  15.             return new FileInputStream(new File(fileName));  
  16.         } catch (FileNotFoundException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         return new ByteArrayInputStream(new byte[0]);  
  20.     }  
  21.   
  22.     /** 
  23.      * @see org.eclipse.compare.IModificationDate#getModificationDate() 
  24.      */  
  25.     public long getModificationDate() {  
  26.         return time;  
  27.     }  
  28.   
  29.     /** 
  30.      * @see org.eclipse.compare.ITypedElement#getImage() 
  31.      */  
  32.     public Image getImage() {  
  33.         return CompareUI.DESC_CTOOL_NEXT.createImage();  
  34.     }  
  35.   
  36.     /** 
  37.      * @see org.eclipse.compare.ITypedElement#getName() 
  38.      */  
  39.     public String getName() {  
  40.         return fileName;  
  41.     }  
  42.   
  43.     /** 
  44.      * @see org.eclipse.compare.ITypedElement#getType() 
  45.      */  
  46.     public String getType() {  
  47.         return ITypedElement.TEXT_TYPE;  
  48.     }  
  49.   
  50.     /** 
  51.      * @see org.eclipse.compare.IEditableContent#isEditable() 
  52.      */  
  53.     public boolean isEditable() {  
  54.         return true;  
  55.     }  
  56.   
  57.     /** 
  58.      * @see org.eclipse.compare.IEditableContent#replace(org.eclipse.compare.ITypedElement, org.eclipse.compare.ITypedElement) 
  59.      */  
  60.     public ITypedElement replace(ITypedElement dest, ITypedElement src) {  
  61.         return null;  
  62.     }  
  63.   
  64.     public void writeFile() {  
  65.         this.writeFile(this.fileName, this.getContent());  
  66.     }  
  67.   
  68.     private void writeFile(String fileName, byte[] newContent) {  
  69.         FileOutputStream fos = null;  
  70.         try {  
  71.             File file = new File(fileName);  
  72.             if (file.exists()) {  
  73.                 file.delete();  
  74.             }  
  75.   
  76.             file.createNewFile();  
  77.   
  78.             fos = new FileOutputStream(file);  
  79.             fos.write(newContent);  
  80.             fos.flush();  
  81.   
  82.         } catch (IOException e) {  
  83.             e.printStackTrace();  
  84.   
  85.         } finally {  
  86.             try {  
  87.                 fos.close();  
  88.             } catch (IOException e) {  
  89.                 e.printStackTrace();  
  90.             }  
  91.   
  92.             fos = null;  
  93.         }  
  94.     }  
  95. }  

 

    (3)配置CompareConfiguration

Java代码  收藏代码
  1. CompareConfiguration config = new CompareConfiguration();  
  2. config.setProperty(CompareConfiguration.SHOW_PSEUDO_CONFLICTS, Boolean.FALSE);  
  3.   
  4. // left  
  5. config.setLeftEditable(true);  
  6. config.setLeftLabel("Left");  
  7.   
  8. // right  
  9. config.setRightEditable(true);  
  10. config.setRightLabel("Right");  
 

 

    (4)定义CompareEditorInput

Java代码  收藏代码
  1. CompareEditorInput editorInput = new CompareEditorInput(config) {  
  2.     CompareItem left = new CompareItem("C:/A.txt");  
  3.     CompareItem right = new CompareItem("C:/Inject.log");  //要比较的两个文件也可做成自己选取比较文件
  4.       
  5.     @Override  
  6.     protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {  
  7.         return new DiffNode(null, Differencer.CONFLICTING, null, left, right);  
  8.     }  
  9.   
  10.     @Override  
  11.     public void saveChanges(IProgressMonitor pm) throws CoreException {  
  12.         super.saveChanges(pm);  
  13.   
  14.         left.writeFile();  
  15.         right.writeFile();  
  16.     }  
  17. };  
  18.   
  19. editorInput.setTitle("文件比较");  

 

   (5)弹出Compare Editor或Dialog

Java代码  收藏代码
  1. CompareUI.openCompareEditor(editorInput);  // 打开对比Editor  
  2.   
  3. CompareUI.openCompareDialog(editorInput); // 弹出对比Dialog  
 

三 效果

 选取不要比较的文件路径


(1)Editor效果


 

 

(2)Dialog效果

Compare Editor

 

 下载:http://download.csdn.net/detail/luoww1/7521193 可运行的elipse rcp 工程



0 0