Eclipse PDE: Everything about Editor
Editors are the most important part of eclipse IDE framework. Editor are the primary framework to create and modify resource like files. Every eclipse plug-in or eclipse based products needs to provide custom editor for editing proprietary resources.
I am tried to put important parts of Eclipse Editor Framework in this pos. This is the first post from the series "Everything About Editor"
Eclipse divides the concept of a text editor into two parts: the document and the viewer.
While the document holds the actual content of the editor, the viewer is responsible
for handling the content display. This nice Model-View-Controller (MVC)-style
separation allows you to separate code that manipulates the content of the editor
from the part that manipulates its display.
While the document holds the actual content of the editor, the viewer is responsible
for handling the content display. This nice Model-View-Controller (MVC)-style
separation allows you to separate code that manipulates the content of the editor
from the part that manipulates its display.
The Document represents the "model" part of the text editor framework. It contains the actual textual content of the editor. It does not concern itself with the display of
the text in any way. A document allows you to set the actual textual content of an editor and contains methods to manipulate the content. It is defined by the org.eclipse.jface.text.IDocument interface. The document provides abstraction for lines. It does it with help of Position, which are like sticker to portion of text in the document.
the text in any way. A document allows you to set the actual textual content of an editor and contains methods to manipulate the content. It is defined by the org.eclipse.jface.text.IDocument interface. The document provides abstraction for lines. It does it with help of Position, which are like sticker to portion of text in the document.
The DocumentProvider create Document , it bridges between files on the disk and its representation as a document. DocmentProvider is responsible for loading file from disk and save it. There are many extension interfaces are provided for DocumenProvider , almost one for every release. concrete implementation are available for almost all extensions, you have to implement two methods createDocument and crateEmptyDocument. CreateDocument is responsible for defining partitions in document. It handles many task related files like
- loading file from disk
- representing it as Document in memory
- saving the file to disk after modification
- creating file modification notification
The Viewer is responsible or displaying the content of the document and is defined
by the org.eclipse.jface.text.ITextViewer interface. Eclipse provide a special subclass of ITextViwer specially for displaying source code "SourceViewer".
by the org.eclipse.jface.text.ITextViewer interface. Eclipse provide a special subclass of ITextViwer specially for displaying source code "SourceViewer".
The org.eclipse.ui.editors.text.TextEditor class ties the document and
the viewer together and inserts Eclipse-specific functionality.The TextEditor implements the org.eclipse.ui.IEditorPart class, which -- as the name implies -- denotes to the Eclipse Workbench that it is part of the workbench and is an editor. An editor is associated with an IEditorInput class, which defines the protocol of editor input.
The SourceViewerConfiguration class provides methods tothe viewer together and inserts Eclipse-specific functionality.The TextEditor implements the org.eclipse.ui.IEditorPart class, which -- as the name implies -- denotes to the Eclipse Workbench that it is part of the workbench and is an editor. An editor is associated with an IEditorInput class, which defines the protocol of editor input.
access most of the UI-related helper objects the Eclipse framework provides and
that would be useful while creating the editor. It is responsible for following set of task
- Hyperlink detection
- Text Hover
- Syntax highlighter
- Auto edit
- Code completion
- Document petitioner
- Undo Manager
Gets started with creating Custom Editor skeleton.
1. Create a plug-in project
2. Extend “org.eclipse.ui.editors” extension point
<extension
point="org.eclipse.ui.editors">
<editor
class="com.ydtech.eclipse.editor.CustomEditor"
default="false"
extensions="properties"
id="com.ydtech.eclipse.myeditor.editor1"
name="Properties Editor">
</editor>
</extension>
3. Create class CustomEditor which extends org.eclipse.ui.editors.text.TextEditor.
4. Create MyDocument class extending Document.
5. Create document provider extending FileDocumentProvider and override createDocument & createEmptyDocument. Here createEmptyDocument returns new instance of MyDocument just created in previous steps
public class MyDocumentProvider extends FileDocumentProvider {
@Override
protected IDocument createEmptyDocument() {
return new MyDocument();
}
@Override
protected IDocument createDocument(Object element) throws CoreException {
// custom code .....
}
}
6. As explained previously Editor is responsible for bringing together Document and Viewer, it is only responsible for creating Document Provider. So override constructor of TextEditor and call setDocumentProvider()
public class CustomEditor extends TextEditor {
public CustomEditor() {
setDocumentProvider(new MyDocumentProvider());
}
}
Here we have skeleton ready for .properties file.
There is much more to come in this series.
Thanks for doing this - it'll be much appreciated!
ReplyDeleteNote that in most cases you don't really need to create your own document and provider classes - using Document and TextFileDocumentProvider is often sufficient.
Also, although a number of available examples instantiate the document provider in the editor's constructor, it's not actually the best idea. Generally, if you have more than one editor open on the same file, then you want them to share the same document using document "connections" managed through the provider. To take advantage of this, all editors which edit the document must share the same provider, which can be obtained from the DocumentProviderRegistry(accessed automatically in AbstractDecoratedTextEditor). You'll have to do a little magic in your plugin.xml to set up the registry entry, but then you'll be able to edit your content in one editor and see the changes occurring also in the other editor.