`
lggege
  • 浏览: 372774 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

借用Eclipse 实现文本内容对比功能

    博客分类:
  • RCP
阅读更多

一 相关知识

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效果

Compare效果图

 

 

(2)Dialog效果

Compare Editor

 

 

四 其他

    (1) 代码是基于Eclipse插件项目的email Template 创建的, 可直接使用附件中的源码替换到默认生成的类。

    (2) 代码演示了在Eclipse RCP环境下使用Compare功能。

分享到:
评论
7 楼 lyx262 2013-04-15  
我只想用到左右那个框框,什么组件呢?比较内容我的是自己写的控件,得自己写比较。
6 楼 bluesky_hust 2011-11-26  
作者好!
我按照您的代码去实现,但左右两边出不来啊.
5 楼 emc2100 2010-08-10  
谢谢啰...非常感谢你的教学
4 楼 lggege 2009-12-06  
谢谢Dollyn, 明白了. 非常感谢. 
3 楼 Dollyn 2009-12-03  
RCP也可以加工作区间的。

link就是一个链接文件,比如你在Eclipse里,new一个File,新建向导有个高级选项,链接到文件系统中的文件,这样就好像在工作区间内创建了一个文件,但实际上是在文件系统的其他位置。

这个文件也会在Eclipse的文件树上显示,也可以和工作区间内的其他文件比较。
2 楼 lggege 2009-12-03  
对的. 本来也是基于org.eclipse.compare 插件的功能, 你说的比较工作空间下的文件, 那么肯定没法给RCP使用, 而我的代码正好演示的是怎么在RCP下使用.

你指的link是什么意思?
1 楼 Dollyn 2009-12-01  
纯文本比较本身就有的,不过只能比较工作区间下的文件。
但可以把外面的文件link进来比较。

相关推荐

Global site tag (gtag.js) - Google Analytics