我正在编写一个Eclipse插件,为此,我需要一个自定义的TextEditor。
TextEditor的内容应该是从自定义xml结构提取的文本。我已经完成了所有数据提取工作,但是现在却很难为TextEditor提供文本。
我实际上可以设置一些要显示的文本,并且可以正常工作,问题是我无法编辑它。
到目前为止,我的编辑器代码:

FooEditor:

package fooeditor.editor;

import org.eclipse.ui.texteditor.AbstractTextEditor;

public class FooEditor extends AbstractTextEditor {

    public FooEditor() {
    super();
    setSourceViewerConfiguration(new FooSourceViewerConfiguration());
    setDocumentProvider(new FooDocumentProvider());

}

}


FooSourceViewerConfiguration:
    软件包fooeditor.editor;

import org.eclipse.jface.text.source.SourceViewerConfiguration;

public class FooSourceViewerConfiguration extends SourceViewerConfiguration     {

    public FooSourceViewerConfiguration() {

}



}


FooDocumentProvider:
    软件包fooeditor.editor;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.DefaultPartitioner;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.AbstractDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.IElementStateListener;

public class FooDocumentProvider extends AbstractDocumentProvider {

    IDocumentPartitioner partitioner;

    /**
     *
     */
    public FooDocumentProvider() {
        super();
        partitioner = new FastPartitioner(new FooPartitionScanner(), null);
    }

    @Override
    protected IDocument createDocument(Object element) throws CoreException {
        if (element instanceof FileEditorInput) {
            IDocument document = new Document();
            document.set("hi");
            document.setDocumentPartitioner(partitioner);
            return document;

        } else {
            throw new CoreException(null);
        }

    }

    @Override
    protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite)
        throws CoreException {
        // TODO Auto-generated method stub

    }

    @Override
    protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
        // TODO Auto-generated method stub
        return null;
    }

}


FooPartitionScanner:
    软件包fooeditor.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IPartitionTokenScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
import org.eclipse.jface.text.rules.Token;

public class FooPartitionScanner extends RuleBasedPartitionScanner implements IPartitionTokenScanner {

    public FooPartitionScanner() {
        super();
        List rules = new ArrayList();

        IPredicateRule[] result = new IPredicateRule[rules.size()];
        rules.toArray(result);
        setPredicateRules(result);

    }

}

最佳答案

AbstractTextEditor根据isEditable方法的结果设置文本是否可编辑。缺省代码是:

@Override
public boolean isEditable() {
    IDocumentProvider provider= getDocumentProvider();
    if (provider instanceof IDocumentProviderExtension) {
        IDocumentProviderExtension extension= (IDocumentProviderExtension) provider;
        return extension.isModifiable(getEditorInput());
    }
    return false;
}


您扩展的AbstractDocumentProvider确实实现了IDocumentProviderExtension,但是isModifiable的默认实现是:

@Override
public boolean isModifiable(Object element) {
    return false;
}


因此,您需要重写它(或者也许要看使用StorageDocumentProvider)。

10-06 14:08