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.

http://www.nakedtechnologist.com/?p=677
http://www.nakedtechnologist.com/?p=1179

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:

http://stackoverflow.com/questions/5744520/adding-jars-to-a-eclipse-plugin

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))
return;
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
e.printStackTrace();
}
try {
doc.replace(offset, 0, UpdateStr+"\n");
    } catch (BadLocationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

For further references, you can refer these posts:
http://wiki.eclipse.org/FAQ_How_do_I_insert_text_in_the_active_text_editor%3F

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

org.eclipse.jface.text;bundle-version="3.7.0",
 org.eclipse.ui.workbench.texteditor;bundle-version="3.7.0",
 org.eclipse.jdt.ui;bundle-version="3.7.0"

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.

Thanks!