Monday, 6 August 2012

Eclipse plugin to generate dynamic text

Hello Everyone,

In this post, I would like to share my learning about how to create a eclipse plugin that generates the code in code editor dynamically. This was really a frustrating period for me, as I was working on this particular thing for more than 10 days. I did receive vital information from several blogs and websites - but the information were scattered and I had to assemble them. So, in this blog I am planning to explain the sequence of flow from the scratch to end in the process of creating an eclipse plugin that would generate/populate the code in code editor dynamically.

Step 1:

First step in this process is to create an eclipse plugin. I referred the blog post mentioned here to gather information, and this post provides us with the comprehensive information about the process of creating an eclipse plugin.

Step 2:

After creating a plugin, we need to reference the external jars that we are planning to use in our program with the plugin project. With plugin project, we need to follow different procedure for referencing the jar files. Post mentioned below summarizes the steps involved in referencing external jars with plugin project:

Step 3:

Next step is to make use of eclipse's IDocument class in our java program which would update our code editor with texts dynamically. Below code snippet can be used for it:

private IWorkbench wb = null;
private IWorkbenchWindow win = null;
private IWorkbenchPage page = null;
private IEditorPart part = null;
private CompilationUnitEditor compEditor = null;
private IEditorInput input = null;
private IDocumentProvider dp = null;
private IDocument doc = null;
int offset = 0;

wb = PlatformUI.getWorkbench();//.getActiveWorkbenchWindow().getActiveWorkbenchPage();
win = wb.getActiveWorkbenchWindow();
page = win.getActivePage();
part = page.getActiveEditor();
if (!(part instanceof CompilationUnitEditor))
compEditor = (CompilationUnitEditor)part;
input = ((IEditorPart) compEditor).getEditorInput();  

dp = ((ITextEditor) compEditor).getDocumentProvider(); //editor.getDocumentProvider();
doc = dp.getDocument(input);//editor.getEditorInput());

try {
offset = doc.getLineOffset(doc.getNumberOfLines()-4);
    } catch (BadLocationException e) {
// TODO Auto-generated catch block
try {
doc.replace(offset, 0, UpdateStr+"\n");
    } catch (BadLocationException e) {
    // TODO Auto-generated catch block

For further references, you can refer these posts:

Step 4:
Usage of IDocument would require the reference of following jar files:


User has to reference these jar files as per the procedure mentioned in Step 2 of this post.

But even after referencing the jars, user has to ensure that these jar files are mentioned as part of 'Require bundle' package in 'Manifest.MF' file of eclipse plugin.

If these jar files are not mentioned as part of 'Require Bundle', user has to make them by following these steps:

1. Navigate to 'Dependencies' tab of 'Manifest.MF' file.
2. Under 'Required Plugins' section, search for the mentioned files and add them as required plugin.

These are the steps that I followed while developing the plugin. Please let me know if you face any difficulties during your setup.