如何在Eclipse中创建只读编辑器(Eclipse插件开发)

时间:2021-10-05 06:36:08

I'm wondering how to make a really read only eclipse editor.. My editor extends TextEditor, so when I reimplement method isEditable to always return false.

我想知道如何制作一个真正只读的eclipse编辑器。我的编辑器扩展了TextEditor,所以当我重新实现方法isEditable时总是返回false。

It's the easiest way, which prevents user from typing or deleting anything in the document opened in the editor. But you can still change content of the document for example by using find/replace. And this is not desired..

这是最简单的方法,可以防止用户在编辑器中打开的文档中键入或删除任何内容。但您仍然可以通过使用find / replace来更改文档的内容。这是不希望的..

Is there any other aesy way how to arhieve this goal?

如何实现这一目标还有其他任何方式吗?

4 个解决方案

#1


I wanted to use editor instead of viewer because the editor was already made, so I just used a 3rd party plugin..

我想使用编辑器而不是查看器,因为编辑器已经制作好了,所以我只使用了第三方插件..

I found my solution - maybee not very clean but does the job and is pretty easy so it wins

我找到了我的解决方案 - 也许不是很干净但是做得很好而且非常容易,所以它赢了

I've overriden theese methods:

我已经覆盖了theese方法:

@Override
public boolean isEditable() {
    return false;
}

@Override
public boolean isEditorInputModifiable() {
    return false;
}

@Override
public boolean isEditorInputReadOnly() {
    return true;
}

@Override
public boolean isDirty() {
    return false;
}

#2


Have you tried to create your own SourceViewer? Something like this. I haven't tried the code myself.

您是否尝试过创建自己的SourceViewer?像这样的东西。我自己没有尝试过代码。

class ReadOnlyViewer extends SourceViewer
{
   protected StyledText createTextWidget(Composite parent, int styles) 
   {
    return new StyledText(parent, styles | SWT.READ_ONLY);
   }
}

class MyEditor extends TextEditor
{
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) 
     {
        fAnnotationAccess= getAnnotationAccess();
        fOverviewRuler= createOverviewRuler(getSharedColors());

        ISourceViewer viewer= new ReadOnlyViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles);
        // ensure decoration support has been created and configured.
        getSourceViewerDecorationSupport(viewer);

        return viewer;
    }
}

#3


In the SWT styles, specify SWT.READ_ONLY. This should reject all APIs which modify the document (with the exception of setText(), I hope ...)

在SWT样式中,指定SWT.READ_ONLY。这应该拒绝所有修改文档的API(setText()除外,我希望...)

If not, please file a bug.

如果没有,请提交错误。

#4


Why you are using a TextEditor instead of using a TextViewer?

为什么使用TextEditor而不是使用TextViewer?

#1


I wanted to use editor instead of viewer because the editor was already made, so I just used a 3rd party plugin..

我想使用编辑器而不是查看器,因为编辑器已经制作好了,所以我只使用了第三方插件..

I found my solution - maybee not very clean but does the job and is pretty easy so it wins

我找到了我的解决方案 - 也许不是很干净但是做得很好而且非常容易,所以它赢了

I've overriden theese methods:

我已经覆盖了theese方法:

@Override
public boolean isEditable() {
    return false;
}

@Override
public boolean isEditorInputModifiable() {
    return false;
}

@Override
public boolean isEditorInputReadOnly() {
    return true;
}

@Override
public boolean isDirty() {
    return false;
}

#2


Have you tried to create your own SourceViewer? Something like this. I haven't tried the code myself.

您是否尝试过创建自己的SourceViewer?像这样的东西。我自己没有尝试过代码。

class ReadOnlyViewer extends SourceViewer
{
   protected StyledText createTextWidget(Composite parent, int styles) 
   {
    return new StyledText(parent, styles | SWT.READ_ONLY);
   }
}

class MyEditor extends TextEditor
{
protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) 
     {
        fAnnotationAccess= getAnnotationAccess();
        fOverviewRuler= createOverviewRuler(getSharedColors());

        ISourceViewer viewer= new ReadOnlyViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(), styles);
        // ensure decoration support has been created and configured.
        getSourceViewerDecorationSupport(viewer);

        return viewer;
    }
}

#3


In the SWT styles, specify SWT.READ_ONLY. This should reject all APIs which modify the document (with the exception of setText(), I hope ...)

在SWT样式中,指定SWT.READ_ONLY。这应该拒绝所有修改文档的API(setText()除外,我希望...)

If not, please file a bug.

如果没有,请提交错误。

#4


Why you are using a TextEditor instead of using a TextViewer?

为什么使用TextEditor而不是使用TextViewer?