我正在编写一个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: package fooeditor.editor;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
public class FooSourceViewerConfiguration extends SourceViewerConfiguration {
public FooSourceViewerConfiguration() {
}
}
FooDocumentProvider: package 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: package 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);
}
}
答案 0 :(得分:1)
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
)。