下面列出了怎么用org.eclipse.ui.editors.text.TextFileDocumentProvider的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Obtains the document of a file.
* @param file the file
* @return the document of the file, or <code>null</code> if none
*/
public static IDocument getDocument(IFile file) {
if (file == null) {
return null;
}
try {
TextFileDocumentProvider provider = new TextFileDocumentProvider();
provider.connect(file);
IDocument doc = provider.getDocument(file);
provider.disconnect(file);
return doc;
} catch (CoreException e) {
e.printStackTrace();
}
return null;
}
public static Position getPosition(IFile file, TextSpan textSpan) throws BadLocationException {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
ITextFileBuffer buffer = bufferManager.getTextFileBuffer(file.getLocation(), LocationKind.IFILE);
if (buffer != null) {
return getPosition(buffer.getDocument(), textSpan);
}
IDocumentProvider provider = new TextFileDocumentProvider();
try {
provider.connect(file);
IDocument document = provider.getDocument(file);
if (document != null) {
return getPosition(document, textSpan);
}
} catch (CoreException e) {
} finally {
provider.disconnect(file);
}
return null;
}
@Override
public String getContentType()
{
try
{
IContentType contentType = ((TextFileDocumentProvider) getDocumentProvider())
.getContentType(getEditorInput());
if (contentType != null)
{
IContentType baseType = contentType.getBaseType();
if (baseType != null && IXMLConstants.CONTENT_TYPE_XML.equals(baseType.getId()))
{
return contentType.getId();
}
}
}
catch (Exception e)
{
}
return IXMLConstants.CONTENT_TYPE_XML;
}
public String getContentType()
{
try
{
IContentType contentType = ((TextFileDocumentProvider) getDocumentProvider())
.getContentType(getEditorInput());
if (contentType != null)
{
return contentType.getId();
}
}
catch (Exception e)
{
IdeLog.logError(CommonEditorPlugin.getDefault(), e);
}
return null;
}
private static boolean mustSaveDirtyEditor(IEditorPart ep, IEditorInput input, boolean saveUnknownEditors) {
/*
* Goal: save all editors that could interfere with refactoring operations.
*
* Always save all editors for compilation units that are not working copies.
* (Leaving them dirty would cause problems, since the file buffer could have been
* modified but the Java model is not reconciled.)
*
* If <code>saveUnknownEditors</code> is <code>true</code>, save all editors
* whose implementation is probably not based on file buffers.
*/
IResource resource= (IResource) input.getAdapter(IResource.class);
if (resource == null)
return saveUnknownEditors;
IJavaElement javaElement= JavaCore.create(resource);
if (javaElement instanceof ICompilationUnit) {
ICompilationUnit cu= (ICompilationUnit) javaElement;
if (!cu.isWorkingCopy()) {
return true;
}
}
if (! (ep instanceof ITextEditor))
return saveUnknownEditors;
ITextEditor textEditor= (ITextEditor) ep;
IDocumentProvider documentProvider= textEditor.getDocumentProvider();
if (! (documentProvider instanceof TextFileDocumentProvider))
return saveUnknownEditors;
return false;
}
private static void findAndLogDocumentProviderIssues(final IEditorPart editorPart) {
final IDocumentProvider defaultDocumentProvider =
EditorAPI.getDocumentProvider(editorPart.getEditorInput());
if (!(defaultDocumentProvider instanceof TextFileDocumentProvider)) {
log.warn(
"The default document provider "
+ defaultDocumentProvider
+ " for editor with title '"
+ editorPart.getTitle()
+ "' might not support shared access. It is likely that the editor content is not properly synchronized!");
return;
}
final ITextEditor textEditor = editorPart.getAdapter(ITextEditor.class);
if (textEditor == null) return;
final IDocumentProvider editorDocumentProvider = textEditor.getDocumentProvider();
if (!(editorDocumentProvider instanceof TextFileDocumentProvider)) {
log.warn(
"The document provider "
+ editorDocumentProvider
+ " for editor with title '"
+ editorPart.getTitle()
+ "' might not support shared access. It is likely that the editor content is not properly synchronized!");
}
}
/**
* Creates a new Oberon-2 source file document provider and sets up the parent chain.
*/
public OberonDocumentProvider() {
super( IModulaPartitions.M2_PARTITIONING
, new OberonDocumentSetupParticipant()
, new TextFileDocumentProvider() );
}
/**
* Creates a new Modula-2 source file document provider and sets up the parent chain.
*/
public ModulaDocumentProvider() {
super( IModulaPartitions.M2_PARTITIONING
, new ModulaDocumentSetupParticipant()
, new TextFileDocumentProvider() );
}